From 98759b6f52ff95964d8b3649c868ba327f3e2228 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 18 Nov 2024 13:24:58 +0100 Subject: [PATCH] Use form builder for user option categories --- .../acp/templates/userOptionCategoryAdd.tpl | 40 +---- .../form/UserOptionCategoryAddForm.class.php | 160 ++++++++---------- .../form/UserOptionCategoryEditForm.class.php | 97 +++-------- 3 files changed, 96 insertions(+), 201 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/userOptionCategoryAdd.tpl b/wcfsetup/install/files/acp/templates/userOptionCategoryAdd.tpl index bfddaab68b..c05bd3ff93 100644 --- a/wcfsetup/install/files/acp/templates/userOptionCategoryAdd.tpl +++ b/wcfsetup/install/files/acp/templates/userOptionCategoryAdd.tpl @@ -12,46 +12,8 @@ {event name='contentHeaderNavigation'} - -{include file='shared_formNotice'} - -
-
- -
-
- - {if $errorField == 'categoryName'} - - {if $errorType == 'multilingual'} - {lang}wcf.global.form.error.multilingual{/lang} - {else} - {lang}wcf.acp.user.option.category.name.error.{@$errorType}{/lang} - {/if} - - {/if} -
- - {include file='shared_multipleLanguageInputJavascript' elementIdentifier='categoryName' forceSelection=true} - -
-
-
- -
-
- - {event name='dataFields'} -
- - {event name='sections'} - -
- - {csrfToken} -
-
+{unsafe:$form->getHtml()} {include file='footer'} diff --git a/wcfsetup/install/files/lib/acp/form/UserOptionCategoryAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserOptionCategoryAddForm.class.php index dc2cfba2b0..568ce03d27 100644 --- a/wcfsetup/install/files/lib/acp/form/UserOptionCategoryAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserOptionCategoryAddForm.class.php @@ -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 + * @author Olaf Braun, Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License + * + * @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(); } } diff --git a/wcfsetup/install/files/lib/acp/form/UserOptionCategoryEditForm.class.php b/wcfsetup/install/files/lib/acp/form/UserOptionCategoryEditForm.class.php index ad968173ad..261249c952 100644 --- a/wcfsetup/install/files/lib/acp/form/UserOptionCategoryEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserOptionCategoryEditForm.class.php @@ -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(); } } -- 2.20.1