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
|
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Eventdb\Forms\Event;
use Exception;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Eventdb\Eventdb;
use Icinga\Web\Form;
/**
* Form for managing the connection to the EventDB backend
*/
class EventCommentForm extends Form
{
/**
* @var Eventdb
*/
protected $db;
protected $filter;
protected static $types = array(
'comment',
'ack',
'revoke'
);
/**
* {@inheritdoc}
*/
public static $defaultElementDecorators = array(
array('ViewHelper', array('separator' => '')),
array('Errors', array('separator' => '')),
);
/**
* {@inheritdoc}
*/
public function init()
{
$this->setSubmitLabel($this->translate('Submit'));
}
public function setDb(Eventdb $db)
{
$this->db = $db;
return $this;
}
public function setFilter(Filter $filter)
{
$this->filter = $filter;
return $this;
}
/**
* {@inheritdoc}
*/
public function createElements(array $formData)
{
$this->addElement(
'select',
'type',
array(
'label' => $this->translate('Type'),
'multiOptions' => array(
0 => $this->translate('Comment'),
1 => $this->translate('Acknowledge'),
2 => $this->translate('Revoke')
),
'required' => true,
'value' => 1,
)
);
$this->addElement(
'text',
'comment',
array(
'label' => $this->translate('Comment'),
'required' => true
)
);
}
public function onSuccess()
{
$type = $this->getValue('type');
$comment = $this->getValue('comment');
$username = $this->Auth()->getUser()->getUsername();
$events = $this->db->select()->from('event', array('id'))->applyFilter($this->filter);
$dbAdapter = $this->db->getDataSource()->getDbAdapter();
$dbAdapter->beginTransaction();
try {
foreach ($events as $event) {
$this->db->insert('comment', array(
'event_id' => $event->id,
'type' => $type,
'message' => $comment,
'created' => date(Eventdb::DATETIME_FORMAT),
'modified' => date(Eventdb::DATETIME_FORMAT),
'user' => $username
));
if ($type !== '0') {
$ackFilter = Filter::expression('id', '=', $event->id);
if ($this->db->hasCorrelatorExtensions()) {
$ackFilter = Filter::matchAny($ackFilter, Filter::where('group_leader', $event->id));
}
$this->db->update('event', array(
'ack' => $type === '1' ? 1 : 0
), $ackFilter);
}
}
$dbAdapter->commit();
return true;
} catch (Exception $e) {
$dbAdapter->rollback();
$this->error($e->getMessage());
return false;
}
}
/**
* {@inheritdoc}
*/
public function loadDefaultDecorators()
{
parent::loadDefaultDecorators();
$this->removeDecorator('FormHints');
}
public function addSubmitButton()
{
parent::addSubmitButton();
$btn = $this->getElement('btn_submit');
$btn->removeDecorator('HtmlTag');
}
}
|