From 23249dcf7dac9a564c07921cce7a9c3bc01dd905 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 16 Aug 2017 23:27:13 +0200 Subject: [PATCH] HostController: refactor all involved tables... ...for single services and sets --- application/controllers/HostController.php | 198 ++++++++---------- .../IcingaHostAppliedForServiceTable.php | 95 --------- .../tables/IcingaHostAppliedServicesTable.php | 125 ----------- application/tables/IcingaHostServiceTable.php | 100 --------- .../tables/IcingaServiceSetServiceTable.php | 173 --------------- .../IcingaHostAppliedForServiceTable.php | 84 ++++++++ .../Table/IcingaHostAppliedServicesTable.php | 114 ++++++++++ .../Web/Table/IcingaHostServiceTable.php | 118 +++++++++++ .../Table/IcingaServiceSetServiceTable.php | 189 +++++++++++++++++ 9 files changed, 587 insertions(+), 609 deletions(-) delete mode 100644 application/tables/IcingaHostAppliedForServiceTable.php delete mode 100644 application/tables/IcingaHostAppliedServicesTable.php delete mode 100644 application/tables/IcingaHostServiceTable.php delete mode 100644 application/tables/IcingaServiceSetServiceTable.php create mode 100644 library/Director/Web/Table/IcingaHostAppliedForServiceTable.php create mode 100644 library/Director/Web/Table/IcingaHostAppliedServicesTable.php create mode 100644 library/Director/Web/Table/IcingaHostServiceTable.php create mode 100644 library/Director/Web/Table/IcingaServiceSetServiceTable.php diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 8b3ab11d..7c809cfd 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Controllers; use Exception; +use Icinga\Module\Director\CustomVariable\CustomVariableDictionary; use Icinga\Module\Director\Db\AppliedServiceSetLoader; use Icinga\Module\Director\Forms\IcingaServiceForm; use Icinga\Module\Director\Objects\IcingaHost; @@ -12,6 +13,10 @@ use Icinga\Module\Director\Restriction\HostgroupRestriction; use Icinga\Module\Director\Repository\IcingaTemplateRepository; use Icinga\Module\Director\Web\Controller\ObjectController; use Icinga\Module\Director\Web\SelfService; +use Icinga\Module\Director\Web\Table\IcingaHostAppliedForServiceTable; +use Icinga\Module\Director\Web\Table\IcingaHostAppliedServicesTable; +use Icinga\Module\Director\Web\Table\IcingaHostServiceTable; +use Icinga\Module\Director\Web\Table\IcingaServiceSetServiceTable; use Icinga\Web\Url; use ipl\Html\Link; @@ -22,153 +27,115 @@ class HostController extends ObjectController $this->assertPermission('director/hosts'); } - protected function loadRestrictions() - { - return [$this->getHostgroupRestriction()]; - } - protected function getHostgroupRestriction() { return new HostgroupRestriction($this->db(), $this->Auth()); } - /** - * @param IcingaHostForm $form - */ - protected function beforeHandlingAddRequest($form) - { - $form->setApi($this->getApiIfAvailable()); - } - - /** - * @param IcingaHostForm $form - */ - protected function beforeHandlingEditRequest($form) - { - $form->setApi($this->getApiIfAvailable()); - } - public function editAction() { parent::editAction(); - $host = $this->object; - try { - $mon = $this->monitoring(); - if ($host->isObject() && $mon->isAvailable() && $mon->hasHost($host->object_name)) { - $this->actions()->add(Link::create( - $this->translate('Show'), - 'monitoring/host/show', - ['host' => $host->getObjectName()], - [ - 'class' => 'icon-globe critical', - 'data-base-target' => '_next' - ] - )); - } - } catch (Exception $e) { - // Silently ignore errors in the monitoring module - } + $this->addOptionalMonitoringLink(); + } + + public function serviceAction() + { + $host = $this->getHostObject(); + $this->addServicesHeader(); + $this->addTitle($this->translate('Add Service: %s'), $host->getObjectName()); + } + + public function servicesetAction() + { + $host = $this->getHostObject(); + $this->addServicesHeader(); + $this->addTitle($this->translate('Add Service Set: %s'), $host->getObjectName()); + } + + protected function addServicesHeader() + { + $host = $this->getHostObject(); + $hostname = $host->getObjectName(); + $this->tabs()->activate('services'); + + $this->actions()->add(Link::create( + $this->translate('Add service'), + 'director/host/service', + ['name' => $hostname], + ['class' => 'icon-plus'] + ))->add(Link::create( + $this->translate('Add service set'), + 'director/serviceset', + ['host' => $hostname], + ['class' => 'icon-plus'] + )); } public function servicesAction() { + $this->addServicesHeader(); $db = $this->db(); $host = $this->getHostObject(); - $hostname = $host->getObjectName(); - - $this->tabs()->activate('services'); $this->addTitle($this->translate('Services: %s'), $host->getObjectName()); - - $this->actions()->add(Link::create( - $this->translate('Add service'), - 'director/service/add', - ['host' => $hostname], - ['class' => 'icon-plus'] - ))->add(Link::create( - $this->translate('Add service set'), - 'director/serviceset/add', - ['host' => $hostname], - ['class' => 'icon-plus'] - )); - - $resolver = $this->object->templateResolver(); - - $tables = array(); - $table = $this->loadTable('IcingaHostService') - ->setHost($host) - ->setTitle($this->translate('Individual Service objects')) - ->enforceFilter('host_id', $host->id) - ->setConnection($db); + $content = $this->content(); + $table = IcingaHostServiceTable::load($host) + ->setTitle($this->translate('Individual Service objects')); if (count($table)) { - $tables[0] = $table; + $content->add($table); } if ($applied = $host->vars()->get($db->settings()->magic_apply_for)) { - $table = $this->loadTable('IcingaHostAppliedForService') - ->setHost($host) - ->setDictionary($applied) - ->setTitle($this->translate('Generated from host vars')); + if ($applied instanceof CustomVariableDictionary) { + $table = IcingaHostAppliedForServiceTable::load($host, $applied) + ->setTitle($this->translate('Generated from host vars')); + if (count($table)) { + $content->add($table); + } + } + } + /** @var IcingaHost[] $parents */ + $parents = IcingaTemplateRepository::instanceByObject($this->object) + ->getTemplatesFor($this->object); + foreach ($parents as $parent) { + $table = IcingaHostServiceTable::load($parent)->setInheritedBy($host); if (count($table)) { - $tables[1] = $table; + $content->add( + $table->setTitle(sprintf( + 'Inherited from %s', + $parent->getObjectName() + )) + ); } } - $parents = $resolver->fetchResolvedParents(); + $this->addHostServiceSetTables($host); foreach ($parents as $parent) { - $table = $this->loadTable('IcingaHostService') - ->setHost($parent) - ->setInheritedBy($host) - ->enforceFilter('host_id', $parent->id) - ->setConnection($db); - if (! count($table)) { - continue; - } - - // dup dup - $title = sprintf( - 'Inherited from %s', - $parent->object_name - ); - - $tables[$title] = $table->setTitle($title); - } - - $this->addHostServiceSetTables($host, $tables); - foreach ($parents as $parent) { - $this->addHostServiceSetTables($parent, $tables, $host); + $this->addHostServiceSetTables($parent, $host); } $appliedSets = AppliedServiceSetLoader::fetchForHost($host); foreach ($appliedSets as $set) { $title = sprintf($this->translate('%s (Applied Service set)'), $set->getObjectName()); - $table = $this->loadTable('IcingaServiceSetService') - ->setServiceSet($set) - // ->setHost($host) - ->setAffectedHost($host) - ->setTitle($title) - ->setConnection($db); - $tables[$title] = $table; + $content->add( + IcingaServiceSetServiceTable::load($set) + // ->setHost($host) + ->setAffectedHost($host) + ->setTitle($title) + ); } - $title = $this->translate('Applied services'); - $table = $this->loadTable('IcingaHostAppliedServices') - ->setHost($host) - ->setTitle($title) - ->setConnection($db); + $table = IcingaHostAppliedServicesTable::load($host) + ->setTitle($this->translate('Applied services')); if (count($table)) { - $tables[$title] = $table; - } - - foreach ($tables as $table) { - $this->content()->add($table); + $content->add($table); } } - protected function addHostServiceSetTables(IcingaHost $host, & $tables, IcingaHost $affectedHost = null) + protected function addHostServiceSetTables(IcingaHost $host, IcingaHost $affectedHost = null) { $db = $this->db(); if ($affectedHost === null) { @@ -190,16 +157,15 @@ class HostController extends ObjectController )->where('hs.host_id = ?', $host->id); $sets = IcingaServiceSet::loadAll($db, $query, 'object_name'); + /** @var IcingaServiceSet $set*/ foreach ($sets as $name => $set) { $title = sprintf($this->translate('%s (Service set)'), $name); - $table = $this->loadTable('IcingaServiceSetService') - ->setServiceSet($set) - ->setHost($host) - ->setAffectedHost($affectedHost) - ->setTitle($title) - ->setConnection($db); - - $tables[$title] = $table; + $this->content()->add( + IcingaServiceSetServiceTable::load($set) + ->setHost($host) + ->setAffectedHost($affectedHost) + ->setTitle($title) + ); } } diff --git a/application/tables/IcingaHostAppliedForServiceTable.php b/application/tables/IcingaHostAppliedForServiceTable.php deleted file mode 100644 index a6b59c82..00000000 --- a/application/tables/IcingaHostAppliedForServiceTable.php +++ /dev/null @@ -1,95 +0,0 @@ -getValue() as $key => $var) { - $data[] = (object) array( - 'service' => $key, - ); - } - - $this->setConnection(new ArrayDatasource($data)); - return $this; - } - - public function count() - { - $query = clone($this->getBaseQuery()); - $this->applyFiltersToQuery($query); - return $query->count(); - } - - public function fetchData() - { - $query = $this->getBaseQuery()->columns($this->getColumns()); - - if ($this->hasLimit() || $this->hasOffset()) { - $query->limit($this->getLimit(), $this->getOffset()); - } - - $this->applyFiltersToQuery($query); - - return $query->fetchAll(); - } - - public function getColumns() - { - return array( - 'service' => 'service' - ); - } - - public function setTitle($title) - { - $this->title = $title; - return $this; - } - - public function setHost(IcingaHost $host) - { - $this->host = $host; - return $this; - } - - protected function getActionUrl($row) - { - $params = array( - 'name' => $this->host->object_name, - 'service' => $row->service, - ); - - return $this->url('director/host/appliedservice', $params); - } - - public function getTitles() - { - $view = $this->view(); - return array( - 'service' => $this->title ?: $view->translate('Servicename'), - ); - } - - public function getBaseQuery() - { - return $this->db()->select(); - } -} diff --git a/application/tables/IcingaHostAppliedServicesTable.php b/application/tables/IcingaHostAppliedServicesTable.php deleted file mode 100644 index 379245e4..00000000 --- a/application/tables/IcingaHostAppliedServicesTable.php +++ /dev/null @@ -1,125 +0,0 @@ - 'id', - 'name' => 'name', - 'assign_filter' => 'assign_filter', - ); - } - - public function setTitle($title) - { - $this->title = $title; - return $this; - } - - public function getTitles() - { - $view = $this->view(); - return array( - 'label' => $this->title - ); - } - - public function count() - { - return $this->getBaseQuery()->count(); - } - - public function setHost(IcingaHost $host) - { - $this->host = $host; - return $this; - } - - protected function getActionUrl($row) - { - $params = array( - 'name' => $this->host->object_name, - 'service_id' => $row->id, - ); - - return $this->url('director/host/appliedservice', $params); - } - - protected function renderRow($row) - { - $row->label = sprintf( - $this->view()->translate('%s (where %s)'), - $row->name, - $row->filter - ); - - return parent::renderRow($row); - } - - public function fetchData() - { - return $this->getBaseQuery()->fetchAll(); - } - - public function getBaseQuery() - { - if ($this->baseQuery === null) { - $services = array(); - $matcher = HostApplyMatches::prepare($this->host); - foreach ($this->getAllApplyRules() as $rule) { - if ($matcher->matchesFilter($rule->filter)) { - $services[] = $rule; - } - } - - $ds = new ArrayDatasource($services); - $this->baseQuery = $ds->select(); - } - - return $this->baseQuery; - } - - protected function getAllApplyRules() - { - if ($this->allApplyRules === null) { - $this->allApplyRules = $this->fetchAllApplyRules(); - foreach ($this->allApplyRules as $rule) { - $rule->filter = Filter::fromQueryString($rule->assign_filter); - } - } - - return $this->allApplyRules; - } - - protected function fetchAllApplyRules() - { - $db = $this->db(); - $query = $db->select()->from( - array('s' => 'icinga_service'), - array( - 'id' => 's.id', - 'name' => 's.object_name', - 'assign_filter' => 's.assign_filter', - ) - )->where('object_type = ? AND assign_filter IS NOT NULL', 'apply'); - - return $db->fetchAll($query); - } -} diff --git a/application/tables/IcingaHostServiceTable.php b/application/tables/IcingaHostServiceTable.php deleted file mode 100644 index 38e61eba..00000000 --- a/application/tables/IcingaHostServiceTable.php +++ /dev/null @@ -1,100 +0,0 @@ - 's.id', - 'host_id' => 's.host_id', - 'host' => 'h.object_name', - 'service' => 's.object_name', - 'object_type' => 's.object_type', - ); - } - - public function setTitle($title) - { - $this->title = $title; - return $this; - } - - public function setHost(IcingaHost $host) - { - $this->host = $host; - return $this; - } - - public function setInheritedBy(IcingaHost $host) - { - $this->inheritedBy = $host; - return $this; - } - - protected function getActionUrl($row) - { - if ($target = $this->inheritedBy) { - $params = array( - 'name' => $target->object_name, - 'service' => $row->service, - 'inheritedFrom' => $row->host, - ); - - return $this->url('director/host/inheritedservice', $params); - } - - if ($row->object_type === 'apply') { - $params['id'] = $row->id; - } else { - $params = array('name' => $row->service); - if ($row->host !== null) { - $params['host'] = $row->host; - } - } - - return $this->url('director/service/edit', $params); - } - - public function getTitles() - { - $view = $this->view(); - return array( - 'service' => $this->title ?: $view->translate('Servicename'), - ); - } - - public function getUnfilteredQuery() - { - return $this->db()->select()->from( - array('s' => 'icinga_service'), - array() - )->joinLeft( - array('h' => 'icinga_host'), - 'h.id = s.host_id', - array() - )->order('s.object_name'); - } - - public function getBaseQuery() - { - return $this->getUnfilteredQuery()->where( - 's.host_id = ?', - $this->host->id - ); - } -} diff --git a/application/tables/IcingaServiceSetServiceTable.php b/application/tables/IcingaServiceSetServiceTable.php deleted file mode 100644 index 48735de6..00000000 --- a/application/tables/IcingaServiceSetServiceTable.php +++ /dev/null @@ -1,173 +0,0 @@ - 's.id', - 'service_set_id' => 's.service_set_id', - 'host_id' => 'ss.host_id', - 'service_set' => 'ss.object_name', - 'service' => 's.object_name', - 'disabled' => 's.disabled', - 'object_type' => 's.object_type', - ); - } - - public function setTitle($title) - { - $this->title = $title; - return $this; - } - - public function setHost(IcingaHost $host) - { - $this->host = $host; - return $this; - } - - public function setAffectedHost(IcingaHost $host) - { - $this->affectedHost = $host; - return $this; - } - - public function setServiceSet(IcingaServiceSet $set) - { - $this->set = $set; - return $this; - } - - protected function renderTitles($row) - { - if ($this->host || $this->affectedHost) { - return $this->renderHostTitles($row); - } else { - return parent::renderTitles($row); - } - } - - protected function getActionUrl($row) - { - if ($this->affectedHost) { - $params = array( - 'name' => $this->affectedHost->getObjectName(), - 'service' => $row->service, - 'set' => $row->service_set - ); - - return $this->url('director/host/servicesetservice', $params); - } else { - $params = array( - 'name' => $row->service, - 'set' => $row->service_set - ); - - return $this->url('director/service', $params); - } - } - - protected function getRowClasses($row) - { - if ($row->disabled === 'y') { - return ['disabled']; - } else { - return array(); - } - } - - public function getTitles() - { - $view = $this->view(); - return array( - 'service' => $this->title ?: $view->translate('Servicename'), - ); - } - - protected function renderHostTitles($row) - { - $view = $this->view(); - // Hint: row is an array of titles!?!?!? - $title = $view->escape(array_shift($row)); - - $htm = "\n \n"; - if (! $this->host) { - $deleteLink = ''; - } elseif ($this->affectedHost->id !== $this->host->id) { - $deleteLink = $view->qlink( - $this->host->getObjectName(), - 'director/host/services', - array( - 'name' => $this->host->getObjectName(), - ), - array( - 'class' => 'icon-paste', - 'style' => 'float: right; font-weight: normal', - 'data-base-target' => '_next', - 'title' => sprintf( - $view->translate('This set has been inherited from %s'), - $this->host->getObjectName() - ) - ) - ); - } else { - $deleteLink = $view->qlink( - $view->translate('Remove'), - 'director/host/removeset', - array( - 'name' => $this->host->getObjectName(), - 'setId' => $this->set->id - ), - array( - 'class' => 'icon-cancel', - 'style' => 'float: right; font-weight: normal', - 'title' => $view->translate('Remove this set from this host') - ) - ); - } - - $htm .= ' ' . $view->escape($title) . "$deleteLink\n"; - - return $htm . " \n\n"; - } - - public function getUnfilteredQuery() - { - return $this->db()->select()->from( - array('s' => 'icinga_service'), - array() - )->joinLeft( - array('ss' => 'icinga_service_set'), - 'ss.id = s.service_set_id', - array() - )->order('s.object_name'); - } - - public function getBaseQuery() - { - return $this->getUnfilteredQuery()->where( - 's.service_set_id = ?', - $this->set->id - ); - } -} diff --git a/library/Director/Web/Table/IcingaHostAppliedForServiceTable.php b/library/Director/Web/Table/IcingaHostAppliedForServiceTable.php new file mode 100644 index 00000000..1d6563ff --- /dev/null +++ b/library/Director/Web/Table/IcingaHostAppliedForServiceTable.php @@ -0,0 +1,84 @@ +setHost($host)->setDictionary($dict); + } + + public function setDictionary(CustomVariableDictionary $dict) + { + $this->cv = $dict; + return $this; + } + + public function setTitle($title) + { + $this->title = $title; + return $this; + } + + public function setHost(IcingaHost $host) + { + $this->host = $host; + return $this; + } + + public function renderRow($row) + { + return $this::row([ + Link::create( + $row->service, + 'director/host/appliedservice', + [ + 'name' => $this->host->object_name, + 'service' => $row->service, + ] + ) + ]); + } + + public function getColumnsToBeRendered() + { + return [ + $this->title ?: $this->translate('Service name'), + ]; + } + + public function prepareQuery() + { + $data = []; + foreach ($this->cv->getValue() as $key => $var) { + $data[] = (object) array( + 'service' => $key, + ); + } + + return (new ArrayDatasource($data))->select(); + } +} diff --git a/library/Director/Web/Table/IcingaHostAppliedServicesTable.php b/library/Director/Web/Table/IcingaHostAppliedServicesTable.php new file mode 100644 index 00000000..60fbf7d6 --- /dev/null +++ b/library/Director/Web/Table/IcingaHostAppliedServicesTable.php @@ -0,0 +1,114 @@ +setHost($host); + } + + public function setTitle($title) + { + $this->title = $title; + return $this; + } + + public function getColumnsToBeRendered() + { + return [$this->title]; + } + + public function setHost(IcingaHost $host) + { + $this->host = $host; + $this->db = $host->getDb(); + return $this; + } + + public function renderRow($row) + { + return $this::row([ + Link::create( + sprintf( + $this->translate('%s (where %s)'), + $row->name, + $row->filter + ), + 'director/host/appliedservice', + [ + 'name' => $this->host->getObjectName(), + 'service_id' => $row->id, + ] + ) + ]); + } + + public function prepareQuery() + { + $services = []; + $matcher = HostApplyMatches::prepare($this->host); + foreach ($this->getAllApplyRules() as $rule) { + if ($matcher->matchesFilter($rule->filter)) { + $services[] = $rule; + } + } + + $ds = new ArrayDatasource($services); + return $ds->select()->columns([ + 'id' => 'id', + 'name' => 'name', + 'filter' => 'filter', + 'assign_filter' => 'assign_filter', + ]); + } + + protected function getAllApplyRules() + { + if ($this->allApplyRules === null) { + $this->allApplyRules = $this->fetchAllApplyRules(); + foreach ($this->allApplyRules as $rule) { + $rule->filter = Filter::fromQueryString($rule->assign_filter); + } + } + + return $this->allApplyRules; + } + + protected function fetchAllApplyRules() + { + $db = $this->db; + $query = $db->select()->from( + ['s' => 'icinga_service'], + [ + 'id' => 's.id', + 'name' => 's.object_name', + 'assign_filter' => 's.assign_filter', + ] + )->where('object_type = ? AND assign_filter IS NOT NULL', 'apply'); + + return $db->fetchAll($query); + } +} diff --git a/library/Director/Web/Table/IcingaHostServiceTable.php b/library/Director/Web/Table/IcingaHostServiceTable.php new file mode 100644 index 00000000..c5a80719 --- /dev/null +++ b/library/Director/Web/Table/IcingaHostServiceTable.php @@ -0,0 +1,118 @@ +getConnection()); + $table->setHost($host); + $table->attributes()->set('data-base-target', '_self'); + return $table; + } + + public function setTitle($title) + { + $this->title = $title; + return $this; + } + + public function setHost(IcingaHost $host) + { + $this->host = $host; + return $this; + } + + public function setInheritedBy(IcingaHost $host) + { + $this->inheritedBy = $host; + return $this; + } + + public function renderRow($row) + { + return $this::row([ + $this->getServiceLink($row) + ]); + } + + protected function getServiceLink($row) + { + if ($target = $this->inheritedBy) { + $params = array( + 'name' => $target->object_name, + 'service' => $row->service, + 'inheritedFrom' => $row->host, + ); + + return Link::create( + $row->service, + 'director/host/inheritedservice', + $params + ); + } + + if ($row->object_type === 'apply') { + $params['id'] = $row->id; + } else { + $params = array('name' => $row->service); + if ($row->host !== null) { + $params['host'] = $row->host; + } + } + + return Link::create( + $row->service, + 'director/service/edit', + $params + ); + } + + public function getColumnsToBeRendered() + { + return [ + $this->title ?: $this->translate('Servicename'), + ]; + } + + public function prepareQuery() + { + return $this->db()->select()->from( + ['s' => 'icinga_service'], + [ + 'id' => 's.id', + 'host_id' => 's.host_id', + 'host' => 'h.object_name', + 'service' => 's.object_name', + 'object_type' => 's.object_type', + ] + )->joinLeft( + ['h' => 'icinga_host'], + 'h.id = s.host_id', + [] + )->where( + 's.host_id = ?', + $this->host->get('id') + )->order('s.object_name'); + } +} diff --git a/library/Director/Web/Table/IcingaServiceSetServiceTable.php b/library/Director/Web/Table/IcingaServiceSetServiceTable.php new file mode 100644 index 00000000..3c860260 --- /dev/null +++ b/library/Director/Web/Table/IcingaServiceSetServiceTable.php @@ -0,0 +1,189 @@ +getConnection()); + $table->set = $set; + $table->attributes()->set('data-base-target', '_self'); + return $table; + } + + /** + * @param string $title + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + return $this; + } + + /** + * @param IcingaHost $host + * @return $this + */ + public function setHost(IcingaHost $host) + { + $this->host = $host; + return $this; + } + + /** + * @param IcingaHost $host + * @return $this + */ + public function setAffectedHost(IcingaHost $host) + { + $this->affectedHost = $host; + return $this; + } + + protected function addHeaderColumnsTo(Element $parent) + { + if ($this->host || $this->affectedHost) { + $this->addHostHeaderTo($parent); + } else { + parent::addHeaderColumnsTo($parent); + } + + return $parent; + } + + protected function getServiceLink($row) + { + if ($this->affectedHost) { + $params = [ + 'name' => $this->affectedHost->getObjectName(), + 'service' => $row->service, + 'set' => $row->service_set + ]; + $url = 'director/host/servicesetservice'; + } else { + $params = [ + 'name' => $row->service, + 'set' => $row->service_set + ]; + $url = 'director/service'; + } + + return Link::create( + $row->service, + $url, + $params + ); + } + + public function renderRow($row) + { + $tr = $this::row([ + $this->getServiceLink($row) + ]); + + if ($row->disabled === 'y') { + $tr->attributes()->add('class', 'disabled'); + } + + return $tr; + } + + public function getColumnsToBeRendered() + { + return ['Will not be rendered']; + } + + protected function getTitle() + { + return $this->title ?: $this->translate('Servicename'); + } + + protected function addHostHeaderTo(Element $parent) + { + if (! $this->host) { + $deleteLink = ''; + } elseif ($this->affectedHost->get('id') !== $this->host->get('id')) { + $host = $this->host; + $deleteLink = Link::create( + $host->getObjectName(), + 'director/host/services', + ['name' => $host->getObjectName()], + [ + 'class' => 'icon-paste', + 'style' => 'float: right; font-weight: normal', + 'data-base-target' => '_next', + 'title' => sprintf( + $this->translate('This set has been inherited from %s'), + $host->getObjectName() + ) + ] + ); + } else { + $deleteLink = Link::create( + $this->translate('Remove'), + 'director/host/removeset', + [ + 'name' => $this->host->getObjectName(), + 'setId' => $this->set->get('id') + ], + [ + 'class' => 'icon-cancel', + 'style' => 'float: right; font-weight: normal', + 'title' => $this->translate('Remove this set from this host') + ] + ); + } + + $parent->add($this::th([$this->getTitle(), $deleteLink])); + } + + public function prepareQuery() + { + return $this->db()->select()->from( + ['s' => 'icinga_service'], + [ + 'id' => 's.id', + 'service_set_id' => 's.service_set_id', + 'host_id' => 'ss.host_id', + 'service_set' => 'ss.object_name', + 'service' => 's.object_name', + 'disabled' => 's.disabled', + 'object_type' => 's.object_type', + ] + )->joinLeft( + ['ss' => 'icinga_service_set'], + 'ss.id = s.service_set_id', + [] + )->where( + 's.service_set_id = ?', + $this->set->get('id') + )->order('s.object_name'); + } +}