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 CWidgetConfig {
23
24	/**
25	 * Return list of all widget types with names.
26	 *
27	 * @static
28	 *
29	 * @return array
30	 */
31	public static function getKnownWidgetTypes() {
32		return [
33			WIDGET_ACTION_LOG			=> _('Action log'),
34			WIDGET_CLOCK				=> _('Clock'),
35			WIDGET_DATA_OVER			=> _('Data overview'),
36			WIDGET_DISCOVERY			=> _('Discovery status'),
37			WIDGET_FAV_GRAPHS			=> _('Favourite graphs'),
38			WIDGET_FAV_MAPS				=> _('Favourite maps'),
39			WIDGET_FAV_SCREENS			=> _('Favourite screens'),
40			WIDGET_GRAPH				=> _('Graph (classic)'),
41			WIDGET_GRAPH_PROTOTYPE		=> _('Graph prototype'),
42			WIDGET_HOST_AVAIL			=> _('Host availability'),
43			WIDGET_MAP					=> _('Map'),
44			WIDGET_NAV_TREE				=> _('Map navigation tree'),
45			WIDGET_PLAIN_TEXT			=> _('Plain text'),
46			WIDGET_PROBLEM_HOSTS		=> _('Problem hosts'),
47			WIDGET_PROBLEMS				=> _('Problems'),
48			WIDGET_PROBLEMS_BY_SV		=> _('Problems by severity'),
49			WIDGET_SVG_GRAPH			=> _('Graph'),
50			WIDGET_SYSTEM_INFO			=> _('System information'),
51			WIDGET_TRIG_OVER			=> _('Trigger overview'),
52			WIDGET_URL					=> _('URL'),
53			WIDGET_WEB					=> _('Web monitoring')
54		];
55	}
56
57	/**
58	 * Get default widget dimensions.
59	 *
60	 * @static
61	 *
62	 * @return array
63	 */
64	private static function getDefaultDimensions() {
65		return [
66			WIDGET_ACTION_LOG			=> ['width' => 12,	'height' => 5],
67			WIDGET_CLOCK				=> ['width' => 4,	'height' => 3],
68			WIDGET_DATA_OVER			=> ['width' => 12,	'height' => 5],
69			WIDGET_DISCOVERY			=> ['width' => 6,	'height' => 3],
70			WIDGET_FAV_GRAPHS			=> ['width' => 4,	'height' => 3],
71			WIDGET_FAV_MAPS				=> ['width' => 4,	'height' => 3],
72			WIDGET_FAV_SCREENS			=> ['width' => 4,	'height' => 3],
73			WIDGET_GRAPH				=> ['width' => 12,	'height' => 5],
74			WIDGET_GRAPH_PROTOTYPE		=> ['width' => 16,	'height' => 5],
75			WIDGET_HOST_AVAIL			=> ['width' => 6,	'height' => 3],
76			WIDGET_MAP					=> ['width' => 18,	'height' => 5],
77			WIDGET_NAV_TREE				=> ['width' => 6,	'height' => 5],
78			WIDGET_PLAIN_TEXT			=> ['width' => 6,	'height' => 3],
79			WIDGET_PROBLEM_HOSTS		=> ['width' => 12,	'height' => 5],
80			WIDGET_PROBLEMS				=> ['width' => 12,	'height' => 5],
81			WIDGET_PROBLEMS_BY_SV		=> ['width' => 12,	'height' => 5],
82			WIDGET_SVG_GRAPH			=> ['width' => 12,	'height' => 5],
83			WIDGET_SYSTEM_INFO			=> ['width' => 12,	'height' => 5],
84			WIDGET_TRIG_OVER			=> ['width' => 12,	'height' => 5],
85			WIDGET_URL					=> ['width' => 12,	'height' => 5],
86			WIDGET_WEB					=> ['width' => 6,	'height' => 3]
87		];
88	}
89
90	/**
91	 * Return default values for widgets.
92	 *
93	 * @static
94	 *
95	 * @return array
96	 */
97	public static function getDefaults() {
98		$ret = [];
99		$dimensions = self::getDefaultDimensions();
100
101		foreach (self::getKnownWidgetTypes() as $type => $name) {
102			$ret[$type] = [
103				'header' => $name,
104				'size' => $dimensions[$type],
105				'iterator' => self::isIterator($type)
106			];
107		}
108
109		return $ret;
110	}
111
112	/**
113	 * Return default refresh rate for widget type.
114	 *
115	 * @static
116	 *
117	 * @param int $type  WIDGET_ constant
118	 *
119	 * @return int  default refresh rate, "0" for no refresh
120	 */
121	public static function getDefaultRfRate($type) {
122		switch ($type) {
123			case WIDGET_ACTION_LOG:
124			case WIDGET_DATA_OVER:
125			case WIDGET_DISCOVERY:
126			case WIDGET_GRAPH:
127			case WIDGET_GRAPH_PROTOTYPE:
128			case WIDGET_PLAIN_TEXT:
129			case WIDGET_PROBLEM_HOSTS:
130			case WIDGET_PROBLEMS:
131			case WIDGET_PROBLEMS_BY_SV:
132			case WIDGET_SVG_GRAPH:
133			case WIDGET_TRIG_OVER:
134			case WIDGET_WEB:
135				return SEC_PER_MIN;
136
137			case WIDGET_CLOCK:
138			case WIDGET_FAV_GRAPHS:
139			case WIDGET_FAV_MAPS:
140			case WIDGET_FAV_SCREENS:
141			case WIDGET_HOST_AVAIL:
142			case WIDGET_MAP:
143			case WIDGET_NAV_TREE:
144			case WIDGET_SYSTEM_INFO:
145				return 15 * SEC_PER_MIN;
146
147			case WIDGET_URL:
148				return 0;
149		}
150	}
151
152	/**
153	 * Get all possible widget refresh intervals.
154	 *
155	 * @return array
156	 */
157	public static function getRfRates() {
158		return [
159			0 => _('No refresh'),
160			SEC_PER_MIN / 6 => _n('%1$s second', '%1$s seconds', 10),
161			SEC_PER_MIN / 2 => _n('%1$s second', '%1$s seconds', 30),
162			SEC_PER_MIN => _n('%1$s minute', '%1$s minutes', 1),
163			SEC_PER_MIN * 2 => _n('%1$s minute', '%1$s minutes', 2),
164			SEC_PER_MIN * 10 => _n('%1$s minute', '%1$s minutes', 10),
165			SEC_PER_MIN * 15 => _n('%1$s minute', '%1$s minutes', 15)
166		];
167	}
168
169	/**
170	 * Detect if widget uses time selector.
171	 *
172	 * @static
173	 *
174	 * @param array $widget
175	 * @param array $widget[type]
176	 * @param array $widget[fields]
177	 *
178	 * @return bool
179	 */
180	public static function usesTimeSelector(array $widget) {
181		switch ($widget['type']) {
182			case WIDGET_GRAPH:
183			case WIDGET_GRAPH_PROTOTYPE:
184				return true;
185
186			case WIDGET_SVG_GRAPH:
187				return !CWidgetFormSvgGraph::hasOverrideTime($widget['fields']);
188
189			default:
190				return false;
191		}
192	}
193
194	public static function isIterator($type) {
195		switch ($type) {
196			case WIDGET_GRAPH_PROTOTYPE:
197				return true;
198
199			default:
200				return false;
201		}
202	}
203
204	/**
205	 * Detect if widget dialogue should be sticked to top instead of being centered vertically.
206	 *
207	 * @param string $type  Widget type
208	 *
209	 * @return bool
210	 */
211	public static function getDialogueStickToTop($type) {
212		switch ($type) {
213			case WIDGET_SVG_GRAPH:
214				return true;
215
216			default:
217				return false;
218		}
219	}
220
221	/**
222	 * Detect if widget has padding or not
223	 *
224	 * @static
225	 *
226	 * @param string $type       Widget type
227	 * @param array  $fields     Widget form fields
228	 * @param int    $view_mode  Widget view mode. ZBX_WIDGET_VIEW_MODE_NORMAL by default
229	 *
230	 * @return bool
231	 */
232	private static function hasPadding($type, $fields, $view_mode) {
233		if ($view_mode == ZBX_WIDGET_VIEW_MODE_HIDDEN_HEADER) {
234			switch ($type) {
235				case WIDGET_CLOCK:
236				case WIDGET_GRAPH:
237				case WIDGET_MAP:
238				case WIDGET_SVG_GRAPH:
239					return true;
240
241				default:
242					return false;
243			}
244		}
245		else {
246			switch ($type) {
247				case WIDGET_HOST_AVAIL:
248					return (count($fields['interface_type']) != 1);
249
250				case WIDGET_PROBLEMS_BY_SV:
251					return $fields['show_type'] != WIDGET_PROBLEMS_BY_SV_SHOW_TOTALS;
252
253				case WIDGET_GRAPH_PROTOTYPE:
254				case WIDGET_URL:
255					return false;
256
257				default:
258					return true;
259			}
260		}
261	}
262
263	/**
264	 * Get widget configuration based on widget type, fields and current view mode.
265	 *
266	 * @param string $type       Widget type
267	 * @param array  $fields     Widget form fields
268	 * @param int    $view_mode  Widget view mode
269	 *
270	 * @return array
271	 */
272	public static function getConfiguration($type, $fields, $view_mode) {
273		return [
274			'padding' => self::hasPadding($type, $fields, $view_mode)
275		];
276	}
277
278	/**
279	 * Return Form object for widget with provided data.
280	 *
281	 * @static
282	 *
283	 * @param string $type  Widget type - 'WIDGET_' constant.
284	 * @param string $data  JSON string with widget fields.
285	 *
286	 * @return CWidgetForm
287	 */
288	public static function getForm($type, $data) {
289		switch ($type) {
290			case WIDGET_ACTION_LOG:
291				return new CWidgetFormActionLog($data);
292
293			case WIDGET_CLOCK:
294				return new CWidgetFormClock($data);
295
296			case WIDGET_DATA_OVER:
297				return new CWidgetFormDataOver($data);
298
299			case WIDGET_GRAPH:
300				return new CWidgetFormGraph($data);
301
302			case WIDGET_GRAPH_PROTOTYPE:
303				return new CWidgetFormGraphPrototype($data);
304
305			case WIDGET_HOST_AVAIL:
306				return new CWidgetFormHostAvail($data);
307
308			case WIDGET_MAP:
309				return new CWidgetFormMap($data);
310
311			case WIDGET_NAV_TREE:
312				return new CWidgetFormNavTree($data);
313
314			case WIDGET_PLAIN_TEXT:
315				return new CWidgetFormPlainText($data);
316
317			case WIDGET_PROBLEM_HOSTS:
318				return new CWidgetFormProblemHosts($data);
319
320			case WIDGET_PROBLEMS:
321				return new CWidgetFormProblems($data);
322
323			case WIDGET_PROBLEMS_BY_SV:
324				return new CWidgetFormProblemsBySv($data);
325
326			case WIDGET_SVG_GRAPH:
327				return new CWidgetFormSvgGraph($data);
328
329			case WIDGET_TRIG_OVER:
330				return new CWidgetFormTrigOver($data);
331
332			case WIDGET_URL:
333				return new CWidgetFormUrl($data);
334
335			case WIDGET_WEB:
336				return new CWidgetFormWeb($data);
337
338			default:
339				return new CWidgetForm($data, $type);
340		}
341	}
342}
343