Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / SmileyAddForm.class.php
CommitLineData
dcc2332d
MW
1<?php
2namespace wcf\acp\form;
3use wcf\data\category\Category;
4use wcf\data\category\CategoryNodeTree;
dcc2332d
MW
5use wcf\data\smiley\SmileyAction;
6use wcf\data\smiley\SmileyEditor;
7use wcf\form\AbstractForm;
cda6d496 8use wcf\system\database\util\PreparedStatementConditionBuilder;
dcc2332d
MW
9use wcf\system\exception\UserInputException;
10use wcf\system\language\I18nHandler;
11use wcf\system\WCF;
1aa8ebee 12use wcf\util\FileUtil;
dcc2332d
MW
13use wcf\util\StringUtil;
14
15/**
16 * Shows the smiley add form.
17 *
18 * @author Tim Duesterhus
ca4ba303 19 * @copyright 2001-2014 WoltLab GmbH
dcc2332d 20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
f4f05aa5 21 * @package com.woltlab.wcf
dcc2332d
MW
22 * @subpackage acp.form
23 * @category Community Framework
24 */
25class SmileyAddForm extends AbstractForm {
26 /**
0ad90fc3 27 * @see \wcf\page\AbstractPage::$activeMenuItem
dcc2332d
MW
28 */
29 public $activeMenuItem = 'wcf.acp.menu.link.smiley.add';
30
31 /**
0ad90fc3 32 * @see \wcf\page\AbstractPage::$templateName
dcc2332d
MW
33 */
34 public $templateName = 'smileyAdd';
35
36 /**
0ad90fc3 37 * @see \wcf\page\AbstractPage::$neededPermissions
dcc2332d
MW
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
1aa8ebee 71 /**
f165cbfa 72 * path to the smiley file
1aa8ebee
TD
73 * @var string
74 */
75 public $smileyPath = '';
76
dcc2332d
MW
77 /**
78 * node tree with available smiley categories
0ad90fc3 79 * @var \wcf\data\category\CategoryNodeTree
dcc2332d
MW
80 */
81 public $categoryNodeTree = null;
82
2935bc94
MS
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
dcc2332d 95 /**
0ad90fc3 96 * @see \wcf\page\IPage::assignVariables()
dcc2332d
MW
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,
1aa8ebee 110 'smileyPath' => $this->smileyPath,
2935bc94
MS
111 'categoryNodeList' => $this->categoryNodeTree->getIterator(),
112 'uploadedFilename' => $this->uploadedFilename
dcc2332d
MW
113 ));
114 }
115
116 /**
0ad90fc3 117 * @see \wcf\page\IPage::readData()
dcc2332d
MW
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
dcc2332d 134 /**
0ad90fc3 135 * @see \wcf\page\IForm::readFormParameters()
dcc2332d
MW
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
1aa8ebee
TD
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']));
2935bc94
MS
149 if (isset($_POST['uploadedFilename'])) $this->uploadedFilename = StringUtil::trim($_POST['uploadedFilename']);
150 if (isset($_FILES['fileUpload'])) $this->fileUpload = $_FILES['fileUpload'];
dcc2332d
MW
151 }
152
153 /**
0ad90fc3 154 * @see \wcf\page\IForm::save()
dcc2332d
MW
155 */
156 public function save() {
157 parent::save();
158
159 $this->objectAction = new SmileyAction(array(), 'create', array(
e94d7556 160 'data' => array_merge($this->additionalFields, array(
dcc2332d
MW
161 'smileyTitle' => $this->smileyTitle,
162 'smileyCode' => $this->smileyCode,
1aa8ebee
TD
163 'aliases' => $this->aliases,
164 'smileyPath' => $this->smileyPath,
dcc2332d
MW
165 'showOrder' => $this->showOrder,
166 'categoryID' => $this->categoryID ?: null,
1aa8ebee 167 'packageID' => 1
2935bc94
MS
168 )),
169 'fileLocation' => $this->uploadedFilename ? WCF_DIR.'images/smilies/tmp/'.$this->uploadedFilename : ''
dcc2332d
MW
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')) {
a33717d8 177 I18nHandler::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1);
dcc2332d
MW
178
179 // update title
180 $smileyEditor->update(array(
65d1e388 181 'smileyTitle' => 'wcf.smiley.title'.$smileyID
dcc2332d
MW
182 ));
183 }
184
185 // reset values
186 $this->smileyCode = '';
187 $this->categoryID = 0;
188 $this->showOrder = 0;
f165cbfa 189 $this->smileyPath = '';
9fd4943c 190 $this->aliases = '';
2935bc94 191 $this->uploadedFilename = '';
dcc2332d
MW
192
193 I18nHandler::getInstance()->reset();
194
195 $this->saved();
196
197 // show success message
198 WCF::getTPL()->assign('success', true);
199 }
200
201 /**
0ad90fc3 202 * @see \wcf\page\IForm::validate()
dcc2332d
MW
203 */
204 public function validate() {
205 parent::validate();
206
2935bc94
MS
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
dcc2332d
MW
236 // validate title
237 if (!I18nHandler::getInstance()->validateValue('smileyTitle')) {
f165cbfa
MS
238 if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) {
239 throw new UserInputException('smileyTitle');
240 }
241 else {
242 throw new UserInputException('smileyTitle', 'multilingual');
243 }
dcc2332d
MW
244 }
245
a33717d8 246 if ($this->categoryID) {
dcc2332d
MW
247 $category = new Category($this->categoryID);
248 if (!$category->categoryID) {
249 throw new UserInputException('categoryID', 'notValid');
250 }
251 }
252
a33717d8 253 if (empty($this->smileyCode)) {
dcc2332d
MW
254 throw new UserInputException('smileyCode');
255 }
256
a33717d8 257 // validate smiley code and aliases against existing smilies
cda6d496
TD
258 $conditionBuilder = new PreparedStatementConditionBuilder();
259 if (isset($this->smiley)) {
260 $conditionBuilder->add('smileyID <> ?', array($this->smiley->smileyID));
261 }
a33717d8 262 $sql = "SELECT smileyCode, aliases
cda6d496
TD
263 FROM wcf".WCF_N."_smiley
264 ".$conditionBuilder;
a33717d8 265 $statement = WCF::getDB()->prepareStatement($sql);
cda6d496 266 $statement->execute($conditionBuilder->getParameters());
a33717d8
AE
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 }
dcc2332d
MW
286 }
287}