icingaweb2/application/forms/Announcement/AcknowledgeAnnouncementForm...

93 lines
2.6 KiB
PHP
Raw Normal View History

2016-11-29 15:12:33 +01:00
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Announcement;
use Icinga\Data\Filter\Filter;
2016-11-29 15:12:33 +01:00
use Icinga\Web\Announcement\AnnouncementCookie;
use Icinga\Web\Announcement\AnnouncementIniRepository;
2016-11-29 15:12:33 +01:00
use Icinga\Web\Form;
2017-11-20 13:35:17 +01:00
use Icinga\Web\Url;
2016-11-29 15:12:33 +01:00
class AcknowledgeAnnouncementForm extends Form
{
/**
* {@inheritdoc}
*/
public function init()
{
2017-11-20 13:35:17 +01:00
$this->setAction(Url::fromPath('announcements/acknowledge'));
2016-11-29 15:12:33 +01:00
$this->setAttrib('class', 'form-inline 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;
}
2016-11-29 15:12:33 +01:00
$acknowledged[] = $this->getElement('hash')->getValue();
$cookie->setAcknowledged($acknowledged);
$this->getResponse()->setCookie($cookie);
return true;
}
}