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 CControllerWidgetActionLogView extends CControllerWidget { 23 24 public function __construct() { 25 parent::__construct(); 26 27 $this->setType(WIDGET_ACTION_LOG); 28 $this->setValidationRules([ 29 'name' => 'string', 30 'fields' => 'json' 31 ]); 32 } 33 34 protected function doAction() { 35 $fields = $this->getForm()->getFieldsData(); 36 37 list($sortfield, $sortorder) = self::getSorting($fields['sort_triggers']); 38 $alerts = $this->getAlerts($sortfield, $sortorder, $fields['show_lines']); 39 $db_users = $this->getDbUsers($alerts); 40 41 $actions = API::Action()->get([ 42 'output' => ['actionid', 'name'], 43 'actionids' => array_unique(zbx_objectValues($alerts, 'actionid')), 44 'preservekeys' => true 45 ]); 46 47 $this->setResponse(new CControllerResponseData([ 48 'name' => $this->getInput('name', $this->getDefaultHeader()), 49 'actions' => $actions, 50 'alerts' => $alerts, 51 'db_users' => $db_users, 52 'sortfield' => $sortfield, 53 'sortorder' => $sortorder, 54 'user' => [ 55 'debug_mode' => $this->getDebugMode() 56 ] 57 ])); 58 } 59 60 /** 61 * Get alerts. 62 * 63 * @param string $sortfield 64 * @param string $sortorder 65 * @param int $show_lines 66 * 67 * @return array 68 */ 69 private function getAlerts($sortfield, $sortorder, $show_lines) { 70 $alerts = API::Alert()->get([ 71 'output' => ['clock', 'sendto', 'subject', 'message', 'status', 'retries', 'error', 'userid', 'actionid', 72 'mediatypeid', 'alerttype' 73 ], 74 'selectMediatypes' => ['name', 'maxattempts'], 75 'sortfield' => $sortfield, 76 'sortorder' => $sortorder, 77 'limit' => $show_lines 78 ]); 79 80 foreach ($alerts as &$alert) { 81 $alert['description'] = ''; 82 if ($alert['mediatypeid'] != 0 && array_key_exists(0, $alert['mediatypes'])) { 83 $alert['description'] = $alert['mediatypes'][0]['name']; 84 $alert['maxattempts'] = $alert['mediatypes'][0]['maxattempts']; 85 } 86 unset($alert['mediatypes']); 87 88 $alert['action_type'] = ZBX_EVENT_HISTORY_ALERT; 89 } 90 unset($alert); 91 92 return $alerts; 93 } 94 95 /** 96 * Get users. 97 * 98 * @param array $alerts 99 * 100 * @return array 101 */ 102 private function getDbUsers(array $alerts) { 103 $userids = []; 104 105 foreach ($alerts as $alert) { 106 $userids[$alert['userid']] = true; 107 } 108 unset($userids[0]); 109 110 return $userids 111 ? API::User()->get([ 112 'output' => ['userid', 'alias', 'name', 'surname'], 113 'userids' => array_keys($userids), 114 'preservekeys' => true 115 ]) 116 : []; 117 } 118 119 /** 120 * Get sorting. 121 * 122 * @param int $sort_triggers 123 * 124 * @static 125 * 126 * @return array 127 */ 128 private static function getSorting($sort_triggers) { 129 switch ($sort_triggers) { 130 case SCREEN_SORT_TRIGGERS_TIME_ASC: 131 return ['clock', ZBX_SORT_UP]; 132 133 case SCREEN_SORT_TRIGGERS_TIME_DESC: 134 default: 135 return ['clock', ZBX_SORT_DOWN]; 136 137 case SCREEN_SORT_TRIGGERS_TYPE_ASC: 138 return ['mediatypeid', ZBX_SORT_UP]; 139 140 case SCREEN_SORT_TRIGGERS_TYPE_DESC: 141 return ['mediatypeid', ZBX_SORT_DOWN]; 142 143 case SCREEN_SORT_TRIGGERS_STATUS_ASC: 144 return ['status', ZBX_SORT_UP]; 145 146 case SCREEN_SORT_TRIGGERS_STATUS_DESC: 147 return ['status', ZBX_SORT_DOWN]; 148 149 case SCREEN_SORT_TRIGGERS_RECIPIENT_ASC: 150 return ['sendto', ZBX_SORT_UP]; 151 152 case SCREEN_SORT_TRIGGERS_RECIPIENT_DESC: 153 return ['sendto', ZBX_SORT_DOWN]; 154 } 155 } 156} 157