FormDataFilter: cleanup

This commit is contained in:
Thomas Gelf 2018-06-08 18:52:12 +02:00
parent 310ae7e623
commit e3c086c78c
3 changed files with 69 additions and 29 deletions

View File

@ -4,9 +4,9 @@ use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterChain; use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression; use Icinga\Data\Filter\FilterExpression;
use Icinga\Data\FilterColumns; use Icinga\Data\FilterColumns;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaObjectGroup; use Icinga\Module\Director\Objects\IcingaObjectGroup;
use Icinga\Module\Director\Web\Form\Element\Boolean;
use Icinga\Module\Director\Web\Form\IconHelper; use Icinga\Module\Director\Web\Form\IconHelper;
/** /**
@ -17,8 +17,6 @@ use Icinga\Module\Director\Web\Form\IconHelper;
*/ */
class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
{ {
private $currentId;
private $fieldName; private $fieldName;
private $cachedColumnSelect; private $cachedColumnSelect;
@ -41,6 +39,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
* @param array $attribs Attributes for the element tag. * @param array $attribs Attributes for the element tag.
* *
* @return string The element XHTML. * @return string The element XHTML.
* @throws Zend_Form_Exception
*/ */
public function formDataFilter($name, $value = null, $attribs = null) public function formDataFilter($name, $value = null, $attribs = null)
{ {
@ -72,6 +71,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->endRoot(); . $this->endRoot();
} }
/**
* @param Filter $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilter(Filter $filter) protected function renderFilter(Filter $filter)
{ {
if ($filter instanceof FilterChain) { if ($filter instanceof FilterChain) {
@ -79,7 +83,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
} elseif ($filter instanceof FilterExpression) { } elseif ($filter instanceof FilterExpression) {
return $this->renderFilterExpression($filter); return $this->renderFilterExpression($filter);
} else { } else {
throw new ProgrammingError('Got a Filter being neither expression nor chain'); throw new InvalidArgumentException('Got a Filter being neither expression nor chain');
} }
} }
@ -93,6 +97,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return '</ul>'; return '</ul>';
} }
/**
* @param FilterChain $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilterChain(FilterChain $filter) protected function renderFilterChain(FilterChain $filter)
{ {
$parts = array(); $parts = array();
@ -143,6 +152,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return "</div>\n"; return "</div>\n";
} }
/**
* @param FilterExpression $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function filterExpressionHtml(FilterExpression $filter) protected function filterExpressionHtml(FilterExpression $filter)
{ {
return $this->selectColumn($filter) return $this->selectColumn($filter)
@ -154,6 +168,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->expandLink($filter); . $this->expandLink($filter);
} }
/**
* @param FilterExpression $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilterExpression(FilterExpression $filter) protected function renderFilterExpression(FilterExpression $filter)
{ {
return $this->beginExpression($filter) return $this->beginExpression($filter)
@ -161,6 +180,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->endExpression($filter); . $this->endExpression($filter);
} }
/**
* @param FilterExpression|null $filter
* @return Boolean|string
* @throws Zend_Form_Exception
*/
protected function element(FilterExpression $filter = null) protected function element(FilterExpression $filter = null)
{ {
if ($filter) { if ($filter) {
@ -194,7 +218,12 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return $this->text($filter); return $this->text($filter);
} }
protected function selectGroup($type, Filter $filter) /**
* @param $type
* @param FilterExpression $filter
* @return Zend_Form_Element
*/
protected function selectGroup($type, FilterExpression $filter)
{ {
$available = IcingaObjectGroup::enumForType($type); $available = IcingaObjectGroup::enumForType($type);
@ -205,11 +234,16 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
); );
} }
protected function boolean(Filter $filter = null) /**
* @param FilterExpression|null $filter
* @return Boolean
* @throws Zend_Form_Exception
*/
protected function boolean(FilterExpression $filter = null)
{ {
$value = $filter === null ? '' : $filter->getExpression(); $value = $filter === null ? '' : $filter->getExpression();
$el = new Icinga\Module\Director\Web\Form\Element\Boolean( $el = new Boolean(
$this->elementId('value', $filter), $this->elementId('value', $filter),
array( array(
'value' => $value, 'value' => $value,
@ -226,7 +260,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return strlen($col) && $col[0] === '"'; return strlen($col) && $col[0] === '"';
} }
protected function text(Filter $filter = null) /**
* @param FilterExpression|null $filter
* @return mixed
*/
protected function text(FilterExpression $filter = null)
{ {
$value = $filter === null ? '' : $filter->getExpression(); $value = $filter === null ? '' : $filter->getExpression();
if (is_array($value)) { if (is_array($value)) {
@ -242,6 +280,9 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
); );
} }
/**
* @return \Icinga\Data\Filter\FilterMatch
*/
protected function emptyExpression() protected function emptyExpression()
{ {
return Filter::expression('', '=', ''); return Filter::expression('', '=', '');
@ -259,6 +300,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
$res[$k] = $v; $res[$k] = $v;
} }
} }
// sort($res); // sort($res);
return $res; return $res;
} }
@ -271,33 +313,29 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return $prefix . $filter->getId() . $suffix; return $prefix . $filter->getId() . $suffix;
} }
protected function selectOperator(Filter $filter = null) /**
* @param FilterChain|null $filter
* @return mixed
*/
protected function selectOperator(FilterChain $filter = null)
{ {
$ops = array( $ops = [
'AND' => 'AND', 'AND' => 'AND',
'OR' => 'OR', 'OR' => 'OR',
'NOT' => 'NOT' 'NOT' => 'NOT'
); ];
return $this->view->formSelect(
$this->elementId('operator', $filter),
$filter === null ? null : $filter->getOperatorName(),
array(
'class' => 'operator autosubmit',
),
$ops
);
return $this->select( return $this->select(
$this->elementId('operator', $filter), $this->elementId('operator', $filter),
$ops, $ops,
$filter === null ? null : $filter->getOperatorName(), $filter === null ? null : $filter->getOperatorName(),
array('class' => 'operator autosubmit') ['class' => 'operator autosubmit']
); );
} }
protected function selectSign(FilterExpression $filter = null) protected function selectSign(FilterExpression $filter = null)
{ {
$signs = array( $signs = [
'=' => '=', '=' => '=',
'!=' => '!=', '!=' => '!=',
'>' => '>', '>' => '>',
@ -308,7 +346,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
'contains' => 'contains', 'contains' => 'contains',
'true' => 'is true (or set)', 'true' => 'is true (or set)',
'false' => 'is false (or not set)', 'false' => 'is false (or not set)',
); ];
if ($filter === null) { if ($filter === null) {
$sign = null; $sign = null;
@ -433,7 +471,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
); );
asort($this->cachedColumnSelect); asort($this->cachedColumnSelect);
} elseif ($this->cachedColumnSelect === null) { } elseif ($this->cachedColumnSelect === null) {
throw new ProgrammingError('No columns set nor does the query provide any'); throw new RuntimeException('No columns set nor does the query provide any');
} }
} }

