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