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
27<script type="text/x-jquery-tmpl" id="preprocessing-steps-tmpl">
28	<?php
29	$preproc_types_select = (new CSelect('preprocessing[#{rowNum}][type]'))
30		->setId('preprocessing_#{rowNum}_type')
31		->setValue(ZBX_PREPROC_REGSUB)
32		->setWidthAuto();
33
34	foreach (get_preprocessing_types(null, true, $data['preprocessing_types']) as $group) {
35		$opt_group = new CSelectOptionGroup($group['label']);
36
37		foreach ($group['types'] as $type => $label) {
38			$opt_group->addOption(new CSelectOption($type, $label));
39		}
40
41		$preproc_types_select->addOptionGroup($opt_group);
42	}
43
44	echo (new CListItem([
45		(new CDiv([
46			(new CDiv())->addClass(ZBX_STYLE_DRAG_ICON),
47			(new CDiv($preproc_types_select))
48				->addClass('list-numbered-item')
49				->addClass('step-name'),
50			(new CDiv())->addClass('step-parameters'),
51			(new CDiv(new CCheckBox('preprocessing[#{rowNum}][on_fail]')))->addClass('step-on-fail'),
52			(new CDiv([
53				(new CButton('preprocessing[#{rowNum}][test]', _('Test')))
54					->addClass(ZBX_STYLE_BTN_LINK)
55					->addClass('preprocessing-step-test')
56					->removeId(),
57				(new CButton('preprocessing[#{rowNum}][remove]', _('Remove')))
58					->addClass(ZBX_STYLE_BTN_LINK)
59					->addClass('element-table-remove')
60					->removeId()
61			]))->addClass('step-action')
62		]))->addClass('preprocessing-step'),
63		(new CDiv([
64			new CLabel(_('Custom on fail')),
65			(new CRadioButtonList('preprocessing[#{rowNum}][error_handler]', ZBX_PREPROC_FAIL_DISCARD_VALUE))
66				->addValue(_('Discard value'), ZBX_PREPROC_FAIL_DISCARD_VALUE)
67				->addValue(_('Set value to'), ZBX_PREPROC_FAIL_SET_VALUE)
68				->addValue(_('Set error to'), ZBX_PREPROC_FAIL_SET_ERROR)
69				->setModern(true)
70				->setEnabled(false),
71			(new CTextBox('preprocessing[#{rowNum}][error_handler_params]'))
72				->setEnabled(false)
73				->addStyle('display: none;')
74		]))
75			->addClass('on-fail-options')
76			->addStyle('display: none;')
77	]))
78		->addClass('preprocessing-list-item')
79		->addClass('sortable')
80		->setAttribute('data-step', '#{rowNum}');
81	?>
82</script>
83
84<script type="text/x-jquery-tmpl" id="preprocessing-steps-parameters-single-tmpl">
85	<?= (new CTextBox('preprocessing[#{rowNum}][params][0]', ''))->setAttribute('placeholder', '#{placeholder}') ?>
86</script>
87
88<script type="text/x-jquery-tmpl" id="preprocessing-steps-parameters-double-tmpl">
89	<?= (new CTextBox('preprocessing[#{rowNum}][params][0]', ''))->setAttribute('placeholder', '#{placeholder_0}').
90			(new CTextBox('preprocessing[#{rowNum}][params][1]', ''))->setAttribute('placeholder', '#{placeholder_1}')
91	?>
92</script>
93
94<script type="text/x-jquery-tmpl" id="preprocessing-steps-parameters-multiline-tmpl">
95	<?= (new CMultilineInput('preprocessing[#{rowNum}][params][0]', '', ['add_post_js' => false])) ?>
96</script>
97
98<script type="text/x-jquery-tmpl" id="preprocessing-steps-parameters-custom-width-chkbox-tmpl">
99	<?= (new CTextBox('preprocessing[#{rowNum}][params][0]', ''))
100			->setAttribute('placeholder', '#{placeholder_0}')
101			->setWidth('#{width_0}')
102			->setAttribute('maxlength', 1).
103		(new CTextBox('preprocessing[#{rowNum}][params][1]', ''))
104			->setAttribute('placeholder', '#{placeholder_1}')
105			->setWidth('#{width_1}')
106			->setAttribute('maxlength', 1).
107		(new CCheckBox('preprocessing[#{rowNum}][params][2]', '#{chkbox_value}'))
108			->setLabel('#{chkbox_label}')
109			->setChecked('#{chkbox_default}')
110	?>
111</script>
112
113<script type="text/javascript">
114	jQuery(function($) {
115		function makeParameterInput(index, type) {
116			var preproc_param_single_tmpl = new Template($('#preprocessing-steps-parameters-single-tmpl').html()),
117				preproc_param_double_tmpl = new Template($('#preprocessing-steps-parameters-double-tmpl').html()),
118				preproc_param_custom_width_chkbox_tmpl =
119					new Template($('#preprocessing-steps-parameters-custom-width-chkbox-tmpl').html()),
120				preproc_param_multiline_tmpl = new Template($('#preprocessing-steps-parameters-multiline-tmpl').html());
121
122			switch (type) {
123				case '<?= ZBX_PREPROC_MULTIPLIER ?>':
124					return $(preproc_param_single_tmpl.evaluate({
125						rowNum: index,
126						placeholder: <?= json_encode(_('number')) ?>
127					})).css('width', <?= ZBX_TEXTAREA_NUMERIC_BIG_WIDTH ?>);
128
129				case '<?= ZBX_PREPROC_RTRIM ?>':
130				case '<?= ZBX_PREPROC_LTRIM ?>':
131				case '<?= ZBX_PREPROC_TRIM ?>':
132					return $(preproc_param_single_tmpl.evaluate({
133						rowNum: index,
134						placeholder: <?= json_encode(_('list of characters')) ?>
135					})).css('width', <?= ZBX_TEXTAREA_SMALL_WIDTH ?>);
136
137				case '<?= ZBX_PREPROC_XPATH ?>':
138				case '<?= ZBX_PREPROC_ERROR_FIELD_XML ?>':
139					return $(preproc_param_single_tmpl.evaluate({
140						rowNum: index,
141						placeholder: <?= json_encode(_('XPath')) ?>
142					}));
143
144				case '<?= ZBX_PREPROC_JSONPATH ?>':
145				case '<?= ZBX_PREPROC_ERROR_FIELD_JSON ?>':
146					return $(preproc_param_single_tmpl.evaluate({
147						rowNum: index,
148						placeholder: <?= json_encode(_('$.path.to.node')) ?>
149					}));
150
151				case '<?= ZBX_PREPROC_REGSUB ?>':
152				case '<?= ZBX_PREPROC_ERROR_FIELD_REGEX ?>':
153					return $(preproc_param_double_tmpl.evaluate({
154						rowNum: index,
155						placeholder_0: <?= json_encode(_('pattern')) ?>,
156						placeholder_1: <?= json_encode(_('output')) ?>
157					}));
158
159				case '<?= ZBX_PREPROC_VALIDATE_RANGE ?>':
160					return $(preproc_param_double_tmpl.evaluate({
161						rowNum: index,
162						placeholder_0: <?= json_encode(_('min')) ?>,
163						placeholder_1: <?= json_encode(_('max')) ?>
164					}));
165
166				case '<?= ZBX_PREPROC_VALIDATE_REGEX ?>':
167				case '<?= ZBX_PREPROC_VALIDATE_NOT_REGEX ?>':
168					return $(preproc_param_single_tmpl.evaluate({
169						rowNum: index,
170						placeholder: <?= json_encode(_('pattern')) ?>
171					}));
172
173				case '<?= ZBX_PREPROC_THROTTLE_TIMED_VALUE ?>':
174					return $(preproc_param_single_tmpl.evaluate({
175						rowNum: index,
176						placeholder: <?= json_encode(_('seconds')) ?>
177					})).css('width', <?= ZBX_TEXTAREA_NUMERIC_BIG_WIDTH ?>);
178
179				case '<?= ZBX_PREPROC_SCRIPT ?>':
180					return $(preproc_param_multiline_tmpl.evaluate({rowNum: index})).multilineInput({
181						title: <?= json_encode(_('JavaScript')) ?>,
182						placeholder: <?= json_encode(_('script')) ?>,
183						placeholder_textarea: 'return value',
184						label_before: 'function (value) {',
185						label_after: '}',
186						grow: 'auto',
187						rows: 0,
188						maxlength: <?= $data['preprocessing_script_maxlength'] ?>
189					});
190
191				case '<?= ZBX_PREPROC_PROMETHEUS_PATTERN ?>':
192					return $(preproc_param_double_tmpl.evaluate({
193						rowNum: index,
194						placeholder_0: <?= json_encode(
195							_('<metric name>{<label name>="<label value>", ...} == <value>')
196						) ?>,
197						placeholder_1: <?= json_encode(_('<label name>')) ?>
198					}));
199
200				case '<?= ZBX_PREPROC_PROMETHEUS_TO_JSON ?>':
201					return $(preproc_param_single_tmpl.evaluate({
202						rowNum: index,
203						placeholder: <?= json_encode(
204							_('<metric name>{<label name>="<label value>", ...} == <value>')
205						) ?>
206					}));
207
208				case '<?= ZBX_PREPROC_CSV_TO_JSON ?>':
209					return $(preproc_param_custom_width_chkbox_tmpl.evaluate({
210						rowNum: index,
211						width_0: <?= ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH ?>,
212						width_1: <?= ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH ?>,
213						placeholder_0: ',',
214						placeholder_1: '"',
215						chkbox_label: <?= json_encode(_('With header row')) ?>,
216						chkbox_value: <?= ZBX_PREPROC_CSV_HEADER ?>,
217						chkbox_default: true
218					}));
219
220				case '<?= ZBX_PREPROC_STR_REPLACE ?>':
221					return $(preproc_param_double_tmpl.evaluate({
222						rowNum: index,
223						placeholder_0: <?= json_encode(_('search string')) ?>,
224						placeholder_1: <?= json_encode(_('replacement')) ?>
225					}));
226
227				default:
228					return '';
229			}
230		}
231
232		var $preprocessing = $('#preprocessing'),
233			step_index = $preprocessing.find('li.sortable').length;
234
235		$preprocessing.sortable({
236			disabled: $preprocessing.find('div.<?= ZBX_STYLE_DRAG_ICON ?>').hasClass('<?= ZBX_STYLE_DISABLED ?>'),
237			items: 'li.sortable',
238			axis: 'y',
239			containment: 'parent',
240			cursor: 'grabbing',
241			handle: 'div.<?= ZBX_STYLE_DRAG_ICON ?>',
242			tolerance: 'pointer',
243			opacity: 0.6
244		});
245
246		$preprocessing
247			.on('click', '.element-table-add', function() {
248				var preproc_row_tmpl = new Template($('#preprocessing-steps-tmpl').html()),
249					$row = $(preproc_row_tmpl.evaluate({rowNum: step_index}));
250					type = $('z-select[name*="type"]', $row).val();
251
252				$('.step-parameters', $row).html(makeParameterInput(step_index, type));
253				$(this).closest('.preprocessing-list-foot').before($row);
254
255				$('.preprocessing-list-head').show();
256
257				var sortable_count = $preprocessing.find('li.sortable').length;
258
259				if (sortable_count == 1) {
260					$('#preproc_test_all').show();
261					$preprocessing
262						.sortable('disable')
263						.find('div.<?= ZBX_STYLE_DRAG_ICON ?>').addClass('<?= ZBX_STYLE_DISABLED ?>');
264				}
265				else if (sortable_count > 1) {
266					$preprocessing
267						.sortable('enable')
268						.find('div.<?= ZBX_STYLE_DRAG_ICON ?>').removeClass('<?= ZBX_STYLE_DISABLED ?>');
269				}
270
271				step_index++;
272			})
273			.on('click', '#preproc_test_all', function() {
274				var step_nums = [];
275				$('z-select[name^="preprocessing"][name$="[type]"]', $preprocessing).each(function() {
276					var str = $(this).attr('name');
277					step_nums.push(str.substr(14, str.length - 21));
278				});
279
280				openItemTestDialog(step_nums, true, false, this, -1);
281			})
282			.on('click', '.preprocessing-step-test', function() {
283				var str = $(this).attr('name'),
284					step_nr = $(this).attr('data-step'),
285					num = str.substr(14, str.length - 21);
286
287				openItemTestDialog([num], false, false, this, num);
288			})
289			.on('click', '.element-table-remove', function() {
290				$(this).closest('li.sortable').remove();
291
292				var sortable_count = $preprocessing.find('li.sortable').length;
293
294				if (sortable_count == 0) {
295					$('#preproc_test_all').hide();
296					$('.preprocessing-list-head').hide();
297				}
298				else if (sortable_count == 1) {
299					$preprocessing
300						.sortable('disable')
301						.find('div.<?= ZBX_STYLE_DRAG_ICON ?>').addClass('<?= ZBX_STYLE_DISABLED ?>');
302				}
303			})
304			.on('change', 'z-select[name*="type"]', function() {
305				var $row = $(this).closest('.preprocessing-list-item'),
306					type = $(this).val(),
307					$on_fail = $row.find('[name*="on_fail"]');
308
309				$('.step-parameters', $row).html(makeParameterInput($row.data('step'), type));
310
311				// Disable "Custom on fail" for some of the preprocessing types.
312				switch (type) {
313					case '<?= ZBX_PREPROC_RTRIM ?>':
314					case '<?= ZBX_PREPROC_LTRIM ?>':
315					case '<?= ZBX_PREPROC_TRIM ?>':
316					case '<?= ZBX_PREPROC_THROTTLE_VALUE ?>':
317					case '<?= ZBX_PREPROC_THROTTLE_TIMED_VALUE ?>':
318					case '<?= ZBX_PREPROC_SCRIPT ?>':
319					case '<?= ZBX_PREPROC_STR_REPLACE ?>':
320						$on_fail
321							.prop('checked', false)
322							.prop('disabled', true)
323							.trigger('change');
324						break;
325
326					default:
327						$on_fail.prop('disabled', false);
328						break;
329				}
330			})
331			.on('change', 'input[type="text"][name*="params"]', function() {
332				$(this).attr('title', $(this).val());
333			})
334			.on('change', 'input[name*="on_fail"]', function() {
335				var $on_fail_options = $(this).closest('.preprocessing-list-item').find('.on-fail-options');
336
337				if ($(this).is(':checked')) {
338					$on_fail_options.find('input').prop('disabled', false);
339					$on_fail_options.show();
340				}
341				else {
342					$on_fail_options.find('input').prop('disabled', true);
343					$on_fail_options.hide();
344				}
345			})
346			.on('change', 'input[name*="error_handler]"]', function() {
347				var error_handler = $(this).val(),
348					$error_handler_params = $(this).closest('.on-fail-options').find('[name*="error_handler_params"]');
349
350				if (error_handler == '<?= ZBX_PREPROC_FAIL_DISCARD_VALUE ?>') {
351					$error_handler_params
352						.prop('disabled', true)
353						.hide();
354				}
355				else if (error_handler == '<?= ZBX_PREPROC_FAIL_SET_VALUE ?>') {
356					$error_handler_params
357						.prop('disabled', false)
358						.attr('placeholder', <?= json_encode(_('value')) ?>)
359						.show();
360				}
361				else if (error_handler == '<?= ZBX_PREPROC_FAIL_SET_ERROR ?>') {
362					$error_handler_params
363						.prop('disabled', false)
364						.attr('placeholder', <?= json_encode(_('error message')) ?>)
365						.show();
366				}
367			});
368	});
369</script>
370