diff options
Diffstat (limited to 'ui/app/controllers/CControllerDashboardWidgetEdit.php')
-rw-r--r-- | ui/app/controllers/CControllerDashboardWidgetEdit.php | 174 |
1 files changed, 92 insertions, 82 deletions
diff --git a/ui/app/controllers/CControllerDashboardWidgetEdit.php b/ui/app/controllers/CControllerDashboardWidgetEdit.php index bf020dfe72d..7390872df0d 100644 --- a/ui/app/controllers/CControllerDashboardWidgetEdit.php +++ b/ui/app/controllers/CControllerDashboardWidgetEdit.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types = 0); /* ** Zabbix ** Copyright (C) 2001-2022 Zabbix SIA @@ -19,18 +19,36 @@ **/ +use Zabbix\Core\{ + CModule, + CWidget +}; + +use Zabbix\Widgets\CWidgetForm; + +use Zabbix\Widgets\Fields\{ + CWidgetFieldMultiSelectGraph, + CWidgetFieldMultiSelectGraphPrototype, + CWidgetFieldMultiSelectGroup, + CWidgetFieldMultiSelectHost, + CWidgetFieldMultiSelectItem, + CWidgetFieldMultiSelectItemPrototype, + CWidgetFieldMultiSelectService, + CWidgetFieldMultiSelectSla, + CWidgetFieldSelectResource +}; + class CControllerDashboardWidgetEdit extends CController { - private $context; + private ?CWidget $widget; - protected function checkInput() { + protected function checkInput(): bool { $fields = [ + 'type' => 'string|required', + 'fields' => 'array', 'templateid' => 'db dashboard.templateid', - 'type' => 'string', 'name' => 'string', 'view_mode' => 'in '.implode(',', [ZBX_WIDGET_VIEW_MODE_NORMAL, ZBX_WIDGET_VIEW_MODE_HIDDEN_HEADER]), - 'prev_type' => 'string', - 'fields' => 'json', 'unique_id' => 'string', 'dashboard_page_unique_id' => 'string' ]; @@ -38,94 +56,92 @@ class CControllerDashboardWidgetEdit extends CController { $ret = $this->validateInput($fields); if ($ret) { - $this->context = $this->hasInput('templateid') - ? CWidgetConfig::CONTEXT_TEMPLATE_DASHBOARD - : CWidgetConfig::CONTEXT_DASHBOARD; + $widget = APP::ModuleManager()->getModule($this->getInput('type')); - if ($this->hasInput('type')) { - if (!CWidgetConfig::isWidgetTypeSupportedInContext($this->getInput('type'), $this->context)) { - error(_('Widget type is not supported in this context.')); + if ($widget !== null && $widget->getType() === CModule::TYPE_WIDGET) { + $this->widget = $widget; + } + else { + error(_('Inaccessible widget type.')); - $ret = false; - } + $ret = false; } } + if ($ret && $this->hasInput('templateid') && !$this->widget->hasTemplateSupport()) { + error(_('Widget type is not supported in this context.')); + + $ret = false; + } + if (!$ret) { $this->setResponse( - (new CControllerResponseData(['main_block' => json_encode([ - 'error' => [ - 'messages' => array_column(get_and_clear_messages(), 'message') - ] - ])]))->disableView() + (new CControllerResponseData([ + 'main_block' => json_encode([ + 'header' => $this->hasInput('unique_id') ? _('Edit widget') : _('Add widget'), + 'error' => [ + 'messages' => array_column(get_and_clear_messages(), 'message') + ] + ], JSON_THROW_ON_ERROR) + ]))->disableView() ); } return $ret; } - protected function checkPermissions() { - return ($this->context === CWidgetConfig::CONTEXT_TEMPLATE_DASHBOARD) + protected function checkPermissions(): bool { + return $this->hasInput('templateid') ? ($this->getUserType() >= USER_TYPE_ZABBIX_ADMIN) : ($this->getUserType() >= USER_TYPE_ZABBIX_USER); } - protected function doAction() { - $known_widget_types = CWidgetConfig::getKnownWidgetTypes($this->context); - - natsort($known_widget_types); + protected function doAction(): void { + $known_types = []; + $deprecated_types = []; - if ($this->hasInput('type')) { - $type = $this->getInput('type'); - - if (!array_key_exists($type, $known_widget_types) || $this->getInput('prev_type', $type) !== $type) { - CProfile::update('web.dashboard.last_widget_type', $type, PROFILE_TYPE_STR); + /** @var CWidget $widget */ + foreach (APP::ModuleManager()->getWidgets($this->hasInput('templateid')) as $widget) { + if (!$widget->isDeprecated()) { + $known_types[$widget->getId()] = $widget->getDefaultName(); } - } - else { - $type = CProfile::get('web.dashboard.last_widget_type'); - if (!array_key_exists($type, $known_widget_types)) { - $type = array_keys($known_widget_types)[0]; + else { + $deprecated_types[$widget->getId()] = $widget->getDefaultName(); } } - $templateid = ($this->context === CWidgetConfig::CONTEXT_TEMPLATE_DASHBOARD) - ? $this->getInput('templateid') - : null; + natcasesort($known_types); + natcasesort($deprecated_types); - $form = CWidgetConfig::getForm($type, $this->getInput('fields', '{}'), $templateid); + $form = $this->widget->getForm($this->getInput('fields', []), + $this->hasInput('templateid') ? $this->getInput('templateid') : null + ); // Transforms corrupted data to default values. $form->validate(); $this->setResponse(new CControllerResponseData([ - 'user' => [ - 'debug_mode' => $this->getDebugMode() - ], - 'dialogue' => [ - 'type' => $type, - 'name' => $this->getInput('name', ''), - 'view_mode' => $this->getInput('view_mode', ZBX_WIDGET_VIEW_MODE_NORMAL), - 'fields' => $form->getFields() - ], - 'templateid' => $templateid, + 'name' => $this->getInput('name', ''), + 'type' => $this->getInput('type'), + 'known_types' => $known_types, + 'deprecated_types' => $deprecated_types, + 'fields' => $form->getFields(), + 'view_mode' => $this->getInput('view_mode', ZBX_WIDGET_VIEW_MODE_NORMAL), 'unique_id' => $this->hasInput('unique_id') ? $this->getInput('unique_id') : null, 'dashboard_page_unique_id' => $this->hasInput('dashboard_page_unique_id') ? $this->getInput('dashboard_page_unique_id') : null, - 'known_widget_types' => $known_widget_types, - 'captions' => $this->getCaptions($form) + 'captions' => $this->getCaptions($form), + 'user' => [ + 'debug_mode' => $this->getDebugMode() + ] ])); } /** * Prepares mapped list of names for all required resources. - * - * @param CWidgetForm $form - * - * @return array */ - private function getCaptions($form) { + private function getCaptions(CWidgetForm $form): array { $captions = ['simple' => [], 'ms' => []]; foreach ($form->getFields() as $field) { @@ -137,12 +153,8 @@ class CControllerDashboardWidgetEdit extends CController { $captions['simple'][$resource_type] = []; } - if ($id != 0) { - switch ($resource_type) { - case WIDGET_FIELD_SELECT_RES_SYSMAP: - $captions['simple'][$resource_type][$id] = _('Inaccessible map'); - break; - } + if ($id != 0 && $resource_type == CWidgetFieldSelectResource::RESOURCE_TYPE_SYSMAP) { + $captions['simple'][$resource_type][$id] = _('Inaccessible map'); } } } @@ -152,19 +164,17 @@ class CControllerDashboardWidgetEdit extends CController { continue; } - switch ($resource_type) { - case WIDGET_FIELD_SELECT_RES_SYSMAP: - $maps = API::Map()->get([ - 'sysmapids' => array_keys($list), - 'output' => ['sysmapid', 'name'] - ]); - - if ($maps) { - foreach ($maps as $map) { - $list[$map['sysmapid']] = $map['name']; - } + if ($resource_type == CWidgetFieldSelectResource::RESOURCE_TYPE_SYSMAP) { + $maps = API::Map()->get([ + 'sysmapids' => array_keys($list), + 'output' => ['sysmapid', 'name'] + ]); + + if ($maps) { + foreach ($maps as $map) { + $list[$map['sysmapid']] = $map['name']; } - break; + } } } unset($list); @@ -182,35 +192,35 @@ class CControllerDashboardWidgetEdit extends CController { ]; foreach ($form->getFields() as $field) { - if ($field instanceof CWidgetFieldMsGroup) { + if ($field instanceof CWidgetFieldMultiSelectGroup) { $key = 'groups'; $var = 'group'; } - elseif ($field instanceof CWidgetFieldMsHost) { + elseif ($field instanceof CWidgetFieldMultiSelectHost) { $key = 'hosts'; $var = 'host'; } - elseif ($field instanceof CWidgetFieldMsItem) { + elseif ($field instanceof CWidgetFieldMultiSelectItem) { $key = 'items'; $var = 'item'; } - elseif ($field instanceof CWidgetFieldMsGraph) { + elseif ($field instanceof CWidgetFieldMultiSelectGraph) { $key = 'graphs'; $var = 'graph'; } - elseif ($field instanceof CWidgetFieldMsItemPrototype) { + elseif ($field instanceof CWidgetFieldMultiSelectItemPrototype) { $key = 'item_prototypes'; $var = 'prototype_item'; } - elseif ($field instanceof CWidgetFieldMsGraphPrototype) { + elseif ($field instanceof CWidgetFieldMultiSelectGraphPrototype) { $key = 'graph_prototypes'; $var = 'prototype_graph'; } - elseif ($field instanceof CWidgetFieldMsService) { + elseif ($field instanceof CWidgetFieldMultiSelectService) { $key = 'services'; $var = 'service'; } - elseif ($field instanceof CWidgetFieldMsSla) { + elseif ($field instanceof CWidgetFieldMultiSelectSla) { $key = 'slas'; $var = 'sla'; } |