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
21require_once dirname(__FILE__).'/include/config.inc.php';
22
23$page['title'] = _('Configuration of icon mapping');
24$page['file'] = 'adm.iconmapping.php';
25
26require_once dirname(__FILE__).'/include/page_header.php';
27
28// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
29$fields = [
30	'iconmapid' =>		[T_ZBX_INT, O_OPT, P_SYS,			DB_ID,	'(isset({form}) && {form} == "update") || isset({delete})'],
31	'iconmap' =>		[T_ZBX_STR, O_OPT, null,			null,	'isset({add}) || isset({update})'],
32	'add' =>			[T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,	null],
33	'update' =>			[T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,	null],
34	'delete' =>			[T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,	null],
35	'clone' =>			[T_ZBX_STR, O_OPT, null,			null,	null],
36	'form' =>			[T_ZBX_STR, O_OPT, P_SYS,			null,	null],
37	'form_refresh' =>	[T_ZBX_INT, O_OPT, null,			null,	null]
38];
39check_fields($fields);
40
41/*
42 * Permissions
43 */
44if (hasRequest('iconmapid')) {
45	$iconMap = API::IconMap()->get([
46		'output' => ['iconmapid', 'name', 'default_iconid'],
47		'selectMappings' => ['inventory_link', 'expression', 'iconid', 'sortorder'],
48		'iconmapids' => getRequest('iconmapid'),
49		'editable' => true
50	]);
51	if (!$iconMap) {
52		access_deny();
53	}
54}
55
56/*
57 * Actions
58 */
59if (hasRequest('add') || hasRequest('update')) {
60	$_REQUEST['iconmap']['mappings'] = isset($_REQUEST['iconmap']['mappings'])
61		? $_REQUEST['iconmap']['mappings']
62		: [];
63
64	if (hasRequest('update')) {
65		$_REQUEST['iconmap']['iconmapid'] = $_REQUEST['iconmapid'];
66		$result = (bool) API::IconMap()->update($_REQUEST['iconmap']);
67		show_messages($result, _('Icon map updated'), _('Cannot update icon map'));
68	}
69	else {
70		$result = (bool) API::IconMap()->create($_REQUEST['iconmap']);
71		show_messages($result, _('Icon map created'), _('Cannot create icon map'));
72	}
73
74	if ($result) {
75		unset($_REQUEST['form']);
76	}
77}
78elseif (hasRequest('delete')) {
79	$result = (bool) API::IconMap()->delete([getRequest('iconmapid')]);
80
81	if ($result) {
82		unset($_REQUEST['form']);
83	}
84
85	show_messages($result, _('Icon map deleted'), _('Cannot delete icon map'));
86}
87elseif (isset($_REQUEST['clone'])) {
88	unset($_REQUEST['iconmapid']);
89	$_REQUEST['form'] = 'clone';
90}
91
92/*
93 * Display
94 */
95$data = [
96	'iconmapid' => getRequest('iconmapid'),
97	'iconList' => [],
98	'inventoryList' => []
99];
100
101$iconList = API::Image()->get([
102	'output' => ['imageid', 'name'],
103	'filter' => ['imagetype' => IMAGE_TYPE_ICON],
104	'preservekeys' => true
105]);
106order_result($iconList, 'name');
107
108foreach ($iconList as $icon) {
109	$data['iconList'][$icon['imageid']] = $icon['name'];
110}
111
112reset($iconList);
113$data['default_imageid'] = key($iconList);
114
115$inventoryFields = getHostInventories();
116foreach ($inventoryFields as $field) {
117	$data['inventoryList'][$field['nr']] = $field['title'];
118}
119
120if (isset($_REQUEST['form'])) {
121	if (hasRequest('form_refresh') || ($_REQUEST['form'] === 'clone')) {
122		$data['iconmap'] = getRequest('iconmap');
123	}
124	elseif (isset($_REQUEST['iconmapid'])) {
125		$data['iconmap'] = reset($iconMap);
126	}
127	else {
128		$firstIcon = reset($iconList);
129
130		$data['iconmap'] = [
131			'name' => '',
132			'default_iconid' => $firstIcon['imageid'],
133			'mappings' => []
134		];
135	}
136
137	$view = new CView('administration.general.iconmap.edit', $data);
138}
139else {
140	$data['iconmaps'] = API::IconMap()->get([
141		'output' => API_OUTPUT_EXTEND,
142		'editable' => true,
143		'preservekeys' => true,
144		'selectMappings' => API_OUTPUT_EXTEND
145	]);
146	order_result($data['iconmaps'], 'name');
147
148	$view = new CView('administration.general.iconmap.list', $data);
149}
150
151$view->render();
152$view->show();
153
154require_once dirname(__FILE__).'/include/page_footer.php';
155