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 CControllerWidgetProblemsView extends CControllerWidget {
23
24	public function __construct() {
25		parent::__construct();
26
27		$this->setType(WIDGET_PROBLEMS);
28		$this->setValidationRules([
29			'name' => 'string',
30			'fields' => 'json',
31			'initial_load' => 'in 0,1'
32		]);
33	}
34
35	protected function doAction() {
36		$fields = $this->getForm()->getFieldsData();
37
38		$data = CScreenProblem::getData([
39			'show' => $fields['show'],
40			'groupids' => $fields['groupids'],
41			'exclude_groupids' => $fields['exclude_groupids'],
42			'hostids' => $fields['hostids'],
43			'name' => $fields['problem'],
44			'severities' => $fields['severities'],
45			'evaltype' => $fields['evaltype'],
46			'tags' => $fields['tags'],
47			'show_suppressed' => $fields['show_suppressed'],
48			'unacknowledged' => $fields['unacknowledged'],
49			'show_opdata' => $fields['show_opdata']
50		]);
51		list($sortfield, $sortorder) = self::getSorting($fields['sort_triggers']);
52		$data = CScreenProblem::sortData($data, $sortfield, $sortorder);
53
54		if (count($data['problems']) > $fields['show_lines']) {
55			$info = _n('%1$d of %3$d%2$s problem is shown', '%1$d of %3$d%2$s problems are shown',
56				min($fields['show_lines'], count($data['problems'])),
57				(count($data['problems']) > CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT)) ? '+' : '',
58				min(CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT), count($data['problems']))
59			);
60		}
61		else {
62			$info = '';
63		}
64		$data['problems'] = array_slice($data['problems'], 0, $fields['show_lines'], true);
65
66		$data = CScreenProblem::makeData($data, [
67			'show' => $fields['show'],
68			'details' => 0,
69			'show_opdata' => $fields['show_opdata']
70		]);
71
72		if ($fields['show_tags']) {
73			$data['tags'] = makeTags($data['problems'], true, 'eventid', $fields['show_tags'], $fields['tags'],
74				$fields['tag_name_format'], $fields['tag_priority']
75			);
76		}
77
78		if ($data['problems']) {
79			$data['triggers_hosts'] = getTriggersHostsList($data['triggers']);
80		}
81
82		$this->setResponse(new CControllerResponseData([
83			'name' => $this->getInput('name', $this->getDefaultName()),
84			'initial_load' => (bool) $this->getInput('initial_load', 0),
85			'fields' => [
86				'show' => $fields['show'],
87				'show_lines' => $fields['show_lines'],
88				'show_tags' => $fields['show_tags'],
89				'show_timeline' => $fields['show_timeline'],
90				'tags' => $fields['tags'],
91				'tag_name_format' => $fields['tag_name_format'],
92				'tag_priority' => $fields['tag_priority'],
93				'show_opdata' => $fields['show_opdata']
94			],
95			'data' => $data,
96			'info' => $info,
97			'sortfield' => $sortfield,
98			'sortorder' => $sortorder,
99			'user' => [
100				'debug_mode' => $this->getDebugMode()
101			],
102			'config' => [
103				'problem_ack_style' => CSettingsHelper::get(CSettingsHelper::PROBLEM_ACK_STYLE),
104				'problem_unack_style' => CSettingsHelper::get(CSettingsHelper::PROBLEM_UNACK_STYLE),
105				'blink_period' => CSettingsHelper::get(CSettingsHelper::BLINK_PERIOD)
106			],
107			'allowed_ui_problems' => $this->checkAccess(CRoleHelper::UI_MONITORING_PROBLEMS),
108			'allowed_add_comments' => $this->checkAccess(CRoleHelper::ACTIONS_ADD_PROBLEM_COMMENTS),
109			'allowed_change_severity' => $this->checkAccess(CRoleHelper::ACTIONS_CHANGE_SEVERITY),
110			'allowed_acknowledge' => $this->checkAccess(CRoleHelper::ACTIONS_ACKNOWLEDGE_PROBLEMS),
111			'allowed_close' => $this->checkAccess(CRoleHelper::ACTIONS_CLOSE_PROBLEMS)
112		]));
113	}
114
115	/**
116	 * Get sorting.
117	 *
118	 * @param int $sort_triggers
119	 *
120	 * @static
121	 *
122	 * @return array
123	 */
124	private static function getSorting($sort_triggers)
125	{
126		switch ($sort_triggers) {
127			case SCREEN_SORT_TRIGGERS_TIME_ASC:
128				return ['clock', ZBX_SORT_UP];
129
130			case SCREEN_SORT_TRIGGERS_TIME_DESC:
131			default:
132				return ['clock', ZBX_SORT_DOWN];
133
134			case SCREEN_SORT_TRIGGERS_SEVERITY_ASC:
135				return ['severity', ZBX_SORT_UP];
136
137			case SCREEN_SORT_TRIGGERS_SEVERITY_DESC:
138				return ['severity', ZBX_SORT_DOWN];
139
140			case SCREEN_SORT_TRIGGERS_HOST_NAME_ASC:
141				return ['host', ZBX_SORT_UP];
142
143			case SCREEN_SORT_TRIGGERS_HOST_NAME_DESC:
144				return ['host', ZBX_SORT_DOWN];
145
146			case SCREEN_SORT_TRIGGERS_NAME_ASC:
147				return ['name', ZBX_SORT_UP];
148
149			case SCREEN_SORT_TRIGGERS_NAME_DESC:
150				return ['name', ZBX_SORT_DOWN];
151		}
152	}
153}
154