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 CScreenActions extends CScreenBase {
23
24	/**
25	 * Process screen.
26	 *
27	 * @return CDiv (screen inside container)
28	 */
29	public function get() {
30		$sortfield = 'clock';
31		$sortorder = ZBX_SORT_DOWN;
32
33		switch ($this->screenitem['sort_triggers']) {
34			case SCREEN_SORT_TRIGGERS_TIME_ASC:
35				$sortfield = 'clock';
36				$sortorder = ZBX_SORT_UP;
37				break;
38
39			case SCREEN_SORT_TRIGGERS_TIME_DESC:
40				$sortfield = 'clock';
41				$sortorder = ZBX_SORT_DOWN;
42				break;
43
44			case SCREEN_SORT_TRIGGERS_TYPE_ASC:
45				$sortfield = 'mediatypeid';
46				$sortorder = ZBX_SORT_UP;
47				break;
48
49			case SCREEN_SORT_TRIGGERS_TYPE_DESC:
50				$sortfield = 'mediatypeid';
51				$sortorder = ZBX_SORT_DOWN;
52				break;
53
54			case SCREEN_SORT_TRIGGERS_STATUS_ASC:
55				$sortfield = 'status';
56				$sortorder = ZBX_SORT_UP;
57				break;
58
59			case SCREEN_SORT_TRIGGERS_STATUS_DESC:
60				$sortfield = 'status';
61				$sortorder = ZBX_SORT_DOWN;
62				break;
63
64			case SCREEN_SORT_TRIGGERS_RECIPIENT_ASC:
65				$sortfield = 'sendto';
66				$sortorder = ZBX_SORT_UP;
67				break;
68
69			case SCREEN_SORT_TRIGGERS_RECIPIENT_DESC:
70				$sortfield = 'sendto';
71				$sortorder = ZBX_SORT_DOWN;
72				break;
73		}
74
75		$alerts = API::Alert()->get([
76			'output' => ['clock', 'sendto', 'subject', 'message', 'status', 'retries', 'error', 'userid', 'actionid',
77				'mediatypeid', 'alerttype'
78			],
79			'selectMediatypes' => ['name', 'maxattempts'],
80			'filter' => [
81				'alerttype' => ALERT_TYPE_MESSAGE
82			],
83			'time_from' => $this->timeline['from_ts'],
84			'time_till' => $this->timeline['to_ts'],
85			'sortfield' => $sortfield,
86			'sortorder' => $sortorder,
87			'limit' => $this->screenitem['elements']
88		]);
89
90		$userids = [];
91		foreach ($alerts as $alert) {
92			if ($alert['userid'] != 0) {
93				$userids[$alert['userid']] = true;
94			}
95		}
96
97		$db_users = $userids
98			? API::User()->get([
99				'output' => ['userid', 'alias', 'name', 'surname'],
100				'userids' => array_keys($userids),
101				'preservekeys' => true
102			])
103			: [];
104
105		// indicator of sort field
106		$sort_div = (new CSpan())->addClass(($sortorder === ZBX_SORT_DOWN) ? ZBX_STYLE_ARROW_DOWN : ZBX_STYLE_ARROW_UP);
107
108		// create alert table
109		$table = (new CTableInfo())
110			->setHeader([
111				($sortfield === 'clock') ? [('Time'), $sort_div] : _('Time'),
112				_('Action'),
113				($sortfield === 'mediatypeid') ? [_('Type'), $sort_div] : _('Type'),
114				($sortfield === 'sendto') ? [_('Recipient'), $sort_div] : _('Recipient'),
115				_('Message'),
116				($sortfield === 'status') ? [_('Status'), $sort_div] : _('Status'),
117				_('Info')
118			]);
119
120		$actions = API::Action()->get([
121			'output' => ['actionid', 'name'],
122			'actionids' => array_unique(zbx_objectValues($alerts, 'actionid')),
123			'preservekeys' => true
124		]);
125
126		foreach ($alerts as $alert) {
127			if ($alert['alerttype'] == ALERT_TYPE_MESSAGE && array_key_exists(0, $alert['mediatypes'])
128					&& ($alert['status'] == ALERT_STATUS_NOT_SENT || $alert['status'] == ALERT_STATUS_NEW)) {
129				$info_icons = makeWarningIcon(_n('%1$s retry left', '%1$s retries left',
130					$alert['mediatypes'][0]['maxattempts'] - $alert['retries'])
131				);
132			}
133			elseif ($alert['error'] !== '') {
134				$info_icons = makeErrorIcon($alert['error']);
135			}
136			else {
137				$info_icons = null;
138			}
139
140			$alert['action_type'] = ZBX_EVENT_HISTORY_ALERT;
141
142			$action_type = '';
143			if ($alert['mediatypeid'] != 0 && array_key_exists(0, $alert['mediatypes'])) {
144				$action_type = $alert['mediatypes'][0]['name'];
145				$alert['maxattempts'] = $alert['mediatypes'][0]['maxattempts'];
146			}
147
148			$action_name = '';
149			if (array_key_exists($alert['actionid'], $actions)) {
150				$action_name = $actions[$alert['actionid']]['name'];
151			}
152
153			$table->addRow([
154				zbx_date2str(DATE_TIME_FORMAT_SECONDS, $alert['clock']),
155				$action_name,
156				$action_type,
157				makeEventDetailsTableUser($alert, $db_users),
158				[bold($alert['subject']), BR(), BR(), zbx_nl2br($alert['message'])],
159				makeActionTableStatus($alert),
160				makeInformationList($info_icons)
161			]);
162		}
163
164		$footer = (new CList())
165			->addItem(_s('Updated: %1$s', zbx_date2str(TIME_FORMAT_SECONDS)))
166			->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT);
167
168		return $this->getOutput((new CUiWidget(uniqid(), [$table, $footer]))->setHeader(_('Action log')));
169	}
170}
171