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
;
12 use wcf\util\FileUtil
;
13 use wcf\util\StringUtil
;
16 * Shows the smiley add form.
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
25 class SmileyAddForm
extends AbstractForm
{
27 * @see \wcf\page\AbstractPage::$activeMenuItem
29 public $activeMenuItem = 'wcf.acp.menu.link.smiley.add';
32 * @see \wcf\page\AbstractPage::$templateName
34 public $templateName = 'smileyAdd';
37 * @see \wcf\page\AbstractPage::$neededPermissions
39 public $neededPermissions = array('admin.content.smiley.canManageSmiley');
45 public $smileyCode = '';
51 public $showOrder = 0;
57 public $categoryID = 0;
63 public $smileyTitle = '';
72 * path to the smiley file
75 public $smileyPath = '';
78 * node tree with available smiley categories
79 * @var \wcf\data\category\CategoryNodeTree
81 public $categoryNodeTree = null;
84 * data of the uploaded smiley file
87 public $fileUpload = array();
90 * temporary name of the uploaded smiley file
93 public $uploadedFilename = '';
96 * @see \wcf\page\IPage::assignVariables()
98 public function assignVariables() {
99 parent
::assignVariables();
101 I18nHandler
::getInstance()->assignVariables();
103 WCF
::getTPL()->assign(array(
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
117 * @see \wcf\page\IPage::readData()
119 public function readData() {
122 $this->categoryNodeTree
= new CategoryNodeTree('com.woltlab.wcf.bbcode.smiley', 0, true);
126 * @see \wcf\page\IPage::readParameters()
128 public function readParameters() {
129 parent
::readParameters();
131 I18nHandler
::getInstance()->register('smileyTitle');
135 * @see \wcf\page\IForm::readFormParameters()
137 public function readFormParameters() {
138 parent
::readFormParameters();
140 I18nHandler
::getInstance()->readValues();
142 if (I18nHandler
::getInstance()->isPlainValue('smileyTitle')) $this->smileyTitle
= I18nHandler
::getInstance()->getValue('smileyTitle');
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'];
154 * @see \wcf\page\IForm::save()
156 public function save() {
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,
169 'fileLocation' => $this->uploadedFilename ? WCF_DIR
.'images/smilies/tmp/'.$this->uploadedFilename
: ''
171 $this->objectAction
->executeAction();
172 $returnValues = $this->objectAction
->getReturnValues();
173 $smileyEditor = new SmileyEditor($returnValues['returnValues']);
174 $smileyID = $returnValues['returnValues']->smileyID
;
176 if (!I18nHandler
::getInstance()->isPlainValue('smileyTitle')) {
177 I18nHandler
::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1);
180 $smileyEditor->update(array(
181 'smileyTitle' => 'wcf.smiley.title'.$smileyID
186 $this->smileyCode
= '';
187 $this->categoryID
= 0;
188 $this->showOrder
= 0;
189 $this->smileyPath
= '';
191 $this->uploadedFilename
= '';
193 I18nHandler
::getInstance()->reset();
197 // show success message
198 WCF
::getTPL()->assign('success', true);
202 * @see \wcf\page\IForm::validate()
204 public function validate() {
207 if ($this->uploadedFilename
) {
208 if (!file_exists(WCF_DIR
.'images/smilies/tmp/'.$this->uploadedFilename
)) {
209 throw new UserInputException('fileUpload', 'uploadFailed');
212 else if (!empty($this->fileUpload
['name'])) {
213 if (!getimagesize($this->fileUpload
['tmp_name'])) {
214 throw new UserInputException('fileUpload', 'noImage');
218 $this->uploadedFilename
= StringUtil
::getRandomID().'.'.mb_strtolower(mb_substr($this->fileUpload
['name'], mb_strrpos($this->fileUpload
['name'], '.') +
1));
220 while (file_exists(WCF_DIR
.'images/smilies/tmp/'.$this->uploadedFilename
));
222 if (!@move_uploaded_file
($this->fileUpload
['tmp_name'], WCF_DIR
.'images/smilies/tmp/'.$this->uploadedFilename
)) {
223 throw new UserInputException('fileUpload', 'uploadFailed');
227 if (empty($this->smileyPath
)) {
228 throw new UserInputException('smileyPath');
231 if (!is_file(WCF_DIR
.$this->smileyPath
)) {
232 throw new UserInputException('smileyPath', 'notFound');
237 if (!I18nHandler
::getInstance()->validateValue('smileyTitle')) {
238 if (I18nHandler
::getInstance()->isPlainValue('smileyTitle')) {
239 throw new UserInputException('smileyTitle');
242 throw new UserInputException('smileyTitle', 'multilingual');
246 if ($this->categoryID
) {
247 $category = new Category($this->categoryID
);
248 if (!$category->categoryID
) {
249 throw new UserInputException('categoryID', 'notValid');
253 if (empty($this->smileyCode
)) {
254 throw new UserInputException('smileyCode');
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
));
262 $sql = "SELECT smileyCode, aliases
263 FROM wcf".WCF_N
."_smiley
265 $statement = WCF
::getDB()->prepareStatement($sql);
266 $statement->execute($conditionBuilder->getParameters());
268 $aliases = explode("\n", $this->aliases
);
269 while ($row = $statement->fetchArray()) {
271 if (!empty($row['aliases'])) {
272 $known = explode("\n", $row['aliases']);
274 $known[] = $row['smileyCode'];
276 if (in_array($this->smileyCode
, $known)) {
277 throw new UserInputException('smileyCode', 'notUnique');
280 $conflicts = array_intersect($aliases, $known);
281 if (!empty($conflicts)) {
282 throw new UserInputException('aliases', 'notUnique');