Change `notValid` to `invalid`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / NoticeAddForm.class.php
CommitLineData
20933e61
MS
1<?php
2namespace wcf\acp\form;
8810504c 3use wcf\data\object\type\ObjectType;
20933e61
MS
4use wcf\data\object\type\ObjectTypeCache;
5use wcf\data\notice\NoticeAction;
6use wcf\data\notice\NoticeEditor;
7use wcf\form\AbstractForm;
8use wcf\system\condition\ConditionHandler;
9use wcf\system\exception\UserInputException;
10use wcf\system\language\I18nHandler;
e70b5175 11use wcf\system\Regex;
20933e61
MS
12use wcf\system\WCF;
13use wcf\util\StringUtil;
14
15/**
16 * Shows the form to create a new notice.
17 *
18 * @author Matthias Schmidt
7d739af0 19 * @copyright 2001-2016 WoltLab GmbH
20933e61 20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 21 * @package WoltLabSuite\Core\Acp\Form
20933e61
MS
22 */
23class NoticeAddForm extends AbstractForm {
24 /**
0fcfe5f6 25 * @inheritDoc
20933e61
MS
26 */
27 public $activeMenuItem = 'wcf.acp.menu.link.notice.add';
28
e70b5175
MS
29 /**
30 * list pf pre-defined CSS class names
7a23a706 31 * @var string[]
e70b5175 32 */
058cbd6a 33 public $availableCssClassNames = [
e70b5175 34 'info',
ed80b2c6 35 'success',
e70b5175
MS
36 'warning',
37 'error',
38
39 'custom'
058cbd6a 40 ];
e70b5175
MS
41
42 /**
43 * name of the chosen CSS class name
44 * @var string
45 */
923160e6 46 public $cssClassName = 'info';
e70b5175
MS
47
48 /**
49 * custom CSS class name
50 * @var string
51 */
52 public $customCssClassName = '';
53
20933e61
MS
54 /**
55 * grouped notice condition object types
e4499881 56 * @var ObjectType[][]
20933e61 57 */
058cbd6a 58 public $groupedConditionObjectTypes = [];
20933e61
MS
59
60 /**
61 * 1 if the notice is disabled
62 * @var integer
63 */
64 public $isDisabled = 0;
65
66 /**
67 * 1 if the notice is dismissible
68 * @var integer
69 */
70 public $isDismissible = 0;
71
72 /**
0fcfe5f6 73 * @inheritDoc
20933e61 74 */
058cbd6a 75 public $neededPermissions = ['admin.notice.canManageNotice'];
20933e61
MS
76
77 /**
78 * name of the notice
79 * @var string
80 */
81 public $noticeName = '';
82
83 /**
84 * 1 if html is used in the notice text
85 * @var integer
86 */
87 public $noticeUseHtml = 0;
88
89 /**
90 * order used to the show the notices
91 * @var integer
92 */
93 public $showOrder = 0;
94
95 /**
0fcfe5f6 96 * @inheritDoc
20933e61
MS
97 */
98 public function assignVariables() {
99 parent::assignVariables();
100
101 I18nHandler::getInstance()->assignVariables();
102
058cbd6a 103 WCF::getTPL()->assign([
20933e61 104 'action' => 'add',
e70b5175
MS
105 'availableCssClassNames' => $this->availableCssClassNames,
106 'cssClassName' => $this->cssClassName,
107 'customCssClassName' => $this->customCssClassName,
20933e61
MS
108 'isDisabled' => $this->isDisabled,
109 'isDismissible' => $this->isDismissible,
110 'groupedConditionObjectTypes' => $this->groupedConditionObjectTypes,
111 'noticeName' => $this->noticeName,
112 'noticeUseHtml' => $this->noticeUseHtml,
113 'showOrder' => $this->showOrder
058cbd6a 114 ]);
20933e61
MS
115 }
116
117 /**
0fcfe5f6 118 * @inheritDoc
20933e61
MS
119 */
120 public function readData() {
121 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.condition.notice');
122 foreach ($objectTypes as $objectType) {
123 if (!$objectType->conditionobject) continue;
124
125 if (!isset($this->groupedConditionObjectTypes[$objectType->conditionobject])) {
058cbd6a 126 $this->groupedConditionObjectTypes[$objectType->conditionobject] = [];
20933e61
MS
127 }
128
129 if ($objectType->conditiongroup) {
130 if (!isset($this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->conditiongroup])) {
058cbd6a 131 $this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->conditiongroup] = [];
20933e61
MS
132 }
133
134 $this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->conditiongroup][$objectType->objectTypeID] = $objectType;
135 }
136 else {
137 $this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->objectTypeID] = $objectType;
138 }
139 }
140
141 parent::readData();
142 }
143
144 /**
0fcfe5f6 145 * @inheritDoc
20933e61
MS
146 */
147 public function readFormParameters() {
148 parent::readFormParameters();
149
150 I18nHandler::getInstance()->readValues();
151
e70b5175
MS
152 if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
153 if (isset($_POST['customCssClassName'])) $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']);
20933e61
MS
154 if (isset($_POST['isDisabled'])) $this->isDisabled = 1;
155 if (isset($_POST['isDismissible'])) $this->isDismissible = 1;
156 if (isset($_POST['noticeName'])) $this->noticeName = StringUtil::trim($_POST['noticeName']);
157 if (isset($_POST['noticeUseHtml'])) $this->noticeUseHtml = 1;
158 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
159
160 foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) {
161 foreach ($groupedObjectTypes as $objectTypes) {
162 if (is_array($objectTypes)) {
163 foreach ($objectTypes as $objectType) {
164 $objectType->getProcessor()->readFormParameters();
165 }
166 }
167 else {
168 $objectTypes->getProcessor()->readFormParameters();
169 }
170 }
171 }
172 }
173
174 /**
0fcfe5f6 175 * @inheritDoc
20933e61
MS
176 */
177 public function readParameters() {
178 parent::readParameters();
179
180 I18nHandler::getInstance()->register('notice');
181 }
182
183 /**
0fcfe5f6 184 * @inheritDoc
20933e61
MS
185 */
186 public function save() {
187 parent::save();
188
058cbd6a
MS
189 $this->objectAction = new NoticeAction([], 'create', [
190 'data' => array_merge($this->additionalFields, [
e70b5175 191 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName,
20933e61
MS
192 'isDisabled' => $this->isDisabled,
193 'isDismissible' => $this->isDismissible,
194 'notice' => I18nHandler::getInstance()->isPlainValue('notice') ? I18nHandler::getInstance()->getValue('notice') : '',
195 'noticeName' => $this->noticeName,
196 'noticeUseHtml' => $this->noticeUseHtml,
197 'showOrder' => $this->showOrder
058cbd6a
MS
198 ])
199 ]);
20933e61
MS
200 $returnValues = $this->objectAction->executeAction();
201
202 if (!I18nHandler::getInstance()->isPlainValue('notice')) {
203 I18nHandler::getInstance()->save('notice', 'wcf.notice.notice.notice'.$returnValues['returnValues']->noticeID, 'wcf.notice', 1);
204
205 // update notice name
206 $noticeEditor = new NoticeEditor($returnValues['returnValues']);
058cbd6a 207 $noticeEditor->update([
20933e61 208 'notice' => 'wcf.notice.notice.notice'.$returnValues['returnValues']->noticeID
058cbd6a 209 ]);
20933e61
MS
210 }
211
212 // transform conditions array into one-dimensional array
058cbd6a 213 $conditions = [];
20933e61
MS
214 foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) {
215 foreach ($groupedObjectTypes as $objectTypes) {
216 if (is_array($objectTypes)) {
217 $conditions = array_merge($conditions, $objectTypes);
218 }
219 else {
220 $conditions[] = $objectTypes;
221 }
222 }
223 }
224
225 ConditionHandler::getInstance()->createConditions($returnValues['returnValues']->noticeID, $conditions);
226
227 $this->saved();
228
229 // reset values
e70b5175
MS
230 $this->cssClassName = '';
231 $this->customCssClassName = '';
20933e61
MS
232 $this->isDisabled = 0;
233 $this->isDismissible = 0;
234 $this->noticeName = '';
235 $this->noticeUseHtml = 0;
236 $this->showOrder = 0;
237 I18nHandler::getInstance()->reset();
238
239 foreach ($conditions as $condition) {
240 $condition->getProcessor()->reset();
241 }
242
243 WCF::getTPL()->assign('success', true);
244 }
245
246 /**
0fcfe5f6 247 * @inheritDoc
20933e61
MS
248 */
249 public function validate() {
250 parent::validate();
251
252 if (empty($this->noticeName)) {
253 throw new UserInputException('noticeName');
254 }
255
256 if (!I18nHandler::getInstance()->validateValue('notice')) {
257 if (I18nHandler::getInstance()->isPlainValue('notice')) {
258 throw new UserInputException('notice');
259 }
260 else {
261 throw new UserInputException('notice', 'multilingual');
262 }
263 }
264
e70b5175
MS
265 // validate class name
266 if (empty($this->cssClassName)) {
267 throw new UserInputException('cssClassName');
268 }
269 else if (!in_array($this->cssClassName, $this->availableCssClassNames)) {
063bbf46 270 throw new UserInputException('cssClassName', 'invalid');
e70b5175
MS
271 }
272 else if ($this->cssClassName == 'custom') {
273 if (empty($this->cssClassName)) {
274 throw new UserInputException('cssClassName');
275 }
276 if (!Regex::compile('^-?[_a-zA-Z]+[_a-zA-Z0-9-]+$')->match($this->customCssClassName)) {
063bbf46 277 throw new UserInputException('cssClassName', 'invalid');
e70b5175
MS
278 }
279 }
280
20933e61
MS
281 foreach ($this->groupedConditionObjectTypes as $groupedObjectTypes) {
282 foreach ($groupedObjectTypes as $objectTypes) {
283 if (is_array($objectTypes)) {
284 foreach ($objectTypes as $objectType) {
285 $objectType->getProcessor()->validate();
286 }
287 }
288 else {
289 $objectTypes->getProcessor()->validate();
290 }
291 }
292 }
293 }
294}