Use \PDO::fetchAll() instead of PreparedStatement::fetchColumns()
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / MenuAddForm.class.php
CommitLineData
e7fb3339
MW
1<?php
2namespace wcf\acp\form;
aa779877 3use wcf\data\box\Box;
e7fb3339
MW
4use wcf\data\menu\MenuAction;
5use wcf\data\menu\MenuEditor;
53cf1c00 6use wcf\data\page\PageNodeTree;
e7fb3339 7use wcf\form\AbstractForm;
53cf1c00 8use wcf\system\database\util\PreparedStatementConditionBuilder;
e7fb3339
MW
9use wcf\system\exception\UserInputException;
10use wcf\system\language\I18nHandler;
11use wcf\system\WCF;
53cf1c00 12use wcf\util\ArrayUtil;
aa779877 13use wcf\util\StringUtil;
e7fb3339
MW
14
15/**
16 * Shows the menu add form.
17 *
18 * @author Marcel Werk
19 * @copyright 2001-2015 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package com.woltlab.wcf
22 * @subpackage acp.form
23 * @category Community Framework
2f53b086 24 * @since 2.2k
e7fb3339
MW
25 */
26class MenuAddForm extends AbstractForm {
27 /**
d121de81 28 * @inheritDoc
e7fb3339 29 */
d121de81 30 public $activeMenuItem = 'wcf.acp.menu.link.cms.menu.add';
e7fb3339
MW
31
32 /**
d121de81 33 * @inheritDoc
e7fb3339 34 */
d121de81 35 public $neededPermissions = ['admin.content.cms.canManageMenu'];
e7fb3339
MW
36
37 /**
38 * menu title
893aace3 39 * @var string
e7fb3339
MW
40 */
41 public $title = '';
42
aa779877
MW
43 /**
44 * box position
45 * @var string
46 */
47 public $position = '';
48
49 /**
50 * show order
51 * @var integer
52 */
53 public $showOrder = 0;
54
55 /**
56 * true if created box is visible everywhere
57 * @var boolean
58 */
59 public $visibleEverywhere = 1;
60
61 /**
62 * css class name of created box
63 * @var string
64 */
65 public $cssClassName = '';
66
67 /**
68 * true if box header is visible
69 * @var boolean
70 */
71 public $showHeader = 1;
72
53cf1c00
MW
73 /**
74 * page ids
75 * @var integer[]
76 */
77 public $pageIDs = [];
78
e7fb3339 79 /**
d121de81 80 * @inheritDoc
e7fb3339
MW
81 */
82 public function readParameters() {
83 parent::readParameters();
84
85 I18nHandler::getInstance()->register('title');
86 }
87
88 /**
d121de81 89 * @inheritDoc
e7fb3339
MW
90 */
91 public function readFormParameters() {
92 parent::readFormParameters();
93
94 I18nHandler::getInstance()->readValues();
95
96 if (I18nHandler::getInstance()->isPlainValue('title')) $this->title = I18nHandler::getInstance()->getValue('title');
aa779877 97
fb4b9f70 98 $this->visibleEverywhere = $this->showHeader = $this->showOrder = 0;
aa779877
MW
99 if (isset($_POST['position'])) $this->position = $_POST['position'];
100 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
101 if (isset($_POST['visibleEverywhere'])) $this->visibleEverywhere = intval($_POST['visibleEverywhere']);
102 if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
103 if (isset($_POST['showHeader'])) $this->showHeader = intval($_POST['showHeader']);
53cf1c00 104 if (isset($_POST['pageIDs']) && is_array($_POST['pageIDs'])) $this->pageIDs = ArrayUtil::toIntegerArray($_POST['pageIDs']);
e7fb3339
MW
105 }
106
107 /**
d121de81 108 * @inheritDoc
e7fb3339
MW
109 */
110 public function validate() {
111 parent::validate();
112
113 // validate menu title
114 if (!I18nHandler::getInstance()->validateValue('title')) {
115 if (I18nHandler::getInstance()->isPlainValue('title')) {
116 throw new UserInputException('title');
117 }
118 else {
119 throw new UserInputException('title', 'multilingual');
120 }
121 }
aa779877
MW
122
123 // validate box position
124 $this->validatePosition();
53cf1c00
MW
125
126 // validate page ids
127 if (!empty($this->pageIDs)) {
128 $conditionBuilder = new PreparedStatementConditionBuilder();
129 $conditionBuilder->add('pageID IN (?)', [$this->pageIDs]);
130 $sql = "SELECT pageID
131 FROM wcf".WCF_N."_page
132 " . $conditionBuilder;
133 $statement = WCF::getDB()->prepareStatement($sql);
134 $statement->execute($conditionBuilder->getParameters());
cd975610 135 $this->pageIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
53cf1c00 136 }
aa779877
MW
137 }
138
139 /**
140 * Validates box position.
141 *
893aace3 142 * @throws UserInputException
aa779877
MW
143 */
144 protected function validatePosition() {
145 if (!in_array($this->position, Box::$availablePositions)) {
146 throw new UserInputException('position');
147 }
e7fb3339
MW
148 }
149
150 /**
d121de81 151 * @inheritDoc
e7fb3339
MW
152 */
153 public function save() {
154 parent::save();
155
156 // save label
157 $this->objectAction = new MenuAction(array(), 'create', array('data' => array_merge($this->additionalFields, array(
158 'title' => $this->title,
159 'packageID' => 1,
160 'identifier' => ''
aa779877
MW
161 )), 'boxData' => array(
162 'name' => $this->title,
163 'boxType' => 'menu',
164 'position' => $this->position,
165 'visibleEverywhere' => ($this->visibleEverywhere) ? 1 : 0,
166 'showHeader' => ($this->showHeader) ? 1 : 0,
167 'showOrder' => $this->showOrder,
168 'cssClassName' => $this->cssClassName,
169 'packageID' => 1
53cf1c00 170 ), 'pageIDs' => $this->pageIDs));
e7fb3339
MW
171 $returnValues = $this->objectAction->executeAction();
172 // set generic identifier
173 $menuEditor = new MenuEditor($returnValues['returnValues']);
174 $menuEditor->update(array(
aa779877 175 'identifier' => 'com.woltlab.wcf.genericMenu'.$menuEditor->menuID
e7fb3339
MW
176 ));
177 // save i18n
178 if (!I18nHandler::getInstance()->isPlainValue('title')) {
179 I18nHandler::getInstance()->save('title', 'wcf.menu.menu'.$menuEditor->menuID, 'wcf.menu', 1);
180
181 // update title
182 $menuEditor->update(array(
183 'title' => 'wcf.menu.menu'.$menuEditor->menuID
184 ));
185 }
186 $this->saved();
187
188 // reset values
189 $this->title = '';
190
191 // show success
192 WCF::getTPL()->assign(array(
193 'success' => true
194 ));
195
196 I18nHandler::getInstance()->reset();
197 }
198
199 /**
d121de81 200 * @inheritDoc
e7fb3339
MW
201 */
202 public function assignVariables() {
203 parent::assignVariables();
204
205 I18nHandler::getInstance()->assignVariables();
206
207 WCF::getTPL()->assign(array(
208 'action' => 'add',
aa779877
MW
209 'title' => 'title',
210 'position' => $this->position,
211 'cssClassName' => $this->cssClassName,
212 'showOrder' => $this->showOrder,
213 'visibleEverywhere' => $this->visibleEverywhere,
214 'showHeader' => $this->showHeader,
53cf1c00
MW
215 'pageIDs' => $this->pageIDs,
216 'availablePositions' => Box::$availableMenuPositions,
217 'pageNodeList' => (new PageNodeTree())->getNodeList()
e7fb3339
MW
218 ));
219 }
220}