summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Web/Control/ProblemToggle.php
blob: c5aed82da93070f71f2c853f41b151a853c504cd (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
<?php

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

namespace Icinga\Module\Icingadb\Web\Control;

use ipl\Web\Common\FormUid;
use ipl\Web\Compat\CompatForm;

class ProblemToggle extends CompatForm
{
    use FormUid;

    protected $filter;

    protected $protector;

    protected $defaultAttributes = [
        'name'    => 'problem-toggle',
        'class'   => 'icinga-form icinga-controls inline'
    ];

    public function __construct($filter)
    {
        $this->filter = $filter;
    }

    /**
     * Set callback to protect ids with
     *
     * @param   callable $protector
     *
     * @return  $this
     */
    public function setIdProtector(callable $protector): self
    {
        $this->protector = $protector;

        return $this;
    }

    /**
     * Get whether the toggle is checked
     *
     * @return bool
     */
    public function isChecked(): bool
    {
        $this->ensureAssembled();

        return $this->getElement('problems')->isChecked();
    }

    protected function assemble()
    {
        $this->addElement('checkbox', 'problems', [
            'class'     => 'autosubmit',
            'id'        => $this->protectId('problems'),
            'label'     => t('Problems Only'),
            'value'     => $this->filter !== null
        ]);

        $this->add($this->createUidElement());
    }

    private function protectId($id)
    {
        if (is_callable($this->protector)) {
            return call_user_func($this->protector, $id);
        }

        return $id;
    }
}