summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/TableWithBranchSupport.php
blob: 9e412c375e1e392c7a0a1b146f7ee2df7e73facf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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;
        }
        if (isset($result['count_services'])) {
            $result['count_services'] = 'COUNT(DISTINCT COALESCE(o.uuid, bo.uuid))';
        }

        return $result;
    }

    protected function stripSearchColumnAliases()
    {
        foreach ($this->searchColumns as &$column) {
            $column = preg_replace('/^[a-z]+\./', '', $column);
        }
    }
}