Commit | Line | Data |
---|---|---|
dcc2332d MW |
1 | <?php |
2 | namespace wcf\acp\form; | |
3 | use wcf\data\category\Category; | |
4 | use wcf\data\category\CategoryNodeTree; | |
dcc2332d MW |
5 | use wcf\data\smiley\SmileyAction; |
6 | use wcf\data\smiley\SmileyEditor; | |
7 | use wcf\form\AbstractForm; | |
cda6d496 | 8 | use wcf\system\database\util\PreparedStatementConditionBuilder; |
dcc2332d MW |
9 | use wcf\system\exception\UserInputException; |
10 | use wcf\system\language\I18nHandler; | |
11 | use wcf\system\WCF; | |
1aa8ebee | 12 | use wcf\util\FileUtil; |
dcc2332d MW |
13 | use 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 | */ | |
25 | class 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 | ||
83 | /** | |
0ad90fc3 | 84 | * @see \wcf\page\IPage::assignVariables() |
dcc2332d MW |
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, | |
1aa8ebee | 98 | 'smileyPath' => $this->smileyPath, |
dcc2332d MW |
99 | 'categoryNodeList' => $this->categoryNodeTree->getIterator() |
100 | )); | |
101 | } | |
102 | ||
103 | /** | |
0ad90fc3 | 104 | * @see \wcf\page\IPage::readData() |
dcc2332d MW |
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 | ||
dcc2332d | 121 | /** |
0ad90fc3 | 122 | * @see \wcf\page\IForm::readFormParameters() |
dcc2332d MW |
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 | ||
1aa8ebee TD |
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'])); | |
dcc2332d MW |
136 | } |
137 | ||
138 | /** | |
0ad90fc3 | 139 | * @see \wcf\page\IForm::save() |
dcc2332d MW |
140 | */ |
141 | public function save() { | |
142 | parent::save(); | |
143 | ||
144 | $this->objectAction = new SmileyAction(array(), 'create', array( | |
e94d7556 | 145 | 'data' => array_merge($this->additionalFields, array( |
dcc2332d MW |
146 | 'smileyTitle' => $this->smileyTitle, |
147 | 'smileyCode' => $this->smileyCode, | |
1aa8ebee TD |
148 | 'aliases' => $this->aliases, |
149 | 'smileyPath' => $this->smileyPath, | |
dcc2332d MW |
150 | 'showOrder' => $this->showOrder, |
151 | 'categoryID' => $this->categoryID ?: null, | |
1aa8ebee | 152 | 'packageID' => 1 |
e94d7556 | 153 | )) |
dcc2332d MW |
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')) { | |
a33717d8 | 161 | I18nHandler::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1); |
dcc2332d MW |
162 | |
163 | // update title | |
164 | $smileyEditor->update(array( | |
65d1e388 | 165 | 'smileyTitle' => 'wcf.smiley.title'.$smileyID |
dcc2332d MW |
166 | )); |
167 | } | |
168 | ||
169 | // reset values | |
170 | $this->smileyCode = ''; | |
171 | $this->categoryID = 0; | |
172 | $this->showOrder = 0; | |
f165cbfa | 173 | $this->smileyPath = ''; |
9fd4943c | 174 | $this->aliases = ''; |
dcc2332d MW |
175 | |
176 | I18nHandler::getInstance()->reset(); | |
177 | ||
178 | $this->saved(); | |
179 | ||
180 | // show success message | |
181 | WCF::getTPL()->assign('success', true); | |
182 | } | |
183 | ||
184 | /** | |
0ad90fc3 | 185 | * @see \wcf\page\IForm::validate() |
dcc2332d MW |
186 | */ |
187 | public function validate() { | |
188 | parent::validate(); | |
189 | ||
190 | // validate title | |
191 | if (!I18nHandler::getInstance()->validateValue('smileyTitle')) { | |
f165cbfa MS |
192 | if (I18nHandler::getInstance()->isPlainValue('smileyTitle')) { |
193 | throw new UserInputException('smileyTitle'); | |
194 | } | |
195 | else { | |
196 | throw new UserInputException('smileyTitle', 'multilingual'); | |
197 | } | |
dcc2332d MW |
198 | } |
199 | ||
a33717d8 | 200 | if ($this->categoryID) { |
dcc2332d MW |
201 | $category = new Category($this->categoryID); |
202 | if (!$category->categoryID) { | |
203 | throw new UserInputException('categoryID', 'notValid'); | |
204 | } | |
205 | } | |
206 | ||
a33717d8 | 207 | if (empty($this->smileyCode)) { |
dcc2332d MW |
208 | throw new UserInputException('smileyCode'); |
209 | } | |
210 | ||
1aa8ebee TD |
211 | if (empty($this->smileyPath)) { |
212 | throw new UserInputException('smileyPath'); | |
213 | } | |
214 | ||
9a80b6fd | 215 | if (!is_file(WCF_DIR.$this->smileyPath)) { |
1aa8ebee TD |
216 | throw new UserInputException('smileyPath', 'notFound'); |
217 | } | |
218 | ||
a33717d8 | 219 | // validate smiley code and aliases against existing smilies |
cda6d496 TD |
220 | $conditionBuilder = new PreparedStatementConditionBuilder(); |
221 | if (isset($this->smiley)) { | |
222 | $conditionBuilder->add('smileyID <> ?', array($this->smiley->smileyID)); | |
223 | } | |
a33717d8 | 224 | $sql = "SELECT smileyCode, aliases |
cda6d496 TD |
225 | FROM wcf".WCF_N."_smiley |
226 | ".$conditionBuilder; | |
a33717d8 | 227 | $statement = WCF::getDB()->prepareStatement($sql); |
cda6d496 | 228 | $statement->execute($conditionBuilder->getParameters()); |
a33717d8 AE |
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 | } | |
dcc2332d MW |
248 | } |
249 | } |