Fields: add a possibility to show fields based...
...on filter rules
This commit is contained in:
parent
d2bd133d51
commit
5847913987
|
@ -2,17 +2,16 @@
|
|||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class IcingaHostField extends DbObject
|
||||
class IcingaHostField extends IcingaObjectField
|
||||
{
|
||||
protected $keyName = array('host_id', 'datafield_id');
|
||||
|
||||
protected $table = 'icinga_host_field';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'host_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null
|
||||
'host_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null,
|
||||
'var_filter' => null,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,9 +2,7 @@
|
|||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class IcingaNotificationField extends DbObject
|
||||
class IcingaNotificationField extends IcingaObjectField
|
||||
{
|
||||
protected $keyName = array('notification_id', 'datafield_id');
|
||||
|
||||
|
@ -13,6 +11,7 @@ class IcingaNotificationField extends DbObject
|
|||
protected $defaultProperties = array(
|
||||
'notification_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null
|
||||
'is_required' => null,
|
||||
'var_filter' => null,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Data\Filter\Filter;
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
abstract class IcingaObjectField extends DbObject
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param Filter|string $filter
|
||||
*
|
||||
* @throws ProgrammingError
|
||||
*
|
||||
* @return self
|
||||
* @codingStandardsIgnoreStart
|
||||
*/
|
||||
protected function setVar_filter($value)
|
||||
{
|
||||
// @codingStandardsIgnoreEnd
|
||||
if ($value instanceof Filter) {
|
||||
$value = $value->toQueryString();
|
||||
}
|
||||
|
||||
return $this->reallySet('var_filter', $value);
|
||||
}
|
||||
}
|
|
@ -2,17 +2,16 @@
|
|||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class IcingaServiceField extends DbObject
|
||||
class IcingaServiceField extends IcingaObjectField
|
||||
{
|
||||
protected $keyName = array('service_id', 'datafield_id');
|
||||
|
||||
protected $table = 'icinga_service_field';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'service_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null
|
||||
'service_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null,
|
||||
'var_filter' => null,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,17 +2,16 @@
|
|||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class IcingaUserField extends DbObject
|
||||
class IcingaUserField extends IcingaObjectField
|
||||
{
|
||||
protected $keyName = array('user_id', 'datafield_id');
|
||||
|
||||
protected $table = 'icinga_user_field';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'user_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null
|
||||
'user_id' => null,
|
||||
'datafield_id' => null,
|
||||
'is_required' => null,
|
||||
'var_filter' => null,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
namespace Icinga\Module\Director\Web\Form;
|
||||
|
||||
use Exception;
|
||||
use Icinga\Data\Filter\Filter;
|
||||
use Icinga\Data\Filter\FilterExpression;
|
||||
use Icinga\Exception\IcingaException;
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||
|
@ -179,7 +181,46 @@ class IcingaObjectFieldLoader
|
|||
*/
|
||||
protected function attachFieldsToForm(DirectorObjectForm $form)
|
||||
{
|
||||
$filters = array();
|
||||
if ($this->fields === null) {
|
||||
return;
|
||||
}
|
||||
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();
|
||||
foreach ($filters as $key => $filter) {
|
||||
/** @var $filter FilterChain|FilterExpression */
|
||||
foreach ($filter->listFilteredColumns() as $column) {
|
||||
$columns[$column] = $column;
|
||||
}
|
||||
|
||||
if (! $filter->matches($vars)) {
|
||||
$kill[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($kill as $key) {
|
||||
unset($elements[$key]);
|
||||
}
|
||||
foreach ($columns as $col) {
|
||||
if (array_key_exists($col, $elements)) {
|
||||
$el = $elements[$col];
|
||||
$existingClass = $el->getAttrib('class');
|
||||
if (strlen($existingClass)) {
|
||||
$el->setAttrib('class', $existingClass . ' autosubmit');
|
||||
} else {
|
||||
$el->setAttrib('class', 'autosubmit');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($elements as $element) {
|
||||
$form->addElement($element);
|
||||
}
|
||||
|
@ -351,14 +392,15 @@ class IcingaObjectFieldLoader
|
|||
$query = $db->select()->from(
|
||||
array('df' => 'director_datafield'),
|
||||
array(
|
||||
'object_id' => $idColumn,
|
||||
'is_required' => 'f.is_required',
|
||||
'id' => 'df.id',
|
||||
'varname' => 'df.varname',
|
||||
'caption' => 'df.caption',
|
||||
'description' => 'df.description',
|
||||
'datatype' => 'df.datatype',
|
||||
'format' => 'df.format',
|
||||
'object_id' => $idColumn,
|
||||
'var_filter' => 'f.var_filter',
|
||||
'is_required' => 'f.is_required',
|
||||
'id' => 'df.id',
|
||||
'varname' => 'df.varname',
|
||||
'caption' => 'df.caption',
|
||||
'description' => 'df.description',
|
||||
'datatype' => 'df.datatype',
|
||||
'format' => 'df.format',
|
||||
)
|
||||
)->join(
|
||||
array('f' => $object->getTableName() . '_field'),
|
||||
|
|
Loading…
Reference in New Issue