From 69406d631cc7dadec1884fbcc0d6f3a03e5203d6 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 20 Apr 2017 09:12:48 +0200 Subject: [PATCH] ServicesOnHosts: add a new table --- .../controllers/ServicetemplateController.php | 17 +++ .../Web/Table/ServicesOnHostsTable.php | 108 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 application/controllers/ServicetemplateController.php create mode 100644 library/Director/Web/Table/ServicesOnHostsTable.php diff --git a/application/controllers/ServicetemplateController.php b/application/controllers/ServicetemplateController.php new file mode 100644 index 00000000..198efafc --- /dev/null +++ b/application/controllers/ServicetemplateController.php @@ -0,0 +1,17 @@ +addSingleTab($this->translate('Hosts using this service Template')); + $this->content()->add( + new ServicesOnHostsTable($this->db()) + ); + } +} diff --git a/library/Director/Web/Table/ServicesOnHostsTable.php b/library/Director/Web/Table/ServicesOnHostsTable.php new file mode 100644 index 00000000..60f415d8 --- /dev/null +++ b/library/Director/Web/Table/ServicesOnHostsTable.php @@ -0,0 +1,108 @@ + ['simple', 'common-table', 'table-row-selectable', 'multiselect'], + 'data-base-target' => '_next', + ]; + + private $db; + + public function __construct(Db $connection) + { + $this->db = $connection->getDbAdapter(); + $this->addMultiSelectAttributes(); + $this->header(); + $this->fetchRows(); + } + + public function getColumnsToBeRendered() + { + return ['Service Name', 'Host']; + } + + protected function addMultiSelectAttributes() + { + $props = $this->getMultiselectProperties(); + + if (empty($props)) { + return $this; + } + + $prefix = 'data-icinga-multiselect'; + $multi = [ + "$prefix-url" => Url::fromPath($props['url']), + "$prefix-controllers" => Url::fromPath($props['sourceUrl']), + "$prefix-data" => implode(',', $props['keys']), + ]; + + $this->addAttributes($multi); + + return $this; + } + + protected function getMultiselectProperties() + { + return [ + 'url' => 'director/services/edit', + 'sourceUrl' => 'director/service/edit', + // TODO: evaluate 'keys' => ['name', 'host'], + 'keys' => ['id'], + ]; + } + protected function fetchRows() + { + $body = $this->body(); + foreach ($this->fetch() as $row) { + $body->add($this->renderRow($row)); + } + } + + public function renderRow($row) + { + $url = Url::fromPath('director/service/edit', [ + 'name' => $row->service, + 'host' => $row->host, + 'id' => $row->id, + ]); + + return static::tr([ + static::td(Link::create($row->host, $url)), + static::td($row->service) + ]); + } + + public function fetch() + { + return $this->db->fetchAll( + $this->prepareQuery() + ); + } + + public function prepareQuery() + { + $columns = [ + 'host' => 'h.object_name', + 'service' => 's.object_name', + 'id' => 's.id', + ]; + $query = $this->db->select()->from( + ['s' => 'icinga_service'], + $columns + )->join( + ['h' => 'icinga_host'], + "s.host_id = h.id AND h.object_type = 'object'", + [] + ); + + return $query; + } +}