Add possibility to sort labels in ACP
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / LabelAddForm.class.php
CommitLineData
3b75466f
MW
1<?php
2namespace wcf\acp\form;
3use wcf\data\label\group\LabelGroupList;
4use wcf\data\label\LabelAction;
5use wcf\data\label\LabelEditor;
526ccccd 6use wcf\data\object\type\ObjectTypeCache;
3b75466f
MW
7use wcf\form\AbstractForm;
8use wcf\system\exception\UserInputException;
9use wcf\system\language\I18nHandler;
10use wcf\system\Regex;
11use wcf\system\WCF;
12use wcf\util\StringUtil;
13
14/**
15 * Shows the label add form.
16 *
17 * @author Alexander Ebert
d8475f48 18 * @copyright 2001-2016 WoltLab GmbH
3b75466f 19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 20 * @package com.woltlab.wcf
3b75466f
MW
21 * @subpackage acp.form
22 * @category Community Framework
23 */
24class LabelAddForm extends AbstractForm {
25 /**
d8475f48 26 * @inheritDoc
3b75466f
MW
27 */
28 public $activeMenuItem = 'wcf.acp.menu.link.label.add';
29
30 /**
d8475f48 31 * @inheritDoc
3b75466f 32 */
d8475f48 33 public $neededPermissions = ['admin.content.label.canManageLabel'];
3b75466f
MW
34
35 /**
36 * label group id
37 * @var integer
38 */
39 public $groupID = 0;
40
41 /**
42 * label value
43 * @var string
44 */
45 public $label = '';
46
47 /**
48 * label group list object
0ad90fc3 49 * @var \wcf\data\label\group\LabelGroupList
3b75466f
MW
50 */
51 public $labelGroupList = null;
52
53 /**
54 * CSS class name
55 * @var string
56 */
57 public $cssClassName = '';
58
59 /**
60 * custom CSS class name
61 * @var string
62 */
63 public $customCssClassName = '';
64
65 /**
66 * list of pre-defined css class names
d8475f48 67 * @var string[]
3b75466f 68 */
d8475f48 69 public $availableCssClassNames = [
3b75466f
MW
70 'yellow',
71 'orange',
72 'brown',
73 'red',
74 'pink',
75 'purple',
76 'blue',
77 'green',
78 'black',
79
80 'none', /* not a real value */
81 'custom' /* not a real value */
d8475f48 82 ];
3b75466f
MW
83
84 /**
d8475f48
MS
85 * show order
86 * @var integer
87 */
88 public $showOrder = 0;
89
90 /**
91 * @inheritDoc
3b75466f
MW
92 */
93 public function readParameters() {
94 parent::readParameters();
95
96 I18nHandler::getInstance()->register('label');
97 }
98
99 /**
d8475f48 100 * @inheritDoc
3b75466f
MW
101 */
102 public function readFormParameters() {
103 parent::readFormParameters();
104
105 I18nHandler::getInstance()->readValues();
106
107 if (I18nHandler::getInstance()->isPlainValue('label')) $this->label = I18nHandler::getInstance()->getValue('label');
108 if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
109 if (isset($_POST['customCssClassName'])) $this->customCssClassName = StringUtil::trim($_POST['customCssClassName']);
110 if (isset($_POST['groupID'])) $this->groupID = intval($_POST['groupID']);
d8475f48 111 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
3b75466f
MW
112 }
113
114 /**
d8475f48 115 * @inheritDoc
3b75466f
MW
116 */
117 public function validate() {
118 parent::validate();
119
bfde33f9
MS
120 // validate group
121 if (!$this->groupID) {
122 throw new UserInputException('groupID');
123 }
124 $groups = $this->labelGroupList->getObjects();
125 if (!isset($groups[$this->groupID])) {
126 throw new UserInputException('groupID', 'notValid');
127 }
128
3b75466f
MW
129 // validate label
130 if (!I18nHandler::getInstance()->validateValue('label')) {
131 if (I18nHandler::getInstance()->isPlainValue('label')) {
132 throw new UserInputException('label');
133 }
134 else {
135 throw new UserInputException('label', 'multilingual');
136 }
137 }
138
bfde33f9 139 // validate class name
3b75466f
MW
140 if (empty($this->cssClassName)) {
141 throw new UserInputException('cssClassName', 'empty');
142 }
143 else if (!in_array($this->cssClassName, $this->availableCssClassNames)) {
144 throw new UserInputException('cssClassName', 'notValid');
145 }
146 else if ($this->cssClassName == 'custom') {
147 if (!empty($this->customCssClassName) && !Regex::compile('^-?[_a-zA-Z]+[_a-zA-Z0-9-]+$')->match($this->customCssClassName)) {
148 throw new UserInputException('cssClassName', 'notValid');
149 }
150 }
d8475f48
MS
151
152 if ($this->showOrder < 0) $this->showOrder = 0;
3b75466f
MW
153 }
154
155 /**
d8475f48 156 * @inheritDoc
3b75466f
MW
157 */
158 public function save() {
159 parent::save();
160
161 // save label
d8475f48 162 $this->objectAction = new LabelAction([], 'create', ['data' => array_merge($this->additionalFields, [
3b75466f
MW
163 'label' => $this->label,
164 'cssClassName' => ($this->cssClassName == 'custom' ? $this->customCssClassName : $this->cssClassName),
d8475f48
MS
165 'groupID' => $this->groupID,
166 'showOrder' => $this->showOrder
167 ])]);
3b75466f
MW
168 $this->objectAction->executeAction();
169
170 if (!I18nHandler::getInstance()->isPlainValue('label')) {
171 $returnValues = $this->objectAction->getReturnValues();
172 $labelID = $returnValues['returnValues']->labelID;
a37b39b6 173 I18nHandler::getInstance()->save('label', 'wcf.acp.label.label'.$labelID, 'wcf.acp.label', 1);
3b75466f
MW
174
175 // update group name
176 $labelEditor = new LabelEditor($returnValues['returnValues']);
d8475f48 177 $labelEditor->update([
3b75466f 178 'label' => 'wcf.acp.label.label'.$labelID
d8475f48 179 ]);
3b75466f
MW
180 }
181
526ccccd
AE
182 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.label.objectType');
183 foreach ($objectTypes as $objectType) {
184 $objectType->getProcessor()->save();
185 }
186
3b75466f
MW
187 $this->saved();
188
189 // reset values
190 $this->label = $this->cssClassName = $this->customCssClassName = '';
d8475f48 191 $this->groupID = $this->showOrder = 0;
3b75466f
MW
192
193 I18nHandler::getInstance()->reset();
194
195 // show success
d8475f48 196 WCF::getTPL()->assign('success', true);
3b75466f
MW
197 }
198
199 /**
d8475f48 200 * @inheritDoc
3b75466f
MW
201 */
202 public function readData() {
203 $this->labelGroupList = new LabelGroupList();
204 $this->labelGroupList->readObjects();
205
206 parent::readData();
207 }
208
209 /**
d8475f48 210 * @inheritDoc
3b75466f
MW
211 */
212 public function assignVariables() {
213 parent::assignVariables();
214
215 I18nHandler::getInstance()->assignVariables();
216
d8475f48 217 WCF::getTPL()->assign([
3b75466f
MW
218 'action' => 'add',
219 'availableCssClassNames' => $this->availableCssClassNames,
220 'cssClassName' => $this->cssClassName,
221 'customCssClassName' => $this->customCssClassName,
222 'groupID' => $this->groupID,
223 'label' => $this->label,
d8475f48
MS
224 'labelGroupList' => $this->labelGroupList,
225 'showOrder' => $this->showOrder
226 ]);
3b75466f
MW
227 }
228}