File: EditForm.php

package info (click to toggle)
icingaweb2-module-toplevelview 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,496 kB
  • sloc: javascript: 9,646; php: 2,358; sh: 54; xml: 51; makefile: 25
file content (194 lines) | stat: -rw-r--r-- 5,820 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Toplevelview\Forms;

use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Module\Toplevelview\Model\View;

use Exception;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Url;

class EditForm extends Form
{
    /**
     * @var ViewConfig
     */
    protected $viewconfig;

    /**
     * @var View
     */
    protected $view;

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setName('form_toplevelview_edit');
    }

    public function setViews(View $view)
    {
        $this->view = $view;
        return $this;
    }

    public function setViewConfig(ViewConfig $config)
    {
        $this->viewconfig = $config;
        return $this;
    }

    /**
     * @see Form::onSuccess()
     */
    public function onSuccess()
    {
        try {
            $this->view->setName($this->getValue('name'));
            $this->view->setText($this->getValue('config'));

            // ensure config can be parsed...
            $this->view->getMetaData();
            $this->view->getTree();

            $cancel = $this->getElement('btn_submit_cancel');
            $delete = $this->getElement('btn_submit_delete');

            if ($this->getElement('btn_submit_save_file')->getValue() !== null) {
                // Store the view to its YAML file
                $this->viewconfig->storeToFile($this->view);
                Notification::success($this->translate('Top Level View successfully saved'));
            } elseif ($cancel !== null && $cancel->getValue() !== null) {
                // Clear the stored session data for the view
                $this->viewconfig->clearSession($this->view);
                Notification::success($this->translate('Top Level View restored from disk'));
            } elseif ($delete != null && $delete->getValue() !== null) {
                // Delete the view's YAML file
                $this->viewconfig->delete($this->view);
                $this->setRedirectUrl('toplevelview');
                Notification::success($this->translate('Top Level View successfully deleted'));
            } else {
                // Store the view to the user's session by default
                $this->viewconfig->storeToSession($this->view);
                Notification::success($this->translate('Top Level View successfully saved for the current session'));
            }
            return true;
        } catch (Exception $e) {
            $this->error(sprintf('Could not save config: %s', $e->getMessage()));
            return false;
        }
    }

    public function getRedirectUrl()
    {
        if ($this->redirectUrl === null && ($name = $this->view->getName()) !== null) {
            $this->redirectUrl = Url::fromPath('toplevelview/show', ['name' => $name]);
        }
        return parent::getRedirectUrl();
    }

    /**
     * Populate form
     *
     * @see Form::onRequest()
     */
    public function onRequest()
    {
        $values = [];
        $values['name'] = $this->view->getName();
        $values['config'] = $this->view->getText();

        $this->populate($values);
    }

    /**
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        if ($this->view->hasBeenLoadedFromSession()) {
            $this->warning(
                $this->translate(
                    'This config is only stored in your session!'
                    . ' Make sure to save it to disk once your work is complete!'
                ),
                false
            );
        }

        $this->addElement(
            'text',
            'name',
            [
                'label' => $this->translate('File name'),
                'required' => true
            ]
        );
        $this->addElement(
            'textarea',
            'config',
            [
                'label' => $this->translate('YAML Config'),
                'class' => 'code-editor codemirror',
                'decorators' => [
                    ['Label', ['tag' => 'div', 'separator' => '']],
                    ['HtmlTag', ['tag' => 'div']],
                    'ViewHelper'
                ],
                'data-codemirror-mode' => 'yaml'
            ]
        );

        $this->addElement(
            'submit',
            'btn_submit_save_session',
            [
                'ignore'     => true,
                'label'      => $this->translate('Save for the current Session'),
                'decorators' => ['ViewHelper']
            ]
        );

        $this->addElement(
            'submit',
            'btn_submit_save_file',
            [
                'ignore'     => true,
                'label'      => $this->translate('Save to config file'),
                'decorators' => ['ViewHelper']
            ]
        );

        if ($this->view->hasBeenLoadedFromSession()) {
            $this->addElement(
                'submit',
                'btn_submit_cancel',
                [
                    'ignore'     => true,
                    'label'      => $this->translate('Cancel editing'),
                    'class'      => 'btn-cancel',
                    'decorators' => ['ViewHelper']
                ]
            );
        }

        if ($this->view->hasBeenLoaded()) {
            $this->addElement(
                'submit',
                'btn_submit_delete',
                [
                    'ignore'     => true,
                    'label'      => $this->translate('Delete config'),
                    'data-confirmation' => $this->translate('Confirm deletion'),
                    'class'      => 'btn-remove',
                    'decorators' => ['ViewHelper']
                ]
            );
        }
    }
}