Use the FormBuilder to create/edit template groups
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 08:52:52 +0000 (09:52 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 08:52:52 +0000 (09:52 +0100)
wcfsetup/install/files/acp/templates/templateGroupAdd.tpl
wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php
wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php

index 306cb79a600020d4bd32f7417efb3ec68e57a3cd..0b891494215410fb2ed8a92b9f2c78ccc0c2d6e8 100644 (file)
@@ -11,8 +11,8 @@
                                'wcf.acp.template.group.folderName.error.notUnique': '{jslang}wcf.acp.template.group.folderName.error.notUnique{/jslang}',
                                'wcf.global.name': '{jslang}wcf.global.name{/jslang}'
                        });
-                       
-                       AcpUiTemplateGroupCopy.init({$templateGroupID});
+
+                       AcpUiTemplateGroupCopy.init({$formObject->templateGroupID});
                });
        </script>
 {/if}
        </nav>
 </header>
 
-{include file='shared_formNotice'}
-
-<form method="post" action="{if $action == 'add'}{link controller='TemplateGroupAdd'}{/link}{else}{link controller='TemplateGroupEdit' id=$templateGroupID}{/link}{/if}">
-       <div class="section">
-               {if $availableTemplateGroups|count}
-                       <dl>
-                               <dt><label for="parentTemplateGroupID">{lang}wcf.acp.template.group.parentTemplateGroup{/lang}</label></dt>
-                               <dd>
-                                       <select name="parentTemplateGroupID" id="parentTemplateGroupID">
-                                               <option value="0">{lang}wcf.acp.template.group.default{/lang}</option>
-                                               {htmlOptions options=$availableTemplateGroups selected=$parentTemplateGroupID disableEncoding=true}
-                                       </select>
-                                       {if $errorField == 'parentTemplateGroupID'}
-                                               <small class="innerError">
-                                                       {if $errorType == 'empty'}
-                                                               {lang}wcf.global.form.error.empty{/lang}
-                                                       {else}
-                                                               {lang}wcf.acp.template.group.parentTemplateGroupID.error.{@$errorType}{/lang}
-                                                       {/if}
-                                               </small>
-                                       {/if}
-                               </dd>
-                       </dl>
-               {/if}
-               
-               <dl{if $errorField == 'templateGroupName'} class="formError"{/if}>
-                       <dt><label for="templateGroupName">{lang}wcf.global.name{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="templateGroupName" name="templateGroupName" value="{$templateGroupName}" required class="long">
-                               {if $errorField == 'templateGroupName'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.template.group.name.error.{@$errorType}{/lang}
-                                               {/if}
-                                       </small>
-                               {/if}
-                       </dd>
-               </dl>
-               
-               <dl{if $errorField == 'templateGroupFolderName'} class="formError"{/if}>
-                       <dt><label for="templateGroupFolderName">{lang}wcf.acp.template.group.folderName{/lang}</label></dt>
-                       <dd>
-                               <input type="text" id="templateGroupFolderName" name="templateGroupFolderName" value="{$templateGroupFolderName}" required class="long">
-                               {if $errorField == 'templateGroupFolderName'}
-                                       <small class="innerError">
-                                               {if $errorType == 'empty'}
-                                                       {lang}wcf.global.form.error.empty{/lang}
-                                               {else}
-                                                       {lang}wcf.acp.template.group.folderName.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 fc829f55b69c3fdfee54921f57ae474c3eb8ea48..6ae29ee63b47345faf9dca7c99cb0cd310a42ea8 100644 (file)
@@ -4,21 +4,24 @@ namespace wcf\acp\form;
 
 use wcf\data\template\group\TemplateGroup;
 use wcf\data\template\group\TemplateGroupAction;
-use wcf\form\AbstractForm;
-use wcf\system\exception\UserInputException;
-use wcf\system\request\LinkHandler;
+use wcf\form\AbstractFormBuilderForm;
+use wcf\system\form\builder\field\SelectFormField;
+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\WCF;
 use wcf\util\FileUtil;
-use wcf\util\StringUtil;
 
 /**
  * Shows the form for adding new template groups.
  *
- * @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 TemplateGroup $formObject
  */
-class TemplateGroupAddForm extends AbstractForm
+class TemplateGroupAddForm extends AbstractFormBuilderForm
 {
     /**
      * @inheritDoc
@@ -30,164 +33,94 @@ class TemplateGroupAddForm extends AbstractForm
      */
     public $neededPermissions = ['admin.template.canManageTemplate'];
 
-    /**
-     * template group name
-     * @var string
-     */
-    public $templateGroupName = '';
-
-    /**
-     * template group folder
-     * @var int
-     */
-    public $templateGroupFolderName = '';
-
-    /**
-     * parent template group id
-     * @var int
-     */
-    public $parentTemplateGroupID = 0;
-
-    /**
-     * available template groups
-     * @var array
-     */
-    public $availableTemplateGroups = [];
-
     /**
      * @inheritDoc
      */
-    public function readFormParameters()
-    {
-        parent::readFormParameters();
-
-        if (isset($_POST['templateGroupName'])) {
-            $this->templateGroupName = StringUtil::trim($_POST['templateGroupName']);
-        }
-        if (!empty($_POST['templateGroupFolderName'])) {
-            $this->templateGroupFolderName = StringUtil::trim($_POST['templateGroupFolderName']);
-            if ($this->templateGroupFolderName) {
-                $this->templateGroupFolderName = FileUtil::addTrailingSlash($this->templateGroupFolderName);
-            }
-        }
-        if (isset($_POST['parentTemplateGroupID'])) {
-            $this->parentTemplateGroupID = \intval($_POST['parentTemplateGroupID']);
-        }
-    }
+    public $objectActionClass = TemplateGroupAction::class;
 
     /**
      * @inheritDoc
      */
-    public function validate()
-    {
-        parent::validate();
-
-        $this->validateName();
-        $this->validateFolderName();
-
-        if ($this->parentTemplateGroupID && !isset($this->availableTemplateGroups[$this->parentTemplateGroupID])) {
-            throw new UserInputException('parentTemplateGroupID', 'invalid');
-        }
-    }
-
-    /**
-     * Validates the template group name.
-     */
-    protected function validateName()
-    {
-        if (empty($this->templateGroupName)) {
-            throw new UserInputException('templateGroupName');
-        }
+    public $objectEditLinkController = TemplateGroupEditForm::class;
 
-        $sql = "SELECT  COUNT(*)
-                FROM    wcf1_template_group
-                WHERE   templateGroupName = ?";
-        $statement = WCF::getDB()->prepare($sql);
-        $statement->execute([$this->templateGroupName]);
-
-        if ($statement->fetchSingleColumn()) {
-            throw new UserInputException('templateGroupName', 'notUnique');
-        }
-    }
-
-    /**
-     * Validates the template group folder name.
-     */
-    protected function validateFolderName()
+    #[\Override]
+    protected function createForm()
     {
-        if (empty($this->templateGroupFolderName)) {
-            throw new UserInputException('templateGroupFolderName');
-        }
-
-        if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $this->templateGroupFolderName)) {
-            throw new UserInputException('templateGroupFolderName', 'invalid');
-        }
-
-        $sql = "SELECT  COUNT(*)
-                FROM    wcf1_template_group
-                WHERE   templateGroupFolderName = ?";
-        $statement = WCF::getDB()->prepare($sql);
-        $statement->execute([$this->templateGroupFolderName]);
-
-        if ($statement->fetchSingleColumn()) {
-            throw new UserInputException('templateGroupFolderName', 'notUnique');
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function save()
-    {
-        parent::save();
-
-        $this->objectAction = new TemplateGroupAction([], 'create', [
-            'data' => \array_merge($this->additionalFields, [
-                'templateGroupName' => $this->templateGroupName,
-                'templateGroupFolderName' => $this->templateGroupFolderName,
-                'parentTemplateGroupID' => $this->parentTemplateGroupID ?: null,
-            ]),
-        ]);
-        $returnValues = $this->objectAction->executeAction();
-        $this->saved();
-
-        // reset values
-        $this->templateGroupName = $this->templateGroupFolderName = '';
-        $this->parentTemplateGroupID = 0;
-
-        // show success message
-        WCF::getTPL()->assign([
-            'success' => true,
-            'objectEditLink' => LinkHandler::getInstance()->getControllerLink(
-                TemplateGroupEditForm::class,
-                ['id' => $returnValues['returnValues']->templateGroupID]
-            ),
-        ]);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function readData()
-    {
-        $this->availableTemplateGroups = TemplateGroup::getSelectList([-1], 1);
-
-        parent::readData();
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        WCF::getTPL()->assign([
-            'action' => 'add',
-            'templateGroupName' => $this->templateGroupName,
-            'templateGroupFolderName' => $this->templateGroupFolderName,
-            'parentTemplateGroupID' => $this->parentTemplateGroupID,
-            'availableTemplateGroups' => $this->availableTemplateGroups,
+        parent::createForm();
+
+        $availableTemplateGroups = TemplateGroup::getSelectList([-1], 1);
+
+        $this->form->appendChildren([
+            SelectFormField::create('parentTemplateGroupID')
+                ->label('wcf.acp.template.group.parentTemplateGroup')
+                ->options($availableTemplateGroups)
+                ->available(\count($availableTemplateGroups) > 0),
+            TextFormField::create('templateGroupName')
+                ->label('wcf.global.name')
+                ->required()
+                ->addValidator(
+                    new FormFieldValidator('templateNameValidator', function (TextFormField $formField) {
+                        if ($formField->getValue() === $this->formObject?->templateGroupName) {
+                            return;
+                        }
+
+                        $sql = "SELECT  COUNT(*)
+                                FROM    wcf1_template_group
+                                WHERE   templateGroupName = ?";
+                        $statement = WCF::getDB()->prepare($sql);
+                        $statement->execute([$formField->getValue()]);
+
+                        if ($statement->fetchSingleColumn()) {
+                            $formField->addValidationError(
+                                new FormFieldValidationError(
+                                    'notUnique',
+                                    'wcf.acp.template.group.name.error.notUnique'
+                                )
+                            );
+                        }
+                    })
+                ),
+            TextFormField::create('templateGroupFolderName')
+                ->label('wcf.acp.template.group.folderName')
+                ->required()
+                ->addValidator(
+                    new FormFieldValidator('folderNameValidator', function (TextFormField $formField) {
+                        $formField->value(FileUtil::addTrailingSlash($formField->getValue()));
+
+                        if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $formField->getValue())) {
+                            $formField->addValidationError(
+                                new FormFieldValidationError(
+                                    'invalid',
+                                    'wcf.acp.template.group.folderName.error.invalid'
+                                )
+                            );
+                        }
+                    })
+                )
+                ->addValidator(
+                    new FormFieldValidator('uniqueFolderNameValidator', function (TextFormField $formField) {
+                        $formField->value(FileUtil::addTrailingSlash($formField->getValue()));
+
+                        if ($formField->getValue() === $this->formObject?->templateGroupFolderName) {
+                            return;
+                        }
+
+                        $sql = "SELECT  COUNT(*)
+                                FROM    wcf1_template_group
+                                WHERE   templateGroupFolderName = ?";
+                        $statement = WCF::getDB()->prepare($sql);
+                        $statement->execute([$formField->getValue()]);
+
+                        if ($statement->fetchSingleColumn()) {
+                            $formField->addValidationError(
+                                new FormFieldValidationError(
+                                    'notUnique',
+                                    'wcf.acp.template.group.folderName.error.notUnique'
+                                )
+                            );
+                        }
+                    })
+                ),
         ]);
     }
 }
index a39f9aa2f357a4769fa6f8d18790e8112c25ba3b..3c1acca51b8d5d17ab9df7fd66ad03056e1bf3c8 100644 (file)
@@ -2,19 +2,18 @@
 
 namespace wcf\acp\form;
 
+use CuyZ\Valinor\Mapper\MappingError;
 use wcf\data\template\group\TemplateGroup;
-use wcf\data\template\group\TemplateGroupAction;
-use wcf\form\AbstractForm;
+use wcf\http\Helper;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\PermissionDeniedException;
-use wcf\system\WCF;
 
 /**
  * Shows the form for editing template groups.
  *
- * @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>
  */
 class TemplateGroupEditForm extends TemplateGroupAddForm
 {
@@ -24,16 +23,9 @@ class TemplateGroupEditForm extends TemplateGroupAddForm
     public $activeMenuItem = 'wcf.acp.menu.link.template.group.list';
 
     /**
-     * template group id
-     * @var int
-     */
-    public $templateGroupID = 0;
-
-    /**
-     * template group object
-     * @var TemplateGroup
+     * @inheritDoc
      */
-    public $templateGroup;
+    public $formAction = 'edit';
 
     /**
      * @inheritDoc
@@ -42,91 +34,26 @@ class TemplateGroupEditForm extends TemplateGroupAddForm
     {
         parent::readParameters();
 
-        if (isset($_REQUEST['id'])) {
-            $this->templateGroupID = \intval($_REQUEST['id']);
-        }
-        $this->templateGroup = new TemplateGroup($this->templateGroupID);
-        if (!$this->templateGroup->templateGroupID) {
+        try {
+            $queryParameters = Helper::mapQueryParameters(
+                $_GET,
+                <<<'EOT'
+                    array {
+                        id: positive-int
+                    }
+                    EOT
+            );
+            $this->formObject = new TemplateGroup($queryParameters['id']);
+
+            if (!$this->formObject->getObjectID()) {
+                throw new IllegalLinkException();
+            }
+
+            if ($this->formObject->isImmutable()) {
+                throw new PermissionDeniedException();
+            }
+        } catch (MappingError) {
             throw new IllegalLinkException();
         }
-        if ($this->templateGroup->isImmutable()) {
-            throw new PermissionDeniedException();
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    protected function validateName()
-    {
-        if ($this->templateGroupName != $this->templateGroup->templateGroupName) {
-            parent::validateName();
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    protected function validateFolderName()
-    {
-        if ($this->templateGroupFolderName != $this->templateGroup->templateGroupFolderName) {
-            parent::validateFolderName();
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function save()
-    {
-        AbstractForm::save();
-
-        $this->objectAction = new TemplateGroupAction(
-            [$this->templateGroup],
-            'update',
-            [
-                'data' => \array_merge($this->additionalFields, [
-                    'templateGroupName' => $this->templateGroupName,
-                    'templateGroupFolderName' => $this->templateGroupFolderName,
-                    'parentTemplateGroupID' => $this->parentTemplateGroupID ?: null,
-                ]),
-            ]
-        );
-        $this->objectAction->executeAction();
-        $this->saved();
-
-        // show success message
-        WCF::getTPL()->assign('success', true);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function readData()
-    {
-        $this->availableTemplateGroups = TemplateGroup::getSelectList([$this->templateGroupID, -1], 1);
-
-        AbstractForm::readData();
-
-        // default values
-        if (!\count($_POST)) {
-            $this->templateGroupName = $this->templateGroup->templateGroupName;
-            $this->templateGroupFolderName = $this->templateGroup->templateGroupFolderName;
-            $this->parentTemplateGroupID = $this->templateGroup->parentTemplateGroupID;
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public function assignVariables()
-    {
-        parent::assignVariables();
-
-        WCF::getTPL()->assign([
-            'action' => 'edit',
-            'templateGroupID' => $this->templateGroupID,
-            'templateGroup' => $this->templateGroup,
-        ]);
     }
 }