From 4d1923680ea8ef759921b5cc4dbb2ddadf797a23 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 16 Mar 2019 15:58:44 +0100 Subject: [PATCH] Handle global form success message directly in form builder API This way, there is no need to manually add the relevant code to the template. See #2509 --- com.woltlab.wcf/templates/__form.tpl | 4 ++ .../install/files/acp/templates/__form.tpl | 4 ++ .../acp/templates/devtoolsProjectAdd.tpl | 4 -- .../templates/devtoolsProjectPipEntryAdd.tpl | 4 -- .../files/acp/templates/languageItemAdd.tpl | 4 -- .../files/acp/templates/reactionTypeAdd.tpl | 4 -- .../form/AbstractFormBuilderForm.class.php | 2 + .../form/builder/FormDocument.class.php | 71 ++++++++++++++++++- .../form/builder/IFormDocument.class.php | 45 +++++++++++- 9 files changed, 122 insertions(+), 20 deletions(-) diff --git a/com.woltlab.wcf/templates/__form.tpl b/com.woltlab.wcf/templates/__form.tpl index bacb6c0db4..4a1fcadb94 100644 --- a/com.woltlab.wcf/templates/__form.tpl +++ b/com.woltlab.wcf/templates/__form.tpl @@ -9,6 +9,10 @@ {/if} +{if $form->showsSuccessMessage()} +

{@$form->getSuccessMessage()}

+{/if} + {if $form->isAjax()}
getClasses()|empty} class="{implode from=$form->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{* diff --git a/wcfsetup/install/files/acp/templates/__form.tpl b/wcfsetup/install/files/acp/templates/__form.tpl index bacb6c0db4..4a1fcadb94 100644 --- a/wcfsetup/install/files/acp/templates/__form.tpl +++ b/wcfsetup/install/files/acp/templates/__form.tpl @@ -9,6 +9,10 @@ {/if} +{if $form->showsSuccessMessage()} +

{@$form->getSuccessMessage()}

+{/if} + {if $form->isAjax()}
getClasses()|empty} class="{implode from=$form->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{* diff --git a/wcfsetup/install/files/acp/templates/devtoolsProjectAdd.tpl b/wcfsetup/install/files/acp/templates/devtoolsProjectAdd.tpl index 8150f51d30..5f64c1b6f0 100644 --- a/wcfsetup/install/files/acp/templates/devtoolsProjectAdd.tpl +++ b/wcfsetup/install/files/acp/templates/devtoolsProjectAdd.tpl @@ -20,10 +20,6 @@

{lang}wcf.acp.devtools.project.edit.warning{/lang}

{/if} -{if $success|isset} -

{lang}wcf.global.success.{$action}{/lang}

-{/if} - {@$form->getHtml()} {include file='footer'} diff --git a/wcfsetup/install/files/acp/templates/devtoolsProjectPipEntryAdd.tpl b/wcfsetup/install/files/acp/templates/devtoolsProjectPipEntryAdd.tpl index b5b59f9ddc..b1d48da4fe 100644 --- a/wcfsetup/install/files/acp/templates/devtoolsProjectPipEntryAdd.tpl +++ b/wcfsetup/install/files/acp/templates/devtoolsProjectPipEntryAdd.tpl @@ -16,10 +16,6 @@ -{if $success|isset} -

{lang}wcf.global.success.{$action}{/lang}

-{/if} - {@$pipObject->getPip()->getAdditionalTemplateCode()} {@$form->getHtml()} diff --git a/wcfsetup/install/files/acp/templates/languageItemAdd.tpl b/wcfsetup/install/files/acp/templates/languageItemAdd.tpl index f8e5602761..ad39f6e9bf 100644 --- a/wcfsetup/install/files/acp/templates/languageItemAdd.tpl +++ b/wcfsetup/install/files/acp/templates/languageItemAdd.tpl @@ -14,10 +14,6 @@ -{if $success|isset} -

{lang}wcf.global.success.{$action}{/lang}

-{/if} - {@$form->getHtml()} {include file='footer'} diff --git a/wcfsetup/install/files/acp/templates/reactionTypeAdd.tpl b/wcfsetup/install/files/acp/templates/reactionTypeAdd.tpl index 5b13f86a77..fc7ccf08f9 100644 --- a/wcfsetup/install/files/acp/templates/reactionTypeAdd.tpl +++ b/wcfsetup/install/files/acp/templates/reactionTypeAdd.tpl @@ -14,10 +14,6 @@ -{if $success|isset} -

{lang}wcf.global.success.{$action}{/lang}

