summaryrefslogtreecommitdiffstats
path: root/application/forms/Announcement/AcknowledgeAnnouncementForm.php
blob: 85fecdc0bf11f3a7fa5421b6e1ae40499bbb85fe (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
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms\Announcement;

use Icinga\Data\Filter\Filter;
use Icinga\Web\Announcement\AnnouncementCookie;
use Icinga\Web\Announcement\AnnouncementIniRepository;
use Icinga\Web\Form;
use Icinga\Web\Url;

class AcknowledgeAnnouncementForm extends Form
{
    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setAction(Url::fromPath('announcements/acknowledge'));
        $this->setAttrib('class', 'acknowledge-announcement-control');
        $this->setRedirectUrl('layout/announcements');
    }

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

    /**
     * {@inheritdoc}
     */
    public function createElements(array $formData = array())
    {
        $this->addElements(
            array(
                array(
                    'hidden',
                    'hash',
                    array(
                        'required' => true,
                        'validators' => array('NotEmpty'),
                        'decorators' => array('ViewHelper')
                    )
                )
            )
        );

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function onSuccess()
    {
        $cookie = new AnnouncementCookie();
        $repo = new AnnouncementIniRepository();
        $query = $repo->findActive();
        $filter = array();
        foreach ($cookie->getAcknowledged() as $hash) {
            $filter[] = Filter::expression('hash', '=', $hash);
        }
        $query->addFilter(Filter::matchAny($filter));
        $acknowledged = array();
        foreach ($query as $row) {
            $acknowledged[] = $row->hash;
        }
        $acknowledged[] = $this->getElement('hash')->getValue();
        $cookie->setAcknowledged($acknowledged);
        $this->getResponse()->setCookie($cookie);
        return true;
    }
}