SuggestController: reduce duplicate code

This commit is contained in:
Thomas Gelf 2018-06-15 01:30:36 +02:00
parent 0c2cb9e484
commit d6a3d0e229
1 changed files with 22 additions and 32 deletions

View File

@ -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)