summaryrefslogtreecommitdiffstats
path: root/application/forms/EditForm.php
blob: 86e7880816abf501cf24499a8884583dfbdf68cb (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
172
173
174
175
176
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Toplevelview\Forms;

use Exception;
use Icinga\Module\Toplevelview\ViewConfig;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Url;

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

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

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

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

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

            $this->viewConfig->storeToSession();

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

            if ($this->getElement('btn_submit_save_file')->getValue() !== null) {
                $this->viewConfig->store();
                Notification::success($this->translate('Top Level View successfully saved'));
            } elseif ($cancel !== null && $cancel->getValue() !== null) {
                $this->viewConfig->clearSession();
                Notification::success($this->translate('Top Level View restored from disk'));
            } elseif ($delete != null && $delete->getValue() !== null) {
                $this->viewConfig->delete();
                $this->setRedirectUrl('toplevelview');
                Notification::success($this->translate('Top Level View successfully deleted'));
            } else {
                Notification::success($this->translate('Top Level View successfully saved for the current session'));
            }
            return true;
        } catch (Exception $e) {
            $this->addError(sprintf('Could not save config: %s', $e->getMessage()));
            return false;
        }
    }

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

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

        $this->populate($values);
    }

    /**
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        if ($this->viewConfig->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',
            array(
                'label'    => $this->translate('File name'),
                'required' => true
            )
        );
        $this->addElement(
            'textarea',
            'config',
            array(
                //'required'             => true,
                'label'                => $this->translate('YAML Config'),
                'class'                => 'code-editor codemirror',
                'decorators'           => array(
                    array('Label', array('tag'=>'div', 'separator' => '')),
                    array('HtmlTag', array('tag' => 'div')),
                    'ViewHelper'
                ),
                'data-codemirror-mode' => 'yaml'
            )
        );

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

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

        if ($this->viewConfig->hasBeenLoadedFromSession()) {
            $this->addElement(
                'submit',
                'btn_submit_cancel',
                array(
                    'ignore'     => true,
                    'label'      => $this->translate('Cancel editing'),
                    'decorators' => array('ViewHelper')
                )
            );
        }

        if ($this->viewConfig->hasBeenLoaded()) {
            $this->addElement(
                'submit',
                'btn_submit_delete',
                array(
                    'ignore'     => true,
                    'label'      => $this->translate('Delete config'),
                    'decorators' => array('ViewHelper')
                )
            );
        }
    }
}