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// Create form.
23$expression_form = (new CForm())
24	->cleanItems()
25	->setName('expression')
26	->addVar('action', 'popup.triggerexpr')
27	->addVar('dstfrm', $data['dstfrm'])
28	->addVar('dstfld1', $data['dstfld1'])
29	->addItem((new CVar('hostid', $data['hostid']))->removeId())
30	->addVar('groupid', $data['groupid'])
31	->addVar('itemid', $data['itemid'])
32	->addItem((new CInput('submit', 'submit'))
33		->addStyle('display: none;')
34		->removeId()
35	);
36
37if ($data['parent_discoveryid'] !== '') {
38	$expression_form->addVar('parent_discoveryid', $data['parent_discoveryid']);
39}
40
41// Create form list.
42$expression_form_list = new CFormList();
43
44// Append item to form list.
45$popup_options = [
46	'srctbl' => 'items',
47	'srcfld1' => 'itemid',
48	'srcfld2' => 'name',
49	'dstfrm' => $expression_form->getName(),
50	'dstfld1' => 'itemid',
51	'dstfld2' => 'item_description',
52	'with_webitems' => '1',
53	'writeonly' => '1'
54];
55if ($data['groupid'] && $data['hostid']) {
56	$popup_options['groupid'] = $data['groupid'];
57	$popup_options['hostid'] = $data['hostid'];
58}
59if ($data['parent_discoveryid'] !== '') {
60	$popup_options['normal_only'] = '1';
61}
62
63$item = [
64	(new CTextBox('item_description', $data['item_description'], true))
65		->setAriaRequired()
66		->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH),
67	(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
68	(new CButton('select', _('Select')))
69		->addClass(ZBX_STYLE_BTN_GREY)
70		->onClick('return PopUp("popup.generic",'.CJs::encodeJson($popup_options).', null, this);')
71];
72
73if ($data['parent_discoveryid'] !== '') {
74	$item[] = (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN);
75	$item[] = (new CButton('select', _('Select prototype')))
76		->addClass(ZBX_STYLE_BTN_GREY)
77		->onClick('return PopUp("popup.generic",'.
78			CJs::encodeJson([
79				'srctbl' => 'item_prototypes',
80				'srcfld1' => 'itemid',
81				'srcfld2' => 'name',
82				'dstfrm' => $expression_form->getName(),
83				'dstfld1' => 'itemid',
84				'dstfld2' => 'item_description',
85				'parent_discoveryid' => $data['parent_discoveryid']
86			]).', null, this);'
87		)
88		->removeId();
89}
90
91$expression_form_list->addRow((new CLabel(_('Item'), 'item_description'))->setAsteriskMark(), $item);
92
93$function_combo_box = new CComboBox('function', $data['function'], 'reloadPopup(this.form, "popup.triggerexpr")');
94foreach ($data['functions'] as $id => $f) {
95	$function_combo_box->addItem($id, $f['description']);
96}
97$expression_form_list->addRow(_('Function'), $function_combo_box);
98
99if (array_key_exists('params', $data['functions'][$data['selectedFunction']])) {
100	$paramid = 0;
101
102	foreach ($data['functions'][$data['selectedFunction']]['params'] as $param_name => $param_function) {
103		if (array_key_exists($param_name, $data['params'])) {
104			$param_value = $data['params'][$param_name];
105		}
106		else {
107			$param_value = array_key_exists($paramid, $data['params']) ? $data['params'][$paramid] : null;
108		}
109
110		$label = $param_function['A'] ? (new CLabel($param_function['C']))->setAsteriskMark() : $param_function['C'];
111
112		if ($param_function['T'] == T_ZBX_INT) {
113			$param_type_element = null;
114
115			if (in_array($param_name, ['last'])) {
116				if (array_key_exists('M', $param_function)) {
117					if (in_array($data['selectedFunction'], ['last', 'band', 'strlen'])) {
118						$param_type_element = $param_function['M'][PARAM_TYPE_COUNTS];
119						$label = $param_function['C'];
120						$expression_form->addItem((new CVar('paramtype', PARAM_TYPE_COUNTS))->removeId());
121					}
122					else {
123						$param_type_element = new CComboBox('paramtype',
124							$param_value === '' ? PARAM_TYPE_TIME : $data['paramtype'],
125							null, $param_function['M']
126						);
127					}
128				}
129				else {
130					$expression_form->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId());
131					$param_type_element = _('Time');
132				}
133			}
134			elseif (in_array($param_name, ['shift'])) {
135				$param_type_element = _('Time');
136			}
137
138			$param_field = (new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH);
139
140			$expression_form_list->addRow($label, [
141				$param_field,
142				(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
143				$param_type_element
144			]);
145		}
146		else {
147			$expression_form_list->addRow($label,
148				(new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
149			);
150			if ($paramid === 0) {
151				$expression_form->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId());
152			}
153		}
154
155		$paramid++;
156	}
157}
158else {
159	$expression_form->addVar('paramtype', PARAM_TYPE_TIME);
160}
161
162$expression_form_list->addRow(
163	(new CLabel(_('Result'), 'value'))->setAsteriskMark(), [
164		new CComboBox('operator', $data['operator'], null,
165			array_combine($data['functions'][$data['function']]['operators'],
166				$data['functions'][$data['function']]['operators']
167			)
168		),
169		' ',
170		(new CTextBox('value', $data['value']))
171			->setAriaRequired()
172			->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
173	]
174);
175
176$expression_form->addItem($expression_form_list);
177
178$output = [
179	'header' => $data['title'],
180	'body' => (new CDiv([$data['errors'], $expression_form]))->toString(),
181	'buttons' => [
182		[
183			'title' => _('Insert'),
184			'class' => '',
185			'keepOpen' => true,
186			'isSubmit' => true,
187			'action' => 'return validate_trigger_expression("expression", '.
188					'jQuery(window.document.forms["expression"]).closest("[data-dialogueid]").attr("data-dialogueid"));'
189		]
190	],
191	'script_inline' =>
192		'jQuery(function($) {'.
193			'$.valHooks.input = {'.
194				'get: function(elem) {'.
195					'return elem.value;'.
196				'},'.
197				'set: function(elem, value) {'.
198					'var tmp = elem.value;'.
199						'elem.value = value;'.
200
201					'"item_description" === elem.id && tmp !== value && reloadPopup(elem.form, "popup.triggerexpr");'.
202				'}'.
203			'};'.
204		'});'
205];
206
207if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
208	CProfiler::getInstance()->stop();
209	$output['debug'] = CProfiler::getInstance()->make()->toString();
210}
211
212echo (new CJson())->encode($output);
213