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
22require_once dirname(__FILE__).'/include/config.inc.php';
23
24$page['title'] = _('Configuration of macros');
25$page['file'] = 'adm.macros.php';
26
27require_once dirname(__FILE__).'/include/page_header.php';
28
29// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
30$fields = [
31	'macros'		=> [T_ZBX_STR, O_OPT, P_SYS,			null,	null],
32	// actions
33	'update'		=> [T_ZBX_STR, O_OPT, P_SYS|P_ACT,	null,	null],
34	'form_refresh'	=> [T_ZBX_INT, O_OPT,	null,	null,	null]
35];
36check_fields($fields);
37
38/*
39 * Actions
40 */
41if (hasRequest('update')) {
42	$dbMacros = API::UserMacro()->get([
43		'output' => ['globalmacroid', 'macro', 'value'],
44		'globalmacro' => true,
45		'preservekeys' => true
46	]);
47
48	$macros = getRequest('macros', []);
49
50	// remove empty new macro lines
51	foreach ($macros as $idx => $macro) {
52		if (!array_key_exists('globalmacroid', $macro) && $macro['macro'] === '' && $macro['value'] === '') {
53			unset($macros[$idx]);
54		}
55	}
56
57	// update
58	$macrosToUpdate = [];
59	foreach ($macros as $idx => $macro) {
60		if (array_key_exists('globalmacroid', $macro) && array_key_exists($macro['globalmacroid'], $dbMacros)) {
61			$dbMacro = $dbMacros[$macro['globalmacroid']];
62
63			// remove item from new macros array
64			unset($macros[$idx], $dbMacros[$macro['globalmacroid']]);
65
66			// if the macro is unchanged - skip it
67			if ($dbMacro['macro'] === $macro['macro'] && $dbMacro['value'] === $macro['value']) {
68				continue;
69			}
70
71			$macrosToUpdate[] = $macro;
72		}
73	}
74
75	$result = true;
76
77	if ($macrosToUpdate || $dbMacros || $macros) {
78		DBstart();
79
80		// update
81		if ($macrosToUpdate) {
82			$result = (bool) API::UserMacro()->updateGlobal($macrosToUpdate);
83		}
84
85		// deletehe
86		if ($dbMacros) {
87			$result = $result && (bool) API::UserMacro()->deleteGlobal(array_keys($dbMacros));
88		}
89
90		// create
91		if ($macros) {
92			$result = $result && (bool) API::UserMacro()->createGlobal(array_values($macros));
93		}
94
95		$result = DBend($result);
96	}
97
98	show_messages($result, _('Macros updated'), _('Cannot update macros'));
99
100	if ($result) {
101		unset($_REQUEST['form_refresh']);
102	}
103}
104
105/*
106 * Display
107 */
108$data = [];
109
110if (hasRequest('form_refresh')) {
111	$data['macros'] = array_values(getRequest('macros', []));
112}
113else {
114	$data['macros'] = API::UserMacro()->get([
115		'output' => ['globalmacroid', 'macro', 'value'],
116		'globalmacro' => true
117	]);
118	$data['macros'] = array_values(order_macros($data['macros'], 'macro'));
119}
120
121if (!$data['macros']) {
122	$data['macros'][] = ['macro' => '', 'value' => ''];
123}
124
125(new CView('administration.general.macros.edit', $data))->render()->show();
126
127require_once dirname(__FILE__).'/include/page_footer.php';
128