ObjectFields: prefix filters, respect inheritance

refs #13253
This commit is contained in:
Thomas Gelf 2016-12-14 17:33:13 +01:00
parent 949d6c9b54
commit 7e7e2601d3
2 changed files with 83 additions and 36 deletions

View File

@ -2,8 +2,10 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
@ -133,25 +135,41 @@ class IcingaObjectFieldForm extends DirectorObjectForm
)
));
$loader = new IcingaObjectFieldLoader($object);
$fields = $loader->getFields();
$this->addFilterElement('var_filter', array(
'description' => $this->translate(
'You might want to show this field only when certain conditions are met.'
. ' Otherwise it will not be available and values eventually set before'
. ' will be cleared once stored'
),
'columns' => array_keys($fields),
));
$this->addDisplayGroup(array($this->getElement('var_filter')), 'field_filter', array(
'decorators' => array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset',
),
'order' => 30,
'legend' => $this->translate('Show based on filter')
));
$filterFields = array();
$prefix = null;
if ($object instanceof IcingaHost) {
$prefix = 'host.vars.';
} elseif ($object instanceof IcingaService) {
$prefix = 'service.vars.';
}
if ($prefix) {
$loader = new IcingaObjectFieldLoader($object);
$fields = $loader->getFields();
foreach ($fields as $varName => $field) {
$filterFields[$prefix . $field->varname] = $field->caption;
}
$this->addFilterElement('var_filter', array(
'description' => $this->translate(
'You might want to show this field only when certain conditions are met.'
. ' Otherwise it will not be available and values eventually set before'
. ' will be cleared once stored'
),
'columns' => $filterFields,
));
$this->addDisplayGroup(array($this->getElement('var_filter')), 'field_filter', array(
'decorators' => array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset',
),
'order' => 30,
'legend' => $this->translate('Show based on filter')
));
}
$this->setButtons();
}

View File

@ -6,8 +6,10 @@ use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaService;
use stdClass;
use Zend_Form_Element as ZfElement;
@ -181,27 +183,65 @@ class IcingaObjectFieldLoader
*/
protected function attachFieldsToForm(DirectorObjectForm $form)
{
$filters = array();
if ($this->fields === null) {
return;
}
$elements = $this->removeFilteredFields($this->getElements($form));
foreach ($elements as $element) {
$form->addElement($element);
}
if (! empty($elements)) {
$form->addElementsToGroup(
$elements,
'custom_fields',
50,
$form->translate('Custom properties')
);
}
}
/**
* @param ZfElement[] $elements
* @return ZfElement[]
*/
protected function removeFilteredFields(array $elements)
{
$filters = array();
foreach ($this->fields as $key => $field) {
if ($filter = $field->var_filter) {
$filters[$key] = Filter::fromQueryString($filter);
}
}
$elements = $this->getElements($form);
$kill = array();
$columns = array();
$vars = (object) $this->object->vars()->flatten();
$object = $this->object;
$object->invalidateResolveCache();
$vars = $object::fromPlainObject($object->toPlainObject(true))->vars()->flatten();
$prefixedVars = (object) array();
if ($object instanceof IcingaHost) {
$prefix = 'host.vars.';
} elseif ($object instanceof IcingaService) {
$prefix = 'service.vars.';
} else {
return $elements;
}
foreach ($vars as $k => $v) {
$prefixedVars->{$prefix . $k} = $v;
}
foreach ($filters as $key => $filter) {
/** @var $filter FilterChain|FilterExpression */
foreach ($filter->listFilteredColumns() as $column) {
$column = substr($column, strlen($prefix));
$columns[$column] = $column;
}
if (! $filter->matches($vars)) {
if (! $filter->matches($prefixedVars)) {
$kill[] = $key;
}
}
@ -221,18 +261,7 @@ class IcingaObjectFieldLoader
}
}
foreach ($elements as $element) {
$form->addElement($element);
}
if (! empty($elements)) {
$form->addElementsToGroup(
$elements,
'custom_fields',
50,
$form->translate('Custom properties')
);
}
return $elements;
}
protected function getElementVarName($name)