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	 * @param string		$widget_settings['uniqueid']		A string of widget's unique id assigned by dashboard.
99	 */
100	public function __construct(array $sysmap_data, array $widget_settings) {
101		parent::__construct();
102
103		$this->error = $widget_settings['error'];
104		$this->sysmap_data = $sysmap_data;
105		$this->current_sysmapid = $widget_settings['current_sysmapid'];
106		$this->filter_widget_reference = $widget_settings['filter_widget_reference'];
107		$this->source_type = $widget_settings['source_type'];
108		$this->previous_map = $widget_settings['previous_map'];
109		$this->initial_load = $widget_settings['initial_load'];
110		$this->uniqueid = $widget_settings['uniqueid'];
111	}
112
113	/**
114	 * A javascript that is used as widget's script_inline parameter.
115	 *
116	 * @return string
117	 */
118	public function getScriptRun() {
119		$script_run = '';
120
121		if ($this->current_sysmapid !== null && $this->initial_load) {
122			// This should be before other scripts.
123			$script_run .=
124				'jQuery(".dashbrd-grid-container").dashboardGrid('.
125					'\'setWidgetStorageValue\', "'.$this->uniqueid.'", \'current_sysmapid\', '.$this->current_sysmapid.
126				');';
127		}
128
129		if ($this->initial_load) {
130			$script_run .=
131				'jQuery(".dashbrd-grid-container").dashboardGrid("addAction", "timer_refresh", '.
132					'"zbx_sysmap_widget_trigger", "'.$this->uniqueid.'", {'.
133						'parameters: ["onWidgetRefresh"],'.
134						'grid: {widget: 1},'.
135						'trigger_name: "map_widget_timer_refresh_'.$this->uniqueid.'"'.
136					'}'.
137				');';
138
139			$script_run .=
140				'jQuery(".dashbrd-grid-container").dashboardGrid("addAction", "afterUpdateWidgetConfig", '.
141					'"zbx_sysmap_widget_trigger", "'.$this->uniqueid.'", {'.
142						'parameters: ["afterUpdateWidgetConfig"],'.
143						'grid: {widget: 1},'.
144						'trigger_name: "after_map_widget_config_update_'.$this->uniqueid.'"'.
145					'}'.
146				');';
147		}
148
149		if ($this->source_type == WIDGET_SYSMAP_SOURCETYPE_FILTER && $this->filter_widget_reference
150				&& $this->initial_load) {
151			$script_run .=
152				'jQuery(".dashbrd-grid-container").dashboardGrid(\'registerDataExchange\', {'.
153					'uniqueid: "'.$this->uniqueid.'",'.
154					'linkedto: "'.$this->filter_widget_reference.'",'.
155					'data_name: "selected_mapid",'.
156					'callback: function(widget, data) {'.
157						'jQuery(".dashbrd-grid-container").dashboardGrid(\'setWidgetStorageValue\', '.
158							'widget.uniqueid, \'current_sysmapid\', data[0].mapid'.
159						');'.
160						'jQuery(".dashbrd-grid-container").dashboardGrid(\'setWidgetStorageValue\', '.
161							'widget.uniqueid, \'previous_maps\', ""'.
162						');'.
163						'jQuery(".dashbrd-grid-container").dashboardGrid(\'refreshWidget\', widget.widgetid);'.
164					'}'.
165				'});'.
166
167				'jQuery(".dashbrd-grid-container").dashboardGrid("callWidgetDataShare");'.
168
169				'jQuery(".dashbrd-grid-container").dashboardGrid("addAction", "onEditStart", '.
170					'"zbx_sysmap_widget_trigger", "'.$this->uniqueid.'", {'.
171						'parameters: ["onEditStart"],'.
172						'grid: {widget: 1},'.
173					'trigger_name: "map_widget_on_edit_start_'.$this->uniqueid.'"'.
174				'});';
175		}
176
177		if ($this->sysmap_data && $this->error === null) {
178			$this->sysmap_data['container'] = '#map_'.$this->uniqueid;
179
180			$script_run .= 'jQuery(function($) {'.
181				'$("#'.$this->getId().'").zbx_mapwidget({'.
182					'uniqueid: "'.$this->uniqueid.'",'.
183					'map_options: '.zbx_jsvalue($this->sysmap_data).
184				'});'.
185			'});';
186		}
187		elseif ($this->error !== null && $this->source_type == WIDGET_SYSMAP_SOURCETYPE_FILTER) {
188			$error_msg_html = (new CTableInfo())->setNoDataMessage($this->error);
189			$script_run .=
190				'jQuery(".dashbrd-grid-container").dashboardGrid("addAction", "onDashboardReady", '.
191					'"zbx_sysmap_widget_trigger", "'.$this->uniqueid.'", {'.
192						'parameters: ["onDashboardReady", {html: "'. addslashes($error_msg_html).'"}],'.
193						'grid: {widget: 1},'.
194						'priority: 10,'.
195						'trigger_name: "on_dashboard_ready_'.$this->uniqueid.'"'.
196					'}'.
197				');';
198		}
199
200		// Fix text label disappearing on DOM change in Internet Explorer 11 on Windows 7 (important).
201		if ($this->source_type == WIDGET_SYSMAP_SOURCETYPE_MAP && $this->initial_load) {
202			$script_run .=
203				'if (IE11) {'.
204					'jQuery(".dashbrd-grid-container")'.
205						'.dashboardGrid("getWidgetsBy", "uniqueid", "'.$this->uniqueid.'")'.
206						'.each(function(widget) {'.
207							'var observer = new MutationObserver(function() {'.
208									'widget.content_body.find("svg text").each(function() {'.
209										'jQuery(this).attr("textLength", this.getBBox().width);'.
210									'});'.
211								'});'.
212
213							'observer.observe(widget.div[0], {attributes: true});'.
214						'});'.
215				'}';
216		}
217
218		return $script_run;
219	}
220
221	/**
222	 * Build an object of HTML used in widget content.
223	 */
224	private function build() {
225		$this->addClass(ZBX_STYLE_SYSMAP);
226		$this->setId(uniqid());
227
228		if ($this->error === null) {
229			$this->setAttribute('data-uniqueid', $this->uniqueid);
230
231			if ($this->previous_map) {
232				$go_back_div = (new CDiv())
233					->addClass(ZBX_STYLE_BTN_BACK_MAP_CONTAINER)
234					->addItem(
235						(new CLink(
236							(new CSpan())
237								->addClass(ZBX_STYLE_BTN_BACK_MAP)
238								->addItem((new CDiv())->addClass(ZBX_STYLE_BTN_BACK_MAP_ICON))
239								->addItem((new CDiv())
240									->addClass(ZBX_STYLE_BTN_BACK_MAP_CONTENT)
241									->addItem(_s('Go back to %1$s', $this->previous_map['name']))
242								),
243								'javascript: navigateToSubmap('.$this->previous_map['sysmapid'].', "'.
244									$this->uniqueid.'", true);'
245						))
246					);
247
248				$this->addItem($go_back_div);
249			}
250
251			$map_div = (new CDiv((new CDiv($this->sysmap_data['aria_label']))->addClass(ZBX_STYLE_INLINE_SR_ONLY)))
252				->setId('map_'.$this->uniqueid)
253				->addClass('sysmap-widget-container');
254
255			$this->addStyle('position:relative;');
256			$this->addItem($map_div);
257		}
258		elseif ($this->source_type == WIDGET_SYSMAP_SOURCETYPE_MAP) {
259			$this->addItem((new CTableInfo())->setNoDataMessage($this->error));
260		}
261	}
262
263	/**
264	 * Gets string representation of widget HTML content.
265	 *
266	 * @param bool $destroy
267	 *
268	 * @return string
269	 */
270	public function toString($destroy = true) {
271		$this->build();
272
273		return parent::toString($destroy);
274	}
275}
276