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'] = (int) $this->screenitem['height'];
37		$graphDims['width'] = (int) $this->screenitem['width'];
38		$graph = getGraphByGraphId($resourceId);
39		$src = null;
40
41		if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) {
42			// get host
43			$hosts = API::Host()->get([
44				'hostids' => $this->hostid,
45				'output' => ['hostid', 'name']
46			]);
47			$host = reset($hosts);
48
49			// get graph
50			$graph = API::Graph()->get([
51				'graphids' => $resourceId,
52				'output' => API_OUTPUT_EXTEND,
53				'selectHosts' => ['hostid'],
54				'selectGraphItems' => API_OUTPUT_EXTEND
55			]);
56			$graph = reset($graph);
57
58			// if items from one host we change them, or set calculated if not exist on that host
59			if (count($graph['hosts']) == 1) {
60				if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) {
61					$newDynamic = getSameGraphItemsForHost(
62						[['itemid' => $graph['ymax_itemid']]],
63						$this->hostid,
64						false
65					);
66					$newDynamic = reset($newDynamic);
67
68					if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
69						$graph['ymax_itemid'] = $newDynamic['itemid'];
70					}
71					else {
72						$graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
73					}
74				}
75
76				if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) {
77					$newDynamic = getSameGraphItemsForHost(
78						[['itemid' => $graph['ymin_itemid']]],
79						$this->hostid,
80						false
81					);
82					$newDynamic = reset($newDynamic);
83
84					if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
85						$graph['ymin_itemid'] = $newDynamic['itemid'];
86					}
87					else {
88						$graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
89					}
90				}
91			}
92
93			// get url
94			if ($graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED) {
95				$src = (new CUrl('chart7.php'))
96					->setArgument('name', $host['name'].NAME_DELIMITER.$graph['name'])
97					->setArgument('graphtype', $graph['graphtype'])
98					->setArgument('graph3d', $graph['show_3d'])
99					->setArgument('legend', $graph['show_legend']);
100			}
101			else {
102				$src = (new CUrl('chart3.php'))
103					->setArgument('name', $host['name'].NAME_DELIMITER.$graph['name'])
104					->setArgument('ymin_type', $graph['ymin_type'])
105					->setArgument('ymax_type', $graph['ymax_type'])
106					->setArgument('ymin_itemid', $graph['ymin_itemid'])
107					->setArgument('ymax_itemid', $graph['ymax_itemid'])
108					->setArgument('legend', $graph['show_legend'])
109					->setArgument('showworkperiod', $graph['show_work_period'])
110					->setArgument('showtriggers', $graph['show_triggers'])
111					->setArgument('graphtype', $graph['graphtype'])
112					->setArgument('yaxismin', $graph['yaxismin'])
113					->setArgument('yaxismax', $graph['yaxismax'])
114					->setArgument('percent_left', $graph['percent_left'])
115					->setArgument('percent_right', $graph['percent_right']);
116			}
117
118			$newGraphItems = getSameGraphItemsForHost($graph['gitems'], $this->hostid, false);
119			foreach ($newGraphItems as $i => &$newGraphItem) {
120				unset($newGraphItem['gitemid'], $newGraphItem['graphid']);
121			}
122			unset($newGraphItem);
123
124			$src->setArgument('items', $newGraphItems);
125		}
126
127		// get time control
128		$timeControlData = [
129			'id' => $this->getDataId(),
130			'containerid' => $containerId,
131			'objDims' => $graphDims,
132			'loadSBox' => 0,
133			'loadImage' => 1
134		];
135
136		$isDefault = false;
137		if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) {
138			if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $src === null) {
139				$src = (new CUrl('chart6.php'))
140					->setArgument('graphid', $resourceId)
141					->setArgument('screenid', $this->screenitem['screenid']);
142
143				$isDefault = true;
144			}
145
146			$src->setArgument('graph3d', $graph['show_3d']);
147		}
148		else {
149			if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $src === null) {
150				$src = (new CUrl('chart2.php'))
151					->setArgument('graphid', $resourceId)
152					->setArgument('screenid', $this->screenitem['screenid']);
153
154				$isDefault = true;
155			}
156
157			if ($this->mode != SCREEN_MODE_EDIT && $graph['graphid']) {
158				if ($this->mode == SCREEN_MODE_PREVIEW) {
159					$timeControlData['loadSBox'] = 1;
160				}
161			}
162		}
163
164		$src
165			->setArgument('width', $this->screenitem['width'])
166			->setArgument('height', $this->screenitem['height'])
167			->setArgument('legend', $graph['show_legend'])
168			->setArgument('profileIdx', $this->profileIdx)
169			->setArgument('profileIdx2', $this->profileIdx2);
170
171		if ($this->mode == SCREEN_MODE_EDIT) {
172			$src
173				->setArgument('from', ZBX_PERIOD_DEFAULT_FROM)
174				->setArgument('to', ZBX_PERIOD_DEFAULT_TO);
175		}
176		else {
177			$src
178				->setArgument('from', $this->timeline['from'])
179				->setArgument('to', $this->timeline['to']);
180		}
181
182		$timeControlData['src'] = $src->getUrl();
183
184		// output
185		if ($this->mode == SCREEN_MODE_JS) {
186			return 'timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).', '.
187				CJs::encodeJson($timeControlData).')';
188		}
189		else {
190			if ($this->mode == SCREEN_MODE_SLIDESHOW) {
191				insert_js('timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).', '.
192					CJs::encodeJson($timeControlData).');'
193				);
194			}
195			else {
196				zbx_add_post_js('timeControl.addObject("'.$this->getDataId().'", '.CJs::encodeJson($this->timeline).
197					', '.CJs::encodeJson($timeControlData).');'
198				);
199			}
200
201			if (($this->mode == SCREEN_MODE_EDIT || $this->mode == SCREEN_MODE_SLIDESHOW) || !$isDefault) {
202				$item = new CDiv();
203			}
204			elseif ($this->mode == SCREEN_MODE_PREVIEW) {
205				$item = new CLink(null, 'charts.php?graphid='.$resourceId.'&from='.$this->timeline['from'].
206					'&to='.$this->timeline['to']
207				);
208			}
209
210			$item
211				->addClass(ZBX_STYLE_GRAPH_WRAPPER)
212				->setId($containerId);
213
214			return $this->getOutput($item);
215		}
216	}
217}
218