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

namespace Icinga\Module\Director\Web\Table;

use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
use Icinga\Web\Url;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\SimpleQueryBasedTable;

class IcingaObjectDatafieldTable extends SimpleQueryBasedTable
{
    protected $object;

    /** @var int */
    protected $objectId;

    public function __construct(IcingaObject $object)
    {
        $this->object = $object;
        $this->objectId = (int) $object->id;
        return $this;
    }

    protected $searchColumns = array(
        'varname',
        'caption'
    );

    public function getColumns()
    {
        return array(
            'object_id',
            'var_filter',
            'is_required',
            'id',
            'varname',
            'caption',
            'description',
            'datatype',
            'format',
        );
    }

    public function getColumnsToBeRendered()
    {
        return array(
            'caption'     => $this->translate('Label'),
            'varname'     => $this->translate('Field name'),
            'is_required' => $this->translate('Mandatory'),
        );
    }

    public function renderRow($row)
    {
        $definedOnThis = (int) $row->object_id === $this->objectId;
        if ($definedOnThis) {
            $caption = new Link(
                $row->caption,
                Url::fromRequest()->with('field_id', $row->id)
            );
        } else {
            $caption = $row->caption;
        }

        $row = $this::row([
            $caption,
            $row->varname,
            $row->is_required
        ]);

        if (! $definedOnThis) {
            $row->getAttributes()->add('class', 'disabled');
        }

        return $row;
    }

    public function prepareQuery()
    {
        $loader = new IcingaObjectFieldLoader($this->object);
        $fields = $loader->fetchFieldDetailsForObject($this->object);
        $ds = new ArrayDatasource($fields);
        return $ds->select();
    }
}