From 3759a4e48d4c0dbb3c7981dc01797015cae1b8c9 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 18 Jun 2017 17:59:42 +0200 Subject: [PATCH] SuggestController: a few more suggestions --- application/controllers/SuggestController.php | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/application/controllers/SuggestController.php b/application/controllers/SuggestController.php index b9eb2abb..76c11dc3 100644 --- a/application/controllers/SuggestController.php +++ b/application/controllers/SuggestController.php @@ -26,12 +26,13 @@ class SuggestController extends ActionController } else { $all = array(); } - + // TODO: also get cursor position and eventually add an asterisk in the middle + // tODO: filter also when fetching, eventually limit somehow $search = $this->getRequest()->getPost('value'); $begins = array(); $matches = array(); $begin = Filter::expression('value', '=', $search . '*'); - $middle = Filter::expression('value', '=', '*' . $search . '*'); + $middle = Filter::expression('value', '=', '*' . $search . '*')->setCaseSensitive(false); $prefixes = array(); foreach ($all as $str) { if (false !== ($pos = strrpos($str, '.'))) { @@ -45,6 +46,8 @@ class SuggestController extends ActionController } elseif ($middle->matches($row)) { $matches[] = $this->highlight($str, $search); } + } else { + $matches[] = $str; } } @@ -82,7 +85,41 @@ class SuggestController extends ActionController protected function suggestHostnames() { $db = $this->db()->getDbAdapter(); - $query = $db->select()->from('icinga_host', 'object_name')->order('object_name'); + $query = $db->select() + ->from('icinga_host', 'object_name') + ->order('object_name') + ->where("object_type = 'object'"); + return $db->fetchCol($query); + } + + protected function suggestHosttemplates() + { + $db = $this->db()->getDbAdapter(); + $query = $db->select() + ->from('icinga_host', 'object_name') + ->order('object_name') + ->where("object_type = 'template'") + ->where('template_choice_id IS NULL'); + return $db->fetchCol($query); + } + + protected function suggestServicetemplates() + { + $db = $this->db()->getDbAdapter(); + $query = $db->select() + ->from('icinga_service', 'object_name') + ->order('object_name') + ->where("object_type = 'template'"); + return $db->fetchCol($query); + } + + protected function suggestCommandtemplates() + { + $db = $this->db()->getDbAdapter(); + $query = $db->select() + ->from('icinga_command', 'object_name') + ->order('object_name') + ->where("object_type = 'template'"); return $db->fetchCol($query); } @@ -135,6 +172,10 @@ class SuggestController extends ActionController { $search = $this->view->escape($search); $val = $this->view->escape($val); - return str_replace($search, '' . $search . '', $val); + return preg_replace( + '/(' . preg_quote($search, '/') . ')/i', + '\1', + $val + ); } }