-{/if} - {@$form->getHtml()} {include file='footer'} diff --git a/wcfsetup/install/files/lib/form/AbstractFormBuilderForm.class.php b/wcfsetup/install/files/lib/form/AbstractFormBuilderForm.class.php index 179c563a6b..f6a590d9ee 100644 --- a/wcfsetup/install/files/lib/form/AbstractFormBuilderForm.class.php +++ b/wcfsetup/install/files/lib/form/AbstractFormBuilderForm.class.php @@ -175,6 +175,8 @@ abstract class AbstractFormBuilderForm extends AbstractForm { $this->buildForm(); } + + $this->form->showSuccessMessage(true); } /** diff --git a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php index 1c9b724c70..8ef0fb942e 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -113,11 +113,24 @@ class FormDocument implements IFormDocument { protected $requestData; /** - * TODO + * is `true` if global form error message will be shown if there are validation errors and + * is `false` otherwise * @var boolean */ protected $showErrorMessage = true; + /** + * is `true` if global form success message will be shown and is `false` otherwise + * @var boolean + */ + protected $showSuccessMessage = false; + + /** + * global form success message + * @var null|string + */ + protected $successMessage; + /** * Cleans up the form document before the form document object is destroyed. */ @@ -238,7 +251,7 @@ class FormDocument implements IFormDocument { public function errorMessage($languageItem = null, array $variables = []) { if ($languageItem === null) { if (!empty($variables)) { - throw new \InvalidArgumentException("Cannot use variables when unsetting error element of form '{$this->getId()}'"); + throw new \InvalidArgumentException("Cannot use variables when unsetting error message of form '{$this->getId()}'"); } $this->errorMessage = null; @@ -405,6 +418,22 @@ class FormDocument implements IFormDocument { return $this->requestData; } + /** + * @inheritDoc + */ + public function getSuccessMessage() { + if ($this->successMessage === null) { + $suffix = 'edit'; + if ($this->getFormMode() === IFormDocument::FORM_MODE_CREATE) { + $suffix = 'add'; + } + + $this->successMessage = WCF::getLanguage()->getDynamicVariable('wcf.global.success.' . $suffix); + } + + return $this->successMessage; + } + /** * @inheritDoc */ @@ -543,6 +572,15 @@ class FormDocument implements IFormDocument { */ public function showErrorMessage($showErrorMessage = true) { $this->showErrorMessage = $showErrorMessage; + + return $this; + } + + /** + * @inheritDoc + */ + public function showSuccessMessage($showSuccessMessage = true) { + $this->showSuccessMessage = $showSuccessMessage; return $this; } @@ -554,6 +592,35 @@ class FormDocument implements IFormDocument { return $this->showErrorMessage; } + /** + * @inheritDoc + */ + public function showsSuccessMessage() { + return $this->showSuccessMessage; + } + + /** + * @inheritDoc + */ + public function successMessage($languageItem = null, array $variables = []) { + if ($languageItem === null) { + if (!empty($variables)) { + throw new \InvalidArgumentException("Cannot use variables when unsetting success message of form '{$this->getId()}'"); + } + + $this->successMessage = null; + } + else { + if (!is_string($languageItem)) { + throw new \InvalidArgumentException("Given success message language item is no string, " . gettype($languageItem) . " given."); + } + + $this->successMessage = WCF::getLanguage()->getDynamicVariable($languageItem, $variables); + } + + return $this; + } + /** * @inheritDoc */ diff --git a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php index 8491729104..8da4e81567 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php @@ -75,7 +75,7 @@ interface IFormDocument extends IFormParentNode { * @throws \BadMethodCallException if this document has already been built */ public function build(); - + /** * Sets the error message of this form using the given language item and returns this * document. If `null` is passed, the error message is unset. @@ -85,7 +85,6 @@ interface IFormDocument extends IFormParentNode { * * @param null|string $languageItem language item containing the error message or `null` to unset error message * @param array $variables additional variables used when resolving the language item - * * @return static this document * * @throws \InvalidArgumentException if the given form mode is invalid @@ -201,6 +200,16 @@ interface IFormDocument extends IFormParentNode { */ public function getRequestData($index = null); + /** + * Returns the success message for the whole form. + * + * By default, `wcf.global.form.add` or `wcf.global.form.edit` in the active user's language + * is returned depending on the current form mode. + * + * @return string + */ + public function getSuccessMessage(); + /** * Returns `true` if the default button is added to the form during in the `build()` method * and `false` otherwise. @@ -304,6 +313,14 @@ interface IFormDocument extends IFormParentNode { */ public function showErrorMessage($showErrorMessage = true); + /** + * Sets if the global form success message should be shown. + * + * @param boolean $showSuccessMessage + * @return static this document + */ + public function showSuccessMessage($showSuccessMessage = true); + /** * Returns `true` if the global form error message should be shown if the form has validation * errors. @@ -313,4 +330,28 @@ interface IFormDocument extends IFormParentNode { * @return boolean */ public function showsErrorMessage(); + + /** + * Returns `true` if the global form success message should be shown. + * + * By default, the global form error message is not shown. + * + * @return boolean + */ + public function showsSuccessMessage(); + + /** + * Sets the success message of this form using the given language item and returns this + * document. If `null` is passed, the success message is unset. + * + * Unsetting the current success message causes `IFormDocument::getSuccessMessage()()` to + * return the default success message. + * + * @param null|string $languageItem language item containing the success message or `null` to unset error message + * @param array $variables additional variables used when resolving the language item + * @return static this document + * + * @throws \InvalidArgumentException if the given form mode is invalid + */ + public function successMessage($languageItem = null, array $variables = []); } -- 2.20.1