Merge pull request #5987 from WoltLab/acp-dahsboard-box-hight
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / MenuAddForm.class.php
CommitLineData
e7fb3339 1<?php
a9229942 2
e7fb3339 3namespace wcf\acp\form;
a9229942 4
aa779877 5use wcf\data\box\Box;
e7fb3339
MW
6use wcf\data\menu\MenuAction;
7use wcf\data\menu\MenuEditor;
53cf1c00 8use wcf\data\page\PageNodeTree;
e7fb3339 9use wcf\form\AbstractForm;
a695628d 10use wcf\system\acl\simple\SimpleAclHandler;
53cf1c00 11use wcf\system\database\util\PreparedStatementConditionBuilder;
e7fb3339
MW
12use wcf\system\exception\UserInputException;
13use wcf\system\language\I18nHandler;
2dd2e0fe 14use wcf\system\language\LanguageFactory;
3fb859c9 15use wcf\system\request\LinkHandler;
e7fb3339 16use wcf\system\WCF;
53cf1c00 17use wcf\util\ArrayUtil;
aa779877 18use wcf\util\StringUtil;
e7fb3339
MW
19
20/**
21 * Shows the menu add form.
a9229942
TD
22 *
23 * @author Marcel Werk
24 * @copyright 2001-2019 WoltLab GmbH
25 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
a9229942 26 * @since 3.0
e7fb3339 27 */
a9229942
TD
28class MenuAddForm extends AbstractForm
29{
30 /**
31 * @inheritDoc
32 */
33 public $activeMenuItem = 'wcf.acp.menu.link.cms.menu.add';
34
35 /**
36 * @inheritDoc
37 */
38 public $neededPermissions = ['admin.content.cms.canManageMenu'];
39
40 /**
41 * menu title
42 * @var string
43 */
44 public $title = '';
45
46 /**
47 * box position
48 * @var string
49 */
50 public $position = '';
51
52 /**
53 * show order
54 * @var int
55 */
56 public $showOrder = 0;
57
58 /**
59 * true if created box is visible everywhere
60 * @var bool
61 */
62 public $visibleEverywhere = 1;
63
64 /**
65 * css class name of created box
66 * @var string
67 */
68 public $cssClassName = '';
69
70 /**
71 * true if box header is visible
72 * @var bool
73 */
74 public $showHeader = 1;
75
76 /**
77 * page ids
78 * @var int[]
79 */
80 public $pageIDs = [];
81
82 /**
83 * acl values
84 * @var array
85 */
86 public $aclValues = [];
87
88 /**
89 * @inheritDoc
90 */
91 public function readParameters()
92 {
93 parent::readParameters();
94
95 I18nHandler::getInstance()->register('title');
96 }
97
98 /**
99 * @inheritDoc
100 */
101 public function readFormParameters()
102 {
103 parent::readFormParameters();
104
105 I18nHandler::getInstance()->readValues();
106
107 if (I18nHandler::getInstance()->isPlainValue('title')) {
108 $this->title = I18nHandler::getInstance()->getValue('title');
109 }
110
111 $this->visibleEverywhere = $this->showHeader = $this->showOrder = 0;
112 if (isset($_POST['position'])) {
113 $this->position = $_POST['position'];
114 }
115 if (isset($_POST['showOrder'])) {
116 $this->showOrder = \intval($_POST['showOrder']);
117 }
118 if (isset($_POST['visibleEverywhere'])) {
119 $this->visibleEverywhere = \intval($_POST['visibleEverywhere']);
120 }
121 if (isset($_POST['cssClassName'])) {
122 $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
123 }
124 if (isset($_POST['showHeader'])) {
125 $this->showHeader = \intval($_POST['showHeader']);
126 }
127 if (isset($_POST['pageIDs']) && \is_array($_POST['pageIDs'])) {
128 $this->pageIDs = ArrayUtil::toIntegerArray($_POST['pageIDs']);
129 }
130 if (isset($_POST['aclValues']) && \is_array($_POST['aclValues'])) {
131 $this->aclValues = $_POST['aclValues'];
132 }
133 }
134
135 /**
136 * @inheritDoc
137 */
138 public function validate()
139 {
140 parent::validate();
141
142 // validate menu title
143 if (!I18nHandler::getInstance()->validateValue('title')) {
144 if (I18nHandler::getInstance()->isPlainValue('title')) {
145 throw new UserInputException('title');
146 } else {
147 throw new UserInputException('title', 'multilingual');
148 }
149 }
150
151 // validate box position
152 $this->validatePosition();
153
154 // validate page ids
155 if (!empty($this->pageIDs)) {
156 $conditionBuilder = new PreparedStatementConditionBuilder();
157 $conditionBuilder->add('pageID IN (?)', [$this->pageIDs]);
158 $sql = "SELECT pageID
159 FROM wcf" . WCF_N . "_page
160 " . $conditionBuilder;
161 $statement = WCF::getDB()->prepareStatement($sql);
162 $statement->execute($conditionBuilder->getParameters());
163 $this->pageIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
164 }
165 }
166
167 /**
168 * Validates box position.
169 *
170 * @throws UserInputException
171 */
172 protected function validatePosition()
173 {
174 if (!\in_array($this->position, Box::$availablePositions)) {
175 throw new UserInputException('position');
176 }
177 }
178
179 /**
180 * @inheritDoc
181 */
182 public function save()
183 {
184 parent::save();
185
186 $boxName = $this->title;
187 if (!I18nHandler::getInstance()->isPlainValue('title')) {
188 $values = I18nHandler::getInstance()->getValues('title');
189 $boxName = $values[LanguageFactory::getInstance()->getDefaultLanguageID()];
190 }
191
192 // save label
193 $this->objectAction = new MenuAction([], 'create', [
194 'data' => \array_merge($this->additionalFields, [
195 'title' => $this->title,
196 'packageID' => 1,
197 'identifier' => '',
198 ]),
199 'boxData' => [
200 'name' => $boxName,
201 'boxType' => 'menu',
202 'position' => $this->position,
203 'visibleEverywhere' => $this->visibleEverywhere ? 1 : 0,
204 'showHeader' => $this->showHeader ? 1 : 0,
205 'showOrder' => $this->showOrder,
206 'cssClassName' => $this->cssClassName,
207 'packageID' => 1,
208 ],
209 'pageIDs' => $this->pageIDs,
210 ]);
211 $returnValues = $this->objectAction->executeAction();
212 // set generic identifier
213 $menuEditor = new MenuEditor($returnValues['returnValues']);
214 $menuEditor->update([
215 'identifier' => 'com.woltlab.wcf.genericMenu' . $menuEditor->menuID,
216 ]);
217 // save i18n
218 if (!I18nHandler::getInstance()->isPlainValue('title')) {
219 I18nHandler::getInstance()->save(
220 'title',
221 'wcf.menu.com.woltlab.wcf.genericMenu' . $menuEditor->menuID,
222 'wcf.menu',
223 1
224 );
225
226 // update title
227 $menuEditor->update([
228 'title' => 'wcf.menu.com.woltlab.wcf.genericMenu' . $menuEditor->menuID,
229 ]);
230 }
231
232 // save acl
233 SimpleAclHandler::getInstance()->setValues(
234 'com.woltlab.wcf.box',
235 $menuEditor->getDecoratedObject()->getBox()->boxID,
236 $this->aclValues
237 );
238
239 $this->saved();
240
241 // reset values
242 $this->cssClassName = $this->title = '';
243 $this->position = 'contentTop';
244 $this->showOrder = 0;
245 $this->visibleEverywhere = $this->showHeader = 1;
246 $this->pageIDs = $this->aclValues = [];
247
248 // show success message
249 WCF::getTPL()->assign([
250 'success' => true,
251 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
252 MenuEditForm::class,
253 ['id' => $menuEditor->menuID]
254 ),
255 ]);
256
257 I18nHandler::getInstance()->reset();
258 }
259
260 /**
261 * @inheritDoc
262 */
263 public function assignVariables()
264 {
265 parent::assignVariables();
266
267 I18nHandler::getInstance()->assignVariables();
268
269 WCF::getTPL()->assign([
270 'action' => 'add',
271 'title' => 'title',
272 'position' => $this->position,
273 'cssClassName' => $this->cssClassName,
274 'showOrder' => $this->showOrder,
275 'visibleEverywhere' => $this->visibleEverywhere,
276 'showHeader' => $this->showHeader,
277 'pageIDs' => $this->pageIDs,
278 'availablePositions' => Box::$availableMenuPositions,
279 'pageNodeList' => (new PageNodeTree())->getNodeList(),
280 'aclValues' => SimpleAclHandler::getInstance()->getOutputValues($this->aclValues),
281 ]);
282 }
e7fb3339 283}