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
26$output = [
27	'header' => $data['title']
28];
29
30$options = $data['options'];
31
32$overrides_popup_form = (new CForm())
33	->cleanItems()
34	->setId('lldoverride_form')
35	->addItem((new CVar('no', $options['no']))->removeId())
36	->addItem((new CVar('templated', $options['templated']))->removeId())
37	->addVar('old_name', $options['old_name'])
38	->addVar('overrides_names', $options['overrides_names'])
39	->addItem((new CVar('action', 'popup.lldoverride'))->removeId())
40	->addItem((new CInput('submit', 'submit'))
41		->addStyle('display: none;')
42		->removeId()
43	);
44
45$overrides_popup_form_list = (new CFormList())
46	->addRow(
47		(new CLabel(_('Name'), 'override_name'))->setAsteriskMark(),
48		(new CTextBox('name', $options['old_name'], $options['templated'], DB::getFieldLength('lld_override', 'name')))
49			->setAriaRequired()
50			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
51			->setId('override_name')
52	)
53	->addRow(
54		_('If filter matches'),
55		(new CRadioButtonList('stop', (int) $options['stop']))
56			->addValue(_('Continue overrides'), ZBX_LLD_OVERRIDE_STOP_NO)
57			->addValue(_('Stop processing'), ZBX_LLD_OVERRIDE_STOP_YES)
58			->setModern(true)
59			->setReadonly($options['templated'])
60	);
61
62// filters
63$override_evaltype_select = (new CSelect('overrides_evaltype'))
64	->setId('overrides-evaltype')
65	->setValue($options['overrides_evaltype'])
66	->addOptions(CSelect::createOptionsFromArray([
67		CONDITION_EVAL_TYPE_AND_OR => _('And/Or'),
68		CONDITION_EVAL_TYPE_AND => _('And'),
69		CONDITION_EVAL_TYPE_OR => _('Or'),
70		CONDITION_EVAL_TYPE_EXPRESSION => _('Custom expression')
71	]));
72
73if ($options['templated']) {
74	$override_evaltype_select->setReadonly();
75}
76
77$override_evaltype = (new CDiv([
78	(new CDiv([
79		_('Type of calculation'),
80		(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
81		$override_evaltype_select,
82		(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN)
83	]))->addClass(ZBX_STYLE_CELL),
84	(new CDiv([
85		(new CSpan(''))
86			->addStyle('white-space: normal;')
87			->setId('overrides_expression'),
88		(new CTextBox('overrides_formula', $options['overrides_formula'], $options['templated'],
89				DB::getFieldLength('lld_override', 'formula')))
90			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
91			->setId('overrides_formula')
92			->setAttribute('placeholder', 'A or (B and C) &hellip;')
93	]))->addClass(ZBX_STYLE_CELL)
94]))
95	->addClass(ZBX_STYLE_ROW)
96	->setId('overrideRow');
97
98$filter_table = (new CTable())
99	->setId('overrides_filters')
100	->setAttribute('style', 'width: 100%;')
101	->setHeader([_('Label'), _('Macro'), '', _('Regular expression'), (new CColHeader('Action'))->setWidth('100%')]);
102
103$overrides_filters = $options['overrides_filters'];
104if (!$overrides_filters) {
105	$overrides_filters = [[
106		'macro' => '',
107		'operator' => CONDITION_OPERATOR_REGEXP,
108		'value' => '',
109		'formulaid' => num2letter(0)
110	]];
111}
112else {
113	$overrides_filters = CConditionHelper::sortConditionsByFormulaId($overrides_filters);
114}
115
116foreach ($overrides_filters as $i => $overrides_filter) {
117	$formulaid = [
118		new CSpan($overrides_filter['formulaid']),
119		new CVar('overrides_filters['.$i.'][formulaid]', $overrides_filter['formulaid'])
120	];
121
122	$macro = (new CTextBox('overrides_filters['.$i.'][macro]', $overrides_filter['macro'], $options['templated'],
123			DB::getFieldLength('lld_override_condition', 'macro')))
124		->setWidth(ZBX_TEXTAREA_MACRO_WIDTH)
125		->addClass(ZBX_STYLE_UPPERCASE)
126		->addClass('macro')
127		->setAttribute('placeholder', '{#MACRO}')
128		->setAttribute('data-formulaid', $overrides_filter['formulaid']);
129
130	$value = (new CTextBox('overrides_filters['.$i.'][value]', $overrides_filter['value'],$options['templated'],
131			DB::getFieldLength('lld_override_condition', 'value')))
132		->setWidth(ZBX_TEXTAREA_MACRO_VALUE_WIDTH)
133		->setAttribute('placeholder', _('regular expression'));
134
135	$delete_button_cell = [
136		(new CButton('overrides_filters_'.$i.'_remove', _('Remove')))
137			->addClass(ZBX_STYLE_BTN_LINK)
138			->addClass('element-table-remove')
139			->setEnabled(!$options['templated'])
140	];
141
142	$operator_select = (new CSelect('overrides_filters['.$i.'][operator]'))
143		->setValue($overrides_filter['operator'])
144		->addOption(new CSelectOption(CONDITION_OPERATOR_REGEXP, _('matches')))
145		->addOption(new CSelectOption(CONDITION_OPERATOR_NOT_REGEXP, _('does not match')));
146
147	if ($options['templated']) {
148		$operator_select->setReadonly();
149	}
150
151	$row = [$formulaid, $macro, $operator_select, $value, (new CCol($delete_button_cell))->addClass(ZBX_STYLE_NOWRAP)];
152	$filter_table->addRow($row, 'form_row');
153}
154
155$filter_table->setFooter(new CCol(
156	(new CButton('macro_add', _('Add')))
157		->addClass(ZBX_STYLE_BTN_LINK)
158		->addClass('element-table-add')
159		->setEnabled(!$options['templated'])
160		->removeId()
161));
162
163$overrides_popup_form_list->addRow(_('Filters'),
164	(new CDiv([$override_evaltype, $filter_table]))
165		->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
166		->addStyle('min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
167		->addStyle('width: 100%;')
168);
169
170// operations
171$operations_list = (new CTable())
172	->addClass('lld-overrides-operations-table')
173	->addStyle('width: 100%;')
174	->setHeader([
175		_('Condition'),
176		(new CColHeader(''))->setWidth('50')
177	])
178	->addRow(
179		(new CCol(
180			(new CDiv(
181				(new CButton('param_add', _('Add')))
182					->addClass(ZBX_STYLE_BTN_LINK)
183					->addClass('element-table-add')
184					->setEnabled(!$options['templated'])
185					->removeId()
186			))->addClass('step-action')
187		))
188	);
189
190$overrides_popup_form_list->addRow(_('Operations'),
191	(new CDiv($operations_list))
192		->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
193		->addStyle('min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
194		->addStyle('width: 100%;')
195		->addStyle('max-width: 775px;')
196);
197
198$output['buttons'] = [
199	[
200		'title' => $options['old_name'] ? _('Update') : _('Add'),
201		'class' => '',
202		'keepOpen' => true,
203		'isSubmit' => true,
204		'enabled' => !$options['templated'],
205		'action' => 'return lldoverrides.overrides.edit_form.validate(overlay);'
206	]
207];
208
209$overrides_popup_form->addItem($overrides_popup_form_list);
210
211$output['body'] = (new CDiv($overrides_popup_form))->toString();
212$output['script_inline'] = 'lldoverrides.overrides.onStepOverlayReadyCb('.$options['no'].');';
213
214if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
215	CProfiler::getInstance()->stop();
216	$output['debug'] = CProfiler::getInstance()->make()->toString();
217}
218
219echo json_encode($output);
220