Add input suggestion for datalist vars

This commit is contained in:
Markus Frosch 2018-08-07 15:08:49 +02:00 committed by Thomas Gelf
parent f63ee93f71
commit ab86a4fd89
3 changed files with 92 additions and 6 deletions

View File

@ -19,9 +19,19 @@ class SuggestController extends ActionController
{
// TODO: Using some temporarily hardcoded methods, should use DataViews later on
$context = $this->getRequest()->getPost('context');
$key = null;
if (strpos($context, '!') !== false) {
list($context, $key) = preg_split('~!~', $context, 2);
}
$func = 'suggest' . ucfirst($context);
if (method_exists($this, $func)) {
$all = $this->$func();
if (! empty($key)) {
$all = $this->$func($key);
} else {
$all = $this->$func();
}
} else {
$all = array();
}
@ -232,6 +242,56 @@ class SuggestController extends ActionController
]);
}
protected function suggestDataListValues($field = null)
{
if ($field === null) {
// field is required!
return [];
}
$datalistType = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist';
$db = $this->db()->getDbAdapter();
$query = $db->select()
->from(['f' =>'director_datafield'], [])
->join(
['sid' => 'director_datafield_setting'],
'sid.datafield_id = f.id AND sid.setting_name = \'datalist_id\'',
[]
)
->join(
['l' => 'director_datalist'],
'l.id = sid.setting_value',
[]
)
->join(
['e' => 'director_datalist_entry'],
'e.list_id = l.id',
['entry_name', 'entry_value']
)
->where('datatype = ?', $datalistType)
->where('varname = ?', $field)
->order('entry_value');
// TODO: respect allowed_roles
/* this implementation from DataTypeDatalist is broken
$roles = array_map('json_encode', Acl::instance()->listRoleNames());
if (empty($roles)) {
$query->where('allowed_roles IS NULL');
} else {
$query->where('(allowed_roles IS NULL OR allowed_roles IN (?))', $roles);
}
*/
$data = [];
foreach ($db->fetchPairs($query) as $key => $label) {
$data[] = sprintf("%s [%s]", $label, $key);
}
return $data;
}
protected function getFilterColumns($prefix, $keys)
{
if ($prefix === 'host.') {

View File

@ -189,9 +189,12 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
if ($filter) {
// TODO: Make this configurable
$type = 'host';
$prefixLen = strlen($type) + 1;
$filter = clone($filter);
$col = $filter->getColumn();
if ($this->columnIsJson($filter)) {
$col = $filter->getExpression();
$filter->setExpression(json_decode($filter->getColumn()));
} else {
$filter->setExpression(json_decode($filter->getExpression()));
@ -201,7 +204,6 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
return '';
}
$dummy = IcingaObject::createByType($type);
$col = $filter->getColumn();
if ($dummy->hasProperty($col)) {
if ($dummy->propertyIsBoolean($col)) {
return $this->boolean($filter);
@ -212,6 +214,10 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
$type = substr($col, 0, -7);
return $this->selectGroup($type, $filter);
} elseif (substr($col, $prefixLen, 5) === 'vars.') {
$var = substr($col, $prefixLen + 5);
return $this->text($filter, "DataListValues!${var}");
}
}
@ -263,26 +269,38 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
/**
* @param FilterExpression|null $filter
* @param string $suggestionContext
*
* @return mixed
*/
protected function text(FilterExpression $filter = null)
protected function text(FilterExpression $filter = null, $suggestionContext = null)
{
$attr = null;
if ($suggestionContext !== null) {
$attr = [
'class' => 'director-suggest',
'data-suggestion-context' => $suggestionContext,
];
}
$value = $filter === null ? '' : $filter->getExpression();
if (is_array($value)) {
return $this->view->formIplExtensibleSet(
$this->elementId('value', $filter),
$value
$value,
$attr
);
}
return $this->view->formText(
$this->elementId('value', $filter),
$value
$value,
$attr
);
}
/**
* @return \Icinga\Data\Filter\FilterMatch
* @return \Icinga\Data\Filter\FilterExpression
*/
protected function emptyExpression()
{

View File

@ -309,6 +309,14 @@
var $el = $suggestion.closest('ul').siblings('.director-suggest');
var val = $suggestion.text();
// extract label and key from key
var re = /^(.+) \[(\w+)]$/;
var withLabel = val.match(re);
if (withLabel) {
val = withLabel[2];
}
if (val.match(/\.$/)) {
$el.val(val);
this.getSuggestionList($el, true);