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/monitoring.history.js.php';
23
24$historyWidget = new CWidget();
25
26$header = [
27	'left' => _n('%1$s item', '%1$s items', count($this->data['items'])),
28	'right' => new CList()
29];
30$headerPlaintext = [];
31
32$hostNames = [];
33foreach ($this->data['items'] as $itemData) {
34	$hostName = $itemData['hosts'][0]['name'];
35
36	if (!array_key_exists($hostName, $hostNames)) {
37		$hostNames[$hostName] = $hostName;
38	}
39}
40
41$item = reset($this->data['items']);
42$host = reset($item['hosts']);
43
44if ($this->data['action'] != HISTORY_BATCH_GRAPH) {
45	$header['left'] = [
46		$host['name'],
47		NAME_DELIMITER,
48		$item['name_expanded']
49	];
50	$headerPlaintext[] = $host['name'].NAME_DELIMITER.$item['name_expanded'];
51}
52elseif (count($hostNames) == 1) {
53	$header['left'] = [
54		$host['name'],
55		NAME_DELIMITER,
56		$header['left']
57	];
58}
59
60// don't display the action form if we view multiple items on a graph
61if ($this->data['action'] != HISTORY_BATCH_GRAPH) {
62	$actionForm = (new CForm('get'))
63		->addVar('itemids', getRequest('itemids'));
64
65	if (isset($_REQUEST['filter_task'])) {
66		$actionForm->addVar('filter_task', $_REQUEST['filter_task']);
67	}
68	if (isset($_REQUEST['filter'])) {
69		$actionForm->addVar('filter', $_REQUEST['filter']);
70	}
71	if (isset($_REQUEST['mark_color'])) {
72		$actionForm->addVar('mark_color', $_REQUEST['mark_color']);
73	}
74
75	$actions = [];
76	if (isset($this->data['iv_numeric'][$this->data['value_type']])) {
77		$actions[HISTORY_GRAPH] = _('Graph');
78	}
79	$actions[HISTORY_VALUES] = _('Values');
80	$actions[HISTORY_LATEST] = _('500 latest values');
81	$actionForm->addItem(new CComboBox('action', $this->data['action'], 'submit()', $actions));
82
83	if ($this->data['action'] != HISTORY_GRAPH) {
84		$actionForm->addItem([
85			(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
86			new CSubmit('plaintext', _('As plain text'))
87		]);
88	}
89
90	$header['right']->addItem($actionForm);
91}
92
93if ($this->data['action'] != HISTORY_BATCH_GRAPH) {
94	if ($this->data['action'] == HISTORY_GRAPH) {
95		$header['right']->addItem(get_icon('favourite', [
96			'fav' => 'web.favorite.graphids',
97			'elid' => $item['itemid'],
98			'elname' => 'itemid'
99		]));
100	}
101}
102
103$header['right']->addItem(get_icon('fullscreen', ['fullscreen' => $this->data['fullscreen']]));
104
105// create filter
106if ($this->data['action'] == HISTORY_VALUES || $this->data['action'] == HISTORY_LATEST) {
107	if (isset($this->data['iv_string'][$this->data['value_type']])) {
108		$filterForm = (new CFilter('web.history.filter.state'))
109			->addVar('fullscreen', $this->data['fullscreen'])
110			->addVar('action', $this->data['action']);
111		foreach (getRequest('itemids') as $itemId) {
112			$filterForm->addVar('itemids['.$itemId.']', $itemId);
113		}
114
115		$itemListbox = new CListBox('cmbitemlist[]');
116		$itemsData = [];
117		foreach ($this->data['items'] as $itemid => $item) {
118			if (!isset($this->data['iv_string'][$item['value_type']])) {
119				unset($this->data['items'][$itemid]);
120				continue;
121			}
122
123			$host = reset($item['hosts']);
124			$itemsData[$itemid]['id'] = $itemid;
125			$itemsData[$itemid]['name'] = $host['name'].NAME_DELIMITER.$item['name_expanded'];
126		}
127
128		order_result($itemsData, 'name');
129		foreach ($itemsData as $item) {
130			$itemListbox->addItem($item['id'], $item['name']);
131		}
132
133		$addItemButton = (new CButton('add_log', _('Add')))
134			->onClick("return PopUp('popup.php?multiselect=1&real_hosts=1".
135					'&reference=itemid&srctbl=items&value_types[]='.$this->data['value_type']."&srcfld1=itemid');");
136		$deleteItemButton = null;
137
138		if (count($this->data['items']) > 1) {
139			$deleteItemButton = [
140				(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
141				new CButton('remove_log', _('Remove selected'))
142			];
143		}
144
145		$filterColumn1 = (new CFormList())
146			->addRow(_('Items list'), [$itemListbox, BR(), $addItemButton, $deleteItemButton])
147			->addRow(_('Select rows with value like'),
148				(new CTextBox('filter', getRequest('filter', '')))->setWidth(ZBX_TEXTAREA_FILTER_SMALL_WIDTH)
149			);
150
151		$filterTask = getRequest('filter_task', 0);
152
153		$tasks = [new CComboBox('filter_task', $filterTask, 'submit()', [
154			FILTER_TASK_SHOW => _('Show selected'),
155			FILTER_TASK_HIDE => _('Hide selected'),
156			FILTER_TASK_MARK => _('Mark selected'),
157			FILTER_TASK_INVERT_MARK => _('Mark others')
158		])];
159
160		if (str_in_array($filterTask, [FILTER_TASK_MARK, FILTER_TASK_INVERT_MARK])) {
161			$tasks[] = ' ';
162			$tasks[] = new CComboBox('mark_color', getRequest('mark_color', 0), null, [
163				MARK_COLOR_RED => _('as Red'),
164				MARK_COLOR_GREEN => _('as Green'),
165				MARK_COLOR_BLUE => _('as Blue')
166			]);
167		}
168
169		$filterColumn1->addRow(_('Selected'), $tasks);
170		$filterForm->addColumn($filterColumn1);
171	}
172}
173
174// for batch graphs don't remember the time selection in the profiles
175if ($this->data['action'] == HISTORY_BATCH_GRAPH) {
176	$profileIdx = false;
177	$profileIdx2 = false;
178	$updateProfile = false;
179}
180else {
181	$profileIdx = 'web.item.graph';
182	$profileIdx2 = reset($this->data['itemids']);
183	$updateProfile = ($this->data['action'] != HISTORY_BATCH_GRAPH);
184}
185
186// create history screen
187$screen = CScreenBuilder::getScreen([
188	'resourcetype' => SCREEN_RESOURCE_HISTORY,
189	'action' => $this->data['action'],
190	'itemids' => $data['itemids'],
191	'profileIdx' => $profileIdx,
192	'profileIdx2' => $profileIdx2,
193	'updateProfile' => $updateProfile,
194	'period' => $this->data['period'],
195	'stime' => $this->data['stime'],
196	'filter' => getRequest('filter'),
197	'filter_task' => getRequest('filter_task'),
198	'mark_color' => getRequest('mark_color'),
199	'plaintext' => $this->data['plaintext'],
200	'graphtype' => $this->data['graphtype']
201]);
202
203// append plaintext to widget
204if ($this->data['plaintext']) {
205	foreach ($headerPlaintext as $text) {
206		$historyWidget->addItem([new CSpan($text), BR()]);
207	}
208
209	$screen = $screen->get();
210
211	$pre = new CPre();
212	foreach ($screen as $text) {
213		$pre->addItem([$text, BR()]);
214	}
215	$historyWidget->addItem($pre);
216}
217else {
218	$historyWidget->setTitle($header['left'])
219		->setControls($header['right']);
220
221	if (isset($this->data['iv_string'][$this->data['value_type']])) {
222		$filterForm->addNavigator();
223	}
224
225	if (in_array($this->data['action'], [HISTORY_VALUES, HISTORY_GRAPH, HISTORY_BATCH_GRAPH])) {
226		if(!isset($filterForm)) {
227			$filterForm = new CFilter('web.history.filter.state');
228		}
229
230		// display the graph type filter for graphs with multiple items
231		if ($this->data['action'] == HISTORY_BATCH_GRAPH) {
232			$filterForm->addColumn(
233				(new CFormList())->addRow(_('Graph type'),
234					(new CRadioButtonList('graphtype', (int) $this->data['graphtype']))
235						->addValue(_('Normal'), GRAPH_TYPE_NORMAL)
236						->addValue(_('Stacked'), GRAPH_TYPE_STACKED)
237						->setModern(true)
238				)
239			);
240			$filterForm->removeButtons();
241			$filterForm->addVar('fullscreen', $this->data['fullscreen']);
242			$filterForm->addVar('action', $this->data['action']);
243			$filterForm->addVar('itemids', $this->data['itemids']);
244		}
245
246		$filterForm->addNavigator();
247		$historyWidget->addItem($filterForm);
248	}
249
250	$historyWidget->addItem($screen->get());
251
252	if ($data['action'] !== HISTORY_LATEST) {
253		CScreenBuilder::insertScreenStandardJs([
254			'timeline' => $screen->timeline,
255			'profileIdx' => $screen->profileIdx
256		]);
257	}
258}
259
260return $historyWidget;
261