Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / BBCodeAddForm.class.php
CommitLineData
dcc2332d
MW
1<?php
2namespace wcf\acp\form;
3use wcf\data\bbcode\attribute\BBCodeAttributeAction;
4use wcf\data\bbcode\BBCode;
5use wcf\data\bbcode\BBCodeAction;
6use wcf\data\bbcode\BBCodeEditor;
dcc2332d
MW
7use wcf\form\AbstractForm;
8use wcf\system\exception\UserInputException;
9use wcf\system\language\I18nHandler;
10use wcf\system\Regex;
3fb859c9 11use wcf\system\request\LinkHandler;
dcc2332d
MW
12use wcf\system\WCF;
13use wcf\util\StringUtil;
14
15/**
16 * Shows the bbcode add form.
17 *
18 * @author Tim Duesterhus
7b7b9764 19 * @copyright 2001-2019 WoltLab GmbH
dcc2332d 20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 21 * @package WoltLabSuite\Core\Acp\Form
dcc2332d
MW
22 */
23class BBCodeAddForm extends AbstractForm {
24 /**
ece28c21 25 * @inheritDoc
dcc2332d
MW
26 */
27 public $activeMenuItem = 'wcf.acp.menu.link.bbcode.add';
28
29 /**
30 * allowed child bbcodes
31 * @var string
32 */
33 public $allowedChildren = 'all';
34
35 /**
36 * list of attributes
7a23a706 37 * @var object[]
dcc2332d 38 */
ece28c21 39 public $attributes = [];
dcc2332d
MW
40
41 /**
42 * tag name
43 * @var string
44 */
45 public $bbcodeTag = '';
46
47 /**
48 * editor button label
49 * @var string
50 */
51 public $buttonLabel = '';
52
53 /**
54 * class name
55 * @var string
56 */
57 public $className = '';
58
59 /**
60 * closing html tag
61 * @var string
62 */
63 public $htmlClose = '';
64
65 /**
66 * opening html tag
67 * @var string
68 */
69 public $htmlOpen = '';
70
ece28c21
AE
71 /**
72 * true if bbcode is a block element
73 * @var boolean
74 */
75 public $isBlockElement = false;
76
dcc2332d
MW
77 /**
78 * true, if bbcode contains source code
79 * @var boolean
80 */
81 public $isSourceCode = false;
82
83 /**
ece28c21 84 * @inheritDoc
dcc2332d 85 */
ece28c21 86 public $neededPermissions = ['admin.content.bbcode.canManageBBCode'];
dcc2332d
MW
87
88 /**
ece28c21 89 * @inheritDoc
dcc2332d
MW
90 */
91 public $templateName = 'bbcodeAdd';
92
93 /**
94 * show editor button
95 * @var boolean
96 */
97 public $showButton = false;
98
99 /**
100 * wysiwyg editor icon
101 * @var string
102 */
103 public $wysiwygIcon = '';
104
105 /**
ece28c21 106 * @inheritDoc
dcc2332d
MW
107 */
108 public function readParameters() {
109 parent::readParameters();
110
111 I18nHandler::getInstance()->register('buttonLabel');
112 }
113
114 /**
ece28c21 115 * @inheritDoc
dcc2332d
MW
116 */
117 public function readFormParameters() {
118 parent::readFormParameters();
119
120 if (isset($_POST['allowedChildren'])) $this->allowedChildren = StringUtil::trim($_POST['allowedChildren']);
121 if (isset($_POST['attributes'])) $this->attributes = $_POST['attributes'];
195ef0f5 122 if (isset($_POST['bbcodeTag'])) $this->bbcodeTag = mb_strtolower(StringUtil::trim($_POST['bbcodeTag']));
dcc2332d
MW
123 if (isset($_POST['className'])) $this->className = StringUtil::trim($_POST['className']);
124 if (isset($_POST['htmlClose'])) $this->htmlClose = StringUtil::trim($_POST['htmlClose']);
125 if (isset($_POST['htmlOpen'])) $this->htmlOpen = StringUtil::trim($_POST['htmlOpen']);
ece28c21 126 if (isset($_POST['isBlockElement'])) $this->isBlockElement = true;
dcc2332d
MW
127 if (isset($_POST['isSourceCode'])) $this->isSourceCode = true;
128 if (isset($_POST['showButton'])) $this->showButton = true;
129 if (isset($_POST['wysiwygIcon'])) $this->wysiwygIcon = StringUtil::trim($_POST['wysiwygIcon']);
130
dcc2332d
MW
131 $attributeNo = 0;
132 foreach ($this->attributes as $key => $val) {
133 $val['attributeNo'] = $attributeNo++;
134 $val['required'] = (int) isset($val['required']);
135 $val['useText'] = (int) isset($val['useText']);
136 $this->attributes[$key] = (object) $val;
137 }
138
139 I18nHandler::getInstance()->readValues();
15ddbf70
MS
140 $this->readButtonLabelFormParameter();
141 }
142
143 /**
144 * Reads the form parameter for the button label.
145 */
146 protected function readButtonLabelFormParameter() {
dcc2332d
MW
147 if (I18nHandler::getInstance()->isPlainValue('buttonLabel')) $this->buttonLabel = I18nHandler::getInstance()->getValue('buttonLabel');
148 }
149
150 /**
ece28c21 151 * @inheritDoc
dcc2332d
MW
152 */
153 public function validate() {
154 parent::validate();
155
156 // tag name must not be empty
157 if (empty($this->bbcodeTag)) {
158 throw new UserInputException('bbcodeTag');
159 }
160
161 // tag may only contain alphanumeric chars
162 if (!Regex::compile('^[a-z0-9]+$', Regex::CASE_INSENSITIVE)->match($this->bbcodeTag)) {
063bbf46 163 throw new UserInputException('bbcodeTag', 'invalid');
dcc2332d
MW
164 }
165
166 // disallow the Pseudo-BBCodes all and none
167 if ($this->bbcodeTag == 'all' || $this->bbcodeTag == 'none') {
063bbf46 168 throw new UserInputException('bbcodeTag', 'invalid');
dcc2332d
MW
169 }
170
171 // check whether the tag is in use
8c96e894 172 $this->validateBBCodeTagUsage();
dcc2332d 173
dcc2332d
MW
174 // validate class
175 if (!empty($this->className) && !class_exists($this->className)) {
176 throw new UserInputException('className', 'notFound');
177 }
178
179 // validate attributes
180 foreach ($this->attributes as $attribute) {
181 // Check whether the pattern is a valid regex
182 if (!Regex::compile($attribute->validationPattern)->isValid()) {
063bbf46 183 throw new UserInputException('attributeValidationPattern'.$attribute->attributeNo, 'invalid');
dcc2332d
MW
184 }
185 }
186
187 // button
188 if ($this->showButton) {
189 // validate label
190 if (!I18nHandler::getInstance()->validateValue('buttonLabel')) {
c20f41f9
MS
191 if (I18nHandler::getInstance()->isPlainValue('buttonLabel')) {
192 throw new UserInputException('buttonLabel');
193 }
194 else {
195 throw new UserInputException('buttonLabel', 'multilingual');
196 }
dcc2332d
MW
197 }
198
199 // validate image path
200 if (empty($this->wysiwygIcon)) {
201 throw new UserInputException('wysiwygIcon');
202 }
203 }
204 else {
205 $this->buttonLabel = '';
206 }
207 }
208
8c96e894
MW
209 /**
210 * Validates the bbcode tag usage.
211 * @throws UserInputException
212 */
213 protected function validateBBCodeTagUsage() {
214 $bbcode = BBCode::getBBCodeByTag($this->bbcodeTag);
215 if ($bbcode->bbcodeID) {
216 throw new UserInputException('bbcodeTag', 'inUse');
217 }
218 }
219
dcc2332d 220 /**
ece28c21 221 * @inheritDoc
dcc2332d
MW
222 */
223 public function save() {
224 parent::save();
225
226 // save bbcode
ece28c21 227 $this->objectAction = new BBCodeAction([], 'create', ['data' => array_merge($this->additionalFields, [
dcc2332d
MW
228 'bbcodeTag' => $this->bbcodeTag,
229 'buttonLabel' => $this->buttonLabel,
230 'className' => $this->className,
231 'htmlOpen' => $this->htmlOpen,
232 'htmlClose' => $this->htmlClose,
63b9817b
MS
233 'isBlockElement' => $this->isBlockElement ? 1 : 0,
234 'isSourceCode' => $this->isSourceCode ? 1 : 0,
a37b39b6 235 'packageID' => 1,
63b9817b 236 'showButton' => $this->showButton ? 1 : 0,
dcc2332d 237 'wysiwygIcon' => $this->wysiwygIcon
ece28c21 238 ])]);
dcc2332d
MW
239 $returnValues = $this->objectAction->executeAction();
240 foreach ($this->attributes as $attribute) {
ece28c21 241 $attributeAction = new BBCodeAttributeAction([], 'create', ['data' => [
dcc2332d
MW
242 'bbcodeID' => $returnValues['returnValues']->bbcodeID,
243 'attributeNo' => $attribute->attributeNo,
244 'attributeHtml' => $attribute->attributeHtml,
245 'validationPattern' => $attribute->validationPattern,
246 'required' => $attribute->required,
3728a6bd 247 'useText' => $attribute->useText
ece28c21 248 ]]);
dcc2332d
MW
249 $attributeAction->executeAction();
250 }
251
252 if ($this->showButton && !I18nHandler::getInstance()->isPlainValue('buttonLabel')) {
253 $bbcodeID = $returnValues['returnValues']->bbcodeID;
dd67c2d1 254 I18nHandler::getInstance()->save('buttonLabel', 'wcf.editor.button.button'.$bbcodeID, 'wcf.editor', 1);
dcc2332d
MW
255
256 // update button label
257 $bbcodeEditor = new BBCodeEditor($returnValues['returnValues']);
ece28c21 258 $bbcodeEditor->update([
dd67c2d1 259 'buttonLabel' => 'wcf.editor.button.button'.$bbcodeID
ece28c21 260 ]);
dcc2332d
MW
261 }
262
263 $this->saved();
264
265 // reset values
266 $this->bbcodeTag = $this->htmlOpen = $this->htmlClose = $this->className = $this->buttonLabel = $this->wysiwygIcon = '';
ece28c21
AE
267 $this->attributes = [];
268 $this->isBlockElement = $this->isSourceCode = $this->showButton = false;
dcc2332d
MW
269
270 I18nHandler::getInstance()->reset();
271
47b90344 272 // show success message
3fb859c9
MW
273 WCF::getTPL()->assign([
274 'success' => true,
5916be9f 275 'objectEditLink' => LinkHandler::getInstance()->getControllerLink(BBCodeEditForm::class, ['id' => $returnValues['returnValues']->bbcodeID]),
3fb859c9 276 ]);
dcc2332d
MW
277 }
278
279 /**
ece28c21 280 * @inheritDoc
dcc2332d
MW
281 */
282 public function assignVariables() {
283 parent::assignVariables();
284
285 I18nHandler::getInstance()->assignVariables();
286
ece28c21 287 WCF::getTPL()->assign([
dcc2332d 288 'action' => 'add',
dcc2332d
MW
289 'attributes' => $this->attributes,
290 'bbcodeTag' => $this->bbcodeTag,
291 'buttonLabel' => $this->buttonLabel,
292 'className' => $this->className,
293 'htmlOpen' => $this->htmlOpen,
294 'htmlClose' => $this->htmlClose,
ece28c21 295 'isBlockElement' => $this->isBlockElement,
dcc2332d
MW
296 'isSourceCode' => $this->isSourceCode,
297 'showButton' => $this->showButton,
298 'wysiwygIcon' => $this->wysiwygIcon
ece28c21 299 ]);
dcc2332d
MW
300 }
301}