From a630baacb91fe304972dbb236a41a689801b0fa2 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Thu, 14 Nov 2024 09:52:52 +0100 Subject: [PATCH] Use the FormBuilder to create/edit template groups --- .../files/acp/templates/templateGroupAdd.tpl | 72 +---- .../acp/form/TemplateGroupAddForm.class.php | 247 +++++++----------- .../acp/form/TemplateGroupEditForm.class.php | 125 ++------- 3 files changed, 119 insertions(+), 325 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl b/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl index 306cb79a60..0b89149421 100644 --- a/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl +++ b/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl @@ -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}); }); {/if} @@ -32,72 +32,6 @@ -{include file='shared_formNotice'} - -
-
- {if $availableTemplateGroups|count} -
-
-
- - {if $errorField == 'parentTemplateGroupID'} - - {if $errorType == 'empty'} - {lang}wcf.global.form.error.empty{/lang} - {else} - {lang}wcf.acp.template.group.parentTemplateGroupID.error.{@$errorType}{/lang} - {/if} - - {/if} -
-
- {/if} - - -
-
- - {if $errorField == 'templateGroupName'} - - {if $errorType == 'empty'} - {lang}wcf.global.form.error.empty{/lang} - {else} - {lang}wcf.acp.template.group.name.error.{@$errorType}{/lang} - {/if} - - {/if} -
- - - -
-
- - {if $errorField == 'templateGroupFolderName'} - - {if $errorType == 'empty'} - {lang}wcf.global.form.error.empty{/lang} - {else} - {lang}wcf.acp.template.group.folderName.error.{@$errorType}{/lang} - {/if} - - {/if} -
- - - {event name='dataFields'} -
- - {event name='sections'} - -
- - {csrfToken} -
-
+{unsafe:$form->getHtml()} {include file='footer'} diff --git a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php index fc829f55b6..6ae29ee63b 100644 --- a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php @@ -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 + * @author Olaf Braun, Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License + * + * @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' + ) + ); + } + }) + ), ]); } } diff --git a/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php index a39f9aa2f3..3c1acca51b 100644 --- a/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php @@ -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 + * @author Olaf Braun, Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License */ 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, - ]); } } -- 2.20.1