From 8efbe6f61329ac537896b167b08f59dab4667a50 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 5 Aug 2013 11:05:11 +0200 Subject: [PATCH] Move submit and cancel handling Move submit and cancel handling from ConfirmationForm to Form. refs #4439 --- library/Icinga/Web/Form.php | 92 +++++++++++++++++++ .../forms/Command/ConfirmationForm.php | 80 ---------------- .../forms/Command/ConfirmationFormTest.php | 6 -- 3 files changed, 92 insertions(+), 86 deletions(-) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index f2d9af954..381981a3c 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -74,6 +74,24 @@ abstract class Form extends \Zend_Form */ private $sessionId = false; + /** + * Label for submit button + * + * If omitted, no button will be shown. + * + * @var string + */ + private $submitLabel; + + /** + * Label for cancel button + * + * If omitted, no button will be shown. + * + * @var string + */ + private $cancelLabel; + /** * Returns the session ID stored in this form instance * @return mixed @@ -160,6 +178,14 @@ abstract class Form extends \Zend_Form $this->initCsrfToken(); $this->create(); + if ($this->getSubmitLabel()) { + $this->addSubmitButton(); + } + + if ($this->getCancelLabel()) { + $this->addCancelButton(); + } + // Empty action if not safe if (!$this->getAction() && $this->getRequest()) { $this->setAction($this->getRequest()->getRequestUri()); @@ -169,6 +195,72 @@ abstract class Form extends \Zend_Form } } + /** + * Setter for cancel label + * @param string $cancelLabel + */ + public function setCancelLabel($cancelLabel) + { + $this->cancelLabel = $cancelLabel; + } + + /** + * Getter for cancel label + * @return string + */ + public function getCancelLabel() + { + return $this->cancelLabel; + } + + /** + * Add cancel button to form + */ + protected function addCancelButton() + { + $cancelLabel = new \Zend_Form_Element_Reset( + array( + 'name' => 'btn_reset', + 'label' => $this->getCancelLabel(), + 'class' => 'btn pull-right' + ) + ); + $this->addElement($cancelLabel); + } + + /** + * Setter for submit label + * @param string $submitLabel + */ + public function setSubmitLabel($submitLabel) + { + $this->submitLabel = $submitLabel; + } + + /** + * Getter for submit label + * @return string + */ + public function getSubmitLabel() + { + return $this->submitLabel; + } + + /** + * Add submit button to form + */ + protected function addSubmitButton() + { + $submitButton = new \Zend_Form_Element_Submit( + array( + 'name' => 'btn_submit', + 'label' => $this->getSubmitLabel(), + 'class' => 'btn btn-primary pull-right' + ) + ); + $this->addElement($submitButton); + } + /** * Enable automatic submission * diff --git a/modules/monitoring/application/forms/Command/ConfirmationForm.php b/modules/monitoring/application/forms/Command/ConfirmationForm.php index da7af5b61..2251c2b18 100644 --- a/modules/monitoring/application/forms/Command/ConfirmationForm.php +++ b/modules/monitoring/application/forms/Command/ConfirmationForm.php @@ -48,24 +48,6 @@ class ConfirmationForm extends Form */ const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss'; - /** - * Label for submit button - * - * If omitted, no button will be shown. - * - * @var string - */ - private $submitLabel; - - /** - * Label for cancel button - * - * If omitted, no button will be shown. - * - * @var string - */ - private $cancelLabel; - /** * Array of messages * @@ -73,46 +55,6 @@ class ConfirmationForm extends Form */ private $notes = array(); - /** - * Setter for cancel label - * - * @param string $cancelLabel - */ - public function setCancelLabel($cancelLabel) - { - $this->cancelLabel = $cancelLabel; - } - - /** - * Getter for cancel label - * - * @return string - */ - public function getCancelLabel() - { - return $this->cancelLabel; - } - - /** - * Setter for submit label - * - * @param string $submitLabel - */ - public function setSubmitLabel($submitLabel) - { - $this->submitLabel = $submitLabel; - } - - /** - * Getter for submit label - * - * @return string - */ - public function getSubmitLabel() - { - return $this->submitLabel; - } - /** * Add message to stack * @@ -173,28 +115,6 @@ class ConfirmationForm extends Form } } - if ($this->getCancelLabel()) { - $cancelLabel = new \Zend_Form_Element_Reset( - array( - 'name' => 'btn_reset', - 'label' => $this->getCancelLabel(), - 'class' => 'btn pull-right' - ) - ); - $this->addElement($cancelLabel); - } - - if ($this->getSubmitLabel()) { - $submitButton = new \Zend_Form_Element_Submit( - array( - 'name' => 'btn_submit', - 'label' => $this->getSubmitLabel(), - 'class' => 'btn btn-primary pull-right' - ) - ); - $this->addElement($submitButton); - } - $this->addElement($this->createInstanceHiddenField()); } diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php index 3a35d48ba..c5e6330ec 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php @@ -20,17 +20,11 @@ class ConfirmationFormTest extends BaseFormTest $form->setRequest($this->getRequest()); - $form->setSubmitLabel('111TEST_SUBMIT'); - - $form->setCancelLabel('888TEST_RESET'); - $form->addNote('444 NOTE 1'); $form->addNote('555 NOTE 2'); $form->buildForm(); $content = $form->render($view); - $this->assertContains('', $content); - $this->assertContains('', $content); $this->assertContains('
', $content); $this->assertContains('
', $content); $this->assertContains('444 NOTE 1
', $content);