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