Updating minified JavaScript files
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / LabelGroupAddForm.class.php
CommitLineData
3b75466f
MW
1<?php
2namespace wcf\acp\form;
3use wcf\data\label\group\LabelGroupAction;
4use wcf\data\object\type\ObjectTypeCache;
5use wcf\form\AbstractForm;
6use wcf\system\acl\ACLHandler;
7use wcf\system\exception\UserInputException;
8use wcf\system\WCF;
9use wcf\util\StringUtil;
10
11/**
12 * Shows the label group add form.
13 *
14 * @author Alexander Ebert
ca4ba303 15 * @copyright 2001-2014 WoltLab GmbH
3b75466f 16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 17 * @package com.woltlab.wcf
3b75466f
MW
18 * @subpackage acp.form
19 * @category Community Framework
20 */
21class LabelGroupAddForm extends AbstractForm {
22 /**
0ad90fc3 23 * @see \wcf\page\AbstractPage::$activeMenuItem
3b75466f
MW
24 */
25 public $activeMenuItem = 'wcf.acp.menu.link.label.group.add';
26
27 /**
0ad90fc3 28 * @see \wcf\page\AbstractPage::$neededPermissions
3b75466f
MW
29 */
30 public $neededPermissions = array('admin.content.label.canManageLabel');
31
32 /**
33 * force users to select a label
34 * @var boolean
35 */
36 public $forceSelection = false;
37
38 /**
39 * group name
40 * @var string
41 */
42 public $groupName = '';
43
44 /**
45 * list of label object type handlers
0ad90fc3 46 * @var array<\wcf\system\label\object\type\ILabelObjectTypeHandler>
3b75466f
MW
47 */
48 public $labelObjectTypes = array();
49
50 /**
51 * list of label object type containers
0ad90fc3 52 * @var array<\wcf\system\label\object\type\LabelObjectTypeContainer>
3b75466f
MW
53 */
54 public $labelObjectTypeContainers = array();
55
56 /**
57 * list of label group to object type relations
58 * @var array<array>
59 */
60 public $objectTypes = array();
61
62 /**
63 * object type id
64 * @var integer
65 */
66 public $objectTypeID = 0;
67
68 /**
0ad90fc3 69 * @see \wcf\page\AbstractPage::readParameters()
3b75466f
MW
70 */
71 public function readParameters() {
72 parent::readParameters();
73
74 $this->objectTypeID = ACLHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.label');
75 }
76
77 /**
0ad90fc3 78 * @see \wcf\form\IForm::readFormParameters()
3b75466f
MW
79 */
80 public function readFormParameters() {
81 parent::readFormParameters();
82
83 if (isset($_POST['forceSelection'])) $this->forceSelection = true;
84 if (isset($_POST['groupName'])) $this->groupName = StringUtil::trim($_POST['groupName']);
85 if (isset($_POST['objectTypes']) && is_array($_POST['objectTypes'])) $this->objectTypes = $_POST['objectTypes'];
86 }
87
88 /**
0ad90fc3 89 * @see \wcf\page\IPage::readData()
3b75466f
MW
90 */
91 public function readData() {
92 // get label object type handlers
93 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.label.objectType');
94 foreach ($objectTypes as $objectType) {
95 $this->labelObjectTypes[$objectType->objectTypeID] = $objectType->getProcessor();
96 $this->labelObjectTypes[$objectType->objectTypeID]->setObjectTypeID($objectType->objectTypeID);
97 }
98
99 foreach ($this->labelObjectTypes as $objectTypeID => $labelObjectType) {
100 $this->labelObjectTypeContainers[$objectTypeID] = $labelObjectType->getContainer();
101 }
102
103 parent::readData();
104
105 // assign new values for object relations
106 $this->setObjectTypeRelations();
107 }
108
109 /**
0ad90fc3 110 * @see \wcf\form\IForm::validate()
3b75466f
MW
111 */
112 public function validate() {
113 parent::validate();
114
115 // validate class name
116 if (empty($this->groupName)) {
117 throw new UserInputException('groupName');
118 }
119
120 // validate object type relations
121 foreach ($this->objectTypes as $objectTypeID => $data) {
122 if (!isset($this->labelObjectTypes[$objectTypeID])) {
123 unset($this->objectTypes[$objectTypeID]);
124 }
125 }
126 }
127
128 /**
0ad90fc3 129 * @see \wcf\form\IForm::save()
3b75466f
MW
130 */
131 public function save() {
132 parent::save();
133
134 // save label
e94d7556 135 $this->objectAction = new LabelGroupAction(array(), 'create', array('data' => array_merge($this->additionalFields, array(
3b75466f
MW
136 'forceSelection' => ($this->forceSelection ? 1 : 0),
137 'groupName' => $this->groupName
e94d7556 138 ))));
3b75466f 139 $returnValues = $this->objectAction->executeAction();
c7e6ce11 140
3b75466f
MW
141 // save acl
142 ACLHandler::getInstance()->save($returnValues['returnValues']->groupID, $this->objectTypeID);
c7e6ce11 143 ACLHandler::getInstance()->disableAssignVariables();
3b75466f
MW
144
145 // save object type relations
146 $this->saveObjectTypeRelations($returnValues['returnValues']->groupID);
c7e6ce11 147
526ccccd
AE
148 foreach ($this->labelObjectTypes as $objectTypeID => $labelObjectType) {
149 $labelObjectType->save();
150 }
151
3b75466f
MW
152 $this->saved();
153
154 // reset values
155 $this->forceSelection = false;
156 $this->groupName = '';
157 $this->objectTypes = array();
158 $this->setObjectTypeRelations();
159
160 // show success
161 WCF::getTPL()->assign(array(
162 'success' => true
163 ));
164 }
165
166 /**
0ad90fc3 167 * @see \wcf\page\IPage::assignVariables()
3b75466f
MW
168 */
169 public function assignVariables() {
170 parent::assignVariables();
171
c7e6ce11
MS
172 ACLHandler::getInstance()->assignVariables($this->objectTypeID);
173
3b75466f
MW
174 WCF::getTPL()->assign(array(
175 'action' => 'add',
176 'forceSelection' => $this->forceSelection,
177 'groupName' => $this->groupName,
178 'labelObjectTypeContainers' => $this->labelObjectTypeContainers,
179 'objectTypeID' => $this->objectTypeID
180 ));
181 }
182
183 /**
184 * Saves label group to object relations.
185 *
186 * @param integer $groupID
187 */
188 protected function saveObjectTypeRelations($groupID) {
189 WCF::getDB()->beginTransaction();
190
191 // remove old relations
192 if ($groupID !== null) {
193 $sql = "DELETE FROM wcf".WCF_N."_label_group_to_object
194 WHERE groupID = ?";
195 $statement = WCF::getDB()->prepareStatement($sql);
196 $statement->execute(array($groupID));
197 }
198
199 // insert new relations
200 if (!empty($this->objectTypes)) {
201 $sql = "INSERT INTO wcf".WCF_N."_label_group_to_object
202 (groupID, objectTypeID, objectID)
203 VALUES (?, ?, ?)";
204 $statement = WCF::getDB()->prepareStatement($sql);
205
206 foreach ($this->objectTypes as $objectTypeID => $data) {
207 foreach ($data as $objectID) {
208 // use "0" (stored as NULL) for simple true/false states
209 if (!$objectID) $objectID = null;
210
211 $statement->execute(array(
212 $groupID,
213 $objectTypeID,
214 $objectID
215 ));
216 }
217 }
218 }
219
220 WCF::getDB()->commitTransaction();
221 }
222
223 /**
224 * Sets object type relations.
225 */
226 protected function setObjectTypeRelations($data = null) {
227 if (!empty($_POST)) {
228 // use POST data
229 $data = &$this->objectTypes;
230 }
231
232 // no data provided and no POST data exists
526ccccd 233 /*if ($data === null || !is_array($data)) {
3b75466f
MW
234 // nothing to do here
235 return;
526ccccd 236 }*/
3b75466f
MW
237
238 foreach ($this->labelObjectTypeContainers as $objectTypeID => $container) {
239 if ($container->isBooleanOption()) {
240 $optionValue = (isset($data[$objectTypeID])) ? 1 : 0;
241 $container->setOptionValue($optionValue);
242 }
243 else {
244 $hasData = (isset($data[$objectTypeID]));
245 foreach ($container as $object) {
246 if (!$hasData) {
247 $object->setOptionValue(0);
248 }
249 else {
250 $optionValue = (in_array($object->getObjectID(), $data[$objectTypeID])) ? 1 : 0;
251 $object->setOptionValue($optionValue);
252 }
253 }
254 }
255 }
256 }
257}