1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class for Dashboard Plain-text widget view.
*/
class CControllerWidgetPlainTextView extends CControllerWidget {
public function __construct() {
parent::__construct();
$this->setType(WIDGET_PLAIN_TEXT);
$this->setValidationRules([
'name' => 'string',
'fields' => 'json',
'dynamic_hostid' => 'db hosts.hostid'
]);
}
protected function doAction() {
$fields = $this->getForm()->getFieldsData();
$error = null;
$dynamic_widget_name = $this->getDefaultHeader();
$same_host = true;
$items = [];
$histories = [];
if ($fields['itemids']) {
$items = API::Item()->get([
'output' => ['itemid', 'hostid', 'name', 'key_', 'value_type', 'valuemapid'],
'selectHosts' => ['name'],
'itemids' => $fields['itemids'],
'webitems' => true,
'preservekeys' => true
]);
$dynamic_hostid = $this->getInput('dynamic_hostid', 0);
$keys = [];
foreach ($items as $item) {
$keys[$item['key_']] = true;
}
if ($items && $fields['dynamic'] && $dynamic_hostid) {
$items = API::Item()->get([
'output' => ['itemid', 'hostid', 'name', 'key_', 'value_type', 'valuemapid'],
'selectHosts' => ['name'],
'filter' => [
'hostid' => $dynamic_hostid,
'key_' => array_keys($keys)
],
'webitems' => true,
'preservekeys' => true
]);
}
}
if (!$items) {
$error = _('No permissions to referred object or it does not exist!');
}
else {
$items = CMacrosResolverHelper::resolveItemNames($items);
$histories = Manager::History()->getLastValues($items, $fields['show_lines']);
if ($histories) {
$histories = call_user_func_array('array_merge', $histories);
foreach ($histories as &$history) {
switch ($items[$history['itemid']]['value_type']) {
case ITEM_VALUE_TYPE_FLOAT:
sscanf($history['value'], '%f', $history['value']);
break;
case ITEM_VALUE_TYPE_TEXT:
case ITEM_VALUE_TYPE_STR:
case ITEM_VALUE_TYPE_LOG:
if ($fields['show_as_html']) {
$history['value'] = new CJsScript($history['value']);
}
break;
}
if ($items[$history['itemid']]['valuemapid'] != 0) {
$history['value'] = applyValueMap($history['value'], $items[$history['itemid']]['valuemapid']);
}
if (!$fields['show_as_html']) {
$history['value'] = new CPre($history['value']);
}
}
unset($history);
}
CArrayHelper::sort($histories, [
['field' => 'clock', 'order' => ZBX_SORT_DOWN],
['field' => 'ns', 'order' => ZBX_SORT_DOWN]
]);
$host_name = '';
foreach ($items as $item) {
if ($host_name === '') {
$host_name = $item['hosts'][0]['name'];
}
elseif ($host_name !== $item['hosts'][0]['name']) {
$same_host = false;
}
}
$items_count = count($items);
if ($items_count == 1) {
$item = reset($items);
$dynamic_widget_name = $host_name.NAME_DELIMITER.$item['name_expanded'];
}
elseif ($same_host && $items_count > 1) {
$dynamic_widget_name = $host_name.NAME_DELIMITER._n('%1$s item', '%1$s items', $items_count);
}
}
$this->setResponse(new CControllerResponseData([
'name' => $this->getInput('name', $dynamic_widget_name),
'items' => $items,
'histories' => $histories,
'style' => $fields['style'],
'same_host' => $same_host,
'show_lines' => $fields['show_lines'],
'error' => $error,
'user' => [
'debug_mode' => $this->getDebugMode()
]
]));
}
}
|