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 * @see \wcf\page\IPage::assignVariables()
86 public function assignVariables() {
87 parent
::assignVariables();
89 I18nHandler
::getInstance()->assignVariables();
91 WCF
::getTPL()->assign(array(
93 'smileyTitle' => $this->smileyTitle
,
94 'showOrder' => $this->showOrder
,
95 'categoryID' => $this->categoryID
,
96 'smileyCode' => $this->smileyCode
,
97 'aliases' => $this->aliases
,
98 'smileyPath' => $this->smileyPath
,
99 'categoryNodeList' => $this->categoryNodeTree
->getIterator()
104 * @see \wcf\page\IPage::readData()
106 public function readData() {
109 $this->categoryNodeTree
= new CategoryNodeTree('com.woltlab.wcf.bbcode.smiley', 0, true);
113 * @see \wcf\page\IPage::readParameters()
115 public function readParameters() {
116 parent
::readParameters();
118 I18nHandler
::getInstance()->register('smileyTitle');
122 * @see \wcf\page\IForm::readFormParameters()
124 public function readFormParameters() {
125 parent
::readFormParameters();
127 I18nHandler
::getInstance()->readValues();
129 if (I18nHandler
::getInstance()->isPlainValue('smileyTitle')) $this->smileyTitle
= I18nHandler
::getInstance()->getValue('smileyTitle');
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']));
139 * @see \wcf\page\IForm::save()
141 public function save() {
144 $this->objectAction
= new SmileyAction(array(), 'create', array(
145 'data' => array_merge($this->additionalFields
, array(
146 'smileyTitle' => $this->smileyTitle
,
147 'smileyCode' => $this->smileyCode
,
148 'aliases' => $this->aliases
,
149 'smileyPath' => $this->smileyPath
,
150 'showOrder' => $this->showOrder
,
151 'categoryID' => $this->categoryID ?
: null,
155 $this->objectAction
->executeAction();
156 $returnValues = $this->objectAction
->getReturnValues();
157 $smileyEditor = new SmileyEditor($returnValues['returnValues']);
158 $smileyID = $returnValues['returnValues']->smileyID
;
160 if (!I18nHandler
::getInstance()->isPlainValue('smileyTitle')) {
161 I18nHandler
::getInstance()->save('smileyTitle', 'wcf.smiley.title'.$smileyID, 'wcf.smiley', 1);
164 $smileyEditor->update(array(
165 'smileyTitle' => 'wcf.smiley.title'.$smileyID
170 $this->smileyCode
= '';
171 $this->categoryID
= 0;
172 $this->showOrder
= 0;
173 $this->smileyPath
= '';
176 I18nHandler
::getInstance()->reset();
180 // show success message
181 WCF
::getTPL()->assign('success', true);
185 * @see \wcf\page\IForm::validate()
187 public function validate() {
191 if (!I18nHandler
::getInstance()->validateValue('smileyTitle')) {
192 if (I18nHandler
::getInstance()->isPlainValue('smileyTitle')) {
193 throw new UserInputException('smileyTitle');
196 throw new UserInputException('smileyTitle', 'multilingual');
200 if ($this->categoryID
) {
201 $category = new Category($this->categoryID
);
202 if (!$category->categoryID
) {
203 throw new UserInputException('categoryID', 'notValid');
207 if (empty($this->smileyCode
)) {
208 throw new UserInputException('smileyCode');
211 if (empty($this->smileyPath
)) {
212 throw new UserInputException('smileyPath');
215 if (!is_file(WCF_DIR
.$this->smileyPath
)) {
216 throw new UserInputException('smileyPath', 'notFound');
219 // validate smiley code and aliases against existing smilies
220 $conditionBuilder = new PreparedStatementConditionBuilder();
221 if (isset($this->smiley
)) {
222 $conditionBuilder->add('smileyID <> ?', array($this->smiley
->smileyID
));
224 $sql = "SELECT smileyCode, aliases
225 FROM wcf".WCF_N
."_smiley
227 $statement = WCF
::getDB()->prepareStatement($sql);
228 $statement->execute($conditionBuilder->getParameters());
230 $aliases = explode("\n", $this->aliases
);
231 while ($row = $statement->fetchArray()) {
233 if (!empty($row['aliases'])) {
234 $known = explode("\n", $row['aliases']);
236 $known[] = $row['smileyCode'];
238 if (in_array($this->smileyCode
, $known)) {
239 throw new UserInputException('smileyCode', 'notUnique');
242 $conflicts = array_intersect($aliases, $known);
243 if (!empty($conflicts)) {
244 throw new UserInputException('aliases', 'notUnique');