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 CControllerValuemapList extends CController {
23
24	protected function init() {
25		$this->disableSIDValidation();
26	}
27
28	protected function checkInput() {
29		$fields = [
30			'uncheck' => 'in 1'
31		];
32
33		$ret = $this->validateInput($fields);
34
35		if (!$ret) {
36			$this->setResponse(new CControllerResponseFatal());
37		}
38
39		return $ret;
40	}
41
42	protected function checkPermissions() {
43		return ($this->getUserType() == USER_TYPE_SUPER_ADMIN);
44	}
45
46	protected function doAction() {
47		$config = select_config();
48
49		$sortfield = getRequest('sort', CProfile::get('web.valuemap.list.sort', 'name'));
50		$sortorder = getRequest('sortorder', CProfile::get('web.valuemap.list.sortorder', ZBX_SORT_UP));
51
52		CProfile::update('web.valuemap.list.sort', $sortfield, PROFILE_TYPE_STR);
53		CProfile::update('web.valuemap.list.sortorder', $sortorder, PROFILE_TYPE_STR);
54
55		$data = [
56			'sort' => $sortfield,
57			'sortorder' => $sortorder,
58			'uncheck' => $this->hasInput('uncheck')
59		];
60
61		$data['valuemaps'] = API::ValueMap()->get([
62			'output' => ['valuemapid', 'name'],
63			'selectMappings' => ['value', 'newvalue'],
64			'sortfield' => $sortfield,
65			'limit' => $config['search_limit'] + 1
66		]);
67
68		// data sort and pager
69		order_result($data['valuemaps'], $sortfield, $sortorder);
70
71		$data['page'] = getRequest('page', 1);
72		CPagerHelper::savePage('valuemap.list', $data['page']);
73		$data['paging'] = CPagerHelper::paginate($data['page'], $data['valuemaps'], $sortorder,
74			(new CUrl('zabbix.php'))->setArgument('action', $this->getAction())
75		);
76
77		foreach ($data['valuemaps'] as &$valuemap) {
78			order_result($valuemap['mappings'], 'value');
79
80			$valuemap['used_in_items'] =
81				(bool) API::Item()->get([
82					'output' => [],
83					'webitems' => true,
84					'filter' => ['valuemapid' => $valuemap['valuemapid']],
85					'limit' => 1
86				])
87				|| (bool) API::ItemPrototype()->get([
88					'output' => [],
89					'filter' => ['valuemapid' => $valuemap['valuemapid']],
90					'limit' => 1
91				]);
92		}
93		unset($valuemap);
94
95		$response = new CControllerResponseData($data);
96		$response->setTitle(_('Configuration of value mapping'));
97
98		$this->setResponse($response);
99	}
100}
101