From 081d8eecfcad0f23da3eac56d3a3279029209d7c Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Thu, 2 Oct 2014 13:47:26 +0200 Subject: [PATCH 01/10] Implement abstract class EnumeratingFilterIterator --- .../Icinga/Util/EnumeratingFilterIterator.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 library/Icinga/Util/EnumeratingFilterIterator.php diff --git a/library/Icinga/Util/EnumeratingFilterIterator.php b/library/Icinga/Util/EnumeratingFilterIterator.php new file mode 100644 index 000000000..44ef9b0c8 --- /dev/null +++ b/library/Icinga/Util/EnumeratingFilterIterator.php @@ -0,0 +1,37 @@ +index = 0; + } + + /** + * @return int + */ + public function key() + { + return $this->index++; + } +} From e192b939c26abe11639e0668c659e53d2846b9eb Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Thu, 2 Oct 2014 13:50:39 +0200 Subject: [PATCH 02/10] Icinga\Protocol\File\FileIterator: extend EnumeratingFilterIterator --- library/Icinga/Protocol/File/FileIterator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Protocol/File/FileIterator.php b/library/Icinga/Protocol/File/FileIterator.php index 2ec3c0ce5..72c4e0a39 100644 --- a/library/Icinga/Protocol/File/FileIterator.php +++ b/library/Icinga/Protocol/File/FileIterator.php @@ -4,7 +4,7 @@ namespace Icinga\Protocol\File; -use FilterIterator; +use Icinga\Util\EnumeratingFilterIterator; use Icinga\Util\File; /** @@ -12,7 +12,7 @@ use Icinga\Util\File; * * Iterate over a file, yielding only fields of non-empty lines which match a PCRE expression */ -class FileIterator extends FilterIterator +class FileIterator extends EnumeratingFilterIterator { /** * A PCRE string with the fields to extract from the file's lines as named subpatterns From 051128b96741f9cd23282ba73b95b4657849272c Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Thu, 2 Oct 2014 13:55:52 +0200 Subject: [PATCH 03/10] Icinga\Protocol\File\FileReader: don't use Icinga\Util\Enumerate --- library/Icinga/Protocol/File/FileReader.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/library/Icinga/Protocol/File/FileReader.php b/library/Icinga/Protocol/File/FileReader.php index 2551489bf..49da989ef 100644 --- a/library/Icinga/Protocol/File/FileReader.php +++ b/library/Icinga/Protocol/File/FileReader.php @@ -6,7 +6,6 @@ namespace Icinga\Protocol\File; use Icinga\Data\Selectable; use Countable; -use Icinga\Util\Enumerate; use Zend_Config; /** @@ -53,9 +52,7 @@ class FileReader implements Selectable, Countable */ public function iterate() { - return new Enumerate( - new FileIterator($this->filename, $this->fields) - ); + return new FileIterator($this->filename, $this->fields); } /** From 1a2d15b6387dfe0e2bf95d22e997654014a6cb39 Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Thu, 2 Oct 2014 13:58:55 +0200 Subject: [PATCH 04/10] Drop Icinga\Util\Enumerate --- library/Icinga/Util/Enumerate.php | 62 ------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 library/Icinga/Util/Enumerate.php diff --git a/library/Icinga/Util/Enumerate.php b/library/Icinga/Util/Enumerate.php deleted file mode 100644 index 0861f7f65..000000000 --- a/library/Icinga/Util/Enumerate.php +++ /dev/null @@ -1,62 +0,0 @@ -iterator = $iterator; - } - - public function rewind() - { - $this->iterator->rewind(); - $this->key = 0; - } - - public function next() - { - $this->iterator->next(); - ++$this->key; - } - - public function valid() - { - return $this->iterator->valid(); - } - - public function current() - { - return $this->iterator->current(); - } - - public function key() - { - return $this->key; - } -} From 8106fe4f79141168a5d07736ab09530c861695fc Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:19:05 +0200 Subject: [PATCH 05/10] form: Add `FormElement' as base class for our elements --- library/Icinga/Web/Form/FormElement.php | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 library/Icinga/Web/Form/FormElement.php diff --git a/library/Icinga/Web/Form/FormElement.php b/library/Icinga/Web/Form/FormElement.php new file mode 100644 index 000000000..690be1acf --- /dev/null +++ b/library/Icinga/Web/Form/FormElement.php @@ -0,0 +1,62 @@ +_disableLoadDefaultDecorators === true; + } + + /** + * Load default decorators + * + * Icinga Web 2 loads its own default element decorators. For loading Zend's default element decorators set + * FormElement::$_disableLoadDefaultDecorators to false. + * + * @return this + * @see Form::$defaultElementDecorators For Icinga Web 2's default element decorators. + */ + public function loadDefaultDecorators() + { + if ($this->loadDefaultDecoratorsIsDisabled()) { + return $this; + } + + if (! isset($this->_disableLoadDefaultDecorators)) { + $decorators = $this->getDecorators(); + if (empty($decorators)) { + // Load Icinga Web 2's default element decorators + $this->addDecorators(Form::$defaultElementDecorators); + } + } else { + // Load Zend's default decorators + parent::loadDefaultDecorators(); + } + return $this; + } +} From 9db76bf371247bd000506ca2cfbc116c4e57df0f Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:19:36 +0200 Subject: [PATCH 06/10] CsrfCounterMeasure: Save calls to `setRequired()' and `setIgnore()' --- .../Web/Form/Element/CsrfCounterMeasure.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Web/Form/Element/CsrfCounterMeasure.php b/library/Icinga/Web/Form/Element/CsrfCounterMeasure.php index 722b1d323..e9bc37edd 100644 --- a/library/Icinga/Web/Form/Element/CsrfCounterMeasure.php +++ b/library/Icinga/Web/Form/Element/CsrfCounterMeasure.php @@ -4,8 +4,8 @@ namespace Icinga\Web\Form\Element; -use Zend_Form_Element_Xhtml; use Icinga\Web\Session; +use Icinga\Web\Form\FormElement; use Icinga\Web\Form\InvalidCSRFTokenException; /** @@ -13,7 +13,7 @@ use Icinga\Web\Form\InvalidCSRFTokenException; * * You must not set a value to successfully use this element, just give it a name and you're good to go. */ -class CsrfCounterMeasure extends Zend_Form_Element_Xhtml +class CsrfCounterMeasure extends FormElement { /** * Default form view helper to use for rendering @@ -22,14 +22,26 @@ class CsrfCounterMeasure extends Zend_Form_Element_Xhtml */ public $helper = 'formHidden'; + /** + * Counter measure element is required + * + * @var bool + */ + protected $_ignore = true; + + /** + * Ignore element when retrieving values at form level + * + * @var bool + */ + protected $_required = true; + /** * Initialize this form element */ public function init() { - $this->setRequired(true); // Not requiring this element would not make any sense - $this->setIgnore(true); // We do not want this element's value being retrieved by Form::getValues() - $this->setDecorators(array('ViewHelper')); + $this->addDecorator('ViewHelper'); $this->setValue($this->generateCsrfToken()); } From 9be52a91486dda1ec13c41ab7bc629e34e32729e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:20:26 +0200 Subject: [PATCH 07/10] DateTimePicker: Extend our FormElement class fixes #7322 --- .../Web/Form/Element/DateTimePicker.php | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/library/Icinga/Web/Form/Element/DateTimePicker.php b/library/Icinga/Web/Form/Element/DateTimePicker.php index 47f9b26e9..728262827 100644 --- a/library/Icinga/Web/Form/Element/DateTimePicker.php +++ b/library/Icinga/Web/Form/Element/DateTimePicker.php @@ -5,27 +5,15 @@ namespace Icinga\Web\Form\Element; use DateTime; -use Zend_Form_Element; +use Icinga\Web\Form; +use Icinga\Web\Form\FormElement; use Icinga\Web\Form\Validator\DateTimeValidator; /** * A date-and-time input control - * - * @method DateTime getValue() */ -class DateTimePicker extends Zend_Form_Element +class DateTimePicker extends FormElement { - /** - * Disable default decorators - * - * \Icinga\Web\Form sets default decorators for elements. - * - * @var bool - * - * @see \Icinga\Web\Form::__construct() For default element decorators. - */ - protected $_disableLoadDefaultDecorators = true; - /** * Form view helper to use for rendering * @@ -54,7 +42,7 @@ class DateTimePicker extends Zend_Form_Element /** * (non-PHPDoc) - * @see \Zend_Form_Element::init() For the method documentation. + * @see Zend_Form_Element::init() For the method documentation. */ public function init() { From a3409b166dfa98aefe0399759d8776ef4c636a1f Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:21:02 +0200 Subject: [PATCH 08/10] Note: Extend our FormElement class --- library/Icinga/Web/Form/Element/Note.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/library/Icinga/Web/Form/Element/Note.php b/library/Icinga/Web/Form/Element/Note.php index 5dd489f0d..78881ab44 100644 --- a/library/Icinga/Web/Form/Element/Note.php +++ b/library/Icinga/Web/Form/Element/Note.php @@ -5,23 +5,13 @@ namespace Icinga\Web\Form\Element; use Zend_Form_Element; +use Icinga\Web\Form; /** * A note */ class Note extends Zend_Form_Element { - /** - * Disable default decorators - * - * \Icinga\Web\Form sets default decorators for elements. - * - * @var bool - * - * @see \Icinga\Web\Form::__construct() For default element decorators. - */ - protected $_disableLoadDefaultDecorators = true; - /** * Form view helper to use for rendering * @@ -36,6 +26,15 @@ class Note extends Zend_Form_Element */ protected $_ignore = true; + /** + * (non-PHPDoc) + * @see Zend_Form_Element::init() For the method documentation. + */ + public function init() + { + $this->setDecorators(Form::$defaultElementDecorators); + } + /** * Validate element value (pseudo) * From fa96a1cbbefc4eaf0b80942957ed6d8d105ab4bc Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:21:17 +0200 Subject: [PATCH 09/10] Number: Extend our FormElement class --- library/Icinga/Web/Form/Element/Number.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/library/Icinga/Web/Form/Element/Number.php b/library/Icinga/Web/Form/Element/Number.php index 75c08165c..836142514 100644 --- a/library/Icinga/Web/Form/Element/Number.php +++ b/library/Icinga/Web/Form/Element/Number.php @@ -4,24 +4,13 @@ namespace Icinga\Web\Form\Element; -use Zend_Form_Element; +use Icinga\Web\Form\FormElement; /** * A number input control */ -class Number extends Zend_Form_Element +class Number extends FormElement { - /** - * Disable default decorators - * - * \Icinga\Web\Form sets default decorators for elements. - * - * @var bool - * - * @see \Icinga\Web\Form::__construct() For default element decorators. - */ - protected $_disableLoadDefaultDecorators = true; - /** * Form view helper to use for rendering * From 2956d9e3423ba4527058a6b1593efa5af4e367f9 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 6 Oct 2014 10:42:24 +0200 Subject: [PATCH 10/10] form: Correctly set our default element decorators --- library/Icinga/Web/Form.php | 46 +++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 2763e073e..b32a03075 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -5,6 +5,7 @@ namespace Icinga\Web; use LogicException; +use Zend_Config; use Zend_Form; use Zend_View_Interface; use Icinga\Application\Icinga; @@ -81,6 +82,19 @@ class Form extends Zend_Form */ protected $uidElementName = 'formUID'; + /** + * Default element decorators + * + * @var array + */ + public static $defaultElementDecorators = array( + 'ViewHelper', + 'Errors', + array('Description', array('tag' => 'span', 'class' => 'description')), + 'Label', + array('HtmlTag', array('tag' => 'div')) + ); + /** * Create a new form * @@ -105,16 +119,6 @@ class Form extends Zend_Form throw new LogicException('The option `onSuccess\' is not callable'); } - if (! isset($options['elementDecorators'])) { - $options['elementDecorators'] = array( - 'ViewHelper', - 'Errors', - array('Description', array('tag' => 'span', 'class' => 'description')), - 'Label', - array('HtmlTag', array('tag' => 'div')) - ); - } - parent::__construct($options); } @@ -417,23 +421,35 @@ class Form extends Zend_Form /** * Create a new element * - * Additionally, all structural form element decorators by Zend are replaced with our own ones. + * Icinga Web 2 loads its own default element decorators. For loading Zend's default element decorators set the + * `disableLoadDefaultDecorators' option to any other value than `true'. For loading custom element decorators use + * the 'decorators' option. * * @param string $type String element type * @param string $name The name of the element to add - * @param array $options The options for the element + * @param mixed $options The options for the element * * @return Zend_Form_Element * - * @see Zend_Form::createElement() + * @see Form::$defaultElementDecorators For Icinga Web 2's default element decorators. */ public function createElement($type, $name, $options = null) { - if (is_array($options) && ! isset($options['disableLoadDefaultDecorators'])) { - $options['disableLoadDefaultDecorators'] = true; + if ($options !== null) { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } + if (! isset($options['decorators']) + && ! array_key_exists('disabledLoadDefaultDecorators', $options) + ) { + $options['decorators'] = static::$defaultElementDecorators; + } + } else { + $options = array('decorators' => static::$defaultElementDecorators); } $el = parent::createElement($type, $name, $options); + if ($el && $el->getAttrib('autosubmit')) { $el->addDecorator(new NoScriptApply()); // Non-JS environments $class = $el->getAttrib('class');