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