From d6a3d0e22928bfde4384360e899d7ddd30c855a2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 15 Jun 2018 01:30:36 +0200 Subject: [PATCH] SuggestController: reduce duplicate code --- application/controllers/SuggestController.php | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/application/controllers/SuggestController.php b/application/controllers/SuggestController.php index e778357a..691d35cc 100644 --- a/application/controllers/SuggestController.php +++ b/application/controllers/SuggestController.php @@ -79,6 +79,8 @@ class SuggestController extends ActionController * TODO: Should not remain here * * @return array + * @throws \Icinga\Exception\ConfigurationError + * @throws \Icinga\Security\SecurityException */ protected function suggestLocations() { @@ -152,39 +154,22 @@ class SuggestController extends ActionController return $r; } - protected function suggestHosttemplates() { $this->assertPermission('director/hosts'); - $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); + return $this->fetchTemplateNames('icinga_host', 'template_choice_id IS NULL'); } protected function suggestServicetemplates() { $this->assertPermission('director/services'); - $db = $this->db()->getDbAdapter(); - $query = $db->select() - ->from('icinga_service', 'object_name') - ->order('object_name') - ->where("object_type = 'template'"); - return $db->fetchCol($query); + return $this->fetchTemplateNames('icinga_service', 'template_choice_id IS NULL'); } protected function suggestNotificationtemplates() { $this->assertPermission('director/notifications'); - $db = $this->db()->getDbAdapter(); - $query = $db->select() - ->from('icinga_notification', 'object_name') - ->order('object_name') - ->where("object_type = 'template'"); - return $db->fetchCol($query); + return $this->fetchTemplateNames('icinga_notification'); } protected function suggestCommandtemplates() @@ -200,12 +185,7 @@ class SuggestController extends ActionController protected function suggestUsertemplates() { $this->assertPermission('director/users'); - $db = $this->db()->getDbAdapter(); - $query = $db->select() - ->from('icinga_user', 'object_name') - ->order('object_name') - ->where("object_type = 'template'"); - return $db->fetchCol($query); + return $this->fetchTemplateNames('icinga_user'); } protected function suggestCheckcommandnames() @@ -219,6 +199,21 @@ class SuggestController extends ActionController return $db->fetchCol($query); } + protected function fetchTemplateNames($table, $where) + { + $db = $this->db()->getDbAdapter(); + $query = $db->select() + ->from($table, 'object_name') + ->where('object_type = ?', 'template') + ->order('object_name'); + + if ($where !== null) { + $query->where('template_choice_id IS NULL'); + } + + return $db->fetchCol($query); + } + protected function suggestHostgroupnames() { $db = $this->db()->getDbAdapter(); @@ -322,12 +317,7 @@ class SuggestController extends ActionController protected function suggestDependencytemplates() { $this->assertPermission('director/hosts'); - $db = $this->db()->getDbAdapter(); - $query = $db->select() - ->from('icinga_dependency', 'object_name') - ->order('object_name') - ->where("object_type = 'template'"); - return $db->fetchCol($query); + return $this->fetchTemplateNames('icinga_dependency'); } protected function highlight($val, $search)