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$output = [];
23
24if (($messages = getMessages()) !== null) {
25	$output['messages'] = $messages->toString();
26}
27
28if (array_key_exists('problems', $data)) {
29	// Show trigger description and URL.
30	$div = new CDiv();
31
32	if ($data['trigger']['comments'] !== '') {
33		$div->addItem(
34			(new CDiv())
35				->addItem(zbx_str2links($data['trigger']['comments']))
36				->addClass(ZBX_STYLE_OVERLAY_DESCR)
37				->addStyle('max-width: 500px')
38		);
39	}
40
41	if ($data['trigger']['url'] !== '') {
42		$trigger_url = CHtmlUrlValidator::validate($data['trigger']['url'], ['allow_user_macro' => false])
43			? $data['trigger']['url']
44			: 'javascript: alert(\''._s('Provided URL "%1$s" is invalid.',
45				json_encode($data['trigger']['url'])).
46			'\');';
47
48		$div->addItem(
49			(new CDiv())
50				->addItem(new CLink(CHtml::encode($data['trigger']['url']), $trigger_url))
51				->addClass(ZBX_STYLE_OVERLAY_DESCR_URL)
52				->addStyle('max-width: 500px')
53		);
54	}
55
56	// sort field indicator
57	$sort_div = (new CSpan())->addClass(ZBX_STYLE_ARROW_DOWN);
58
59	if ($data['show_timeline']) {
60		$header = [
61			(new CColHeader([_('Time'), $sort_div]))->addClass(ZBX_STYLE_RIGHT),
62			(new CColHeader())->addClass(ZBX_STYLE_TIMELINE_TH),
63			(new CColHeader())->addClass(ZBX_STYLE_TIMELINE_TH)
64		];
65	}
66	else {
67		$header = [[_('Time'), $sort_div]];
68	}
69
70	// Show events.
71	$table = (new CTableInfo())
72		->setHeader(array_merge($header, [
73			_('Recovery time'),
74			_('Status'),
75			_('Duration'),
76			_('Ack'),
77			($data['show_tags'] != PROBLEMS_SHOW_TAGS_NONE) ? _('Tags') : null
78		]));
79
80	$today = strtotime('today');
81	$last_clock = 0;
82
83	if ($data['problems'] && $data['show_tags'] != PROBLEMS_SHOW_TAGS_NONE) {
84		$tags = makeTags($data['problems'], true, 'eventid', $data['show_tags'], $data['filter_tags'],
85			$data['tag_name_format'], $data['tag_priority']
86		);
87	}
88
89	$url_details = (new CUrl('tr_events.php'))
90		->setArgument('triggerid', $data['trigger']['triggerid'])
91		->setArgument('eventid', '');
92
93	foreach ($data['problems'] as $problem) {
94		if ($problem['r_eventid'] != 0) {
95			$value = TRIGGER_VALUE_FALSE;
96			$value_str = _('RESOLVED');
97			$value_clock = $problem['r_clock'];
98		}
99		else {
100			$in_closing = false;
101
102			foreach ($problem['acknowledges'] as $acknowledge) {
103				if (($acknowledge['action'] & ZBX_PROBLEM_UPDATE_CLOSE) == ZBX_PROBLEM_UPDATE_CLOSE) {
104					$in_closing = true;
105					break;
106				}
107			}
108
109			$value = $in_closing ? TRIGGER_VALUE_FALSE : TRIGGER_VALUE_TRUE;
110			$value_str = $in_closing ? _('CLOSING') : _('PROBLEM');
111			$value_clock = $in_closing ? time() : $problem['clock'];
112		}
113
114		$url_details->setArgument('eventid', $problem['eventid']);
115
116		$cell_clock = ($problem['clock'] >= $today)
117			? zbx_date2str(TIME_FORMAT_SECONDS, $problem['clock'])
118			: zbx_date2str(DATE_TIME_FORMAT_SECONDS, $problem['clock']);
119		$cell_clock = new CCol(new CLink($cell_clock, $url_details));
120		if ($problem['r_eventid'] != 0) {
121			$cell_r_clock = ($problem['r_clock'] >= $today)
122				? zbx_date2str(TIME_FORMAT_SECONDS, $problem['r_clock'])
123				: zbx_date2str(DATE_TIME_FORMAT_SECONDS, $problem['r_clock']);
124			$cell_r_clock = (new CCol(new CLink($cell_r_clock, $url_details)))
125				->addClass(ZBX_STYLE_NOWRAP)
126				->addClass(ZBX_STYLE_RIGHT);
127		}
128		else {
129			$cell_r_clock = '';
130		}
131
132		$is_acknowledged = ($problem['acknowledged'] == EVENT_ACKNOWLEDGED);
133		$cell_status = new CSpan($value_str);
134
135		// Add colors and blinking to span depending on configuration and trigger parameters.
136		addTriggerValueStyle($cell_status, $value, $value_clock, $is_acknowledged);
137
138		if ($data['show_timeline']) {
139			if ($last_clock != 0) {
140				CScreenProblem::addTimelineBreakpoint($table, $last_clock, $problem['clock'], ZBX_SORT_DOWN);
141			}
142			$last_clock = $problem['clock'];
143
144			$row = [
145				$cell_clock->addClass(ZBX_STYLE_TIMELINE_DATE),
146				(new CCol())
147					->addClass(ZBX_STYLE_TIMELINE_AXIS)
148					->addClass(ZBX_STYLE_TIMELINE_DOT),
149				(new CCol())->addClass(ZBX_STYLE_TIMELINE_TD)
150			];
151		}
152		else {
153			$row = [
154				$cell_clock
155					->addClass(ZBX_STYLE_NOWRAP)
156					->addClass(ZBX_STYLE_RIGHT)
157			];
158		}
159
160		// Create acknowledge link.
161		$problem_update_link = (new CLink($is_acknowledged ? _('Yes') : _('No')))
162			->addClass($is_acknowledged ? ZBX_STYLE_GREEN : ZBX_STYLE_RED)
163			->addClass(ZBX_STYLE_LINK_ALT)
164			->onClick('acknowledgePopUp('.json_encode(['eventids' => [$problem['eventid']]]).', this);');
165
166		$table->addRow(array_merge($row, [
167			$cell_r_clock,
168			$cell_status,
169			(new CCol(
170				($problem['r_eventid'] != 0)
171					? zbx_date2age($problem['clock'], $problem['r_clock'])
172					: zbx_date2age($problem['clock'])
173			))
174				->addClass(ZBX_STYLE_NOWRAP),
175			$problem_update_link,
176			($data['show_tags'] != PROBLEMS_SHOW_TAGS_NONE) ? $tags[$problem['eventid']] : null
177		]));
178	}
179
180	$div->addItem($table);
181
182	$output['data'] = $div->toString();
183}
184
185echo json_encode($output);
186