From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Web/Table/TemplatesTable.php | 156 ++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 library/Director/Web/Table/TemplatesTable.php (limited to 'library/Director/Web/Table/TemplatesTable.php') diff --git a/library/Director/Web/Table/TemplatesTable.php b/library/Director/Web/Table/TemplatesTable.php new file mode 100644 index 0000000..be195b2 --- /dev/null +++ b/library/Director/Web/Table/TemplatesTable.php @@ -0,0 +1,156 @@ +type = strtolower($type); + return $table; + } + + protected function assemble() + { + $type = $this->type; + $this->enableMultiSelect( + "director/${type}s/edittemplates", + "director/${type}template", + ['name'] + ); + } + + public function getType() + { + return $this->type; + } + + public function getColumnsToBeRendered() + { + return [$this->translate('Template Name')]; + } + + public function renderRow($row) + { + $name = $row->object_name; + $type = str_replace('_', '-', $this->getType()); + $caption = $row->is_used === 'y' ? $name : [ + $name, + Html::tag( + 'span', + ['style' => 'font-style: italic'], + $this->translate(' - not in use -') + ) + ]; + + $url = Url::fromPath("director/${type}template/usage", [ + 'name' => $name + ]); + + return $this::row([ + new Link($caption, $url), + [ + new Link(new Icon('plus'), "director/$type/add", [ + 'type' => 'object', + 'imports' => $name + ]), + new Link(new Icon('history'), "director/$type/history", [ + 'uuid' => Uuid::fromBytes(Db\DbUtil::binaryResult($row->uuid))->toString(), + ]) + ] + ]); + } + + public function filterTemplate( + IcingaObject $template, + $inheritance = IcingaObjectFilterHelper::INHERIT_DIRECT + ) { + IcingaObjectFilterHelper::filterByTemplate( + $this->getQuery(), + $template, + 'o', + $inheritance + ); + + return $this; + } + + public function showOnlyUsed() + { + $type = $this->getType(); + $this->getQuery()->where( + "(EXISTS (SELECT ${type}_id FROM icinga_${type}_inheritance" + . " WHERE parent_${type}_id = o.id))" + ); + } + + public function showOnlyUnUsed() + { + $type = $this->getType(); + $this->getQuery()->where( + "(NOT EXISTS (SELECT ${type}_id FROM icinga_${type}_inheritance" + . " WHERE parent_${type}_id = o.id))" + ); + } + + protected function applyRestrictions(ZfSelect $query) + { + $auth = Auth::getInstance(); + $type = $this->type; + $restrictions = $auth->getRestrictions("director/$type/template/filter-by-name"); + if (empty($restrictions)) { + return $query; + } + + $filter = Filter::matchAny(); + foreach ($restrictions as $restriction) { + $filter->addFilter(Filter::where('o.object_name', $restriction)); + } + + return FilterRenderer::applyToQuery($filter, $query); + } + + protected function prepareQuery() + { + $type = $this->getType(); + $used = "CASE WHEN EXISTS(SELECT 1 FROM icinga_${type}_inheritance oi" + . " WHERE oi.parent_${type}_id = o.id) THEN 'y' ELSE 'n' END"; + + $columns = [ + 'object_name' => 'o.object_name', + 'uuid' => 'o.uuid', + 'id' => 'o.id', + 'is_used' => $used, + ]; + $query = $this->db()->select()->from( + ['o' => "icinga_${type}"], + $columns + )->where( + "o.object_type = 'template'" + )->order('o.object_name'); + + return $this->applyRestrictions($query); + } +} -- cgit v1.2.3