b907a3eab84edaee8d7c8eab17a9b236272f7383
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / LabelGroupEditForm.class.php
1 <?php
2
3 namespace wcf\acp\form;
4
5 use wcf\data\label\group\LabelGroup;
6 use wcf\data\label\group\LabelGroupAction;
7 use wcf\form\AbstractForm;
8 use wcf\system\acl\ACLHandler;
9 use wcf\system\exception\IllegalLinkException;
10 use wcf\system\language\I18nHandler;
11 use wcf\system\WCF;
12
13 /**
14 * Shows the label group edit form.
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Acp\Form
20 */
21 class LabelGroupEditForm extends LabelGroupAddForm
22 {
23 /**
24 * @inheritDoc
25 */
26 public $activeMenuItem = 'wcf.acp.menu.link.label.group.list';
27
28 /**
29 * @inheritDoc
30 */
31 public $neededPermissions = ['admin.content.label.canManageLabel'];
32
33 /**
34 * group id
35 * @var int
36 */
37 public $groupID = 0;
38
39 /**
40 * label group object
41 * @var LabelGroup
42 */
43 public $group;
44
45 /**
46 * @inheritDoc
47 */
48 public function readParameters()
49 {
50 parent::readParameters();
51
52 if (isset($_REQUEST['id'])) {
53 $this->groupID = \intval($_REQUEST['id']);
54 }
55 $this->group = new LabelGroup($this->groupID);
56 if (!$this->group->groupID) {
57 throw new IllegalLinkException();
58 }
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public function save()
65 {
66 AbstractForm::save();
67
68 $this->groupName = 'wcf.acp.label.group' . $this->group->groupID;
69 if (I18nHandler::getInstance()->isPlainValue('groupName')) {
70 I18nHandler::getInstance()->remove($this->groupName);
71 $this->groupName = I18nHandler::getInstance()->getValue('groupName');
72 } else {
73 I18nHandler::getInstance()->save('groupName', $this->groupName, 'wcf.acp.label', 1);
74 }
75
76 // update label
77 $this->objectAction = new LabelGroupAction(
78 [$this->groupID],
79 'update',
80 [
81 'data' => \array_merge($this->additionalFields, [
82 'forceSelection' => $this->forceSelection ? 1 : 0,
83 'groupName' => $this->groupName,
84 'groupDescription' => $this->groupDescription,
85 'showOrder' => $this->showOrder,
86 ]),
87 ]
88 );
89 $this->objectAction->executeAction();
90
91 // update acl
92 ACLHandler::getInstance()->save($this->groupID, $this->objectTypeID);
93 ACLHandler::getInstance()->disableAssignVariables();
94
95 // update object type relations
96 $this->saveObjectTypeRelations($this->groupID);
97
98 foreach ($this->labelObjectTypes as $objectTypeID => $labelObjectType) {
99 $labelObjectType->save();
100 }
101
102 $this->saved();
103
104 // show success message
105 WCF::getTPL()->assign('success', true);
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function readData()
112 {
113 parent::readData();
114
115 if (empty($_POST)) {
116 I18nHandler::getInstance()->setOptions('groupName', 1, $this->group->groupName, 'wcf.acp.label.group\d+');
117
118 $this->forceSelection = ($this->group->forceSelection ? true : false);
119 $this->groupName = $this->group->groupName;
120 $this->groupDescription = $this->group->groupDescription;
121 $this->showOrder = $this->group->showOrder;
122 }
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function assignVariables()
129 {
130 parent::assignVariables();
131
132 I18nHandler::getInstance()->assignVariables(!empty($_POST));
133
134 WCF::getTPL()->assign([
135 'action' => 'edit',
136 'groupID' => $this->groupID,
137 'labelGroup' => $this->group,
138 ]);
139 }
140
141 /**
142 * @inheritDoc
143 */
144 protected function setObjectTypeRelations($data = null)
145 {
146 if (empty($_POST)) {
147 // read database values
148 $sql = "SELECT objectTypeID, objectID
149 FROM wcf" . WCF_N . "_label_group_to_object
150 WHERE groupID = ?";
151 $statement = WCF::getDB()->prepareStatement($sql);
152 $statement->execute([$this->groupID]);
153
154 $data = [];
155 while ($row = $statement->fetchArray()) {
156 if (!isset($data[$row['objectTypeID']])) {
157 $data[$row['objectTypeID']] = [];
158 }
159
160 // prevent NULL values which confuse isset()
161 $data[$row['objectTypeID']][] = $row['objectID'] ?: 0;
162 }
163 }
164
165 parent::setObjectTypeRelations($data);
166 }
167 }