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$options = $data['options'];
27$output = [
28	'header' => $data['title'],
29	'script_inline' => $this->readJsFile('popup.triggerwizard.js.php')
30];
31
32// SID for this form is added by JS getUrl() and "form_refresh" is not used at all.
33$form = (new CForm('post', 'zabbix.php'))
34	->cleanItems()
35	->setName('sform')
36	->addVar('action', 'popup.triggerwizard')
37	->addItem((new CInput('submit', 'submit'))->addStyle('display: none;'));
38
39if (array_key_exists('triggerid', $options)) {
40	$form->addVar('triggerid', $options['triggerid']);
41}
42
43$expression_table = (new CTable())
44	->addClass('ui-sortable')
45	->setId('expressions_list')
46	->setAttribute('style', 'width: 100%;')
47	->setHeader(['', _('Expression'), _('Type'), _('Action')]);
48
49$expressions = [];
50
51foreach ($data['expressions'] as $expr) {
52	$expressions[] = [
53		'expression' => $expr['value'],
54		'type_label' => $expr['type'] == CTextTriggerConstructor::EXPRESSION_TYPE_MATCH ? _('Include') : _('Exclude'),
55		'type' => $expr['type']
56	];
57}
58
59$output['script_inline'] = 'jQuery("#'.$expression_table->getId().'").data("rows", '.json_encode($expressions).');'
60	.$output['script_inline'];
61
62$ms_itemid = (new CMultiSelect([
63	'name' => 'itemid',
64	'object_name' => 'items',
65	'multiple' => false,
66	'data' => [['id' => $options['itemid'], 'name' => $options['item_name']]],
67	'popup' => [
68		'parameters' => [
69			'srctbl' => 'items',
70			'srcfld1' => 'itemid',
71			'dstfrm' => $form->getName(),
72			'dstfld1' => 'itemid',
73			'value_types' => [ITEM_VALUE_TYPE_STR, ITEM_VALUE_TYPE_LOG, ITEM_VALUE_TYPE_TEXT]
74		]
75	]
76]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH);
77
78$form->addItem(
79	(new CFormList())
80		->addRow(
81			(new CLabel(_('Name'), 'description'))->setAsteriskMark(),
82			(new CTextBox('description', $options['description']))
83				->setAriaRequired()
84				->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
85		)
86		->addRow(
87			new CLabel(_('Event name'), 'event_name'),
88			(new CTextAreaFlexible('event_name', $options['event_name']))
89				->setMaxlength(DB::getFieldLength('triggers', 'event_name'))
90				->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
91		)
92		->addRow((new CLabel(_('Item'), 'itemid'))->setAsteriskMark(), $ms_itemid)
93		->addRow(_('Severity'), new CSeverity([
94			'name' => 'priority',
95			'value' => (int) $options['priority']
96		]))
97		->addRow((new CLabel(_('Expression'), 'logexpr'))->setAsteriskMark(),
98			(new CTextBox('expression'))
99				->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
100				->setId('logexpr')
101		)
102		->addRow(null, [
103			(new CCheckBox('iregexp'))->setLabel('iregexp'),
104			(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
105			(new CButton('add_key_and', _('AND')))->addClass(ZBX_STYLE_BTN_GREY),
106			(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
107			(new CButton('add_key_or', _('OR')))->addClass(ZBX_STYLE_BTN_GREY),
108			(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
109			(new CSelect('expr_type'))
110				->addOptions(CSelect::createOptionsFromArray([
111					CTextTriggerConstructor::EXPRESSION_TYPE_MATCH => _('Include'),
112					CTextTriggerConstructor::EXPRESSION_TYPE_NO_MATCH => _('Exclude')
113				]))
114				->setId('expr_type'),
115			(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
116			(new CButton('add_exp', _('Add')))->addClass(ZBX_STYLE_BTN_GREY)
117		])
118		->addRow(null,
119			(new CDiv((new CTable())
120				->setId('key_list')
121				->setAttribute('style', 'width: 100%;')
122				->setHeader([
123					_('Keyword'),
124					_('Type'),
125					_('Action')
126				])
127			))
128				->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
129				->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
130		)
131		->addRow(null,
132			(new CDiv($expression_table))
133				->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
134				->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
135		)
136		->addRow(_('URL'), (new CTextBox('url', $options['url']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH))
137		->addRow(_('Description'),
138			(new CTextArea('comments', $options['comments']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
139		)
140		->addRow(_('Enabled'),
141			(new CCheckBox('status'))->setChecked($options['status'] == TRIGGER_STATUS_ENABLED)
142		)
143);
144
145$output['script_inline'] .= $ms_itemid->getPostJS();
146
147$output['body'] = $form->toString();
148
149$output['buttons'] = [[
150	'title' => array_key_exists('triggerid', $options) ? _('Update') : _('Add'),
151	'class' => '',
152	'keepOpen' => true,
153	'isSubmit' => true,
154	'action' => 'return validateTriggerWizard(overlay);'
155]];
156
157if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
158	CProfiler::getInstance()->stop();
159	$output['debug'] = CProfiler::getInstance()->make()->toString();
160}
161
162echo json_encode($output);
163