diff options
Diffstat (limited to 'library/Director/Web/Table/TableWithBranchSupport.php')
-rw-r--r-- | library/Director/Web/Table/TableWithBranchSupport.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/library/Director/Web/Table/TableWithBranchSupport.php b/library/Director/Web/Table/TableWithBranchSupport.php new file mode 100644 index 0000000..7c5b15c --- /dev/null +++ b/library/Director/Web/Table/TableWithBranchSupport.php @@ -0,0 +1,69 @@ +<?php + +namespace Icinga\Module\Director\Web\Table; + +use Icinga\Module\Director\Db\Branch\Branch; +use Ramsey\Uuid\UuidInterface; + +trait TableWithBranchSupport +{ + + /** @var UuidInterface|null */ + protected $branchUuid; + + /** + * Convenience method, only UUID is required + * + * @param Branch|null $branch + * @return $this + */ + public function setBranch(Branch $branch = null) + { + if ($branch && $branch->isBranch()) { + $this->setBranchUuid($branch->getUuid()); + } + + return $this; + } + + public function setBranchUuid(UuidInterface $uuid = null) + { + $this->branchUuid = $uuid; + + return $this; + } + + protected function branchifyColumns($columns) + { + $result = [ + 'uuid' => 'COALESCE(o.uuid, bo.uuid)' + ]; + $ignore = ['o.id', 'os.id', 'o.service_set_id', 'os.host_id']; + foreach ($columns as $alias => $column) { + if (substr($column, 0, 2) === 'o.' && ! in_array($column, $ignore)) { + // bo.column, o.column + $column = "COALESCE(b$column, $column)"; + } + if (substr($column, 0, 3) === 'os.' && ! in_array($column, $ignore)) { + // bo.column, o.column + $column = "COALESCE(b$column, $column)"; + } + + // Used in Service Tables: + if ($column === 'h.object_name' && $alias = 'host') { + $column = "COALESCE(bo.host, $column)"; + } + + $result[$alias] = $column; + } + + return $result; + } + + protected function stripSearchColumnAliases() + { + foreach ($this->searchColumns as &$column) { + $column = preg_replace('/^[a-z]+\./', '', $column); + } + } +} |