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 * data of the uploaded smiley file
85 * @var array()
86 */
87 public $fileUpload = array();
88
89 /**
90 * temporary name of the uploaded smiley file
91 * @var string
92 */
93 public $uploadedFilename = '';
94
95 /**
96 * @see \wcf\page\IPage::assignVariables()
97 */
98 public function assignVariables() {
99 parent::assignVariables();
100
101 I18nHandler::getInstance()->assignVariables();
102
103 WCF::getTPL()->assign(array(
104 'action' => 'add',
105 'smileyTitle' => $this->smileyTitle,
106 'showOrder' => $this->showOrder,
107 'categoryID' => $this->categoryID,
108 'smileyCode' => $this->smileyCode,
109 'aliases' => $this->aliases,
110 'smileyPath' => $this->smileyPath,
111 'categoryNodeList' => $this->categoryNodeTree->getIterator(),
112 'uploadedFilename' => $this->uploadedFilename
113 ));
114 }
115
116 /**
117 * @see \wcf\page\IPage::readData()
118 */
119 public function readData() {
120 parent::readData();
121
122 $this->categoryNodeTree = new CategoryNodeTree('com.woltlab.wcf.bbcode.smiley', 0, true);
123 }
124
125 /**
126 * @see \wcf\page\IPage::readParameters()
127 */
128 public function readParameters() {
129 parent::readParameters();
130
131 I18nHandler::getInstance()->register('smileyTitle');
132 }
133
134 /**
135 * @see \wcf\page\IForm::readFormParameters()
136 */
137 public function readFormParameters() {
138 parent::readFormParameters();
139
140 I18nHandler::getInstance()->readValues();
141
142 if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) $this->smileyTitle = I18nHandler::getInstance()->getValue('smileyTitle');
143
144 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
145 if (isset($_POST['categoryID'])) $this->categoryID = intval($_POST['categoryID']);
146 if (isset($_POST['smileyCode'])) $this->smileyCode = StringUtil::trim($_POST['smileyCode']);
147 if (isset($_POST['aliases'])) $this->aliases = StringUtil::unifyNewlines(StringUtil::trim($_POST['aliases']));
148 if (isset($_POST['smileyPath'])) $this->smileyPath = FileUtil::removeLeadingSlash(StringUtil::trim($_POST['smileyPath']));
149 if (isset($_POST['uploadedFilename'])) $this->uploadedFilename = StringUtil::trim($_POST['uploadedFilename']);
150 if (isset($_FILES['fileUpload'])) $this->fileUpload = $_FILES['fileUpload'];
151 }
152
153 /**
154 * @see \wcf\page\IForm::save()
155 */
156 public function save() {
157 parent::save();
158
159 $this->objectAction = new SmileyAction(array(), 'create', array(
160 'data' => array_merge($this->additionalFields, array(
161 'smileyTitle' => $this->smileyTitle,
162 'smileyCode' => $this->smileyCode,
163 'aliases' => $this->aliases,
164 'smileyPath' => $this->smileyPath,
165 'showOrder' => $this->showOrder,
166 'categoryID' => $this->categoryID ?: null,
167 'packageID' => 1
168 )),
169 'fileLocation' => $this->uploadedFilename ? WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename : ''
170 ));
171 $this->objectAction->executeAction();
172 $returnValues = $this->objectAction->getReturnValues();
173 $smileyEditor = new SmileyEditor($returnValues['returnValues']);
174 $smileyID = $returnValues['returnValues']->smileyID;
175
176 if (!I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
177 I18nHandler::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1);
178
179 // update title
180 $smileyEditor->update(array(
181 'smileyTitle' => 'wcf.smiley.title'.$smileyID
182 ));
183 }
184
185 // reset values
186 $this->smileyCode = '';
187 $this->categoryID = 0;
188 $this->showOrder = 0;
189 $this->smileyPath = '';
190 $this->aliases = '';
191 $this->uploadedFilename = '';
192
193 I18nHandler::getInstance()->reset();
194
195 $this->saved();
196
197 // show success message
198 WCF::getTPL()->assign('success', true);
199 }
200
201 /**
202 * @see \wcf\page\IForm::validate()
203 */
204 public function validate() {
205 parent::validate();
206
207 if ($this->uploadedFilename) {
208 if (!file_exists(WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename)) {
209 throw new UserInputException('fileUpload', 'uploadFailed');
210 }
211 }
212 else if (!empty($this->fileUpload['name'])) {
213 if (!getimagesize($this->fileUpload['tmp_name'])) {
214 throw new UserInputException('fileUpload', 'noImage');
215 }
216
217 do {
218 $this->uploadedFilename = StringUtil::getRandomID().'.'.mb_strtolower(mb_substr($this->fileUpload['name'], mb_strrpos($this->fileUpload['name'], '.') + 1));
219 }
220 while (file_exists(WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename));
221
222 if (!@move_uploaded_file($this->fileUpload['tmp_name'], WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename)) {
223 throw new UserInputException('fileUpload', 'uploadFailed');
224 }
225 }
226 else {
227 if (empty($this->smileyPath)) {
228 throw new UserInputException('smileyPath');
229 }
230
231 if (!is_file(WCF_DIR.$this->smileyPath)) {
232 throw new UserInputException('smileyPath', 'notFound');
233 }
234 }
235
236 // validate title
237 if (!I18nHandler::getInstance()->validateValue('smileyTitle')) {
238 if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
239 throw new UserInputException('smileyTitle');
240 }
241 else {
242 throw new UserInputException('smileyTitle', 'multilingual');
243 }
244 }
245
246 if ($this->categoryID) {
247 $category = new Category($this->categoryID);
248 if (!$category->categoryID) {
249 throw new UserInputException('categoryID', 'notValid');
250 }
251 }
252
253 if (empty($this->smileyCode)) {
254 throw new UserInputException('smileyCode');
255 }
256
257 // validate smiley code and aliases against existing smilies
258 $conditionBuilder = new PreparedStatementConditionBuilder();
259 if (isset($this->smiley)) {
260 $conditionBuilder->add('smileyID <> ?', array($this->smiley->smileyID));
261 }
262 $sql = "SELECT smileyCode, aliases
263 FROM wcf".WCF_N."_smiley
264 ".$conditionBuilder;
265 $statement = WCF::getDB()->prepareStatement($sql);
266 $statement->execute($conditionBuilder->getParameters());
267
268 $aliases = explode("\n", $this->aliases);
269 while ($row = $statement->fetchArray()) {
270 $known = array();
271 if (!empty($row['aliases'])) {
272 $known = explode("\n", $row['aliases']);
273 }
274 $known[] = $row['smileyCode'];
275
276 if (in_array($this->smileyCode, $known)) {
277 throw new UserInputException('smileyCode', 'notUnique');
278 }
279 else {
280 $conflicts = array_intersect($aliases, $known);
281 if (!empty($conflicts)) {
282 throw new UserInputException('aliases', 'notUnique');
283 }
284 }
285 }
286 }
287 }