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