diff --git a/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts b/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts index 1650d84c098..ad6da2484cf 100644 --- a/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts +++ b/ts/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.ts @@ -1,143 +1,29 @@ /** * Provides a dialog to copy an existing template group. * - * @author Alexander Ebert - * @copyright 2001-2019 WoltLab GmbH + * @author Olaf Braun, Alexander Ebert + * @copyright 2001-2024 WoltLab GmbH * @license GNU Lesser General Public License */ -import * as Ajax from "../../../../Ajax"; -import { AjaxCallbackObject, AjaxCallbackSetup } from "../../../../Ajax/Data"; -import { DialogCallbackObject, DialogCallbackSetup } from "../../../../Ui/Dialog/Data"; -import * as Language from "../../../../Language"; -import UiDialog from "../../../../Ui/Dialog"; import * as UiNotification from "../../../../Ui/Notification"; -import DomUtil from "../../../../Dom/Util"; +import { dialogFactory } from "WoltLabSuite/Core/Component/Dialog"; -interface AjaxResponse { - returnValues: { - redirectURL: string; - }; +interface Response { + redirectURL: string; } -interface AjaxResponseError { - returnValues?: { - fieldName?: string; - errorType?: string; - }; +export function init(): void { + const button = document.querySelector(".jsButtonCopy") as HTMLAnchorElement; + button.addEventListener("click", () => void click(button)); } -class AcpUiTemplateGroupCopy implements AjaxCallbackObject, DialogCallbackObject { - private folderName?: HTMLInputElement = undefined; - private name?: HTMLInputElement = undefined; - private readonly templateGroupId: number; - - constructor(templateGroupId: number) { - this.templateGroupId = templateGroupId; - - const button = document.querySelector(".jsButtonCopy") as HTMLAnchorElement; - button.addEventListener("click", (ev) => this.click(ev)); - } - - private click(event: MouseEvent): void { - event.preventDefault(); - - UiDialog.open(this); - } - - _dialogSubmit(): void { - Ajax.api(this, { - parameters: { - templateGroupName: this.name!.value, - templateGroupFolderName: this.folderName!.value, - }, - }); - } - - _ajaxSuccess(data: AjaxResponse): void { - UiDialog.close(this); - +async function click(button: HTMLAnchorElement): Promise { + const result = await dialogFactory().usingFormBuilder().fromEndpoint(button.dataset.endpoint!); + if (result.ok) { UiNotification.show(undefined, () => { - window.location.href = data.returnValues.redirectURL; + window.location.href = result.result.redirectURL; }); } - - _dialogSetup(): ReturnType { - return { - id: "templateGroupCopy", - options: { - onSetup: () => { - ["Name", "FolderName"].forEach((type) => { - const input = document.getElementById("copyTemplateGroup" + type) as HTMLInputElement; - input.value = (document.getElementById("templateGroup" + type) as HTMLInputElement).value; - - if (type === "Name") { - this.name = input; - } else { - this.folderName = input; - } - }); - }, - title: Language.get("wcf.acp.template.group.copy"), - }, - source: `
-
- -
-
- -
-
-
-
- -
-
- -
-
-
- -
`, - }; - } - - _ajaxSetup(): ReturnType { - return { - data: { - actionName: "copy", - className: "wcf\\data\\template\\group\\TemplateGroupAction", - objectIDs: [this.templateGroupId], - }, - failure: (data: AjaxResponseError) => { - if (data && data.returnValues && data.returnValues.fieldName && data.returnValues.errorType) { - if (data.returnValues.fieldName === "templateGroupName") { - DomUtil.innerError( - this.name!, - Language.get(`wcf.acp.template.group.name.error.${data.returnValues.errorType}`), - ); - } else { - DomUtil.innerError( - this.folderName!, - Language.get(`wcf.acp.template.group.folderName.error.${data.returnValues.errorType}`), - ); - } - - return false; - } - - return true; - }, - }; - } } -let acpUiTemplateGroupCopy: AcpUiTemplateGroupCopy; - -export function init(templateGroupId: number): void { - if (!acpUiTemplateGroupCopy) { - acpUiTemplateGroupCopy = new AcpUiTemplateGroupCopy(templateGroupId); - } -} diff --git a/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl b/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl index 306cb79a600..62fce1b22ac 100644 --- a/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl +++ b/wcfsetup/install/files/acp/templates/templateGroupAdd.tpl @@ -2,17 +2,8 @@ {if $action === 'edit'} {/if} @@ -24,7 +15,7 @@ -{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/js/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js index 05c72549c2d..b8ee293ff78 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/Template/Group/Copy.js @@ -1,111 +1,25 @@ /** * Provides a dialog to copy an existing template group. * - * @author Alexander Ebert - * @copyright 2001-2019 WoltLab GmbH + * @author Olaf Braun, Alexander Ebert + * @copyright 2001-2024 WoltLab GmbH * @license GNU Lesser General Public License */ -define(["require", "exports", "tslib", "../../../../Ajax", "../../../../Language", "../../../../Ui/Dialog", "../../../../Ui/Notification", "../../../../Dom/Util"], function (require, exports, tslib_1, Ajax, Language, Dialog_1, UiNotification, Util_1) { +define(["require", "exports", "tslib", "../../../../Ui/Notification", "WoltLabSuite/Core/Component/Dialog"], function (require, exports, tslib_1, UiNotification, Dialog_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = init; - Ajax = tslib_1.__importStar(Ajax); - Language = tslib_1.__importStar(Language); - Dialog_1 = tslib_1.__importDefault(Dialog_1); UiNotification = tslib_1.__importStar(UiNotification); - Util_1 = tslib_1.__importDefault(Util_1); - class AcpUiTemplateGroupCopy { - folderName = undefined; - name = undefined; - templateGroupId; - constructor(templateGroupId) { - this.templateGroupId = templateGroupId; - const button = document.querySelector(".jsButtonCopy"); - button.addEventListener("click", (ev) => this.click(ev)); - } - click(event) { - event.preventDefault(); - Dialog_1.default.open(this); - } - _dialogSubmit() { - Ajax.api(this, { - parameters: { - templateGroupName: this.name.value, - templateGroupFolderName: this.folderName.value, - }, - }); - } - _ajaxSuccess(data) { - Dialog_1.default.close(this); + function init() { + const button = document.querySelector(".jsButtonCopy"); + button.addEventListener("click", () => void click(button)); + } + async function click(button) { + const result = await (0, Dialog_1.dialogFactory)().usingFormBuilder().fromEndpoint(button.dataset.endpoint); + if (result.ok) { UiNotification.show(undefined, () => { - window.location.href = data.returnValues.redirectURL; + window.location.href = result.result.redirectURL; }); } - _dialogSetup() { - return { - id: "templateGroupCopy", - options: { - onSetup: () => { - ["Name", "FolderName"].forEach((type) => { - const input = document.getElementById("copyTemplateGroup" + type); - input.value = document.getElementById("templateGroup" + type).value; - if (type === "Name") { - this.name = input; - } - else { - this.folderName = input; - } - }); - }, - title: Language.get("wcf.acp.template.group.copy"), - }, - source: `
-
- -
-
- -
-
-
-
- -
-
- -
-
-
- -
`, - }; - } - _ajaxSetup() { - return { - data: { - actionName: "copy", - className: "wcf\\data\\template\\group\\TemplateGroupAction", - objectIDs: [this.templateGroupId], - }, - failure: (data) => { - if (data && data.returnValues && data.returnValues.fieldName && data.returnValues.errorType) { - if (data.returnValues.fieldName === "templateGroupName") { - Util_1.default.innerError(this.name, Language.get(`wcf.acp.template.group.name.error.${data.returnValues.errorType}`)); - } - else { - Util_1.default.innerError(this.folderName, Language.get(`wcf.acp.template.group.folderName.error.${data.returnValues.errorType}`)); - } - return false; - } - return true; - }, - }; - } - } - let acpUiTemplateGroupCopy; - function init(templateGroupId) { - if (!acpUiTemplateGroupCopy) { - acpUiTemplateGroupCopy = new AcpUiTemplateGroupCopy(templateGroupId); - } } }); diff --git a/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php b/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php new file mode 100644 index 00000000000..7db0467fd2e --- /dev/null +++ b/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php @@ -0,0 +1,134 @@ + + * @since 6.2 + */ +final class TemplateGroupCopyAction implements RequestHandlerInterface +{ + #[\Override] + public function handle(ServerRequestInterface $request): ResponseInterface + { + if (!WCF::getSession()->getPermission('admin.template.canManageTemplate')) { + throw new PermissionDeniedException(); + } + + try { + $queryParameters = Helper::mapQueryParameters( + $_GET, + <<<'EOT' + array { + id: positive-int + } + EOT + ); + $templateGroup = new TemplateGroup($queryParameters['id']); + + if (!$templateGroup->getObjectID()) { + throw new IllegalLinkException(); + } + } catch (MappingError) { + throw new IllegalLinkException(); + } + + $form = $this->getForm($templateGroup); + + if ($request->getMethod() === 'GET') { + return $form->toResponse(); + } elseif ($request->getMethod() === 'POST') { + $response = $form->validateRequest($request); + if ($response !== null) { + return $response; + } + + $data = $form->getData()['data']; + $data['parentTemplateGroupID'] = $templateGroup->parentTemplateGroupID ?: null; + + $returnValues = (new TemplateGroupAction([], 'create', ['data' => $data]))->executeAction(); + /** @var TemplateGroup $templateGroup */ + $templateGroup = $returnValues['returnValues']; + + $templateList = new TemplateList(); + $templateList->getConditionBuilder()->add( + "template.templateGroupID = ?", + [$templateGroup->templateGroupID] + ); + $templateList->readObjects(); + + foreach ($templateList as $template) { + (new TemplateAction([], 'create', [ + 'data' => [ + 'application' => $template->application, + 'templateName' => $template->templateName, + 'packageID' => $template->packageID, + 'templateGroupID' => $templateGroup->templateGroupID, + ], + 'source' => $template->getSource(), + ]))->executeAction(); + } + + return new JsonResponse([ + 'result' => [ + 'redirectURL' => LinkHandler::getInstance()->getLink( + 'TemplateGroupEdit', + [ + 'isACP' => true, + 'id' => $templateGroup->templateGroupID, + ] + ), + ] + ]); + } else { + throw new \LogicException('Unreachable'); + } + } + + private function getForm(TemplateGroup $templateGroup): Psr15DialogForm + { + $form = new Psr15DialogForm( + TemplateGroupCopyAction::class, + WCF::getLanguage()->get('wcf.acp.template.group.copy') + ); + $form->appendChildren([ + TextFormField::create('templateGroupName') + ->label('wcf.global.name') + ->required() + ->value($templateGroup->templateGroupName) + ->addValidator(TemplateGroupAddForm::getTemplateNameValidator()), + TextFormField::create('templateGroupFolderName') + ->label('wcf.acp.template.group.folderName') + ->required() + ->value($templateGroup->templateGroupFolderName) + ->addValidator(TemplateGroupAddForm::getFolderNameValidator()) + ->addValidator(TemplateGroupAddForm::getUniqueFolderNameValidator()), + ]); + + $form->build(); + + return $form; + } +} diff --git a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php index fc829f55b69..7d1c6de6b71 100644 --- a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php @@ -4,21 +4,25 @@ 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\container\FormContainer; +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 +34,112 @@ 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'); - } - - $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'); - } - } + public $objectEditLinkController = TemplateGroupEditForm::class; - /** - * 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'); - } + parent::createForm(); + + $availableTemplateGroups = TemplateGroup::getSelectList([-1], 1); + + $this->form->appendChildren([ + FormContainer::create('general') + ->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(TemplateGroupAddForm::getTemplateNameValidator($this->formObject)), + TextFormField::create('templateGroupFolderName') + ->label('wcf.acp.template.group.folderName') + ->required() + ->addValidator(TemplateGroupAddForm::getFolderNameValidator()) + ->addValidator(TemplateGroupAddForm::getUniqueFolderNameValidator($this->formObject)), + ]) + ]); } - /** - * @inheritDoc - */ - public function save() + public static function getFolderNameValidator(): FormFieldValidator { - 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] - ), - ]); + return 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' + ) + ); + } + }); } - /** - * @inheritDoc - */ - public function readData() + public static function getUniqueFolderNameValidator(?TemplateGroup $formObject = null): FormFieldValidator { - $this->availableTemplateGroups = TemplateGroup::getSelectList([-1], 1); - - parent::readData(); + return new FormFieldValidator( + 'uniqueFolderNameValidator', + function (TextFormField $formField) use ($formObject) { + $formField->value(FileUtil::addTrailingSlash($formField->getValue())); + + if ($formField->getValue() === $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' + ) + ); + } + } + ); } - /** - * @inheritDoc - */ - public function assignVariables() + public static function getTemplateNameValidator(?TemplateGroup $formObject = null): FormFieldValidator { - parent::assignVariables(); - - WCF::getTPL()->assign([ - 'action' => 'add', - 'templateGroupName' => $this->templateGroupName, - 'templateGroupFolderName' => $this->templateGroupFolderName, - 'parentTemplateGroupID' => $this->parentTemplateGroupID, - 'availableTemplateGroups' => $this->availableTemplateGroups, - ]); + return new FormFieldValidator( + 'templateNameValidator', + function (TextFormField $formField) use ($formObject) { + if ($formField->getValue() === $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' + ) + ); + } + } + ); } } diff --git a/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateGroupEditForm.class.php index a39f9aa2f35..3c1acca51b8 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 @@ public function readParameters() { 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, - ]); } } diff --git a/wcfsetup/install/files/lib/data/template/group/TemplateGroupAction.class.php b/wcfsetup/install/files/lib/data/template/group/TemplateGroupAction.class.php index 34f99aacd9d..b5e17958ef2 100644 --- a/wcfsetup/install/files/lib/data/template/group/TemplateGroupAction.class.php +++ b/wcfsetup/install/files/lib/data/template/group/TemplateGroupAction.class.php @@ -3,12 +3,6 @@ namespace wcf\data\template\group; use wcf\data\AbstractDatabaseObjectAction; -use wcf\data\template\Template; -use wcf\data\template\TemplateAction; -use wcf\data\template\TemplateList; -use wcf\system\exception\UserInputException; -use wcf\system\request\LinkHandler; -use wcf\system\WCF; /** * Executes template group-related actions. @@ -46,101 +40,5 @@ class TemplateGroupAction extends AbstractDatabaseObjectAction /** * @inheritDoc */ - protected $requireACP = ['copy', 'create', 'delete', 'update']; - - /** - * @var TemplateGroupEditor - */ - public $templateGroupEditor; - - /** - * Validates the parameters to copy an existing template group. - * - * @throws UserInputException - */ - public function validateCopy() - { - WCF::getSession()->checkPermissions(['admin.template.canManageTemplate']); - - $this->readString('templateGroupName'); - $this->readString('templateGroupFolderName'); - - $this->templateGroupEditor = $this->getSingleObject(); - - // validate name - $sql = "SELECT COUNT(*) - FROM wcf1_template_group - WHERE templateGroupName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$this->parameters['templateGroupName']]); - - if ($statement->fetchSingleColumn()) { - throw new UserInputException('templateGroupName', 'notUnique'); - } - - // validate folder name - if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $this->parameters['templateGroupFolderName'])) { - throw new UserInputException('templateGroupFolderName', 'invalid'); - } - - $sql = "SELECT COUNT(*) - FROM wcf1_template_group - WHERE templateGroupFolderName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$this->parameters['templateGroupFolderName']]); - - if ($statement->fetchSingleColumn()) { - throw new UserInputException('templateGroupFolderName', 'notUnique'); - } - } - - /** - * Copies an existing template group. - * - * @return string[] - */ - public function copy() - { - // create a new template group - $returnValues = (new self([], 'create', [ - 'data' => [ - 'parentTemplateGroupID' => ($this->templateGroupEditor->parentTemplateGroupID ?: null), - 'templateGroupName' => $this->parameters['templateGroupName'], - 'templateGroupFolderName' => $this->parameters['templateGroupFolderName'], - ], - ]))->executeAction(); - /** @var TemplateGroup $templateGroup */ - $templateGroup = $returnValues['returnValues']; - - // copy over the templates - $templateList = new TemplateList(); - $templateList->getConditionBuilder()->add( - "template.templateGroupID = ?", - [$this->templateGroupEditor->templateGroupID] - ); - $templateList->readObjects(); - - /** @var Template $template */ - foreach ($templateList as $template) { - (new TemplateAction([], 'create', [ - 'data' => [ - 'application' => $template->application, - 'templateName' => $template->templateName, - 'packageID' => $template->packageID, - 'templateGroupID' => $templateGroup->templateGroupID, - ], - 'source' => $template->getSource(), - ]))->executeAction(); - } - - return [ - 'redirectURL' => LinkHandler::getInstance()->getLink( - 'TemplateGroupEdit', - [ - 'isACP' => true, - 'id' => $templateGroup->templateGroupID, - ] - ), - ]; - } + protected $requireACP = ['create', 'delete', 'update']; }