disableCsrfValidation(); } protected function checkInput() { $fields = [ 'hostid' => 'required|db hosts.hostid', 'dashboardid' => 'db dashboard.dashboardid', 'from' => 'range_time', 'to' => 'range_time' ]; $ret = $this->validateInput($fields) && $this->validateTimeSelectorPeriod(); if (!$ret) { $this->setResponse(new CControllerResponseFatal()); } return $ret; } protected function checkPermissions() { if (!$this->checkAccess(CRoleHelper::UI_MONITORING_HOSTS)) { return false; } $hosts = API::Host()->get([ 'output' => ['hostid', 'name'], 'selectParentTemplates' => ['templateid'], 'hostids' => [$this->getInput('hostid')] ]); $this->host = array_shift($hosts); return (bool) $this->host; } /** * @throws APIException|JsonException */ protected function doAction() { $host_dashboards = $this->getSortedHostDashboards(); if (!$host_dashboards) { $data = ['no_data' => true]; } else { $dashboardid = $this->hasInput('dashboardid') ? $this->getInput('dashboardid') : CProfile::get('web.host.dashboard.dashboardid', null, $this->getInput('hostid')); if (!in_array($dashboardid, array_column($host_dashboards, 'dashboardid'))) { $dashboardid = $host_dashboards[0]['dashboardid']; } $dashboards = API::TemplateDashboard()->get([ 'output' => ['dashboardid', 'name', 'templateid', 'display_period', 'auto_start'], 'selectPages' => ['dashboard_pageid', 'name', 'display_period', 'widgets'], 'dashboardids' => [$dashboardid] ]); $dashboard = array_shift($dashboards); if ($dashboard !== null) { CProfile::update('web.host.dashboard.dashboardid', $dashboard['dashboardid'], PROFILE_TYPE_ID, $this->getInput('hostid') ); $dashboard['pages'] = CDashboardHelper::preparePagesForGrid($dashboard['pages'], $dashboard['templateid'], true ); $time_selector_options = [ 'profileIdx' => 'web.dashboard.filter', 'profileIdx2' => $dashboard['dashboardid'], 'from' => $this->hasInput('from') ? $this->getInput('from') : null, 'to' => $this->hasInput('to') ? $this->getInput('to') : null ]; updateTimeSelectorPeriod($time_selector_options); $widget_defaults = APP::ModuleManager()->getWidgetsDefaults(); $data = [ 'host' => $this->host, 'host_dashboards' => $host_dashboards, 'dashboard' => $dashboard, 'widget_defaults' => $widget_defaults, 'configuration_hash' => CDashboardHelper::getConfigurationHash($dashboard, $widget_defaults), 'has_time_selector' => CDashboardHelper::hasTimeSelector($dashboard['pages']), 'time_period' => getTimeSelectorPeriod($time_selector_options), 'active_tab' => CProfile::get('web.dashboard.filter.active', 1) ]; } else { $data = ['error' => _('No permissions to referred object or it does not exist!')]; CProfile::delete('web.host.dashboard.dashboardid', $this->getInput('hostid')); } } $response = new CControllerResponseData($data); $response->setTitle(_('Dashboards')); $this->setResponse($response); } private function getSortedHostDashboards(): array { $dashboards = getHostDashboards($this->host['hostid'], ['dashboardid', 'name']); CArrayHelper::sort($dashboards, [['field' => 'name', 'order' => ZBX_SORT_UP]]); return array_values($dashboards); } }