summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/ChoicesTable.php
blob: 4ba24602c640de68566e3fa7ca2b3208651e765c (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
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;

class ChoicesTable extends ZfQueryBasedTable
{
    protected $searchColumns = ['o.object_name'];

    protected $type;

    /**
     * @param $type
     * @param Db $db
     * @return static
     */
    public static function create($type, Db $db)
    {
        $class = __NAMESPACE__ . '\\ChoicesTable' . ucfirst($type);
        if (! class_exists($class)) {
            $class = __CLASS__;
        }

        /** @var static $table */
        $table = new $class($db);
        $table->type = $type;
        return $table;
    }

    public function getType()
    {
        return $this->type;
    }

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

    public function renderRow($row)
    {
        $type = $this->getType();
        $url = Url::fromPath("director/templatechoice/${type}", [
            'name' => $row->object_name
        ]);

        return $this::row([
            Link::create($row->object_name, $url)
        ]);
    }

    protected function prepareQuery()
    {
        $type = $this->getType();
        $table = "icinga_${type}_template_choice";
        return $this->db()
            ->select()
            ->from(['o' => $table], 'object_name')
            ->order('o.object_name');
    }
}