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