Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / SmileyAddForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\category\Category;
4 use wcf\data\category\CategoryNodeTree;
5 use wcf\data\smiley\SmileyAction;
6 use wcf\data\smiley\SmileyEditor;
7 use wcf\form\AbstractForm;
8 use wcf\system\database\util\PreparedStatementConditionBuilder;
9 use wcf\system\exception\UserInputException;
10 use wcf\system\language\I18nHandler;
11 use wcf\system\WCF;
12 use wcf\util\FileUtil;
13 use wcf\util\StringUtil;
14
15 /**
16 * Shows the smiley add form.
17 *
18 * @author Tim Duesterhus
19 * @copyright 2001-2014 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
24 */
25 class SmileyAddForm extends AbstractForm {
26 /**
27 * @see \wcf\page\AbstractPage::$activeMenuItem
28 */
29 public $activeMenuItem = 'wcf.acp.menu.link.smiley.add';
30
31 /**
32 * @see \wcf\page\AbstractPage::$templateName
33 */
34 public $templateName = 'smileyAdd';
35
36 /**
37 * @see \wcf\page\AbstractPage::$neededPermissions
38 */
39 public $neededPermissions = array('admin.content.smiley.canManageSmiley');
40
41 /**
42 * primary smiley code
43 * @var string
44 */
45 public $smileyCode = '';
46
47 /**
48 * showorder value
49 * @var integer
50 */
51 public $showOrder = 0;
52
53 /**
54 * categoryID value
55 * @var integer
56 */
57 public $categoryID = 0;
58
59 /**
60 * smileyTitle
61 * @var string
62 */
63 public $smileyTitle = '';
64
65 /**
66 * aliases value
67 * @var string
68 */
69 public $aliases = '';
70
71 /**
72 * path to the smiley file
73 * @var string
74 */
75 public $smileyPath = '';
76
77 /**
78 * node tree with available smiley categories
79 * @var \wcf\data\category\CategoryNodeTree
80 */
81 public $categoryNodeTree = null;
82
83 /**
84 * @see \wcf\page\IPage::assignVariables()
85 */
86 public function assignVariables() {
87 parent::assignVariables();
88
89 I18nHandler::getInstance()->assignVariables();
90
91 WCF::getTPL()->assign(array(
92 'action' => 'add',
93 'smileyTitle' => $this->smileyTitle,
94 'showOrder' => $this->showOrder,
95 'categoryID' => $this->categoryID,
96 'smileyCode' => $this->smileyCode,
97 'aliases' => $this->aliases,
98 'smileyPath' => $this->smileyPath,
99 'categoryNodeList' => $this->categoryNodeTree->getIterator()
100 ));
101 }
102
103 /**
104 * @see \wcf\page\IPage::readData()
105 */
106 public function readData() {
107 parent::readData();
108
109 $this->categoryNodeTree = new CategoryNodeTree('com.woltlab.wcf.bbcode.smiley', 0, true);
110 }
111
112 /**
113 * @see \wcf\page\IPage::readParameters()
114 */
115 public function readParameters() {
116 parent::readParameters();
117
118 I18nHandler::getInstance()->register('smileyTitle');
119 }
120
121 /**
122 * @see \wcf\page\IForm::readFormParameters()
123 */
124 public function readFormParameters() {
125 parent::readFormParameters();
126
127 I18nHandler::getInstance()->readValues();
128
129 if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) $this->smileyTitle = I18nHandler::getInstance()->getValue('smileyTitle');
130
131 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
132 if (isset($_POST['categoryID'])) $this->categoryID = intval($_POST['categoryID']);
133 if (isset($_POST['smileyCode'])) $this->smileyCode = StringUtil::trim($_POST['smileyCode']);
134 if (isset($_POST['aliases'])) $this->aliases = StringUtil::unifyNewlines(StringUtil::trim($_POST['aliases']));
135 if (isset($_POST['smileyPath'])) $this->smileyPath = FileUtil::removeLeadingSlash(StringUtil::trim($_POST['smileyPath']));
136 }
137
138 /**
139 * @see \wcf\page\IForm::save()
140 */
141 public function save() {
142 parent::save();
143
144 $this->objectAction = new SmileyAction(array(), 'create', array(
145 'data' => array_merge($this->additionalFields, array(
146 'smileyTitle' => $this->smileyTitle,
147 'smileyCode' => $this->smileyCode,
148 'aliases' => $this->aliases,
149 'smileyPath' => $this->smileyPath,
150 'showOrder' => $this->showOrder,
151 'categoryID' => $this->categoryID ?: null,
152 'packageID' => 1
153 ))
154 ));
155 $this->objectAction->executeAction();
156 $returnValues = $this->objectAction->getReturnValues();
157 $smileyEditor = new SmileyEditor($returnValues['returnValues']);
158 $smileyID = $returnValues['returnValues']->smileyID;
159
160 if (!I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
161 I18nHandler::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1);
162
163 // update title
164 $smileyEditor->update(array(
165 'smileyTitle' => 'wcf.smiley.title'.$smileyID
166 ));
167 }
168
169 // reset values
170 $this->smileyCode = '';
171 $this->categoryID = 0;
172 $this->showOrder = 0;
173 $this->smileyPath = '';
174 $this->aliases = '';
175
176 I18nHandler::getInstance()->reset();
177
178 $this->saved();
179
180 // show success message
181 WCF::getTPL()->assign('success', true);
182 }
183
184 /**
185 * @see \wcf\page\IForm::validate()
186 */
187 public function validate() {
188 parent::validate();
189
190 // validate title
191 if (!I18nHandler::getInstance()->validateValue('smileyTitle')) {
192 if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
193 throw new UserInputException('smileyTitle');
194 }
195 else {
196 throw new UserInputException('smileyTitle', 'multilingual');
197 }
198 }
199
200 if ($this->categoryID) {
201 $category = new Category($this->categoryID);
202 if (!$category->categoryID) {
203 throw new UserInputException('categoryID', 'notValid');
204 }
205 }
206
207 if (empty($this->smileyCode)) {
208 throw new UserInputException('smileyCode');
209 }
210
211 if (empty($this->smileyPath)) {
212 throw new UserInputException('smileyPath');
213 }
214
215 if (!is_file(WCF_DIR.$this->smileyPath)) {
216 throw new UserInputException('smileyPath', 'notFound');
217 }
218
219 // validate smiley code and aliases against existing smilies
220 $conditionBuilder = new PreparedStatementConditionBuilder();
221 if (isset($this->smiley)) {
222 $conditionBuilder->add('smileyID <> ?', array($this->smiley->smileyID));
223 }
224 $sql = "SELECT smileyCode, aliases
225 FROM wcf".WCF_N."_smiley
226 ".$conditionBuilder;
227 $statement = WCF::getDB()->prepareStatement($sql);
228 $statement->execute($conditionBuilder->getParameters());
229
230 $aliases = explode("\n", $this->aliases);
231 while ($row = $statement->fetchArray()) {
232 $known = array();
233 if (!empty($row['aliases'])) {
234 $known = explode("\n", $row['aliases']);
235 }
236 $known[] = $row['smileyCode'];
237
238 if (in_array($this->smileyCode, $known)) {
239 throw new UserInputException('smileyCode', 'notUnique');
240 }
241 else {
242 $conflicts = array_intersect($aliases, $known);
243 if (!empty($conflicts)) {
244 throw new UserInputException('aliases', 'notUnique');
245 }
246 }
247 }
248 }
249 }