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__).'/../../blocks.inc.php';
23
24class CScreenHostTriggers extends CScreenBase {
25
26	/**
27	 * Process screen.
28	 *
29	 * @return CDiv (screen inside container)
30	 */
31	public function get() {
32		$params = [
33			'groupids' => null,
34			'hostids' => null,
35			'maintenance' => null,
36			'trigger_name' => '',
37			'severity' => null,
38			'limit' => $this->screenitem['elements']
39		];
40
41		// by default triggers are sorted by date desc, do we need to override this?
42		switch ($this->screenitem['sort_triggers']) {
43			case SCREEN_SORT_TRIGGERS_DATE_DESC:
44				$params['sortfield'] = 'lastchange';
45				$params['sortorder'] = ZBX_SORT_DOWN;
46				break;
47			case SCREEN_SORT_TRIGGERS_SEVERITY_DESC:
48				$params['sortfield'] = 'priority';
49				$params['sortorder'] = ZBX_SORT_DOWN;
50				break;
51			case SCREEN_SORT_TRIGGERS_HOST_NAME_ASC:
52				// a little black magic here - there is no such field 'hostname' in 'triggers',
53				// but API has a special case for sorting by hostname
54				$params['sortfield'] = 'hostname';
55				$params['sortorder'] = ZBX_SORT_UP;
56				break;
57		}
58
59		if ($this->screenitem['resourceid'] != 0) {
60			$hosts = API::Host()->get([
61				'output' => ['name'],
62				'hostids' => [$this->screenitem['resourceid']]
63			]);
64
65			$header = (new CDiv([
66				new CTag('h4', true, _('Host issues')),
67				(new CList())->addItem([_('Host'), ':', SPACE, $hosts[0]['name']])
68			]))->addClass(ZBX_STYLE_DASHBRD_WIDGET_HEAD);
69
70			$params['hostids'] = $this->screenitem['resourceid'];
71		}
72		else {
73			$groupid = getRequest('tr_groupid', CProfile::get('web.screens.tr_groupid', 0));
74			$hostid = getRequest('tr_hostid', CProfile::get('web.screens.tr_hostid', 0));
75
76			CProfile::update('web.screens.tr_groupid', $groupid, PROFILE_TYPE_ID);
77			CProfile::update('web.screens.tr_hostid', $hostid, PROFILE_TYPE_ID);
78
79			// get groups
80			$groups = API::HostGroup()->get([
81				'output' => ['name'],
82				'monitored_hosts' => true,
83				'preservekeys' => true
84			]);
85			order_result($groups, 'name');
86
87			foreach ($groups as &$group) {
88				$group = $group['name'];
89			}
90			unset($group);
91
92			// get hsots
93			$options = [
94				'output' => ['name'],
95				'monitored_hosts' => true,
96				'preservekeys' => true
97			];
98			if ($groupid != 0) {
99				$options['groupids'] = [$groupid];
100			}
101			$hosts = API::Host()->get($options);
102			order_result($hosts, 'name');
103
104			foreach ($hosts as &$host) {
105				$host = $host['name'];
106			}
107			unset($host);
108
109			$groups = [0 => _('all')] + $groups;
110			$hosts = [0 => _('all')] + $hosts;
111
112			if (!array_key_exists($hostid, $hosts)) {
113				$hostid = 0;
114			}
115
116			if ($groupid != 0) {
117				$params['groupids'] = $groupid;
118			}
119			if ($hostid != 0) {
120				$params['hostids'] = $hostid;
121			}
122
123			$groups_cb = (new CComboBox('tr_groupid', $groupid, 'submit()', $groups))
124				->setEnabled($this->mode != SCREEN_MODE_EDIT);
125			$hosts_cb = (new CComboBox('tr_hostid', $hostid, 'submit()', $hosts))
126				->setEnabled($this->mode != SCREEN_MODE_EDIT);
127
128			$header = (new CDiv([
129				new CTag('h4', true, _('Host issues')),
130				(new CForm('get', $this->pageFile))
131					->addItem(
132						(new CList())
133							->addItem([_('Group'), SPACE, $groups_cb])
134							->addItem(SPACE)
135							->addItem([_('Host'), SPACE, $hosts_cb])
136					)
137			]))->addClass(ZBX_STYLE_DASHBRD_WIDGET_HEAD);
138		}
139
140		list($table, $info) = make_latest_issues($params, $this->pageFile.'?screenid='.$this->screenid);
141
142		$footer = (new CList())
143			->addItem($info)
144			->addItem(_s('Updated: %s', zbx_date2str(TIME_FORMAT_SECONDS)))
145			->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT);
146
147		return $this->getOutput(new CUiWidget('hat_trstatus', [$header, $table, $footer]));
148	}
149}
150