Add possibility to set appearance of notices
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / NoticeAddForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\object\type\ObjectTypeCache;
4 use wcf\data\notice\NoticeAction;
5 use wcf\data\notice\NoticeEditor;
6 use wcf\form\AbstractForm;
7 use wcf\system\condition\ConditionHandler;
8 use wcf\system\exception\UserInputException;
9 use wcf\system\language\I18nHandler;
10 use wcf\system\Regex;
11 use wcf\system\WCF;
12 use wcf\util\StringUtil;
13
14 /**
15 * Shows the form to create a new notice.
16 *
17 * @author Matthias Schmidt
18 * @copyright 2001-2014 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package com.woltlab.wcf
21 * @subpackage acp.form
22 * @category Community Framework
23 */
24 class NoticeAddForm extends AbstractForm {
25 /**
26 * @see \wcf\page\AbstractPage::$activeMenuItem
27 */
28 public $activeMenuItem = 'wcf.acp.menu.link.notice.add';
29
30 /**
31 * list pf pre-defined CSS class names
32 * @var array<string>
33 */
34 public $availableCssClassNames = array(
35 'info',
36 'warning',
37 'error',
38
39 'custom'
40 );
41
42 /**
43 * name of the chosen CSS class name
44 * @var string
45 */
46 public $cssClassName = '';
47
48 /**
49 * custom CSS class name
50 * @var string
51 */
52 public $customCssClassName = '';
53
54 /**
55 * grouped notice condition object types
56 * @var array
57 */
58 public $groupedConditionObjectTypes = array();
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 /**
73 * @see \wcf\page\AbstractPage::$neededPermissions
74 */
75 public $neededPermissions = array('admin.content.notice.canManageNotice');
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 /**
96 * @see \wcf\page\IPage::assignVariables()
97 */
98 public function assignVariables() {
99 parent::assignVariables();
100
101 I18nHandler::getInstance()->assignVariables();
102
103 WCF::getTPL()->assign(array(
104 'action' => 'add',
105 'availableCssClassNames' => $this->availableCssClassNames,
106 'cssClassName' => $this->cssClassName,
107 'customCssClassName' => $this->customCssClassName,
108 'isDisabled' => $this->isDisabled,
109 'isDismissible' => $this->isDismissible,
110 'groupedConditionObjectTypes' => $this->groupedConditionObjectTypes,
111 'noticeName' => $this->noticeName,
112 'noticeUseHtml' => $this->noticeUseHtml,
113 'showOrder' => $this->showOrder
114 ));
115 }
116
117 /**
118 * @see \wcf\page\IPage::readData()
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])) {
126 $this->groupedConditionObjectTypes[$objectType->conditionobject] = array();
127 }
128
129 if ($objectType->conditiongroup) {
130 if (!isset($this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->conditiongroup])) {
131 $this->groupedConditionObjectTypes[$objectType->conditionobject][$objectType->conditiongroup] = array();
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 /**
145 * @see \wcf\form\IForm::readFormParameters()
146 */
147 public function readFormParameters() {
148 parent::readFormParameters();
149
150 I18nHandler::getInstance()->readValues();
151
152 if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
153 if (isset($_POST['customCssClassName'])) $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']);
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 /**
175 * @see \wcf\page\IPage::readParameters()
176 */
177 public function readParameters() {
178 parent::readParameters();
179
180 I18nHandler::getInstance()->register('notice');
181 }
182
183 /**
184 * @see \wcf\form\IForm::save()
185 */
186 public function save() {
187 parent::save();
188
189 $this->objectAction = new NoticeAction(array(), 'create', array(
190 'data' => array_merge($this->additionalFields, array(
191 'cssClassName' => $this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName,
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
198 ))
199 ));
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']);
207 $noticeEditor->update(array(
208 'notice' => 'wcf.notice.notice.notice'.$returnValues['returnValues']->noticeID
209 ));
210 }
211
212 // transform conditions array into one-dimensional array
213 $conditions = array();
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
230 $this->cssClassName = '';
231 $this->customCssClassName = '';
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 /**
247 * @see \wcf\form\IForm::validate()
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
265 // validate class name
266 if (empty($this->cssClassName)) {
267 throw new UserInputException('cssClassName');
268 }
269 else if (!in_array($this->cssClassName, $this->availableCssClassNames)) {
270 throw new UserInputException('cssClassName', 'notValid');
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)) {
277 throw new UserInputException('cssClassName', 'notValid');
278 }
279 }
280
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 }