3 namespace wcf\acp\form
;
5 use wcf\data\label\group\LabelGroupAction
;
6 use wcf\data\label\group\LabelGroupEditor
;
7 use wcf\data\
object\type\ObjectTypeCache
;
8 use wcf\form\AbstractForm
;
9 use wcf\system\acl\ACLHandler
;
10 use wcf\system\exception\UserInputException
;
11 use wcf\system\label\
object\type\ILabelObjectTypeHandler
;
12 use wcf\system\label\
object\type\LabelObjectTypeContainer
;
13 use wcf\system\language\I18nHandler
;
14 use wcf\system\request\LinkHandler
;
16 use wcf\util\StringUtil
;
19 * Shows the label group add form.
21 * @author Alexander Ebert
22 * @copyright 2001-2019 WoltLab GmbH
23 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
24 * @package WoltLabSuite\Core\Acp\Form
26 class LabelGroupAddForm
extends AbstractForm
31 public $activeMenuItem = 'wcf.acp.menu.link.label.group.add';
36 public $neededPermissions = ['admin.content.label.canManageLabel'];
39 * force users to select a label
42 public $forceSelection = false;
48 public $groupName = '';
54 public $groupDescription = '';
57 * list of label object type handlers
58 * @var ILabelObjectTypeHandler[]
60 public $labelObjectTypes = [];
63 * list of label object type containers
64 * @var LabelObjectTypeContainer[]
66 public $labelObjectTypeContainers = [];
69 * list of label group to object type relations
72 public $objectTypes = [];
78 public $objectTypeID = 0;
84 public $showOrder = 0;
89 public function readParameters()
91 parent
::readParameters();
93 $this->objectTypeID
= ACLHandler
::getInstance()->getObjectTypeID('com.woltlab.wcf.label');
95 I18nHandler
::getInstance()->register('groupName');
101 public function readFormParameters()
103 parent
::readFormParameters();
105 I18nHandler
::getInstance()->readValues();
107 if (I18nHandler
::getInstance()->isPlainValue('groupName')) {
108 $this->groupName
= I18nHandler
::getInstance()->getValue('groupName');
111 if (isset($_POST['groupDescription'])) {
112 $this->groupDescription
= StringUtil
::trim($_POST['groupDescription']);
114 if (isset($_POST['forceSelection'])) {
115 $this->forceSelection
= true;
117 if (isset($_POST['objectTypes']) && \
is_array($_POST['objectTypes'])) {
118 $this->objectTypes
= $_POST['objectTypes'];
120 if (isset($_POST['showOrder'])) {
121 $this->showOrder
= \
intval($_POST['showOrder']);
128 public function readData()
130 // get label object type handlers
131 $objectTypes = ObjectTypeCache
::getInstance()->getObjectTypes('com.woltlab.wcf.label.objectType');
132 foreach ($objectTypes as $objectType) {
133 $this->labelObjectTypes
[$objectType->objectTypeID
] = $objectType->getProcessor();
134 $this->labelObjectTypes
[$objectType->objectTypeID
]->setObjectTypeID($objectType->objectTypeID
);
137 foreach ($this->labelObjectTypes
as $objectTypeID => $labelObjectType) {
138 $this->labelObjectTypeContainers
[$objectTypeID] = $labelObjectType->getContainer();
143 // assign new values for object relations
144 $this->setObjectTypeRelations();
150 public function validate()
154 // validate group name
155 if (!I18nHandler
::getInstance()->validateValue('groupName')) {
156 if (I18nHandler
::getInstance()->isPlainValue('groupName')) {
157 throw new UserInputException('groupName');
159 throw new UserInputException('groupName', 'multilingual');
163 // validate object type relations
164 foreach ($this->objectTypes
as $objectTypeID => $data) {
165 if (!isset($this->labelObjectTypes
[$objectTypeID])) {
166 unset($this->objectTypes
[$objectTypeID]);
174 public function save()
179 $this->objectAction
= new LabelGroupAction([], 'create', [
180 'data' => \array_merge
($this->additionalFields
, [
181 'forceSelection' => $this->forceSelection ?
1 : 0,
182 'groupName' => $this->groupName
,
183 'groupDescription' => $this->groupDescription
,
184 'showOrder' => $this->showOrder
,
187 $returnValues = $this->objectAction
->executeAction();
189 if (!I18nHandler
::getInstance()->isPlainValue('groupName')) {
190 I18nHandler
::getInstance()->save(
192 'wcf.acp.label.group' . $returnValues['returnValues']->groupID
,
198 $groupEditor = new LabelGroupEditor($returnValues['returnValues']);
199 $groupEditor->update([
200 'groupName' => 'wcf.acp.label.group' . $returnValues['returnValues']->groupID
,
205 ACLHandler
::getInstance()->save($returnValues['returnValues']->groupID
, $this->objectTypeID
);
206 ACLHandler
::getInstance()->disableAssignVariables();
208 // save object type relations
209 $this->saveObjectTypeRelations($returnValues['returnValues']->groupID
);
211 foreach ($this->labelObjectTypes
as $labelObjectType) {
212 $labelObjectType->save();
218 $this->forceSelection
= false;
219 $this->groupName
= $this->groupDescription
= '';
220 $this->objectTypes
= [];
221 $this->showOrder
= 0;
222 $this->setObjectTypeRelations();
224 // show success message
225 WCF
::getTPL()->assign([
227 'objectEditLink' => LinkHandler
::getInstance()->getControllerLink(
228 LabelGroupEditForm
::class,
229 ['id' => $returnValues['returnValues']->groupID
]
233 I18nHandler
::getInstance()->reset();
239 public function assignVariables()
241 parent
::assignVariables();
243 ACLHandler
::getInstance()->assignVariables($this->objectTypeID
);
244 I18nHandler
::getInstance()->assignVariables();
246 WCF
::getTPL()->assign([
248 'forceSelection' => $this->forceSelection
,
249 'groupName' => $this->groupName
,
250 'groupDescription' => $this->groupDescription
,
251 'labelObjectTypeContainers' => $this->labelObjectTypeContainers
,
252 'objectTypeID' => $this->objectTypeID
,
253 'showOrder' => $this->showOrder
,
258 * Saves label group to object relations.
260 * @param int $groupID
262 protected function saveObjectTypeRelations($groupID)
264 WCF
::getDB()->beginTransaction();
266 // remove old relations
267 if ($groupID !== null) {
268 $sql = "DELETE FROM wcf" . WCF_N
. "_label_group_to_object
270 $statement = WCF
::getDB()->prepareStatement($sql);
271 $statement->execute([$groupID]);
274 // insert new relations
275 if (!empty($this->objectTypes
)) {
276 $sql = "INSERT INTO wcf" . WCF_N
. "_label_group_to_object
277 (groupID, objectTypeID, objectID)
279 $statement = WCF
::getDB()->prepareStatement($sql);
281 foreach ($this->objectTypes
as $objectTypeID => $data) {
282 foreach ($data as $objectID) {
283 // use "0" (stored as NULL) for simple true/false states
288 $statement->execute([
297 WCF
::getDB()->commitTransaction();
301 * Sets object type relations.
303 * @param array|null $data
305 protected function setObjectTypeRelations($data = null)
307 if (!empty($_POST)) {
309 $data = &$this->objectTypes
;
312 foreach ($this->labelObjectTypeContainers
as $objectTypeID => $container) {
313 $hasData = isset($data[$objectTypeID]);
314 foreach ($container as $object) {
316 $object->setOptionValue(0);
318 $optionValue = \
in_array($object->getObjectID(), $data[$objectTypeID]) ?
1 : 0;
319 $object->setOptionValue($optionValue);