View File

@ -3,9 +3,9 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig; use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use RuntimeException;
abstract class IcingaObjectGroup extends IcingaObject abstract class IcingaObjectGroup extends IcingaObject
{ {
@ -36,9 +36,9 @@ abstract class IcingaObjectGroup extends IcingaObject
); );
} }
// Last resort defense against potentiall lousy checks: // Last resort defense against potentially lousy checks:
if (! ctype_alpha($type)) { if (! ctype_alpha($type)) {
throw new IcingaException( throw new RuntimeException(
'Holy shit, you should never have reached this' 'Holy shit, you should never have reached this'
); );
} }

View File

@ -1514,6 +1514,7 @@ abstract class DirectorObjectForm extends DirectorForm
* @param array $properties Form element properties * @param array $properties Form element properties
* *
* @return $this * @return $this
* @throws \Zend_Form_Exception
*/ */
protected function addAssignFilter($properties) protected function addAssignFilter($properties)
{ {
@ -1547,6 +1548,7 @@ abstract class DirectorObjectForm extends DirectorForm
* @param array $properties Form element properties * @param array $properties Form element properties
* *
* @return $this * @return $this
* @throws \Zend_Form_Exception
*/ */
protected function addFilterElement($name, $properties) protected function addFilterElement($name, $properties)
{ {