2 namespace wcf\acp\form
;
3 use wcf\data\bbcode\attribute\BBCodeAttributeAction
;
4 use wcf\data\bbcode\BBCode
;
5 use wcf\data\bbcode\BBCodeAction
;
6 use wcf\data\bbcode\BBCodeEditor
;
7 use wcf\form\AbstractForm
;
8 use wcf\system\exception\UserInputException
;
9 use wcf\system\language\I18nHandler
;
12 use wcf\util\StringUtil
;
15 * Shows the bbcode add form.
17 * @author Tim Duesterhus
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\Acp\Form
22 class BBCodeAddForm
extends AbstractForm
{
26 public $activeMenuItem = 'wcf.acp.menu.link.bbcode.add';
29 * allowed child bbcodes
32 public $allowedChildren = 'all';
38 public $attributes = [];
44 public $bbcodeTag = '';
50 public $buttonLabel = '';
56 public $className = '';
62 public $htmlClose = '';
68 public $htmlOpen = '';
71 * true if bbcode is a block element
74 public $isBlockElement = false;
77 * true, if bbcode contains source code
80 public $isSourceCode = false;
85 public $neededPermissions = ['admin.content.bbcode.canManageBBCode'];
90 public $templateName = 'bbcodeAdd';
96 public $showButton = false;
102 public $wysiwygIcon = '';
107 public function readParameters() {
108 parent
::readParameters();
110 I18nHandler
::getInstance()->register('buttonLabel');
116 public function readFormParameters() {
117 parent
::readFormParameters();
119 if (isset($_POST['allowedChildren'])) $this->allowedChildren
= StringUtil
::trim($_POST['allowedChildren']);
120 if (isset($_POST['attributes'])) $this->attributes
= $_POST['attributes'];
121 if (isset($_POST['bbcodeTag'])) $this->bbcodeTag
= mb_strtolower(StringUtil
::trim($_POST['bbcodeTag']));
122 if (isset($_POST['className'])) $this->className
= StringUtil
::trim($_POST['className']);
123 if (isset($_POST['htmlClose'])) $this->htmlClose
= StringUtil
::trim($_POST['htmlClose']);
124 if (isset($_POST['htmlOpen'])) $this->htmlOpen
= StringUtil
::trim($_POST['htmlOpen']);
125 if (isset($_POST['isBlockElement'])) $this->isBlockElement
= true;
126 if (isset($_POST['isSourceCode'])) $this->isSourceCode
= true;
127 if (isset($_POST['showButton'])) $this->showButton
= true;
128 if (isset($_POST['wysiwygIcon'])) $this->wysiwygIcon
= StringUtil
::trim($_POST['wysiwygIcon']);
131 foreach ($this->attributes
as $key => $val) {
132 $val['attributeNo'] = $attributeNo++
;
133 $val['required'] = (int) isset($val['required']);
134 $val['useText'] = (int) isset($val['useText']);
135 $this->attributes
[$key] = (object) $val;
138 I18nHandler
::getInstance()->readValues();
139 $this->readButtonLabelFormParameter();
143 * Reads the form parameter for the button label.
145 protected function readButtonLabelFormParameter() {
146 if (I18nHandler
::getInstance()->isPlainValue('buttonLabel')) $this->buttonLabel
= I18nHandler
::getInstance()->getValue('buttonLabel');
152 public function validate() {
155 // tag name must not be empty
156 if (empty($this->bbcodeTag
)) {
157 throw new UserInputException('bbcodeTag');
160 // tag may only contain alphanumeric chars
161 if (!Regex
::compile('^[a-z0-9]+$', Regex
::CASE_INSENSITIVE
)->match($this->bbcodeTag
)) {
162 throw new UserInputException('bbcodeTag', 'invalid');
165 // disallow the Pseudo-BBCodes all and none
166 if ($this->bbcodeTag
== 'all' ||
$this->bbcodeTag
== 'none') {
167 throw new UserInputException('bbcodeTag', 'invalid');
170 // check whether the tag is in use
171 $this->validateBBCodeTagUsage();
174 if (!empty($this->className
) && !class_exists($this->className
)) {
175 throw new UserInputException('className', 'notFound');
178 // validate attributes
179 foreach ($this->attributes
as $attribute) {
180 // Check whether the pattern is a valid regex
181 if (!Regex
::compile($attribute->validationPattern
)->isValid()) {
182 throw new UserInputException('attributeValidationPattern'.$attribute->attributeNo
, 'invalid');
187 if ($this->showButton
) {
189 if (!I18nHandler
::getInstance()->validateValue('buttonLabel')) {
190 if (I18nHandler
::getInstance()->isPlainValue('buttonLabel')) {
191 throw new UserInputException('buttonLabel');
194 throw new UserInputException('buttonLabel', 'multilingual');
198 // validate image path
199 if (empty($this->wysiwygIcon
)) {
200 throw new UserInputException('wysiwygIcon');
204 $this->buttonLabel
= '';
209 * Validates the bbcode tag usage.
210 * @throws UserInputException
212 protected function validateBBCodeTagUsage() {
213 $bbcode = BBCode
::getBBCodeByTag($this->bbcodeTag
);
214 if ($bbcode->bbcodeID
) {
215 throw new UserInputException('bbcodeTag', 'inUse');
222 public function save() {
226 $this->objectAction
= new BBCodeAction([], 'create', ['data' => array_merge($this->additionalFields
, [
227 'bbcodeTag' => $this->bbcodeTag
,
228 'buttonLabel' => $this->buttonLabel
,
229 'className' => $this->className
,
230 'htmlOpen' => $this->htmlOpen
,
231 'htmlClose' => $this->htmlClose
,
232 'isBlockElement' => $this->isBlockElement ?
1 : 0,
233 'isSourceCode' => $this->isSourceCode ?
1 : 0,
235 'showButton' => $this->showButton ?
1 : 0,
236 'wysiwygIcon' => $this->wysiwygIcon
238 $returnValues = $this->objectAction
->executeAction();
239 foreach ($this->attributes
as $attribute) {
240 $attributeAction = new BBCodeAttributeAction([], 'create', ['data' => [
241 'bbcodeID' => $returnValues['returnValues']->bbcodeID
,
242 'attributeNo' => $attribute->attributeNo
,
243 'attributeHtml' => $attribute->attributeHtml
,
244 'validationPattern' => $attribute->validationPattern
,
245 'required' => $attribute->required
,
246 'useText' => $attribute->useText
248 $attributeAction->executeAction();
251 if ($this->showButton
&& !I18nHandler
::getInstance()->isPlainValue('buttonLabel')) {
252 $bbcodeID = $returnValues['returnValues']->bbcodeID
;
253 I18nHandler
::getInstance()->save('buttonLabel', 'wcf.editor.button.button'.$bbcodeID, 'wcf.editor', 1);
255 // update button label
256 $bbcodeEditor = new BBCodeEditor($returnValues['returnValues']);
257 $bbcodeEditor->update([
258 'buttonLabel' => 'wcf.editor.button.button'.$bbcodeID
265 $this->bbcodeTag
= $this->htmlOpen
= $this->htmlClose
= $this->className
= $this->buttonLabel
= $this->wysiwygIcon
= '';
266 $this->attributes
= [];
267 $this->isBlockElement
= $this->isSourceCode
= $this->showButton
= false;
269 I18nHandler
::getInstance()->reset();
271 // show success message
272 WCF
::getTPL()->assign('success', true);
278 public function assignVariables() {
279 parent
::assignVariables();
281 I18nHandler
::getInstance()->assignVariables();
283 WCF
::getTPL()->assign([
285 'attributes' => $this->attributes
,
286 'bbcodeTag' => $this->bbcodeTag
,
287 'buttonLabel' => $this->buttonLabel
,
288 'className' => $this->className
,
289 'htmlOpen' => $this->htmlOpen
,
290 'htmlClose' => $this->htmlClose
,
291 'isBlockElement' => $this->isBlockElement
,
292 'isSourceCode' => $this->isSourceCode
,
293 'showButton' => $this->showButton
,
294 'wysiwygIcon' => $this->wysiwygIcon