1<?php declare(strict_types=1);
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 CControllerTemplateDashboardList extends CController {
23
24	protected function init() {
25		$this->disableSIDValidation();
26	}
27
28	protected function checkInput() {
29		$fields = [
30			'templateid' => 'required|db dashboard.templateid',
31			'sort' => 'in name',
32			'sortorder' => 'in '.ZBX_SORT_DOWN.','.ZBX_SORT_UP,
33			'uncheck' => 'in 1',
34			'page' => 'ge 1'
35		];
36
37		$ret = $this->validateInput($fields);
38
39		if (!$ret) {
40			$this->setResponse(new CControllerResponseFatal());
41		}
42
43		return $ret;
44	}
45
46	protected function checkPermissions() {
47		if ($this->getUserType() < USER_TYPE_ZABBIX_ADMIN) {
48			return false;
49		}
50
51		return isWritableHostTemplates((array) $this->getInput('templateid'));
52	}
53
54	protected function doAction() {
55		$sort_field = $this->getInput('sort', CProfile::get('web.templates.dashboard.list.sort', 'name'));
56		$sort_order = $this->getInput('sortorder', CProfile::get('web.templates.dashboard.list.sortorder', ZBX_SORT_UP));
57
58		CProfile::update('web.templates.dashboard.list.sort', $sort_field, PROFILE_TYPE_STR);
59		CProfile::update('web.templates.dashboard.list.sortorder', $sort_order, PROFILE_TYPE_STR);
60
61		$limit = CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT) + 1;
62
63		$dashboards = API::TemplateDashboard()->get([
64			'output' => ['name', 'templateid'],
65			'templateids' => [$this->getInput('templateid')],
66			'sortfield' => $sort_field,
67			'limit' => $limit,
68			'editable' => true,
69			'preservekeys' => true
70		]);
71
72		CArrayHelper::sort($dashboards, [['field' => $sort_field, 'order' => $sort_order]]);
73
74		// pager
75		$page_num = getRequest('page', 1);
76		CPagerHelper::savePage('template.dashboard.list', $page_num);
77		$paging = CPagerHelper::paginate($page_num, $dashboards, $sort_order,
78			(new CUrl('zabbix.php'))
79				->setArgument('action', $this->getAction())
80				->setArgument('templateid', $this->getInput('templateid'))
81		);
82
83		$data = [
84			'templateid' => $this->getInput('templateid'),
85			'uncheck' => $this->hasInput('uncheck'),
86			'sort' => $sort_field,
87			'sortorder' => $sort_order,
88			'dashboards' => $dashboards,
89			'paging' => $paging
90		];
91
92		$response = new CControllerResponseData($data);
93		$response->setTitle(_('Configuration of dashboards'));
94		$this->setResponse($response);
95	}
96}
97