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$table = new CTableInfo();
23
24if ($data['error'] != null) {
25	$table->setNoDataMessage($data['error']);
26}
27else {
28	$table_header = [(new CColHeader(_('Timestamp')))->addClass(ZBX_STYLE_CELL_WIDTH)];
29	$names_at_top = ($data['style'] == STYLE_TOP && count($data['items']) > 1);
30
31	if ($names_at_top) {
32		$table->makeVerticalRotation();
33
34		foreach ($data['items'] as $item) {
35			$table_header[] = (new CColHeader(
36				($data['same_host'] ? '' : $item['hosts'][0]['name'].NAME_DELIMITER).$item['name_expanded']
37			))
38				->addClass('vertical_rotation')
39				->setTitle($item['name_expanded']);
40		}
41	}
42	else {
43		if ($data['style'] == STYLE_LEFT) {
44			$table_header[] = _('Name');
45		}
46		$table_header[] = _('Value');
47	}
48	$table->setHeader($table_header);
49
50	$clock = 0;
51	$row_values = [];
52
53	do {
54		$history_item = array_shift($data['histories']);
55
56		if ($history_item !== null && !$names_at_top) {
57			$table_row = [
58				(new CCol(zbx_date2str(DATE_TIME_FORMAT_SECONDS, $history_item['clock'])))->addClass(ZBX_STYLE_NOWRAP)
59			];
60			if ($data['style'] == STYLE_LEFT) {
61				$table->setHeadingColumn(1);
62				$table_row[] = ($data['same_host']
63					? ''
64					: $data['items'][$history_item['itemid']]['hosts'][0]['name'].NAME_DELIMITER).
65					$data['items'][$history_item['itemid']]['name_expanded'];
66			}
67			$table_row[] = $history_item['value'];
68			$table->addRow($table_row);
69		}
70		else {
71			if (($history_item === null && $row_values)
72					|| $history_item !== null
73					&& (($clock != 0 && $history_item['clock'] != $clock)
74						|| array_key_exists($history_item['itemid'], $row_values))) {
75				$table_row = [
76					(new CCol(zbx_date2str(DATE_TIME_FORMAT_SECONDS, $clock)))->addClass(ZBX_STYLE_NOWRAP)
77				];
78				foreach ($data['items'] as $item) {
79					$table_row[] = array_key_exists($item['itemid'], $row_values)
80						? $row_values[$item['itemid']]
81						: '';
82				}
83				$table->addRow($table_row);
84				$row_values = [];
85			}
86
87			if ($history_item !== null) {
88				$clock = $history_item['clock'];
89				$row_values[$history_item['itemid']] = $history_item['value'];
90			}
91		}
92	} while ($history_item !== null && $table->getNumRows() < $data['show_lines']);
93}
94
95$output = [
96	'header' => $data['name'],
97	'body' => $table->toString()
98];
99
100if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
101	CProfiler::getInstance()->stop();
102	$output['debug'] = CProfiler::getInstance()->make()->toString();
103}
104
105echo (new CJson())->encode($output);
106