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(function ($) {
28	var trigger_row_tmpl = new Template($('#tmpl_expressions_list_row').html()),
29		expr_part_row_tmpl = new Template($('#tmpl_expressions_part_list_row').html()),
30		$expr_table = $('#expressions_list'),
31		$expr_parts_table = $('#key_list'),
32		$expr_input = $('#logexpr'),
33		$iregexp_checkbox = $('#iregexp'),
34		$and_button = $('#add_key_and'),
35		$or_button = $('#add_key_or'),
36		expr_type_select = $('z-select#expr_type').get(0),
37		$add_button = $('#add_exp'),
38		data = $expr_table.data('rows')||[];
39
40	// Expression parts table.
41	$expr_parts_table.on('click', '.<?= ZBX_STYLE_BTN_LINK ?>', function () {
42		var row = $(this).closest('tr');
43
44		if (!row.siblings().length) {
45			$and_button.prop('disabled', false);
46			$or_button.prop('disabled', false);
47		}
48
49		row.remove();
50	});
51
52	// Button AND, OR click handler.
53	$and_button.on('click', addKeywordButtonsClick);
54	$or_button.on('click', addKeywordButtonsClick);
55
56	// Expression sortable table rows initialization.
57	if (data) {
58		data.forEach(function (row_data) {
59			$expr_table.find('tbody').append(trigger_row_tmpl.evaluate(row_data));
60		});
61
62		if (data.length == 1) {
63			$expr_table.find('td.<?= ZBX_STYLE_TD_DRAG_ICON ?>').addClass('<?= ZBX_STYLE_DISABLED ?>');
64		}
65	}
66
67	// Expression sortable table.
68	$expr_table.sortable({
69		disabled: data.length < 2,
70		items: 'tbody tr.sortable',
71		axis: 'y',
72		cursor: 'grabbing',
73		handle: 'div.<?= ZBX_STYLE_DRAG_ICON ?>',
74		containment: '#expressions_list tbody',
75		tolerance: 'pointer',
76		opacity: 0.6
77	}).on('click', '.<?= ZBX_STYLE_BTN_LINK ?>', function() {
78		var row = $(this).closest('tr');
79
80		if (row.siblings().length == 1) {
81			$expr_table.sortable('disable');
82			$expr_table.find('td.<?= ZBX_STYLE_TD_DRAG_ICON ?>').addClass('<?= ZBX_STYLE_DISABLED ?>');
83		}
84
85		row.remove();
86	});
87
88	// Button Add click handler.
89	$add_button.on('click', function () {
90		var expression = [],
91			$inputs = $('[name^="keys["]'),
92			$keywords = $inputs.filter('[name$="[value]"]');
93
94		$inputs.filter('[name$="[type]"]').each(function (i, el) {
95			expression.push(el.value + '(' + $keywords[i].value + ')');
96			$(el).closest('tr').remove();
97		});
98
99		if ($expr_input.val() != '') {
100			expression.push(($iregexp_checkbox.is(':checked') ? 'i' : '') + 'regexp(' + $expr_input.val() + ')');
101			$expr_input.val('');
102		}
103
104		if ($expr_table.find('tbody > tr').length > 0) {
105			$expr_table.sortable('enable');
106		}
107
108		if (expression.length) {
109			const {label, value} = expr_type_select.getOptionByIndex(expr_type_select.selectedIndex);
110			$expr_table.find('tbody').append(trigger_row_tmpl.evaluate({
111				expression: expression.join($and_button.is(':enabled') ? ' and ' : ' or '),
112				type_label: label,
113				type: value
114			}));
115
116			var $icons = $expr_table.find('tbody td.<?= ZBX_STYLE_TD_DRAG_ICON ?>');
117			$icons.toggleClass('<?= ZBX_STYLE_DISABLED ?>', $icons.length == 1);
118
119			$and_button.prop('disabled', false);
120			$or_button.prop('disabled', false);
121		}
122	});
123
124	/**
125	 * Click handler for 'AND' and 'OR' buttons.
126	 */
127	function addKeywordButtonsClick() {
128		if ($expr_input.val() == '') {
129			return;
130		}
131
132		$expr_parts_table.find('tbody').append(expr_part_row_tmpl.evaluate({
133			keyword: $expr_input.val(),
134			type_label: ($iregexp_checkbox.is(':checked') ? 'i' : '') + 'regexp'
135		}));
136
137		if ($(this).is($and_button)) {
138			$or_button.prop('disabled', true);
139		}
140		else {
141			$and_button.prop('disabled', true);
142		}
143
144		$expr_input.val('');
145	}
146
147	$('#event_name')
148		.textareaFlexible()
149		.on('input keydown paste', function() {
150			overlays_stack.end().centerDialog();
151		});
152})(jQuery);
153
154/**
155 * Submit trigger wizard form to save.
156 *
157 * @param {Overlay} overlay
158 */
159function validateTriggerWizard(overlay) {
160	var $form = overlay.$dialogue.find('form'),
161		url = new Curl($form.attr('action'));
162
163	$form.trimValues(['#description', '#logexpr']);
164
165	url.setArgument('save', 1);
166
167	overlay.setLoading();
168	overlay.xhr = jQuery.ajax({
169		url: url.getUrl(),
170		data: $form.serialize(),
171		complete: function() {
172			overlay.unsetLoading();
173		},
174		success: function(ret) {
175			overlay.$dialogue.find('.<?= ZBX_STYLE_MSG_BAD ?>, .<?= ZBX_STYLE_MSG_GOOD ?>').remove();
176
177			if (typeof ret.errors !== 'undefined') {
178				jQuery(ret.errors).insertBefore($form);
179			}
180			else {
181				overlayDialogueDestroy(overlay.dialogueid);
182				window.location.replace(window.location.href);
183			}
184		},
185		dataType: 'json',
186		type: 'POST'
187	});
188}
189