SuggestController: do not fail with no vars...

...and clean up some code
This commit is contained in:
Thomas Gelf 2017-08-25 12:05:02 +02:00
parent c7d7657489
commit ee21c6320a

View File

@ -2,20 +2,13 @@
namespace Icinga\Module\Director\Controllers; namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use ipl\Html\Util;
class SuggestController extends ActionController class SuggestController extends ActionController
{ {
/*
// TODO: Allow any once applying restrictions here
protected function checkDirectorPermissions()
{
}
*/
protected function checkDirectorPermissions() protected function checkDirectorPermissions()
{ {
} }
@ -176,32 +169,40 @@ class SuggestController extends ActionController
protected function suggestHostFilterColumns() protected function suggestHostFilterColumns()
{ {
$all = IcingaHost::enumProperties($this->db(), 'host.'); return $this->getFilterColumns('host.', [
$all = array_merge( $this->translate('Host properties'),
array_keys($all[$this->translate('Host properties')]), $this->translate('Custom variables')
array_keys($all[$this->translate('Custom variables')]) ]);
);
natsort($all);
return $all;
} }
protected function suggestServiceFilterColumns() protected function suggestServiceFilterColumns()
{ {
$all = IcingaService::enumProperties($this->db(), 'service.'); return $this->getFilterColumns('host.', [
$all = array_merge( $this->translate('Service properties'),
array_keys($all[$this->translate('Service properties')]), $this->translate('Host properties'),
array_keys($all[$this->translate('Host properties')]), $this->translate('Host Custom variables'),
array_keys($all[$this->translate('Host Custom variables')]), $this->translate('Custom variables')
array_keys($all[$this->translate('Custom variables')]) ]);
); }
// natsort($all);
return $all; protected function getFilterColumns($prefix, $keys)
{
$all = IcingaService::enumProperties($this->db(), $prefix);
$res = [];
foreach ($keys as $key) {
if (array_key_exists($key, $all)) {
$res = array_merge($res, array_keys($all[$key]));
}
}
natsort($res);
return $res;
} }
protected function highlight($val, $search) protected function highlight($val, $search)
{ {
$search = $this->view->escape($search); $search = ($search);
$val = $this->view->escape($val); $val = Util::escapeForHtml($val);
return preg_replace( return preg_replace(
'/(' . preg_quote($search, '/') . ')/i', '/(' . preg_quote($search, '/') . ')/i',
'<strong>\1</strong>', '<strong>\1</strong>',