Use form builder for user option categories
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 12:24:58 +0000 (13:24 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 12:24:58 +0000 (13:24 +0100)
wcfsetup/install/files/acp/templates/userOptionCategoryAdd.tpl
wcfsetup/install/files/lib/acp/form/UserOptionCategoryAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserOptionCategoryEditForm.class.php

index bfddaab68b4a6eeb9fcc940fad8c4cd8c97ab09e..c05bd3ff933a9e3ebdea73c26ef0b6c136b504bb 100644 (file)
                        {event name='contentHeaderNavigation'}
                </ul>
        </nav>
-
 </header>
 
-{include file='shared_formNotice'}
-
-<form method="post" action="{if $action == 'add'}{link controller='UserOptionCategoryAdd'}{/link}{else}{link controller='UserOptionCategoryEdit' id=$categoryID}{/link}{/if}">
-       <div class="section">
-               <dl{if $errorField == 'categoryName'} class="formError"{/if}>
-                       <dt><label for="categoryName">{lang}wcf.global.name{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="categoryName" name="categoryName" value="{$i18nPlainValues['categoryName']}" required autofocus class="long">
-                               {if $errorField == 'categoryName'}
-                                       <small class="innerError">
-                                               {if $errorType == 'multilingual'}
-                                                       {lang}wcf.global.form.error.multilingual{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.user.option.category.name.error.{@$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                       </dd>
-               </dl>
-               {include file='shared_multipleLanguageInputJavascript' elementIdentifier='categoryName' forceSelection=true}
-               
-               <dl>
-                       <dt><label for="showOrder">{lang}wcf.global.showOrder{/lang}</label></dt>
-                       <dd>
-                               <input type="number" id="showOrder" name="showOrder" value="{$showOrder}" class="short">
-                       </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 dc2cfba2b02f74fdd6f45363638a355fae1f1e86..568ce03d27a5fc83164d32407050d69cb0cb0331 100644 (file)
@@ -2,22 +2,29 @@
 
 namespace wcf\acp\form;
 
+use wcf\data\IStorableObject;
+use wcf\data\user\option\category\UserOptionCategory;
 use wcf\data\user\option\category\UserOptionCategoryAction;
 use wcf\data\user\option\category\UserOptionCategoryEditor;
-use wcf\form\AbstractForm;
-use wcf\system\exception\UserInputException;
+use wcf\data\user\option\category\UserOptionCategoryList;
+use wcf\form\AbstractFormBuilderForm;
+use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\data\processor\CustomFormDataProcessor;
+use wcf\system\form\builder\field\ShowOrderFormField;
+use wcf\system\form\builder\field\TextFormField;
+use wcf\system\form\builder\IFormDocument;
 use wcf\system\language\I18nHandler;
-use wcf\system\request\LinkHandler;
-use wcf\system\WCF;
 
 /**
  * Shows the form for adding new user option categories.
  *
- * @author  Marcel Werk
- * @copyright   2001-2019 WoltLab GmbH
- * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @author      Olaf Braun, Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ *
+ * @property UserOptionCategory $formObject
  */
-class UserOptionCategoryAddForm extends AbstractForm
+class UserOptionCategoryAddForm extends AbstractFormBuilderForm
 {
     /**
      * @inheritDoc
@@ -30,75 +37,76 @@ class UserOptionCategoryAddForm extends AbstractForm
     public $neededPermissions = ['admin.user.canManageUserOption'];
 
     /**
-     * category name
-     * @var string
-     */
-    public $categoryName = '';
-
-    /**
-     * show order
-     * @var int
+     * @inheritDoc
      */
-    public $showOrder = 0;
+    public $objectActionClass = UserOptionCategoryAction::class;
 
     /**
      * @inheritDoc
      */
-    public function readParameters()
-    {
-        parent::readParameters();
-
-        I18nHandler::getInstance()->register('categoryName');
-    }
+    public $objectEditLinkController = UserOptionCategoryEditForm::class;
 
     /**
      * @inheritDoc
      */
-    public function readFormParameters()
-    {
-        parent::readFormParameters();
+    public $additionalFields = ['parentCategoryName' => 'profile'];
 
-        I18nHandler::getInstance()->readValues();
-
-        if (I18nHandler::getInstance()->isPlainValue('categoryName')) {
-            $this->categoryName = I18nHandler::getInstance()->getValue('categoryName');
-        }
-        if (isset($_POST['showOrder'])) {
-            $this->showOrder = \intval($_POST['showOrder']);
-        }
+    #[\Override]
+    protected function createForm()
+    {
+        parent::createForm();
+
+        $this->form->appendChildren([
+            FormContainer::create('general')
+                ->appendChildren([
+                    TextFormField::create('categoryName')
+                        ->required()
+                        ->label('wcf.global.name')
+                        ->i18n()
+                        ->i18nRequired()
+                        ->languageItemPattern('wcf.user.option.category.(category\d+|[\w\.]+)'),
+                    ShowOrderFormField::create()
+                        ->options(function () {
+                            $categoryList = new UserOptionCategoryList();
+                            $categoryList->getConditionBuilder()->add('parentCategoryName = ?', ['profile']);
+                            $categoryList->readObjects();
+                            $categories = [];
+
+                            foreach ($categoryList->getObjects() as $category) {
+                                $categories[$category->categoryID] = $category->getTitle();
+                            }
+
+                            return $categories;
+                        }),
+                ]),
+        ]);
     }
 
-    /**
-     * @inheritDoc
-     */
-    public function validate()
+    #[\Override]
+    protected function finalizeForm()
     {
-        parent::validate();
-
-        if (!I18nHandler::getInstance()->validateValue('categoryName', true)) {
-            throw new UserInputException('categoryName', 'multilingual');
-        }
+        parent::finalizeForm();
+
+        $this->form->getDataHandler()
+            ->addProcessor(
+                new CustomFormDataProcessor(
+                    'categoryName',
+                    null,
+                    function (IFormDocument $document, array $data, IStorableObject $object) {
+                        \assert($object instanceof UserOptionCategory);
+                        $data['categoryName'] = 'wcf.user.option.category.' . $object->categoryName;
+
+                        return $data;
+                    }
+                ),
+            );
     }
 
-    /**
-     * @inheritDoc
-     */
-    public function save()
+    #[\Override]
+    public function saved()
     {
-        parent::save();
-
-        // save label
-        $this->objectAction = new UserOptionCategoryAction([], 'create', [
-            'data' => \array_merge($this->additionalFields, [
-                'parentCategoryName' => 'profile',
-                'categoryName' => $this->categoryName,
-                'showOrder' => $this->showOrder,
-            ]),
-        ]);
-        $this->objectAction->executeAction();
-
-        // update name
         $returnValues = $this->objectAction->getReturnValues();
+
         $categoryID = $returnValues['returnValues']->categoryID;
         I18nHandler::getInstance()->save(
             'categoryName',
@@ -109,37 +117,7 @@ class UserOptionCategoryAddForm extends AbstractForm
         $categoryEditor->update([
             'categoryName' => 'category' . $categoryID,
         ]);
-        $this->saved();
 
-        // reset values
-        $this->categoryName = '';
-        $this->showOrder = 0;
-
-        I18nHandler::getInstance()->reset();
-
-        // show success message
-        WCF::getTPL()->assign([
-            'success' => true,
-            'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
-                UserOptionCategoryEditForm::class,
-                ['id' => $categoryID]
-            ),
-        ]);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        I18nHandler::getInstance()->assignVariables();
-
-        WCF::getTPL()->assign([
-            'action' => 'add',
-            'categoryName' => $this->categoryName,
-            'showOrder' => $this->showOrder,
-        ]);
+        parent::saved();
     }
 }
index ad968173adedfcb66fda9e7fb3b1ac3afa9f4547..261249c9527dd00d52b6806f3ffb0e32642e347f 100644 (file)
@@ -2,12 +2,12 @@
 
 namespace wcf\acp\form;
 
+use CuyZ\Valinor\Mapper\MappingError;
 use wcf\data\user\option\category\UserOptionCategory;
-use wcf\data\user\option\category\UserOptionCategoryAction;
-use wcf\form\AbstractForm;
+use wcf\form\AbstractFormBuilderForm;
+use wcf\http\Helper;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\language\I18nHandler;
-use wcf\system\WCF;
 
 /**
  * Shows the form for editing user option categories.
@@ -23,90 +23,45 @@ class UserOptionCategoryEditForm extends UserOptionCategoryAddForm
      */
     public $activeMenuItem = 'wcf.acp.menu.link.user.option.category.list';
 
-    /**
-     * category id
-     * @var int
-     */
-    public $categoryID = 0;
-
-    /**
-     * category object
-     * @var UserOptionCategory
-     */
-    public $category;
-
     /**
      * @inheritDoc
      */
+    public $formAction = 'edit';
+
+    #[\Override]
     public function readParameters()
     {
         parent::readParameters();
 
-        if (isset($_REQUEST['id'])) {
-            $this->categoryID = \intval($_REQUEST['id']);
-        }
-        $this->category = new UserOptionCategory($this->categoryID);
-        if (!$this->category->categoryID) {
+        try {
+            $queryParameters = Helper::mapQueryParameters(
+                $_GET,
+                <<<'EOT'
+                    array {
+                        id: positive-int
+                    }
+                    EOT
+            );
+            $this->formObject = new UserOptionCategory($queryParameters['id']);
+
+            if (!$this->formObject->getObjectID()) {
+                throw new IllegalLinkException();
+            }
+        } catch (MappingError) {
             throw new IllegalLinkException();
         }
     }
 
-    /**
-     * @inheritDoc
-     */
-    public function save()
-    {
-        AbstractForm::save();
 
+    #[\Override]
+    public function saved()
+    {
         I18nHandler::getInstance()->save(
             'categoryName',
-            'wcf.user.option.category.' . $this->category->categoryName,
+            'wcf.user.option.category.' . $this->formObject->categoryName,
             'wcf.user.option'
         );
 
-        $this->objectAction = new UserOptionCategoryAction([$this->category], 'update', [
-            'data' => \array_merge($this->additionalFields, [
-                'showOrder' => $this->showOrder,
-            ]),
-        ]);
-        $this->objectAction->executeAction();
-        $this->saved();
-
-        WCF::getTPL()->assign('success', true);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function readData()
-    {
-        parent::readData();
-
-        I18nHandler::getInstance()->setOptions(
-            'categoryName',
-            1,
-            'wcf.user.option.category.' . $this->category->categoryName,
-            'wcf.user.option.category.category\d+'
-        );
-
-        if (!\count($_POST)) {
-            $this->showOrder = $this->category->showOrder;
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        I18nHandler::getInstance()->assignVariables(!empty($_POST));
-
-        WCF::getTPL()->assign([
-            'action' => 'edit',
-            'categoryID' => $this->categoryID,
-            'category' => $this->category,
-        ]);
+        AbstractFormBuilderForm::saved();
     }
 }