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 * Problems widget form.
24 */
25class CWidgetFormProblems extends CWidgetForm {
26
27	public function __construct($data) {
28		parent::__construct($data, WIDGET_PROBLEMS);
29
30		$this->data = self::convertDottedKeys($this->data);
31
32		// Output information option.
33		$field_show = (new CWidgetFieldRadioButtonList('show', _('Show'), [
34			TRIGGERS_OPTION_RECENT_PROBLEM => _('Recent problems'),
35			TRIGGERS_OPTION_IN_PROBLEM => _('Problems'),
36			TRIGGERS_OPTION_ALL => _('History')
37		]))
38			->setDefault(TRIGGERS_OPTION_RECENT_PROBLEM)
39			->setModern(true);
40
41		if (array_key_exists('show', $this->data)) {
42			$field_show->setValue($this->data['show']);
43		}
44
45		$this->fields[$field_show->getName()] = $field_show;
46
47		// Host groups.
48		$field_groups = new CWidgetFieldGroup('groupids', _('Host groups'));
49
50		if (array_key_exists('groupids', $this->data)) {
51			$field_groups->setValue($this->data['groupids']);
52		}
53
54		$this->fields[$field_groups->getName()] = $field_groups;
55
56		// Exclude host groups.
57		$field_exclude_groups = new CWidgetFieldGroup('exclude_groupids', _('Exclude host groups'));
58
59		if (array_key_exists('exclude_groupids', $this->data)) {
60			$field_exclude_groups->setValue($this->data['exclude_groupids']);
61		}
62
63		$this->fields[$field_exclude_groups->getName()] = $field_exclude_groups;
64
65		// Hosts field.
66		$field_hosts = new CWidgetFieldHost('hostids', _('Hosts'));
67
68		if (array_key_exists('hostids', $this->data)) {
69			$field_hosts->setValue($this->data['hostids']);
70		}
71
72		$this->fields[$field_hosts->getName()] = $field_hosts;
73
74		// Problem field.
75		$field_problem = new CWidgetFieldTextBox('problem', _('Problem'));
76
77		if (array_key_exists('problem', $this->data)) {
78			$field_problem->setValue($this->data['problem']);
79		}
80
81		$this->fields[$field_problem->getName()] = $field_problem;
82
83		// Severity field.
84		$field_severities = new CWidgetFieldSeverities('severities', _('Severity'));
85
86		if (array_key_exists('severities', $this->data)) {
87			$field_severities->setValue($this->data['severities']);
88		}
89
90		$this->fields[$field_severities->getName()] = $field_severities;
91
92		// Tag evalype (And/Or).
93		$field_evaltype = (new CWidgetFieldRadioButtonList('evaltype', _('Tags'), [
94			TAG_EVAL_TYPE_AND_OR => _('And/Or'),
95			TAG_EVAL_TYPE_OR => _('Or')
96		]))
97			->setDefault(TAG_EVAL_TYPE_AND_OR)
98			->setModern(true);
99
100		if (array_key_exists('evaltype', $this->data)) {
101			$field_evaltype->setValue($this->data['evaltype']);
102		}
103
104		$this->fields[$field_evaltype->getName()] = $field_evaltype;
105
106		// Tags array: tag, operator and value. No label, because it belongs to previous group.
107		$field_tags = new CWidgetFieldTags('tags', '');
108
109		if (array_key_exists('tags', $this->data)) {
110			$field_tags->setValue($this->data['tags']);
111		}
112
113		$this->fields[$field_tags->getName()] = $field_tags;
114
115		// Show tags.
116		$field_show_tags = (new CWidgetFieldRadioButtonList('show_tags', _('Show tags'), [
117			PROBLEMS_SHOW_TAGS_NONE => _('None'),
118			PROBLEMS_SHOW_TAGS_1 => PROBLEMS_SHOW_TAGS_1,
119			PROBLEMS_SHOW_TAGS_2 => PROBLEMS_SHOW_TAGS_2,
120			PROBLEMS_SHOW_TAGS_3 => PROBLEMS_SHOW_TAGS_3
121		]))
122			->setDefault(PROBLEMS_SHOW_TAGS_NONE)
123			->setModern(true)
124			->setAction('var disabled = jQuery(this).filter("[value=\''.PROBLEMS_SHOW_TAGS_NONE.'\']").is(":checked");'.
125				'jQuery("#tag_priority").prop("disabled", disabled);'.
126				'jQuery("#tag_name_format input").prop("disabled", disabled)'
127			);
128
129		if (array_key_exists('show_tags', $this->data)) {
130			$field_show_tags->setValue($this->data['show_tags']);
131		}
132
133		$this->fields[$field_show_tags->getName()] = $field_show_tags;
134
135		// Tag name.
136		$tag_format_line = (new CWidgetFieldRadioButtonList('tag_name_format', _('Tag name'), [
137			PROBLEMS_TAG_NAME_FULL => _('Full'),
138			PROBLEMS_TAG_NAME_SHORTENED => _('Shortened'),
139			PROBLEMS_TAG_NAME_NONE => _('None')
140		]))
141			->setDefault(PROBLEMS_TAG_NAME_FULL)
142			->setModern(true);
143
144		if (array_key_exists('tag_name_format', $this->data)) {
145			$tag_format_line->setValue($this->data['tag_name_format']);
146		}
147		$this->fields[$tag_format_line->getName()] = $tag_format_line;
148
149		// Tag display priority.
150		$tag_priority = (new CWidgetFieldTextBox('tag_priority', _('Tag display priority')));
151
152		if (array_key_exists('tag_priority', $this->data)) {
153			$tag_priority->setValue($this->data['tag_priority']);
154		}
155		$this->fields[$tag_priority->getName()] = $tag_priority;
156
157		// Show suppressed problems.
158		$field_show_suppressed = (new CWidgetFieldCheckBox('show_suppressed', _('Show suppressed problems')))
159			->setDefault(ZBX_PROBLEM_SUPPRESSED_FALSE);
160
161		if (array_key_exists('show_suppressed', $this->data)) {
162			$field_show_suppressed->setValue($this->data['show_suppressed']);
163		}
164
165		$this->fields[$field_show_suppressed->getName()] = $field_show_suppressed;
166
167		// Show unacknowledged only.
168		$field_unacknowledged = (new CWidgetFieldCheckBox('unacknowledged', _('Show unacknowledged only')))
169			->setFlags(CWidgetField::FLAG_ACKNOWLEDGES);
170
171		if (array_key_exists('unacknowledged', $this->data)) {
172			$field_unacknowledged->setValue($this->data['unacknowledged']);
173		}
174
175		$this->fields[$field_unacknowledged->getName()] = $field_unacknowledged;
176
177		// Show last values.
178		$field_show_latest_values = (new CWidgetFieldCheckBox('show_latest_values', _('Show latest values')))
179			->setFlags(CWidgetField::FLAG_ACKNOWLEDGES);
180
181		if (array_key_exists('show_latest_values', $this->data)) {
182			$field_show_latest_values->setValue($this->data['show_latest_values']);
183		}
184
185		$this->fields[$field_show_latest_values->getName()] = $field_show_latest_values;
186
187		$sort_with_enabled_show_timeline = [
188			SCREEN_SORT_TRIGGERS_TIME_DESC => true,
189			SCREEN_SORT_TRIGGERS_TIME_ASC => true
190		];
191
192		// Sort entries by.
193		$field_sort = (new CWidgetFieldComboBox('sort_triggers', _('Sort entries by'), [
194			SCREEN_SORT_TRIGGERS_TIME_DESC => _('Time').' ('._('descending').')',
195			SCREEN_SORT_TRIGGERS_TIME_ASC => _('Time').' ('._('ascending').')',
196			SCREEN_SORT_TRIGGERS_SEVERITY_DESC => _('Severity').' ('._('descending').')',
197			SCREEN_SORT_TRIGGERS_SEVERITY_ASC => _('Severity').' ('._('ascending').')',
198			SCREEN_SORT_TRIGGERS_NAME_DESC => _('Problem').' ('._('descending').')',
199			SCREEN_SORT_TRIGGERS_NAME_ASC => _('Problem').' ('._('ascending').')',
200			SCREEN_SORT_TRIGGERS_HOST_NAME_DESC => _('Host').' ('._('descending').')',
201			SCREEN_SORT_TRIGGERS_HOST_NAME_ASC => _('Host').' ('._('ascending').')'
202		]))
203			->setDefault(SCREEN_SORT_TRIGGERS_TIME_DESC)
204			->setAction(
205				'jQuery(":checkbox[name=show_timeline]")'.
206					'.filter(":disabled").prop("checked", true).end()'.
207					'.prop("disabled", !'.CJs::encodeJson($sort_with_enabled_show_timeline).'[this.value])'.
208					'.filter(":disabled").prop("checked", false)'
209			);
210
211		if (array_key_exists('sort_triggers', $this->data)) {
212			$field_sort->setValue($this->data['sort_triggers']);
213		}
214
215		$this->fields[$field_sort->getName()] = $field_sort;
216
217		// Show timeline.
218		$field_show_timeline = (new CWidgetFieldCheckBox('show_timeline', _('Show timeline')))->setDefault(1);
219
220		if (array_key_exists('show_timeline', $this->data)) {
221			$field_show_timeline->setValue($this->data['show_timeline']);
222		}
223
224		if (!array_key_exists($field_sort->getValue(), $sort_with_enabled_show_timeline)) {
225			$field_show_timeline->setFlags(CWidgetField::FLAG_DISABLED);
226		}
227
228		$this->fields[$field_show_timeline->getName()] = $field_show_timeline;
229
230		// Show lines.
231		$field_lines = (new CWidgetFieldIntegerBox('show_lines', _('Show lines'), ZBX_MIN_WIDGET_LINES,
232			ZBX_MAX_WIDGET_LINES
233		))
234			->setFlags(CWidgetField::FLAG_LABEL_ASTERISK)
235			->setDefault(ZBX_DEFAULT_WIDGET_LINES);
236
237		if (array_key_exists('show_lines', $this->data)) {
238			$field_lines->setValue($this->data['show_lines']);
239		}
240
241		$this->fields[$field_lines->getName()] = $field_lines;
242	}
243}
244