'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'}
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
*/
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'
+ )
+ );
+ }
+ })
+ ),
]);
}
}
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
{
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
{
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,
- ]);
}
}