2022-08-29 15:44:21 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
|
|
|
|
|
2022-09-20 06:20:44 -04:00
|
|
|
use Icinga\Module\Director\Db\Branch\Branch;
|
2022-08-29 15:44:21 -04:00
|
|
|
use Ramsey\Uuid\UuidInterface;
|
|
|
|
|
|
|
|
|
|
trait TableWithBranchSupport
|
|
|
|
|
{
|
|
|
|
|
/** @var UuidInterface|null */
|
|
|
|
|
protected $branchUuid;
|
|
|
|
|
|
2022-09-20 06:20:44 -04:00
|
|
|
/**
|
|
|
|
|
* Convenience method, only UUID is required
|
|
|
|
|
*
|
2026-01-29 09:22:40 -05:00
|
|
|
* @param ?Branch $branch
|
2022-09-20 06:20:44 -04:00
|
|
|
* @return $this
|
|
|
|
|
*/
|
2026-01-29 09:22:40 -05:00
|
|
|
public function setBranch(?Branch $branch = null)
|
2022-09-20 06:20:44 -04:00
|
|
|
{
|
|
|
|
|
if ($branch && $branch->isBranch()) {
|
|
|
|
|
$this->setBranchUuid($branch->getUuid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 09:22:40 -05:00
|
|
|
public function setBranchUuid(?UuidInterface $uuid = null)
|
2022-08-29 15:44:21 -04:00
|
|
|
{
|
|
|
|
|
$this->branchUuid = $uuid;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function branchifyColumns($columns)
|
|
|
|
|
{
|
|
|
|
|
$result = [
|
|
|
|
|
'uuid' => 'COALESCE(o.uuid, bo.uuid)'
|
|
|
|
|
];
|
2022-09-20 06:03:00 -04:00
|
|
|
$ignore = ['o.id', 'os.id', 'o.service_set_id', 'os.host_id'];
|
2022-08-29 15:44:21 -04:00
|
|
|
foreach ($columns as $alias => $column) {
|
|
|
|
|
if (substr($column, 0, 2) === 'o.' && ! in_array($column, $ignore)) {
|
|
|
|
|
// bo.column, o.column
|
|
|
|
|
$column = "COALESCE(b$column, $column)";
|
|
|
|
|
}
|
2022-09-20 06:03:00 -04:00
|
|
|
if (substr($column, 0, 3) === 'os.' && ! in_array($column, $ignore)) {
|
|
|
|
|
// bo.column, o.column
|
|
|
|
|
$column = "COALESCE(b$column, $column)";
|
|
|
|
|
}
|
2022-08-29 15:44:21 -04:00
|
|
|
|
|
|
|
|
// Used in Service Tables:
|
|
|
|
|
if ($column === 'h.object_name' && $alias = 'host') {
|
|
|
|
|
$column = "COALESCE(bo.host, $column)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result[$alias] = $column;
|
|
|
|
|
}
|
2023-04-24 05:09:31 -04:00
|
|
|
if (isset($result['count_services'])) {
|
|
|
|
|
$result['count_services'] = 'COUNT(DISTINCT COALESCE(o.uuid, bo.uuid))';
|
|
|
|
|
}
|
2022-08-29 15:44:21 -04:00
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function stripSearchColumnAliases()
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->searchColumns as &$column) {
|
|
|
|
|
$column = preg_replace('/^[a-z]+\./', '', $column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|