summaryrefslogtreecommitdiffstats
path: root/application/forms/AcknowledgeApplicationStateMessageForm.php
blob: 61f5824f686409372bbd07167c0f45f9f232565f (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
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms;

use Icinga\Application\Hook\ApplicationStateHook;
use Icinga\Web\ApplicationStateCookie;
use Icinga\Web\Form;
use Icinga\Web\Url;

class AcknowledgeApplicationStateMessageForm extends Form
{
    public function init()
    {
        $this->setAction(Url::fromPath('application-state/acknowledge-message'));
        $this->setAttrib('class', 'application-state-acknowledge-message-control');
        $this->setRedirectUrl('application-state/summary');
    }

    public function addSubmitButton()
    {
        $this->addElement(
            'button',
            'btn_submit',
            [
                'class'         => 'link-button spinner',
                'decorators'    => [
                    'ViewHelper',
                    ['HtmlTag', ['tag' => 'div', 'class' => 'control-group form-controls']]
                ],
                'escape'        => false,
                'ignore'        => true,
                'label'         => $this->getView()->icon('cancel'),
                'title'         => $this->translate('Acknowledge message'),
                'type'          => 'submit'
            ]
        );
        return $this;
    }

    public function createElements(array $formData = [])
    {
        $this->addElements(
            [
                [
                    'hidden',
                    'id',
                    [
                        'required' => true,
                        'validators' => ['NotEmpty'],
                        'decorators' => ['ViewHelper']
                    ]
                ]
            ]
        );

        return $this;
    }

    public function onSuccess()
    {
        $cookie = new ApplicationStateCookie();

        $ack = $cookie->getAcknowledgedMessages();
        $ack[] = $this->getValue('id');

        $active = ApplicationStateHook::getAllMessages();

        $cookie->setAcknowledgedMessages(array_keys(array_intersect_key($active, array_flip($ack))));

        $this->getResponse()->setCookie($cookie);

        return true;
    }
}