summaryrefslogtreecommitdiffstats
path: root/application/forms/Dashboard/DashletForm.php
blob: 1af65a954ad93e3ae8ab55a752ec1b7fda680df7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms\Dashboard;

use Icinga\Web\Form;
use Icinga\Web\Form\Validator\InternalUrlValidator;
use Icinga\Web\Form\Validator\UrlValidator;
use Icinga\Web\Url;
use Icinga\Web\Widget\Dashboard;
use Icinga\Web\Widget\Dashboard\Dashlet;

/**
 * Form to add an url a dashboard pane
 */
class DashletForm extends Form
{
    /**
     * @var Dashboard
     */
    private $dashboard;

    /**
     * Initialize this form
     */
    public function init()
    {
        $this->setName('form_dashboard_addurl');
        if (! $this->getSubmitLabel()) {
            $this->setSubmitLabel($this->translate('Add To Dashboard'));
        }
        $this->setAction(Url::fromRequest());
    }

    /**
     * Build AddUrl form elements
     *
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        $groupElements  = array();
        $panes          = array();

        if ($this->dashboard) {
            $panes = $this->dashboard->getPaneKeyTitleArray();
        }

        $sectionNameValidator = ['Callback', true, [
            'callback'  => function ($value) {
                if (strpos($value, '[') === false && strpos($value, ']') === false) {
                    return true;
                }
            },
            'messages'  => [
                'callbackValue' => $this->translate('Brackets ([, ]) cannot be used here')
            ]
        ]];

        $this->addElement(
            'hidden',
            'org_pane',
            array(
                'required' => false
            )
        );

        $this->addElement(
            'hidden',
            'org_dashlet',
            array(
                'required' => false
            )
        );

        $this->addElement(
            'textarea',
            'url',
            array(
                'required'      => true,
                'label'         => $this->translate('Url'),
                'description'   => $this->translate(
                    'Enter url to be loaded in the dashlet. You can paste the full URL, including filters.'
                ),
                'validators'    => array(new UrlValidator(), new InternalUrlValidator())
            )
        );
        $this->addElement(
            'text',
            'dashlet',
            array(
                'required'      => true,
                'label'         => $this->translate('Dashlet Title'),
                'description'   => $this->translate('Enter a title for the dashlet.'),
                'validators'    => [$sectionNameValidator]
            )
        );
        $this->addElement(
            'note',
            'note',
            array(
                'decorators' => array(
                    array('HtmlTag', array('tag' => 'hr'))
                )
            )
        );
        $this->addElement(
            'checkbox',
            'create_new_pane',
            array(
                'autosubmit'    => true,
                'required'      => false,
                'label'         => $this->translate('New dashboard'),
                'description'   => $this->translate('Check this box if you want to add the dashlet to a new dashboard')
            )
        );
        if (empty($panes) || ((isset($formData['create_new_pane']) && $formData['create_new_pane'] != false))) {
            $this->addElement(
                'text',
                'pane',
                array(
                    'required'      => true,
                    'label'         => $this->translate('New Dashboard Title'),
                    'description'   => $this->translate('Enter a title for the new dashboard'),
                    'validators'    => [$sectionNameValidator]
                )
            );
        } else {
            $this->addElement(
                'select',
                'pane',
                array(
                    'required'      => true,
                    'label'         => $this->translate('Dashboard'),
                    'multiOptions'  => $panes,
                    'description'   => $this->translate('Select a dashboard you want to add the dashlet to')
                )
            );
        }
    }

    /**
     * @param \Icinga\Web\Widget\Dashboard $dashboard
     */
    public function setDashboard(Dashboard $dashboard)
    {
        $this->dashboard = $dashboard;
    }

    /**
     * @return \Icinga\Web\Widget\Dashboard
     */
    public function getDashboard()
    {
        return $this->dashboard;
    }

    /**
     * @param Dashlet $dashlet
     */
    public function load(Dashlet $dashlet)
    {
        $this->populate(array(
            'pane'          => $dashlet->getPane()->getTitle(),
            'org_pane'      => $dashlet->getPane()->getName(),
            'dashlet'       => $dashlet->getTitle(),
            'org_dashlet'   => $dashlet->getName(),
            'url'           => $dashlet->getUrl()->getRelativeUrl()
        ));
    }
}