Merge branch 'master' into bugfix/monitoring-list-duplicates-7057

This commit is contained in:
Alexander Fuhr 2014-10-06 13:06:56 +02:00
commit 5143c78f1a
10 changed files with 166 additions and 128 deletions

View File

@ -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

View File

@ -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);
}
/**

View File

@ -1,62 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Util;
use Iterator;
/**
* Class Enumerate
*
* @see https://docs.python.org/2/library/functions.html#enumerate
*
* @package Icinga\Util
*/
class Enumerate implements Iterator
{
/**
* @var Iterator
*/
protected $iterator;
/**
* @var int
*/
protected $key;
/**
* @param Iterator $iterator
*/
public function __construct(Iterator $iterator)
{
$this->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;
}
}

View File

@ -0,0 +1,37 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Util;
use FilterIterator;
/**
* Class EnumeratingFilterIterator
*
* FilterIterator with continuous numeric key (index)
*/
abstract class EnumeratingFilterIterator extends FilterIterator
{
/**
* @var int
*/
private $index;
/**
* @return void
*/
public function rewind()
{
parent::rewind();
$this->index = 0;
}
/**
* @return int
*/
public function key()
{
return $this->index++;
}
}

View File

@ -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');

View File

@ -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());
}

View File

@ -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()
{

View File

@ -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)
*

View File

@ -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
*

View File

@ -0,0 +1,62 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form;
use Zend_Form_Element;
use Icinga\Web\Form;
/**
* Base class for Icinga Web 2 form elements
*/
class FormElement extends Zend_Form_Element
{
/**
* Whether loading default decorators is disabled
*
* Icinga Web 2 loads its own default element decorators. For loading Zend's default element decorators set this
* property to false.
*
* @var null|bool
*/
protected $_disableLoadDefaultDecorators;
/**
* Whether loading default decorators is disabled
*
* @return bool
*/
public function loadDefaultDecoratorsIsDisabled()
{
return $this->_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;
}
}