Add input suggestion for datalist vars
This commit is contained in:
parent
f63ee93f71
commit
ab86a4fd89
|
@ -19,9 +19,19 @@ class SuggestController extends ActionController
|
||||||
{
|
{
|
||||||
// TODO: Using some temporarily hardcoded methods, should use DataViews later on
|
// TODO: Using some temporarily hardcoded methods, should use DataViews later on
|
||||||
$context = $this->getRequest()->getPost('context');
|
$context = $this->getRequest()->getPost('context');
|
||||||
|
$key = null;
|
||||||
|
|
||||||
|
if (strpos($context, '!') !== false) {
|
||||||
|
list($context, $key) = preg_split('~!~', $context, 2);
|
||||||
|
}
|
||||||
|
|
||||||
$func = 'suggest' . ucfirst($context);
|
$func = 'suggest' . ucfirst($context);
|
||||||
if (method_exists($this, $func)) {
|
if (method_exists($this, $func)) {
|
||||||
|
if (! empty($key)) {
|
||||||
|
$all = $this->$func($key);
|
||||||
|
} else {
|
||||||
$all = $this->$func();
|
$all = $this->$func();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$all = array();
|
$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)
|
protected function getFilterColumns($prefix, $keys)
|
||||||
{
|
{
|
||||||
if ($prefix === 'host.') {
|
if ($prefix === 'host.') {
|
||||||
|
|
|
@ -189,9 +189,12 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
|
||||||
if ($filter) {
|
if ($filter) {
|
||||||
// TODO: Make this configurable
|
// TODO: Make this configurable
|
||||||
$type = 'host';
|
$type = 'host';
|
||||||
|
$prefixLen = strlen($type) + 1;
|
||||||
$filter = clone($filter);
|
$filter = clone($filter);
|
||||||
|
$col = $filter->getColumn();
|
||||||
|
|
||||||
if ($this->columnIsJson($filter)) {
|
if ($this->columnIsJson($filter)) {
|
||||||
|
$col = $filter->getExpression();
|
||||||
$filter->setExpression(json_decode($filter->getColumn()));
|
$filter->setExpression(json_decode($filter->getColumn()));
|
||||||
} else {
|
} else {
|
||||||
$filter->setExpression(json_decode($filter->getExpression()));
|
$filter->setExpression(json_decode($filter->getExpression()));
|
||||||
|
@ -201,7 +204,6 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
$dummy = IcingaObject::createByType($type);
|
$dummy = IcingaObject::createByType($type);
|
||||||
$col = $filter->getColumn();
|
|
||||||
if ($dummy->hasProperty($col)) {
|
if ($dummy->hasProperty($col)) {
|
||||||
if ($dummy->propertyIsBoolean($col)) {
|
if ($dummy->propertyIsBoolean($col)) {
|
||||||
return $this->boolean($filter);
|
return $this->boolean($filter);
|
||||||
|
@ -212,6 +214,10 @@ class Zend_View_Helper_FormDataFilter extends Zend_View_Helper_FormElement
|
||||||
$type = substr($col, 0, -7);
|
$type = substr($col, 0, -7);
|
||||||
|
|
||||||
return $this->selectGroup($type, $filter);
|
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 FilterExpression|null $filter
|
||||||
|
* @param string $suggestionContext
|
||||||
|
*
|
||||||
* @return mixed
|
* @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();
|
$value = $filter === null ? '' : $filter->getExpression();
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
return $this->view->formIplExtensibleSet(
|
return $this->view->formIplExtensibleSet(
|
||||||
$this->elementId('value', $filter),
|
$this->elementId('value', $filter),
|
||||||
$value
|
$value,
|
||||||
|
$attr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->view->formText(
|
return $this->view->formText(
|
||||||
$this->elementId('value', $filter),
|
$this->elementId('value', $filter),
|
||||||
$value
|
$value,
|
||||||
|
$attr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Icinga\Data\Filter\FilterMatch
|
* @return \Icinga\Data\Filter\FilterExpression
|
||||||
*/
|
*/
|
||||||
protected function emptyExpression()
|
protected function emptyExpression()
|
||||||
{
|
{
|
||||||
|
|
|
@ -309,6 +309,14 @@
|
||||||
var $el = $suggestion.closest('ul').siblings('.director-suggest');
|
var $el = $suggestion.closest('ul').siblings('.director-suggest');
|
||||||
var val = $suggestion.text();
|
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(/\.$/)) {
|
if (val.match(/\.$/)) {
|
||||||
$el.val(val);
|
$el.val(val);
|
||||||
this.getSuggestionList($el, true);
|
this.getSuggestionList($el, true);
|
||||||
|
|
Loading…
Reference in New Issue