Fix code formatting
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / CronjobAddForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\cronjob\CronjobAction;
4 use wcf\data\cronjob\CronjobEditor;
5 use wcf\form\AbstractForm;
6 use wcf\system\exception\SystemException;
7 use wcf\system\exception\UserInputException;
8 use wcf\system\language\I18nHandler;
9 use wcf\system\WCF;
10 use wcf\util\CronjobUtil;
11 use wcf\util\StringUtil;
12
13 /**
14 * Shows the cronjob add form.
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2014 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage acp.form
21 * @category Community Framework
22 */
23 class CronjobAddForm extends AbstractForm {
24 /**
25 * @see \wcf\page\AbstractPage::$activeMenuItem
26 */
27 public $activeMenuItem = 'wcf.acp.menu.link.cronjob.add';
28
29 /**
30 * @see \wcf\page\AbstractPage::$neededPermissions
31 */
32 public $neededPermissions = array('admin.system.canManageCronjob');
33
34 /**
35 * cronjob class name
36 * @var string
37 */
38 public $className = '';
39
40 /**
41 * cronjob package id
42 * @var integer
43 */
44 public $packageID = PACKAGE_ID;
45
46 /**
47 * cronjob description
48 * @var string
49 */
50 public $description = '';
51
52 /**
53 * execution time (min)
54 * @var string
55 */
56 public $startMinute = '*';
57
58 /**
59 * execution time (hour)
60 * @var string
61 */
62 public $startHour = '*';
63
64 /**
65 * execution time (day of month)
66 * @var string
67 */
68 public $startDom = '*';
69
70 /**
71 * execution time (month)
72 * @var string
73 */
74 public $startMonth = '*';
75
76 /**
77 * execution time (day of week)
78 * @var string
79 */
80 public $startDow = '*';
81
82 /**
83 * @see \wcf\page\IPage::readParameters()
84 */
85 public function readParameters() {
86 parent::readParameters();
87
88 I18nHandler::getInstance()->register('description');
89 }
90
91 /**
92 * @see \wcf\form\IForm::readFormParameters()
93 */
94 public function readFormParameters() {
95 parent::readFormParameters();
96
97 I18nHandler::getInstance()->readValues();
98
99 if (isset($_POST['className'])) $this->className = StringUtil::trim($_POST['className']);
100 if (isset($_POST['description'])) $this->description = StringUtil::trim($_POST['description']);
101 if (isset($_POST['startMinute'])) $this->startMinute = str_replace(' ', '', $_POST['startMinute']);
102 if (isset($_POST['startHour'])) $this->startHour = str_replace(' ', '', $_POST['startHour']);
103 if (isset($_POST['startDom'])) $this->startDom = str_replace(' ', '', $_POST['startDom']);
104 if (isset($_POST['startMonth'])) $this->startMonth = str_replace(' ', '', $_POST['startMonth']);
105 if (isset($_POST['startDow'])) $this->startDow = str_replace(' ', '', $_POST['startDow']);
106 }
107
108 /**
109 * @see \wcf\form\IForm::validate()
110 */
111 public function validate() {
112 parent::validate();
113
114 // validate class name
115 if (empty($this->className)) {
116 throw new UserInputException('className');
117 }
118
119 if (!class_exists($this->className)) {
120 throw new UserInputException('className', 'doesNotExist');
121 }
122
123 // validate description
124 if (!I18nHandler::getInstance()->validateValue('description')) {
125 if (I18nHandler::getInstance()->isPlainValue('description')) {
126 throw new UserInputException('description');
127 }
128 else {
129 throw new UserInputException('description', 'multilingual');
130 }
131 }
132
133 try {
134 CronjobUtil::validate($this->startMinute, $this->startHour, $this->startDom, $this->startMonth, $this->startDow);
135 }
136 catch (SystemException $e) {
137 // extract field name
138 $fieldName = '';
139 if (preg_match("/cronjob attribute '(.*)'/", $e->getMessage(), $match)) {
140 $fieldName = $match[1];
141 }
142
143 throw new UserInputException($fieldName, 'notValid');
144 }
145 }
146
147 /**
148 * @see \wcf\form\IForm::save()
149 */
150 public function save() {
151 parent::save();
152
153 // save cronjob
154 $data = array_merge($this->additionalFields, array(
155 'className' => $this->className,
156 'packageID' => $this->packageID,
157 'description' => $this->description,
158 'startMinute' => $this->startMinute,
159 'startHour' => $this->startHour,
160 'startDom' => $this->startDom,
161 'startMonth' => $this->startMonth,
162 'startDow' => $this->startDow
163 ));
164
165 $this->objectAction = new CronjobAction(array(), 'create', array('data' => $data));
166 $this->objectAction->executeAction();
167
168 if (!I18nHandler::getInstance()->isPlainValue('description')) {
169 $returnValues = $this->objectAction->getReturnValues();
170 $cronjobID = $returnValues['returnValues']->cronjobID;
171 I18nHandler::getInstance()->save('description', 'wcf.acp.cronjob.description.cronjob'.$cronjobID, 'wcf.acp.cronjob', $this->packageID);
172
173 // update group name
174 $cronjobEditor = new CronjobEditor($returnValues['returnValues']);
175 $cronjobEditor->update(array(
176 'description' => 'wcf.acp.cronjob.description.cronjob'.$cronjobID
177 ));
178 }
179
180 $this->saved();
181
182 // reset values
183 $this->className = $this->description = '';
184 $this->startMinute = $this->startHour = $this->startDom = $this->startMonth = $this->startDow = '*';
185 I18nHandler::getInstance()->reset();
186
187 // show success.
188 WCF::getTPL()->assign(array(
189 'success' => true
190 ));
191 }
192
193 /**
194 * @see \wcf\page\IPage::assignVariables()
195 */
196 public function assignVariables() {
197 parent::assignVariables();
198
199 I18nHandler::getInstance()->assignVariables();
200
201 WCF::getTPL()->assign(array(
202 'className' => $this->className,
203 'description' => $this->description,
204 'startMinute' => $this->startMinute,
205 'startHour' => $this->startHour,
206 'startDom' => $this->startDom,
207 'startMonth' => $this->startMonth,
208 'startDow' => $this->startDow,
209 'action' => 'add'
210 ));
211 }
212 }