1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/**
23 * @var CView $this
24 */
25
26require_once dirname(__FILE__).'/js/configuration.correlation.edit.js.php';
27
28$widget = (new CWidget())->setTitle(_('Event correlation rules'));
29
30$form = (new CForm())
31	->setId('correlation.edit')
32	->setName('correlation.edit')
33	->setAttribute('aria-labeledby', ZBX_STYLE_PAGE_TITLE)
34	->addVar('form', $data['form']);
35
36if ($data['correlationid']) {
37	$form->addVar('correlationid', $data['correlationid']);
38}
39
40$correlation_tab = (new CFormList())
41	->addRow(
42		(new CLabel(_('Name'), 'name'))->setAsteriskMark(),
43		(new CTextBox('name', $data['correlation']['name']))
44			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
45			->setAriaRequired()
46			->setAttribute('autofocus', 'autofocus')
47	);
48
49// Create condition table.
50$condition_table = (new CTable(_('No conditions defined.')))
51	->setId('condition_table')
52	->setAttribute('style', 'width: 100%;')
53	->setHeader([_('Label'), _('Name'), _('Action')]);
54
55$i = 0;
56
57if ($data['correlation']['filter']['conditions']) {
58	$correlation_condition_string_values = corrConditionValueToString([$data['correlation']]);
59
60	foreach ($data['correlation']['filter']['conditions'] as $j => $condition) {
61		// For some types operators are optional. Set the default "=" if operator is not set.
62		if (!array_key_exists('operator', $condition)) {
63			$condition['operator'] = CONDITION_OPERATOR_EQUAL;
64		}
65
66		if (!array_key_exists($condition['type'], $data['allowedConditions'])) {
67			continue;
68		}
69
70		$label = isset($condition['formulaid']) ? $condition['formulaid'] : num2letter($i);
71
72		$labelSpan = (new CSpan($label))
73			->addClass('label')
74			->setAttribute('data-type', $condition['type'])
75			->setAttribute('data-formulaid', $label);
76
77		$condition_table->addRow([
78				$labelSpan,
79				(new CCol(getcorrConditionDescription($condition, $correlation_condition_string_values[0][$j])))
80					->addClass(ZBX_STYLE_TABLE_FORMS_OVERFLOW_BREAK),
81				(new CCol([
82					(new CButton('remove', _('Remove')))
83						->onClick('javascript: removeCondition('.$i.');')
84						->addClass(ZBX_STYLE_BTN_LINK)
85						->removeId(),
86					new CVar('conditions['.$i.']', $condition)
87				]))->addClass(ZBX_STYLE_NOWRAP)
88			],
89			null, 'conditions_'.$i
90		);
91
92		$i++;
93	}
94}
95
96$condition_table->addRow([
97	(new CSimpleButton(_('Add')))
98		->onClick('return PopUp("popup.condition.event.corr",'.json_encode([
99			'type' => ZBX_POPUP_CONDITION_TYPE_EVENT_CORR
100		]).', null, this);')
101		->addClass(ZBX_STYLE_BTN_LINK)
102]);
103
104$correlation_tab
105	->addRow(new CLabel(_('Type of calculation'), 'label-evaltype'), [
106		(new CSelect('evaltype'))
107			->setId('evaltype')
108			->setValue($data['correlation']['filter']['evaltype'])
109			->setFocusableElementId('label-evaltype')
110			->addOptions(CSelect::createOptionsFromArray([
111				CONDITION_EVAL_TYPE_AND_OR => _('And/Or'),
112				CONDITION_EVAL_TYPE_AND => _('And'),
113				CONDITION_EVAL_TYPE_OR => _('Or'),
114				CONDITION_EVAL_TYPE_EXPRESSION => _('Custom expression')
115			])),
116		(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
117		(new CSpan())->setId('condition_label'),
118		(new CTextBox('formula', $data['correlation']['filter']['formula']))
119			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
120			->setId('formula')
121			->setAttribute('placeholder', 'A or (B and C) &hellip;')
122	])
123	->addRow(
124		(new CLabel(_('Conditions'), $condition_table->getId()))->setAsteriskMark(),
125		(new CDiv($condition_table))
126			->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
127			->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
128			->setAriaRequired()
129	);
130
131$correlation_tab
132	->addRow(_('Description'),
133		(new CTextArea('description', $data['correlation']['description']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
134	)
135	->addRow(_('Enabled'),
136		(new CCheckBox('status', ZBX_CORRELATION_ENABLED))
137			->setChecked($data['correlation']['status'] == ZBX_CORRELATION_ENABLED)
138	);
139
140// Operations tab.
141$operation_tab = (new CFormList())
142	->addRow(
143		_('Close old events'),
144		(new CCheckBox('operations[][type]', ZBX_CORR_OPERATION_CLOSE_OLD))
145			->setChecked($data['correlation']['operations'][ZBX_CORR_OPERATION_CLOSE_OLD])
146			->setId('operation_0_type')
147	)
148	->addRow(
149		_('Close new event'),
150		(new CCheckBox('operations[][type]', ZBX_CORR_OPERATION_CLOSE_NEW))
151			->setChecked($data['correlation']['operations'][ZBX_CORR_OPERATION_CLOSE_NEW])
152			->setId('operation_1_type')
153	)
154	->addRow('', (new CDiv((new CLabel(_('At least one operation must be selected.')))->setAsteriskMark())));
155
156// Append tabs to form.
157$correlation_tabs = (new CTabView())
158	->addTab('correlationTab', _('Correlation'), $correlation_tab)
159	->addTab('operationTab', _('Operations'), $operation_tab);
160
161if (!hasRequest('form_refresh')) {
162	$correlation_tabs->setSelected(0);
163}
164
165// Append buttons to form.
166if ($data['correlationid']) {
167	$correlation_tabs->setFooter(makeFormFooter(
168		new CSubmit('update', _('Update')), [
169			new CButton('clone', _('Clone')),
170			new CButtonDelete(
171				_('Delete current correlation?'),
172				url_param('form').url_param('correlationid')
173			),
174			new CButtonCancel()
175		]
176	));
177}
178else {
179	$correlation_tabs->setFooter(makeFormFooter(
180		new CSubmit('add', _('Add')),
181		[new CButtonCancel()]
182	));
183}
184
185$form->addItem($correlation_tabs);
186
187$widget->addItem($form);
188
189$widget->show();
190