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
22require_once dirname(__FILE__).'/js/configuration.triggers.expression.js.php';
23
24$expressionWidget = new CWidget();
25
26// create form
27$expressionForm = (new CForm())
28	->setName('expression')
29	->addVar('dstfrm', $this->data['dstfrm'])
30	->addVar('dstfld1', $this->data['dstfld1'])
31	->addVar('itemid', $this->data['itemid']);
32
33if (!empty($this->data['parent_discoveryid'])) {
34	$expressionForm->addVar('parent_discoveryid', $this->data['parent_discoveryid']);
35}
36
37// create form list
38$expressionFormList = new CFormList();
39
40// append item to form list
41$item = [
42	(new CTextBox('description', $this->data['description'], true))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH),
43	(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
44	(new CButton('select', _('Select')))
45		->addClass(ZBX_STYLE_BTN_GREY)
46		->onClick('return PopUp(\'popup.php?writeonly=1&dstfrm='.$expressionForm->getName().
47			(($data['groupid'] && $data['hostid'])
48				? '&groupid='.$data['groupid'].'&hostid='.$data['hostid']
49				: ''
50			).
51			'&dstfld1=itemid&dstfld2=description&submitParent=1'.(!empty($this->data['parent_discoveryid']) ? '&normal_only=1' : '').
52			'&srctbl=items&srcfld1=itemid&srcfld2=name\', 0, 0, \'zbx_popup_item\');')
53];
54
55if (!empty($this->data['parent_discoveryid'])) {
56	$item[] = (new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN);
57	$item[] = (new CButton('select', _('Select prototype')))
58		->addClass(ZBX_STYLE_BTN_GREY)
59		->onClick('return PopUp(\'popup.php?dstfrm='.$expressionForm->getName().
60			'&dstfld1=itemid&dstfld2=description&submitParent=1'.url_param('parent_discoveryid', true).
61			'&srctbl=item_prototypes&srcfld1=itemid&srcfld2=name\', 0, 0, \'zbx_popup_item\');');
62}
63
64$expressionFormList->addRow(_('Item'), $item);
65
66$function_combobox = new CComboBox('function', $data['function']);
67
68foreach ($data['functions'] as $id => $function) {
69	$function_combobox->addItem($id, $function['description']);
70}
71
72$expressionFormList->addRow(_('Function'), $function_combobox);
73
74if (array_key_exists('params', $data['functions'][$data['selectedFunction']])) {
75	$paramid = 0;
76
77	foreach ($data['functions'][$data['selectedFunction']]['params'] as $param_name => $param_function) {
78		if (array_key_exists($param_name, $data['params'])) {
79			$param_value = $data['params'][$param_name];
80		}
81		else {
82			$param_value = array_key_exists($paramid, $data['params']) ? $data['params'][$paramid] : null;
83		}
84
85		if ($param_function['T'] == T_ZBX_INT) {
86			$param_type_element = null;
87
88			if (in_array($param_name, ['last'])) {
89				if ($data['paramtype'] == PARAM_TYPE_COUNTS && $param_value !== null && $param_value !== ''
90						&& $param_value[0] === '#' && $data['is_valid']) {
91					$param_value = substr($param_value, 1);
92				}
93				if (array_key_exists('M', $param_function)) {
94					if (in_array($data['selectedFunction'], ['last', 'band', 'strlen'])) {
95						$param_type_element = $param_function['M'][PARAM_TYPE_COUNTS];
96						$expressionFormList->addItem(new CVar('paramtype', PARAM_TYPE_COUNTS, 'spec_paramtype'));
97					}
98					else {
99						$param_type_element = new CComboBox('paramtype', $data['paramtype'], null,
100							$param_function['M']
101						);
102					}
103				}
104				else {
105					$expressionForm->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId());
106					$param_type_element = _('Time');
107				}
108			}
109			elseif (in_array($param_name, ['shift'])) {
110				$param_type_element = _('Time');
111			}
112
113			$param_field = (new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH);
114
115			$expressionFormList->addRow($param_function['C'], [
116				$param_field,
117				(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
118				$param_type_element
119			]);
120		}
121		else {
122			$expressionFormList->addRow($param_function['C'],
123				(new CTextBox('params['.$param_name.']', $param_value))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
124			);
125			if ($paramid === 0) {
126				$expressionForm->addItem((new CVar('paramtype', PARAM_TYPE_TIME))->removeId());
127			}
128		}
129
130		$paramid++;
131	}
132}
133else {
134	$expressionForm->addVar('paramtype', PARAM_TYPE_TIME);
135}
136
137$expressionFormList->addRow(_('Result'), [
138		new CComboBox('operator', $data['operator'], null,
139			array_combine($data['functions'][$data['function']]['operators'],
140				$data['functions'][$data['function']]['operators']
141			)
142		),
143		' ',
144		(new CTextBox('value', $data['value']))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
145	]
146);
147
148// append tabs to form
149$expressionTab = (new CTabView())->addTab('expressionTab', _('Trigger expression condition'), $expressionFormList);
150
151// append buttons to form
152$expressionTab->setFooter(makeFormFooter(
153	new CSubmit('insert', _('Insert')),
154	[new CButtonCancel(url_params(['parent_discoveryid', 'dstfrm', 'dstfld1']))
155]));
156
157$expressionForm->addItem($expressionTab);
158$expressionWidget->addItem($expressionForm);
159
160return $expressionWidget;
161