summaryrefslogtreecommitdiffstats
path: root/application/forms/AutoRefreshForm.php
blob: 122f635023fb4570566b3fef1fa0b81a43b1bd39 (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
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms;

use Icinga\Application\Logger;
use Icinga\User\Preferences;
use Icinga\Web\Form;
use Icinga\Web\Notification;
use Icinga\Web\Session;
use Icinga\Web\Url;

/**
 * Form class to adjust user auto refresh preferences
 */
class AutoRefreshForm extends Form
{
    /**
     * Initialize this form
     */
    public function init()
    {
        $this->setName('form_auto_refresh');
        // Post against the current location
        $this->setAction('');
    }

    /**
     * Adjust preferences and persist them
     *
     * @see Form::onSuccess()
     */
    public function onSuccess()
    {
        /** @var Preferences $preferences */
        $preferences = $this->getRequest()->getUser()->getPreferences();
        $icingaweb = $preferences->get('icingaweb');

        if ((bool) $preferences->getValue('icingaweb', 'auto_refresh', true) === false) {
            $icingaweb['auto_refresh'] = '1';
            $notification = $this->translate('Auto refresh successfully enabled');
        } else {
            $icingaweb['auto_refresh'] = '0';
            $notification = $this->translate('Auto refresh successfully disabled');
        }
        $preferences->icingaweb = $icingaweb;

        Session::getSession()->user->setPreferences($preferences);
        Notification::success($notification);

        $this->getResponse()->setHeader('X-Icinga-Rerender-Layout', 'yes');
        $this->setRedirectUrl(Url::fromRequest()->without('renderLayout'));
    }

    /**
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        $preferences = $this->getRequest()->getUser()->getPreferences();

        if ((bool) $preferences->getValue('icingaweb', 'auto_refresh', true) === false) {
            $value = $this->translate('Enable auto refresh');
        } else {
            $value = $this->translate('Disable auto refresh');
        }

        $this->addElements(array(
            array(
                'button',
                'btn_submit',
                array(
                    'ignore'        => true,
                    'type'          => 'submit',
                    'value'         => $value,
                    'decorators'    => array('ViewHelper'),
                    'escape'        => false,
                    'class'         => 'link-like'
                )
            )
        ));
    }
}