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$this->addJSfile('js/class.pmaster.js');
23
24/*
25 * Dashboard grid
26 */
27$dashboardGrid = [[], [], []];
28$widgetRefreshParams = [];
29
30$widgets = [
31	WIDGET_FAVOURITE_GRAPHS => [
32		'id' => 'favouriteGraphs',
33		'menu_popup' => ['CMenuPopupHelper', 'getFavouriteGraphs'],
34		'data' => $data['favourite_graphs'],
35		'header' => _('Favourite graphs'),
36		'links' => [
37			['name' => _('Graphs'), 'url' => 'charts.php']
38		],
39		'defaults' => ['col' => 0, 'row' => 0]
40	],
41	WIDGET_FAVOURITE_SCREENS => [
42		'id' => 'favouriteScreens',
43		'menu_popup' => ['CMenuPopupHelper', 'getFavouriteScreens'],
44		'data' => $data['favourite_screens'],
45		'header' => _('Favourite screens'),
46		'links' => [
47			['name' => _('Screens'), 'url' => 'screens.php'],
48			['name' => _('Slide shows'), 'url' => 'slides.php']
49		],
50		'defaults' => ['col' => 0, 'row' => 1]
51	],
52	WIDGET_FAVOURITE_MAPS => [
53		'id' => 'favouriteMaps',
54		'menu_popup' => ['CMenuPopupHelper', 'getFavouriteMaps'],
55		'data' => $data['favourite_maps'],
56		'header' => _('Favourite maps'),
57		'links' => [
58			['name' => _('Maps'), 'url' => 'zabbix.php?action=map.view']
59		],
60		'defaults' => ['col' => 0, 'row' => 2]
61	]
62];
63
64foreach ($widgets as $widgetid => $widget) {
65	$icon = (new CButton(null))
66		->addClass(ZBX_STYLE_BTN_WIDGET_ACTION)
67		->setTitle(_('Action'))
68		->setId($widget['id'])
69		->setMenuPopup(call_user_func($widget['menu_popup']));
70
71	$footer = new CList();
72	foreach ($widget['links'] as $link) {
73		$footer->addItem(new CLink($link['name'], $link['url']));
74	}
75
76	$col = CProfile::get('web.dashboard.widget.'.$widgetid.'.col', $widget['defaults']['col']);
77	$row = CProfile::get('web.dashboard.widget.'.$widgetid.'.row', $widget['defaults']['row']);
78
79	$dashboardGrid[$col][$row] = (new CCollapsibleUiWidget($widgetid, $widget['data']))
80		->setExpanded((bool) CProfile::get('web.dashboard.widget.'.$widgetid.'.state', true))
81		->setHeader($widget['header'], [$icon], true, 'zabbix.php?action=dashboard.widget')
82		->setFooter($footer);
83}
84
85$widgets = [
86	WIDGET_SYSTEM_STATUS => [
87		'action' => 'widget.system.view',
88		'header' => _('System status'),
89		'defaults' => ['col' => 1, 'row' => 1]
90	],
91	WIDGET_HOST_STATUS => [
92		'action' => 'widget.hosts.view',
93		'header' => _('Host status'),
94		'defaults' => ['col' => 1, 'row' => 2]
95	],
96	WIDGET_LAST_ISSUES => [
97		'action' => 'widget.issues.view',
98		'header' => _n('Last %1$d issue', 'Last %1$d issues', DEFAULT_LATEST_ISSUES_CNT),
99		'defaults' => ['col' => 1, 'row' => 3]
100	],
101	WIDGET_WEB_OVERVIEW => [
102		'action' => 'widget.web.view',
103		'header' => _('Web monitoring'),
104		'defaults' => ['col' => 1, 'row' => 4]
105	],
106];
107
108if ($data['show_status_widget']) {
109	$widgets[WIDGET_ZABBIX_STATUS] = [
110		'action' => 'widget.status.view',
111		'header' => _('Status of Zabbix'),
112		'defaults' => ['col' => 1, 'row' => 0]
113	];
114}
115if ($data['show_discovery_widget']) {
116	$widgets[WIDGET_DISCOVERY_STATUS] = [
117		'action' => 'widget.discovery.view',
118		'header' => _('Discovery status'),
119		'defaults' => ['col' => 1, 'row' => 5]
120	];
121}
122
123foreach ($widgets as $widgetid => $widget) {
124	$profile = 'web.dashboard.widget.'.$widgetid;
125
126	$rate = CProfile::get($profile.'.rf_rate', 60);
127	$expanded = (bool) CProfile::get($profile.'.state', true);
128	$col = CProfile::get($profile.'.col', $widget['defaults']['col']);
129	$row = CProfile::get($profile.'.row', $widget['defaults']['row']);
130
131	$icon = (new CButton(null))
132		->addClass(ZBX_STYLE_BTN_WIDGET_ACTION)
133		->setTitle(_('Action'))
134		->setMenuPopup(CMenuPopupHelper::getRefresh($widgetid, $rate));
135
136	$dashboardGrid[$col][$row] = (new CCollapsibleUiWidget($widgetid, (new CDiv())->addClass(ZBX_STYLE_PRELOADER)))
137		->setExpanded($expanded)
138		->setHeader($widget['header'], [$icon], true, 'zabbix.php?action=dashboard.widget')
139		->setFooter((new CList())->setId($widgetid.'_footer'));
140
141	$widgetRefreshParams[$widgetid] = [
142		'frequency' => $rate,
143		'url' => 'zabbix.php?action='.$widget['action'],
144		'counter' => 0,
145		'darken' => 0,
146		'params' => ['widgetRefresh' => $widgetid]
147	];
148}
149
150// sort dashboard grid
151foreach ($dashboardGrid as $key => $val) {
152	ksort($dashboardGrid[$key]);
153}
154
155(new CWidget())
156	->setTitle(_('Dashboard'))
157	->setControls((new CForm())
158		->cleanItems()
159		->addItem((new CList())
160			->addItem(get_icon('dashconf', ['enabled' => $data['filter_enabled']]))
161			->addItem(get_icon('fullscreen', ['fullscreen' => $data['fullscreen']]))
162		)
163	)
164	->addItem(
165		(new CDiv(
166			(new CDiv([
167				(new CDiv($dashboardGrid[0]))->addClass(ZBX_STYLE_CELL),
168				(new CDiv($dashboardGrid[1]))->addClass(ZBX_STYLE_CELL),
169				(new CDiv($dashboardGrid[2]))->addClass(ZBX_STYLE_CELL)
170			]))
171				->addClass(ZBX_STYLE_ROW)
172		))
173			->addClass(ZBX_STYLE_TABLE)
174			->addClass('widget-placeholder')
175	)
176	->show();
177
178/*
179 * Javascript
180 */
181// start refresh process
182$this->addPostJS('initPMaster("dashboard", '.CJs::encodeJson($widgetRefreshParams).');');
183
184// activating blinking
185$this->addPostJS('jqBlink.blink();');
186
187?>
188
189<script type="text/javascript">
190	/**
191	 * @see init.js add.popup event
192	 */
193	function addPopupValues(list) {
194		var favourites = {graphid: 1, itemid: 1, screenid: 1, slideshowid: 1, sysmapid: 1};
195
196		if (isset(list.object, favourites)) {
197			var favouriteIds = [];
198
199			for (var i = 0; i < list.values.length; i++) {
200				favouriteIds.push(list.values[i][list.object]);
201			}
202
203			sendAjaxData('zabbix.php?action=dashboard.favourite&operation=create', {
204				data: {
205					object: list.object,
206					'objectids[]': favouriteIds
207				}
208			});
209		}
210	}
211</script>
212