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/**
23 * Dashboard Map widget class. Creates all widget specific JavaScript and HTML content for map widget's view.
24 */
25class CDashboardWidgetMap extends CDiv {
26
27	/**
28	 * Reference of linked map navigation tree widget.
29	 *
30	 * @var string
31	 */
32	private $filter_widget_reference;
33
34	/**
35	 * Map that will be linked to 'go back to [previous map name]' link in dashboard map widget.
36	 *
37	 * @var array|null - array must contain at least integer value 'sysmapid' and string 'name'.
38	 */
39	private $previous_map;
40
41	/**
42	 * Response array of CMapHelper::get() that represents currently opened map.
43	 *
44	 * @var array|null
45	 */
46	private $sysmap_data;
47
48	/**
49	 * Requested sysmapid.
50	 *
51	 * @var int
52	 */
53	private $current_sysmapid;
54
55	/**
56	 * The type of source of map widget.
57	 *
58	 * @var int	- allowed values are WIDGET_SYSMAP_SOURCETYPE_MAP and WIDGET_SYSMAP_SOURCETYPE_FILTER.
59	 */
60	private $source_type;
61
62	/**
63	 * Represents either this is initial or repeated load of map widget.
64	 *
65	 * @var int	- allowed values are 0 and 1.
66	 */
67	private $initial_load;
68
69	/**
70	 * Unique ID of widget.
71	 *
72	 * @var string
73	 */
74	private $uniqueid;
75
76	/**
77	 * The error message displayed in map widget.
78	 *
79	 * @var string|null
80	 */
81	private $error;
82
83	/**
84	 * Class constructor.
85	 *
86	 * @param array			$sysmap_data		An array of requested map in the form created by CMapHelper::get()
87	 *											method.
88	 * @param array			$widget_settings	An array contains widget settings.
89	 * @param string|null	$widget_settings['error']			A string of error message or null in case if error is
90	 *															not detected.
91	 * @param int			$widget_settings['current_sysmapid'] An integer of requested sysmapid.
92	 * @param string		$widget_settings['filter_widget_reference'] A string of linked map navigation tree
93	 *															reference.
94	 * @param int			$widget_settings['source_type']		The type of source of map widget.
95	 * @param array|null	$widget_settings['previous_map']	Sysmapid and name of map linked as previous.
96	 * @param int			$widget_settings['initial_load']	Integer represents either this is initial load or
97	 *															repeated.
98	 */
99	public function __construct(array $sysmap_data, array $widget_settings) {
100		parent::__construct();
101
102		$this->error = $widget_settings['error'];
103		$this->sysmap_data = $sysmap_data;
104		$this->current_sysmapid = $widget_settings['current_sysmapid'];
105		$this->filter_widget_reference = $widget_settings['filter_widget_reference'];
106		$this->source_type = $widget_settings['source_type'];
107		$this->previous_map = $widget_settings['previous_map'];
108		$this->initial_load = $widget_settings['initial_load'];
109	}
110
111	/**
112	 * A javascript that is used as widget's script_inline parameter.
113	 *
114	 * @return string
115	 */
116	public function getScriptData() {
117		$map_data = [
118			'current_sysmapid' => null,
119			'filter_widget_reference' => null,
120			'map_options' => null
121		];
122
123		if ($this->current_sysmapid !== null && $this->initial_load) {
124			$map_data['current_sysmapid'] = $this->current_sysmapid;
125		}
126
127		if ($this->source_type == WIDGET_SYSMAP_SOURCETYPE_FILTER
128				&& $this->filter_widget_reference
129				&& $this->initial_load) {
130			$map_data['filter_widget_reference'] = $this->filter_widget_reference;
131		}
132
133		if ($this->sysmap_data && $this->error === null) {
134			$map_data['map_options'] = $this->sysmap_data;
135		}
136		elseif ($this->error !== null && $this->source_type == WIDGET_SYSMAP_SOURCETYPE_FILTER) {
137			$map_data['error_msg'] = (new CTableInfo())
138				->setNoDataMessage($this->error)
139				->toString();
140		}
141
142		return $map_data;
143	}
144
145	/**
146	 * Build an object of HTML used in widget content.
147	 */
148	private function build() {
149		$this->addClass(ZBX_STYLE_SYSMAP);
150		$this->setId(uniqid());
151
152		if ($this->error === null) {
153			if ($this->previous_map) {
154				$go_back_div = (new CDiv())
155					->addClass(ZBX_STYLE_BTN_BACK_MAP_CONTAINER)
156					->addItem(
157						(new CLink(
158							(new CSpan())
159								->addClass(ZBX_STYLE_BTN_BACK_MAP)
160								->addItem((new CDiv())->addClass(ZBX_STYLE_BTN_BACK_MAP_ICON))
161								->addItem((new CDiv())
162									->addClass(ZBX_STYLE_BTN_BACK_MAP_CONTENT)
163									->addItem(_s('Go back to %1$s', $this->previous_map['name']))
164								),
165								'#'
166						))->addClass('js-previous-map')
167					);
168
169				$this->addItem($go_back_div);
170			}
171
172			$map_div = (new CDiv((new CDiv($this->sysmap_data['aria_label']))->addClass(ZBX_STYLE_INLINE_SR_ONLY)))
173				->addClass('sysmap-widget-container');
174
175			$this->addStyle('position:relative;');
176			$this->addItem($map_div);
177		}
178		elseif ($this->source_type == WIDGET_SYSMAP_SOURCETYPE_MAP) {
179			$this->addItem((new CTableInfo())->setNoDataMessage($this->error));
180		}
181	}
182
183	/**
184	 * Gets string representation of widget HTML content.
185	 *
186	 * @param bool $destroy
187	 *
188	 * @return string
189	 */
190	public function toString($destroy = true) {
191		$this->build();
192
193		return parent::toString($destroy);
194	}
195}
196