1<?php declare(strict_types = 1);
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/**
23 * Common methods for "charts.view" and "charts.view.json" actions.
24 */
25abstract class CControllerCharts extends CController {
26
27	/**
28	 * Fetches all host graph ids or also intersect with given graphids array (second parameter).
29	 *
30	 * @param array $hostids   If not empty, return the listed host graphids.
31	 * @param array $graphids  If not empty, will be used for graphs filter.
32	 *
33	 * @return array
34	 */
35	protected function getGraphidsByHostids(array $hostids, array $graphids): array {
36		if (!$hostids && $graphids) {
37			return $graphids;
38		}
39
40		$options = [
41			'output' => [],
42			'hostids' => $hostids,
43			'limit' => ZBX_MAX_GRAPHS_PER_PAGE,
44			'preservekeys' => true
45		];
46
47		if ($hostids && $graphids) {
48			$options['graphids'] = $graphids;
49		}
50
51		return array_keys(API::Graph()->get($options));
52	}
53
54	/**
55	 * Fetches graph IDs by pattern for specific host.
56	 *
57	 * @param array $patterns  List of graph patterns.
58	 * @param array $hostids   Hosts for which search graphs by patterns.
59	 *
60	 * @return array
61	 */
62	protected function getGraphidsByPatterns(array $patterns, array $hostids): array {
63		$options = [
64			'output' => [],
65			'hostids' => $hostids,
66			'limit' => ZBX_MAX_GRAPHS_PER_PAGE,
67			'preservekeys' => true
68		];
69
70		if (!in_array('*', $patterns)) {
71			$options['search'] = ['name' => $patterns];
72			$options['searchWildcardsEnabled'] = true;
73			$options['searchByAny'] = true;
74		}
75
76		return array_keys(API::Graph()->get($options));
77	}
78
79	/**
80	 * Prepares graph display details (graph dimensions, URL and sBox-ing flag).
81	 *
82	 * @param array $graphids  Graph IDs for which details need to be prepared.
83	 *
84	 * @return array
85	 */
86	protected function getChartsById(array $graphids): array {
87		$charts = [];
88
89		foreach ($graphids as $graphid) {
90			$chart = [
91				'chartid' => $graphid,
92				'dimensions' => getGraphDims($graphid)
93			];
94
95			if ($chart['dimensions']['graphtype'] == GRAPH_TYPE_PIE
96					|| $chart['dimensions']['graphtype'] == GRAPH_TYPE_EXPLODED) {
97				$chart['sbox'] = false;
98				$chart['src'] = 'chart6.php';
99			}
100			else {
101				$chart['sbox'] = true;
102				$chart['src'] = 'chart2.php';
103			}
104
105			$charts[] = $chart;
106		}
107
108		return $charts;
109	}
110}
111