Use the FormBuilder to create/edit media provider 6.2-formbuilder-media-provider
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 11:51:33 +0000 (12:51 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 11:51:33 +0000 (12:51 +0100)
wcfsetup/install/files/acp/templates/bbcodeMediaProviderAdd.tpl
wcfsetup/install/files/lib/acp/form/BBCodeMediaProviderAddForm.class.php
wcfsetup/install/files/lib/acp/form/BBCodeMediaProviderEditForm.class.php

index 0c304bc4129a7d83c13c828b8c156f3a9d29a089..ba45d0a21bf2c7c166540e211b7610275f53cc2b 100644 (file)
        </nav>
 </header>
 
-{include file='shared_formNotice'}
-
-<form method="post" action="{if $action == 'add'}{link controller='BBCodeMediaProviderAdd'}{/link}{else}{link controller='BBCodeMediaProviderEdit' object=$mediaProvider}{/link}{/if}">
-       <div class="section">
-               <dl{if $errorField == 'title'} class="formError"{/if}>
-                       <dt><label for="title">{lang}wcf.acp.bbcode.mediaProvider.title{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="title" name="title" value="{$title}" required autofocus class="long">
-                               {if $errorField == 'title'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.bbcode.mediaProvider.title.error.{$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                       </dd>
-               </dl>
-               
-               <dl{if $errorField == 'regex'} class="formError"{/if}>
-                       <dt><label for="regex">{lang}wcf.acp.bbcode.mediaProvider.regex{/lang}</label></dt>
-                       <dd>
-                               <textarea id="regex" name="regex" cols="40" rows="5" required>{$regex}</textarea>
-                               {if $errorField == 'regex'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.bbcode.mediaProvider.regex.error.{$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                               <small>{lang}wcf.acp.bbcode.mediaProvider.regex.description{/lang}</small>
-                       </dd>
-               </dl>
-               
-               <dl{if $errorField == 'html'} class="formError"{/if}>
-                       <dt><label for="html">{lang}wcf.acp.bbcode.mediaProvider.html{/lang}</label></dt>
-                       <dd>
-                               <textarea id="html" name="html" cols="40" rows="10">{$html}</textarea>
-                               {if $errorField == 'html'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.bbcode.mediaProvider.html.error.{$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                               <small>{lang}wcf.acp.bbcode.mediaProvider.html.description{/lang}</small>
-                       </dd>
-               </dl>
-               
-               <dl{if $errorField == 'className'} class="formError"{/if}>
-                       <dt><label for="className">{lang}wcf.acp.bbcode.mediaProvider.className{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="className" name="className" value="{$className}" pattern="^\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$" class="long">
-                               {if $errorField == 'className'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.bbcode.mediaProvider.className.error.{@$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                       </dd>
-               </dl>
-               
-               {event name='dataFields'}
-       </div>
-       
-       {event name='sections'}
-       
-       <div class="formSubmit">
-               <input type="submit" value="{lang}wcf.global.button.submit{/lang}" accesskey="s">
-               {csrfToken}
-       </div>
-</form>
+{unsafe:$form->getHtml()}
 
 {include file='footer'}
index 2d84ef2cd5be031b01bb2055a8353d212392ed3a..fb759ce92364a5e1d01596d08ec396977da113c2 100644 (file)
@@ -5,45 +5,33 @@ namespace wcf\acp\form;
 use wcf\data\bbcode\media\provider\BBCodeMediaProvider;
 use wcf\data\bbcode\media\provider\BBCodeMediaProviderAction;
 use wcf\data\bbcode\media\provider\BBCodeMediaProviderEditor;
-use wcf\form\AbstractForm;
-use wcf\system\exception\UserInputException;
+use wcf\form\AbstractFormBuilderForm;
+use wcf\system\bbcode\media\provider\IBBCodeMediaProvider;
+use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\field\ClassNameFormField;
+use wcf\system\form\builder\field\MultilineTextFormField;
+use wcf\system\form\builder\field\TextFormField;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\system\form\builder\field\validation\FormFieldValidator;
 use wcf\system\Regex;
-use wcf\system\request\LinkHandler;
-use wcf\system\WCF;
 use wcf\util\StringUtil;
 
 /**
  * Shows the BBCode media provider add form.
  *
- * @author  Tim Duesterhus
- * @copyright   2001-2019 WoltLab GmbH
- * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @author      Olaf Braun, Tim Duesterhus
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ *
+ * @property BBCodeMediaProvider $formObjects
  */
-class BBCodeMediaProviderAddForm extends AbstractForm
+class BBCodeMediaProviderAddForm extends AbstractFormBuilderForm
 {
     /**
      * @inheritDoc
      */
     public $activeMenuItem = 'wcf.acp.menu.link.bbcode.mediaProvider.add';
 
-    /**
-     * media provider class name
-     * @var string
-     */
-    public $className = '';
-
-    /**
-     * media provider package id
-     * @var int
-     */
-    public $packageID = PACKAGE_ID;
-
-    /**
-     * html value
-     * @var string
-     */
-    public $html = '';
-
     /**
      * @inheritDoc
      */
@@ -54,125 +42,91 @@ class BBCodeMediaProviderAddForm extends AbstractForm
      */
     public $templateName = 'bbcodeMediaProviderAdd';
 
-    /**
-     * title value
-     * @var string
-     */
-    public $title = '';
-
-    /**
-     * regex value
-     * @var string
-     */
-    public $regex = '';
-
     /**
      * @inheritDoc
      */
-    public function readFormParameters()
-    {
-        parent::readFormParameters();
-
-        if (isset($_POST['title'])) {
-            $this->title = StringUtil::trim($_POST['title']);
-        }
-        if (isset($_POST['regex'])) {
-            $this->regex = StringUtil::trim($_POST['regex']);
-        }
-        if (isset($_POST['html'])) {
-            $this->html = StringUtil::trim($_POST['html']);
-        }
-        if (isset($_POST['className'])) {
-            $this->className = StringUtil::trim($_POST['className']);
-        }
-    }
+    public $objectActionClass = BBCodeMediaProviderAction::class;
 
     /**
      * @inheritDoc
      */
-    public function validate()
-    {
-        parent::validate();
+    public $objectEditLinkController = BBCodeMediaProviderEditForm::class;
 
-        // validate fields
-        if (empty($this->title)) {
-            throw new UserInputException('title');
-        }
-        if (empty($this->regex)) {
-            throw new UserInputException('regex');
-        }
-        if (empty($this->className) && empty($this->html)) {
-            throw new UserInputException('html');
-        }
-        // validate class name
-        if (!empty($this->className) && !\class_exists($this->className)) {
-            throw new UserInputException('className', 'notFound');
-        }
-
-        $lines = \explode("\n", StringUtil::unifyNewlines($this->regex));
-
-        foreach ($lines as $line) {
-            if (!Regex::compile($line)->isValid()) {
-                throw new UserInputException('regex', 'invalid');
-            }
-        }
+    #[\Override]
+    protected function createForm()
+    {
+        parent::createForm();
+
+        $this->form->appendChildren([
+            FormContainer::create('general')
+                ->appendChildren([
+                    TextFormField::create('title')
+                        ->label('wcf.acp.bbcode.mediaProvider.title')
+                        ->required(),
+                    MultilineTextFormField::create('regex')
+                        ->label('wcf.acp.bbcode.mediaProvider.regex')
+                        ->description('wcf.acp.bbcode.mediaProvider.regex.description')
+                        ->required()
+                        ->addValidator(
+                            new FormFieldValidator('regexValidator', function (MultilineTextFormField $formField) {
+                                $lines = \explode("\n", StringUtil::unifyNewlines($formField->getValue()));
+
+                                foreach ($lines as $line) {
+                                    if (!Regex::compile($line)->isValid()) {
+                                        $formField->addValidationError(
+                                            new FormFieldValidationError(
+                                                'invalid',
+                                                'wcf.acp.bbcode.mediaProvider.regex.error.invalid'
+                                            )
+                                        );
+                                    }
+                                }
+                            })
+                        ),
+                    MultilineTextFormField::create('html')
+                        ->label('wcf.acp.bbcode.mediaProvider.html')
+                        ->description('wcf.acp.bbcode.mediaProvider.html.description')
+                        ->addValidator(
+                            new FormFieldValidator('emptyValidator', function (MultilineTextFormField $formField) {
+                                $classNameFormField = $formField->getDocument()->getNodeById('className');
+                                \assert($classNameFormField instanceof ClassNameFormField);
+
+                                if (empty($formField->getValue()) && empty($classNameFormField->getValue())) {
+                                    $formField->addValidationError(
+                                        new FormFieldValidationError('empty')
+                                    );
+                                }
+                            })
+                        ),
+                    ClassNameFormField::create('className')
+                        ->label('wcf.acp.bbcode.mediaProvider.className')
+                        ->implementedInterface(IBBCodeMediaProvider::class)
+                ])
+        ]);
     }
 
-    /**
-     * @inheritDoc
-     */
+    #[\Override]
     public function save()
     {
-        parent::save();
-
-        $name = 'placeholder_' . StringUtil::getRandomID();
-
-        // save media provider
-        $this->objectAction = new BBCodeMediaProviderAction([], 'create', [
-            'data' => \array_merge($this->additionalFields, [
-                'title' => $this->title,
-                'regex' => $this->regex,
-                'html' => $this->html,
-                'className' => $this->className,
-                'packageID' => $this->packageID,
-                'name' => $name,
-            ]),
-        ]);
-        $returnValues = $this->objectAction->executeAction();
-        $this->saved();
-
-        /** @var BBCodeMediaProvider $provider */
-        $provider = $returnValues['returnValues'];
-        (new BBCodeMediaProviderEditor($provider))->update([
-            'name' => 'com.woltlab.wcf.generic' . $provider->providerID,
-        ]);
-
-        // reset values
-        $this->title = $this->regex = $this->html = $this->className = '';
+        if ($this->formAction === "create") {
+            $this->additionalFields['packageID'] = PACKAGE_ID;
+            $this->additionalFields['name'] = 'placeholder_' . StringUtil::getRandomID();
+        }
 
-        // show success message
-        WCF::getTPL()->assign([
-            'success' => true,
-            'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
-                BBCodeMediaProviderEditForm::class,
-                ['id' => $provider->providerID]
-            ),
-        ]);
+        parent::save();
     }
 
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
+    #[\Override]
+    public function saved()
     {
-        parent::assignVariables();
+        if ($this->formAction === "create") {
+            /** @var BBCodeMediaProvider $provider */
+            $provider = $this->objectAction->getReturnValues()['returnValues'];
+            (new BBCodeMediaProviderEditor($provider))->update([
+                'name' => 'com.woltlab.wcf.generic' . $provider->providerID,
+            ]);
+        }
 
-        WCF::getTPL()->assign([
-            'action' => 'add',
-            'title' => $this->title,
-            'regex' => $this->regex,
-            'html' => $this->html,
-            'className' => $this->className,
-        ]);
+        parent::saved();
     }
 }
index b49db87ab0cf8dcbdfaede017d0f194679d5420c..0b8876910d9ec6b281b07e371b8b4a53c96a96b0 100644 (file)
@@ -2,11 +2,10 @@
 
 namespace wcf\acp\form;
 
+use CuyZ\Valinor\Mapper\MappingError;
 use wcf\data\bbcode\media\provider\BBCodeMediaProvider;
-use wcf\data\bbcode\media\provider\BBCodeMediaProviderAction;
-use wcf\form\AbstractForm;
+use wcf\http\Helper;
 use wcf\system\exception\IllegalLinkException;
-use wcf\system\WCF;
 
 /**
  * Shows the BBCode media provider edit form.
@@ -27,87 +26,32 @@ class BBCodeMediaProviderEditForm extends BBCodeMediaProviderAddForm
      */
     public $neededPermissions = ['admin.content.bbcode.canManageBBCode'];
 
-    /**
-     * id of the edited media provider
-     * @var int
-     */
-    public $providerID = 0;
-
-    /**
-     * edited media provider object
-     * @var BBCodeMediaProvider
-     */
-    public $mediaProvider;
-
     /**
      * @inheritDoc
      */
+    public $formAction = 'edit';
+
+    #[\Override]
     public function readParameters()
     {
         parent::readParameters();
 
-        if (isset($_REQUEST['id'])) {
-            $this->providerID = \intval($_REQUEST['id']);
-        }
-        $this->mediaProvider = new BBCodeMediaProvider($this->providerID);
-        if (!$this->mediaProvider->providerID) {
+        try {
+            $queryParameters = Helper::mapQueryParameters(
+                $_GET,
+                <<<'EOT'
+                    array {
+                        id: positive-int
+                    }
+                    EOT
+            );
+            $this->formObject = new BBCodeMediaProvider($queryParameters['id']);
+
+            if (!$this->formObject->getObjectID()) {
+                throw new IllegalLinkException();
+            }
+        } catch (MappingError) {
             throw new IllegalLinkException();
         }
     }
-
-    /**
-     * @inheritDoc
-     */
-    public function save()
-    {
-        AbstractForm::save();
-
-        // update media-provider
-        $this->objectAction = new BBCodeMediaProviderAction(
-            [$this->providerID],
-            'update',
-            [
-                'data' => \array_merge($this->additionalFields, [
-                    'title' => $this->title,
-                    'regex' => $this->regex,
-                    'html' => $this->html,
-                    'className' => $this->className,
-                ]),
-            ]
-        );
-        $this->objectAction->executeAction();
-
-        $this->saved();
-
-        // show success message
-        WCF::getTPL()->assign('success', true);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function readData()
-    {
-        parent::readData();
-
-        if (empty($_POST)) {
-            $this->title = $this->mediaProvider->title;
-            $this->regex = $this->mediaProvider->regex;
-            $this->html = $this->mediaProvider->html;
-            $this->className = $this->mediaProvider->className;
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        WCF::getTPL()->assign([
-            'mediaProvider' => $this->mediaProvider,
-            'action' => 'edit',
-        ]);
-    }
 }