summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/ItemTable/BaseRowItem.php
blob: 01896199e4cf05b00b52a6e762d979c083fd6def (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Widget\ItemTable;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Orm\Model;

abstract class BaseRowItem extends BaseHtmlElement
{
    protected $defaultAttributes = ['class' => 'row-item'];

    /** @var Model */
    protected $item;

    /** @var BaseItemTable */
    protected $list;

    protected $tag = 'tr';

    /**
     * Create a new row item
     *
     * @param Model $item
     * @param BaseItemTable $list
     */
    public function __construct(Model $item, BaseItemTable $list)
    {
        $this->item = $item;
        $this->list = $list;

        $this->init();
    }

    /**
     * Initialize the row item
     *
     * If you want to adjust the row item after construction, override this method.
     */
    protected function init()
    {
    }

    abstract protected function assembleVisual(BaseHtmlElement $visual);

    abstract protected function assembleCell(BaseHtmlElement $cell, string $path, $value);

    protected function createVisual(): BaseHtmlElement
    {
        $visual = new HtmlElement('td', Attributes::create(['class' => 'visual']));

        $this->assembleVisual($visual);

        return $visual;
    }

    protected function assemble()
    {
        $this->addHtml($this->createVisual());

        foreach ($this->list->getColumns() as $columnPath => $_) {
            $steps = explode('.', $columnPath);
            if ($steps[0] === $this->item->getTableName()) {
                array_shift($steps);
                $columnPath = implode('.', $steps);
            }

            $column = null;
            $subject = $this->item;
            foreach ($steps as $i => $step) {
                if (isset($subject->$step)) {
                    if ($subject->$step instanceof Model) {
                        $subject = $subject->$step;
                    } else {
                        $column = $step;
                    }
                } else {
                    $columnCandidate = implode('.', array_slice($steps, $i));
                    if (isset($subject->$columnCandidate)) {
                        $column = $columnCandidate;
                    } else {
                        break;
                    }
                }
            }

            $value = null;
            if ($column !== null) {
                $value = $subject->$column;
                if (is_array($value)) {
                    $value = empty($value) ? null : implode(',', $value);
                }
            }

            $cell = new HtmlElement('td');
            if ($value !== null) {
                $this->assembleCell($cell, $columnPath, $value);
            }

            $this->addHtml($cell);
        }
    }
}