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\FilterExpression;
use Icinga\Data\FilterColumns;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaObjectGroup;
use Icinga\Module\Director\Web\Form\Element\Boolean;
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
{
private $currentId;
private $fieldName;
private $cachedColumnSelect;
@ -41,6 +39,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
* @param array $attribs Attributes for the element tag.
*
* @return string The element XHTML.
* @throws Zend_Form_Exception
*/
public function formDataFilter($name, $value = null, $attribs = null)
{
@ -72,6 +71,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->endRoot();
}
/**
* @param Filter $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilter(Filter $filter)
{
if ($filter instanceof FilterChain) {
@ -79,7 +83,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
} elseif ($filter instanceof FilterExpression) {
return $this->renderFilterExpression($filter);
} 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>';
}
/**
* @param FilterChain $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilterChain(FilterChain $filter)
{
$parts = array();
@ -143,6 +152,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return "</div>\n";
}
/**
* @param FilterExpression $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function filterExpressionHtml(FilterExpression $filter)
{
return $this->selectColumn($filter)
@ -154,6 +168,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->expandLink($filter);
}
/**
* @param FilterExpression $filter
* @return string
* @throws Zend_Form_Exception
*/
protected function renderFilterExpression(FilterExpression $filter)
{
return $this->beginExpression($filter)
@ -161,6 +180,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
. $this->endExpression($filter);
}
/**
* @param FilterExpression|null $filter
* @return Boolean|string
* @throws Zend_Form_Exception
*/
protected function element(FilterExpression $filter = null)
{
if ($filter) {
@ -194,7 +218,12 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
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);
@ -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();
$el = new Icinga\Module\Director\Web\Form\Element\Boolean(
$el = new Boolean(
$this->elementId('value', $filter),
array(
'value' => $value,
@ -226,7 +260,11 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
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();
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()
{
return Filter::expression('', '=', '');
@ -259,6 +300,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
$res[$k] = $v;
}
}
// sort($res);
return $res;
}
@ -271,33 +313,29 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
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',
'OR' => 'OR',
'NOT' => 'NOT'
);
];
return $this->view->formSelect(
$this->elementId('operator', $filter),
$filter === null ? null : $filter->getOperatorName(),
array(
'class' => 'operator autosubmit',
),
$ops
);
return $this->select(
$this->elementId('operator', $filter),
$ops,
$filter === null ? null : $filter->getOperatorName(),
array('class' => 'operator autosubmit')
['class' => 'operator autosubmit']
);
}
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',
'true' => 'is true (or set)',
'false' => 'is false (or not set)',
);
];
if ($filter === null) {
$sign = null;
@ -433,7 +471,7 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
);
asort($this->cachedColumnSelect);
} 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;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use RuntimeException;
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)) {
throw new IcingaException(
throw new RuntimeException(
'Holy shit, you should never have reached this'
);
}

View File

@ -1511,9 +1511,10 @@ abstract class DirectorObjectForm extends DirectorForm
* Forms should use this helper method for objects using the typical
* assign_filter column
*
* @param array $properties Form element properties
* @param array $properties Form element properties
*
* @return $this
* @throws \Zend_Form_Exception
*/
protected function addAssignFilter($properties)
{
@ -1543,10 +1544,11 @@ abstract class DirectorObjectForm extends DirectorForm
* TODO: Evaluate whether parts or all of this could be moved to the element
* class.
*
* @param string $name Element name
* @param array $properties Form element properties
* @param string $name Element name
* @param array $properties Form element properties
*
* @return $this
* @throws \Zend_Form_Exception
*/
protected function addFilterElement($name, $properties)
{