summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/IcingaCommandArgumentTable.php
blob: 37cbc78489ce20f6476bce3ef32c5e50d54dcab9 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Module\Director\Data\Json;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Db\Branch\BranchModificationStore;
use Icinga\Module\Director\Objects\IcingaCommand;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;

class IcingaCommandArgumentTable extends ZfQueryBasedTable
{
    /** @var IcingaCommand */
    protected $command;

    /** @var Branch */
    protected $branch;

    protected $searchColumns = [
        'ca.argument_name',
        'ca.argument_value',
    ];

    public function __construct(IcingaCommand $command, Branch $branch)
    {
        $this->command = $command;
        $this->branch = $branch;
        parent::__construct($command->getConnection());
        $this->getAttributes()->set('data-base-target', '_self');
    }

    public function renderRow($row)
    {
        return $this::row([
            Link::create($row->argument_name, 'director/command/arguments', [
                'argument' => $row->argument_name,
                'name'     => $this->command->getObjectName()
            ]),
            $row->argument_value
        ]);
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('Argument'),
            $this->translate('Value'),
        ];
    }

    public function prepareQuery()
    {
        $db = $this->db();
        if ($this->branch->isBranch()) {
            return (new ArrayDatasource((array) $this->command->arguments()->toPlainObject()))->select();
            /** @var Db $connection */
            $connection = $this->connection();
            $store = new BranchModificationStore($connection, 'command');
            $modification = $store->loadOptionalModificationByName(
                $this->command->getObjectName(),
                $this->branch->getUuid()
            );
            if ($modification) {
                $props = $modification->getProperties()->jsonSerialize();
                if (isset($props->arguments)) {
                    return new ArrayDatasource((array) $this->command->arguments()->toPlainObject());
                }
            }
        }
        $id = $this->command->get('id');
        if ($id === null) {
            return new ArrayDatasource([]);
        }
        return $this->db()->select()->from(
            ['ca' => 'icinga_command_argument'],
            [
                'id'             => 'ca.id',
                'argument_name'  => "COALESCE(ca.argument_name, '(none)')",
                'argument_value' => 'ca.argument_value',
            ]
        )->where(
            'ca.command_id = ?',
            $id
        )->order('ca.sort_order')->order('ca.argument_name')->limit(100);
    }
}