summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/Web/Widget/SelectBox.php
blob: 48b98ac5ca13f6f5926dc719534b72d6f688b959 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Web\Widget;

use Icinga\Web\Form;
use Icinga\Web\Request;
use Icinga\Web\Widget\AbstractWidget;

class SelectBox extends AbstractWidget
{
    /**
     * The name of the form that will be created
     *
     * @var string
     */
    private $name;

    /**
     * An array containing all intervals with their associated labels
     *
     * @var array
     */
    private $values;

    /**
     * The label displayed next to the select box
     *
     * @var string
     */
    private $label;

    /**
     * The name of the url parameter to set
     *
     * @var string
     */
    private $parameter;

    /**
     * A request object used for initial form population
     *
     * @var Request
     */
    private $request;

    /**
     * Create a TimelineIntervalBox
     *
     * @param   string  $name       The name of the form that will be created
     * @param   array   $values     An array containing all intervals with their associated labels
     * @param   string  $label      The label displayed next to the select box
     * @param   string  $param      The request parameter name to set
     */
    public function __construct($name, array $values, $label = 'Select', $param = 'selection')
    {
        $this->name = $name;
        $this->values = $values;
        $this->label = $label;
        $this->parameter = $param;
    }

    /**
     * Apply the parameters from the given request on this widget
     *
     * @param   Request     $request    The request to use for populating the form
     */
    public function applyRequest(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Return the chosen interval value or null
     *
     * @param   Request     $request    The request to fetch the value from
     *
     * @return  string|null
     */
    public function getInterval(Request $request = null)
    {
        if ($request === null && $this->request) {
            $request = $this->request;
        }

        if ($request) {
            return $request->getParam('interval');
        }
    }

    /**
     * Renders this widget and returns the HTML as a string
     *
     * @return  string
     */
    public function render()
    {
        $form = new Form();
        $form->setAttrib('class', Form::DEFAULT_CLASSES . ' inline');
        $form->setMethod('GET');
        $form->setUidDisabled();
        $form->setTokenDisabled();
        $form->setName($this->name);
        $form->addElement(
            'select',
            $this->parameter,
            array(
                'label'         => $this->label,
                'multiOptions'  => $this->values,
                'autosubmit'    => true
            )
        );

        if ($this->request) {
            $form->populate($this->request->getParams());
        }

        return $form;
    }
}