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
22class CScreenGraph extends CScreenBase {
23
24	/**
25	 * Process screen.
26	 *
27	 * @return CDiv (screen inside container)
28	 */
29	public function get() {
30		$this->dataId = 'graph_'.$this->screenitem['screenitemid'].'_'.$this->screenitem['screenid'];
31		$resourceId = isset($this->screenitem['real_resourceid'])
32			? $this->screenitem['real_resourceid']
33			: $this->screenitem['resourceid'];
34		$containerId = 'graph_container_'.$this->screenitem['screenitemid'].'_'.$this->screenitem['screenid'];
35		$graphDims = getGraphDims($resourceId);
36		$graphDims['graphHeight'] = $this->screenitem['height'];
37		$graphDims['width'] = $this->screenitem['width'];
38		$graph = getGraphByGraphId($resourceId);
39		$graphId = $graph['graphid'];
40		$legend = $graph['show_legend'];
41		$graph3d = $graph['show_3d'];
42
43		if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) {
44			// get host
45			$hosts = API::Host()->get([
46				'hostids' => $this->hostid,
47				'output' => ['hostid', 'name']
48			]);
49			$host = reset($hosts);
50
51			// get graph
52			$graph = API::Graph()->get([
53				'graphids' => $resourceId,
54				'output' => API_OUTPUT_EXTEND,
55				'selectHosts' => ['hostid'],
56				'selectGraphItems' => API_OUTPUT_EXTEND
57			]);
58			$graph = reset($graph);
59
60			// if items from one host we change them, or set calculated if not exist on that host
61			if (count($graph['hosts']) == 1) {
62				if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) {
63					$newDynamic = getSameGraphItemsForHost(
64						[['itemid' => $graph['ymax_itemid']]],
65						$this->hostid,
66						false
67					);
68					$newDynamic = reset($newDynamic);
69
70					if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
71						$graph['ymax_itemid'] = $newDynamic['itemid'];
72					}
73					else {
74						$graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
75					}
76				}
77
78				if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) {
79					$newDynamic = getSameGraphItemsForHost(
80						[['itemid' => $graph['ymin_itemid']]],
81						$this->hostid,
82						false
83					);
84					$newDynamic = reset($newDynamic);
85
86					if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
87						$graph['ymin_itemid'] = $newDynamic['itemid'];
88					}
89					else {
90						$graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
91					}
92				}
93			}
94
95			// get url
96			$this->screenitem['url'] = ($graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED)
97				? 'chart7.php'
98				: 'chart3.php';
99			$this->screenitem['url'] = new CUrl($this->screenitem['url']);
100
101			foreach ($graph as $name => $value) {
102				if ($name == 'width' || $name == 'height') {
103					continue;
104				}
105				$this->screenitem['url']->setArgument($name, $value);
106			}
107
108			$newGraphItems = getSameGraphItemsForHost($graph['gitems'], $this->hostid, false);
109			foreach ($newGraphItems as $i => $newGraphItem) {
110				unset($newGraphItem['gitemid'], $newGraphItem['graphid']);
111
112				foreach ($newGraphItem as $name => $value) {
113					$this->screenitem['url']->setArgument('items['.$i.']['.$name.']', $value);
114				}
115			}
116
117			$this->screenitem['url']->setArgument('name', $host['name'].NAME_DELIMITER.$graph['name']);
118			$this->screenitem['url'] = $this->screenitem['url']->getUrl();
119		}
120
121		// get time control
122		$timeControlData = [
123			'id' => $this->getDataId(),
124			'containerid' => $containerId,
125			'objDims' => $graphDims,
126			'loadSBox' => 0,
127			'loadImage' => 1,
128			'periodFixed' => CProfile::get('web.screens.timelinefixed', 1),
129			'sliderMaximumTimePeriod' => ZBX_MAX_PERIOD
130		];
131
132		$isDefault = false;
133		if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) {
134			if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $this->screenitem['url'] === '') {
135				$this->screenitem['url'] = 'chart6.php?graphid='.$resourceId.'&screenid='.$this->screenitem['screenid'];
136				$isDefault = true;
137			}
138
139			$this->timeline['starttime'] = date(TIMESTAMP_FORMAT, get_min_itemclock_by_graphid($resourceId));
140
141			$timeControlData['src'] = $this->screenitem['url'].'&width='.$this->screenitem['width']
142				.'&height='.$this->screenitem['height'].'&legend='.$legend
143				.'&graph3d='.$graph3d.$this->getProfileUrlParams();
144			$timeControlData['src'] .= ($this->mode == SCREEN_MODE_EDIT)
145				? '&period=3600&stime='.date(TIMESTAMP_FORMAT, time())
146				: '&period='.$this->timeline['period'].'&stime='.$this->timeline['stimeNow'];
147		}
148		else {
149			if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $this->screenitem['url'] === '') {
150				$this->screenitem['url'] = 'chart2.php?graphid='.$resourceId.'&screenid='.$this->screenitem['screenid'];
151				$isDefault = true;
152			}
153
154			if ($this->mode != SCREEN_MODE_EDIT && $graphId) {
155				if ($this->mode == SCREEN_MODE_PREVIEW) {
156					$timeControlData['loadSBox'] = 1;
157				}
158			}
159
160			$timeControlData['src'] = $this->screenitem['url'].'&width='.$this->screenitem['width']
161				.'&height='.$this->screenitem['height'].'&legend='.$legend.$this->getProfileUrlParams();
162			$timeControlData['src'] .= ($this->mode == SCREEN_MODE_EDIT)
163				? '&period=3600&stime='.date(TIMESTAMP_FORMAT, time())
164				: '&period='.$this->timeline['period'].'&stime='.$this->timeline['stimeNow'];
165		}
166
167		// output
168		if ($this->mode == SCREEN_MODE_JS) {
169			return 'timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).', '
170				.CJs::encodeJson($timeControlData).')';
171		}
172		else {
173			if ($this->mode == SCREEN_MODE_SLIDESHOW) {
174				insert_js('timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).', '
175					.CJs::encodeJson($timeControlData).');'
176				);
177			}
178			else {
179				zbx_add_post_js('timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).', '
180					.CJs::encodeJson($timeControlData).');'
181				);
182			}
183
184			if (($this->mode == SCREEN_MODE_EDIT || $this->mode == SCREEN_MODE_SLIDESHOW) || !$isDefault) {
185				$item = new CDiv();
186			}
187			elseif ($this->mode == SCREEN_MODE_PREVIEW) {
188				$item = new CLink(null, 'charts.php?graphid='.$resourceId.'&period='.$this->timeline['period'].
189						'&stime='.$this->timeline['stimeNow']);
190			}
191			$item->setId($containerId);
192
193			return $this->getOutput($item);
194		}
195	}
196}
197