diff options
Diffstat (limited to 'ui/include/classes/api/services/CDashboardGeneral.php')
-rw-r--r-- | ui/include/classes/api/services/CDashboardGeneral.php | 271 |
1 files changed, 177 insertions, 94 deletions
diff --git a/ui/include/classes/api/services/CDashboardGeneral.php b/ui/include/classes/api/services/CDashboardGeneral.php index e259815ece9..30d41069f60 100644 --- a/ui/include/classes/api/services/CDashboardGeneral.php +++ b/ui/include/classes/api/services/CDashboardGeneral.php @@ -115,20 +115,21 @@ abstract class CDashboardGeneral extends CApiService { DB::delete('dashboard', ['dashboardid' => $dashboardids]); $resource = ($this instanceof CDashboard) ? CAudit::RESOURCE_DASHBOARD : CAudit::RESOURCE_TEMPLATE_DASHBOARD; - $this->addAuditBulk(CAudit::ACTION_DELETE, $resource, $db_dashboards); + self::addAuditLog(CAudit::ACTION_DELETE, $resource, $db_dashboards); return ['dashboardids' => $dashboardids]; } /** - * Add the existing pages, widgets and widget fields to $db_dashboards whether these are affected by the update. + * Add the existing users, user groups, pages, widgets and widget fields to $db_dashboards whether + * these are affected by the update. * * @param array $dashboards * @param array $db_dashboards */ protected function addAffectedObjects(array $dashboards, array &$db_dashboards): void { - // Select pages of these dashboards. - $dashboardids = []; + // Select pages, users and user groups of these dashboards. + $dashboardids = ['pages' => [], 'users' => [], 'userGroups' => []]; // Select widgets of these pages. $dashboard_pageids = []; @@ -137,20 +138,28 @@ abstract class CDashboardGeneral extends CApiService { $widgetids = []; foreach ($dashboards as $dashboard) { + if (array_key_exists('users', $dashboard)) { + $dashboardids['users'][] = $dashboard['dashboardid']; + $db_dashboards[$dashboard['dashboardid']]['users'] = []; + } + + if (array_key_exists('userGroups', $dashboard)) { + $dashboardids['userGroups'][] = $dashboard['dashboardid']; + $db_dashboards[$dashboard['dashboardid']]['userGroups'] = []; + } + if (array_key_exists('pages', $dashboard)) { - $dashboardids[$dashboard['dashboardid']] = true; + $dashboardids['pages'][] = $dashboard['dashboardid']; + $db_dashboards[$dashboard['dashboardid']]['pages'] = []; foreach ($dashboard['pages'] as $dashboard_page) { - if (array_key_exists('dashboard_pageid', $dashboard_page)) { - if (array_key_exists('widgets', $dashboard_page)) { - $dashboard_pageids[$dashboard_page['dashboard_pageid']] = true; - - foreach ($dashboard_page['widgets'] as $widget) { - if (array_key_exists('widgetid', $widget)) { - if (array_key_exists('fields', $widget)) { - $widgetids[$widget['widgetid']] = true; - } - } + if (array_key_exists('dashboard_pageid', $dashboard_page) + && array_key_exists('widgets', $dashboard_page)) { + $dashboard_pageids[] = $dashboard_page['dashboard_pageid']; + + foreach ($dashboard_page['widgets'] as $widget) { + if (array_key_exists('widgetid', $widget) && array_key_exists('fields', $widget)) { + $widgetids[] = $widget['widgetid']; } } } @@ -158,15 +167,36 @@ abstract class CDashboardGeneral extends CApiService { } } - foreach ($db_dashboards as &$db_dashboard) { - $db_dashboard['pages'] = []; + if ($dashboardids['users']) { + $options = [ + 'output' => ['dashboard_userid', 'dashboardid', 'userid', 'permission'], + 'filter' => ['dashboardid' => $dashboardids['users']] + ]; + $db_users = DBselect(DB::makeSql('dashboard_user', $options)); + + while ($db_user = DBfetch($db_users)) { + $db_dashboards[$db_user['dashboardid']]['users'][$db_user['dashboard_userid']] = + array_diff_key($db_user, array_flip(['dashboardid'])); + } + } + + if ($dashboardids['userGroups']) { + $options = [ + 'output' => ['dashboard_usrgrpid', 'dashboardid', 'usrgrpid', 'permission'], + 'filter' => ['dashboardid' => $dashboardids['userGroups']] + ]; + $db_groups = DBselect(DB::makeSql('dashboard_usrgrp', $options)); + + while ($db_group = DBfetch($db_groups)) { + $db_dashboards[$db_group['dashboardid']]['userGroups'][$db_group['dashboard_usrgrpid']] = + array_diff_key($db_group, array_flip(['dashboardid'])); + } } - unset($db_dashboard); - if ($dashboardids) { + if ($dashboardids['pages']) { $db_dashboard_pages = DB::select('dashboard_page', [ - 'output' => array_keys(DB::getSchema('dashboard_page')['fields']), - 'filter' => ['dashboardid' => array_keys($dashboardids)], + 'output' => ['dashboard_pageid', 'dashboardid', 'name', 'display_period', 'sortorder'], + 'filter' => ['dashboardid' => $dashboardids['pages']], 'preservekeys' => true ]); @@ -177,8 +207,10 @@ abstract class CDashboardGeneral extends CApiService { if ($dashboard_pageids) { $db_widgets = DB::select('widget', [ - 'output' => array_keys(DB::getSchema('widget')['fields']), - 'filter' => ['dashboard_pageid' => array_keys($dashboard_pageids)], + 'output' => ['widgetid', 'dashboard_pageid', 'type', 'name', 'x', 'y', 'width', 'height', + 'view_mode' + ], + 'filter' => ['dashboard_pageid' => $dashboard_pageids], 'preservekeys' => true ]); @@ -188,24 +220,33 @@ abstract class CDashboardGeneral extends CApiService { unset($db_widget); if ($widgetids) { - $db_widget_fields = DB::select('widget_field', [ - 'output' => array_keys(DB::getSchema('widget_field')['fields']), - 'filter' => ['widgetid' => array_keys($widgetids)], - 'preservekeys' => true - ]); - - foreach ($db_widget_fields as $widget_fieldid => $db_widget_field) { - $db_widgets[$db_widget_field['widgetid']]['fields'][$widget_fieldid] = $db_widget_field; + $options = [ + 'output' => ['widget_fieldid', 'widgetid', 'type', 'name', 'value_int', 'value_str', + 'value_groupid', 'value_hostid', 'value_itemid', 'value_graphid', 'value_sysmapid' + ], + 'filter' => ['widgetid' => $widgetids] + ]; + $db_widget_fields = DBselect(DB::makeSql('widget_field', $options)); + + while ($db_widget_field = DBfetch($db_widget_fields)) { + $db_widgets[$db_widget_field['widgetid']]['fields'][$db_widget_field['widget_fieldid']] = [ + 'widget_fieldid' => $db_widget_field['widget_fieldid'], + 'type' => $db_widget_field['type'], + 'name' => $db_widget_field['name'], + 'value' => $db_widget_field[self::WIDGET_FIELD_TYPE_COLUMNS[$db_widget_field['type']]] + ]; } } foreach ($db_widgets as $widgetid => $db_widget) { - $db_dashboard_pages[$db_widget['dashboard_pageid']]['widgets'][$widgetid] = $db_widget; + $db_dashboard_pages[$db_widget['dashboard_pageid']]['widgets'][$widgetid] = + array_diff_key($db_widget, array_flip(['dashboard_pageid'])); } } foreach ($db_dashboard_pages as $dashboard_pageid => $db_dashboard_page) { - $db_dashboards[$db_dashboard_page['dashboardid']]['pages'][$dashboard_pageid] = $db_dashboard_page; + $db_dashboards[$db_dashboard_page['dashboardid']]['pages'][$dashboard_pageid] = + array_diff_key($db_dashboard_page, array_flip(['dashboardid'])); } } } @@ -360,8 +401,7 @@ abstract class CDashboardGeneral extends CApiService { foreach ($db_widget['fields'] as $db_widget_field) { if (array_key_exists($db_widget_field['type'], $ids)) { - $value = $db_widget_field[self::WIDGET_FIELD_TYPE_COLUMNS[$db_widget_field['type']]]; - $stored_widget_fields[$db_widget_field['type']][$value] = true; + $stored_widget_fields[$db_widget_field['type']][$db_widget_field['value']] = true; } } } @@ -561,7 +601,7 @@ abstract class CDashboardGeneral extends CApiService { * @param array $dashboards * @param array|null $db_dashboards */ - protected function updatePages(array $dashboards, array $db_dashboards = null): void { + protected function updatePages(array &$dashboards, array $db_dashboards = null): void { $db_dashboard_pages = []; if ($db_dashboards !== null) { @@ -606,24 +646,26 @@ abstract class CDashboardGeneral extends CApiService { if ($ins_dashboard_pages) { $dashboard_pageids = DB::insert('dashboard_page', $ins_dashboard_pages); - - foreach ($dashboards as &$dashboard) { - if (array_key_exists('pages', $dashboard)) { - foreach ($dashboard['pages'] as &$dashboard_page) { - if (!array_key_exists('dashboard_pageid', $dashboard_page)) { - $dashboard_page['dashboard_pageid'] = array_shift($dashboard_pageids); - } - } - unset($dashboard_page); - } - } - unset($dashboard); } if ($upd_dashboard_pages) { DB::update('dashboard_page', $upd_dashboard_pages); } + foreach ($dashboards as &$dashboard) { + if (!array_key_exists('pages', $dashboard)) { + continue; + } + + foreach ($dashboard['pages'] as &$dashboard_page) { + if (!array_key_exists('dashboard_pageid', $dashboard_page)) { + $dashboard_page['dashboard_pageid'] = array_shift($dashboard_pageids); + } + } + unset($dashboard_page); + } + unset($dashboard); + $this->updateWidgets($dashboards, $db_dashboards); if ($db_dashboard_pages) { @@ -639,7 +681,7 @@ abstract class CDashboardGeneral extends CApiService { * @param array $dashboards * @param array|null $db_dashboards */ - protected function updateWidgets(array $dashboards, array $db_dashboards = null): void { + protected function updateWidgets(array &$dashboards, array $db_dashboards = null): void { $db_widgets = []; if ($db_dashboards !== null) { @@ -689,6 +731,7 @@ abstract class CDashboardGeneral extends CApiService { unset($db_widgets[$widget['widgetid']]); } else { + unset($widget['fields']); $ins_widgets[] = ['dashboard_pageid' => $dashboard_page['dashboard_pageid']] + $widget; } } @@ -697,34 +740,38 @@ abstract class CDashboardGeneral extends CApiService { if ($ins_widgets) { $widgetids = DB::insert('widget', $ins_widgets); + } - foreach ($dashboards as &$dashboard) { - if (array_key_exists('pages', $dashboard)) { - foreach ($dashboard['pages'] as &$dashboard_page) { - if (array_key_exists('widgets', $dashboard_page)) { - foreach ($dashboard_page['widgets'] as &$widget) { - if (!array_key_exists('widgetid', $widget)) { - $widget['widgetid'] = array_shift($widgetids); - } - } - unset($widget); - } + if ($upd_widgets) { + DB::update('widget', $upd_widgets); + } + + foreach ($dashboards as &$dashboard) { + if (!array_key_exists('pages', $dashboard)) { + continue; + } + + foreach ($dashboard['pages'] as &$dashboard_page) { + if (!array_key_exists('widgets', $dashboard_page)) { + continue; + } + + foreach ($dashboard_page['widgets'] as &$widget) { + if (!array_key_exists('widgetid', $widget)) { + $widget['widgetid'] = array_shift($widgetids); } - unset($dashboard_page); } + unset($widget); } - unset($dashboard); + unset($dashboard_page); } + unset($dashboard); - if ($upd_widgets) { - DB::update('widget', $upd_widgets); - } + $this->updateWidgetFields($dashboards, $db_dashboards); if ($db_widgets) { self::deleteWidgets(array_keys($db_widgets)); } - - $this->updateWidgetFields($dashboards, $db_dashboards); } /** @@ -735,19 +782,19 @@ abstract class CDashboardGeneral extends CApiService { * @param array $dashboards * @param array|null $db_dashboards */ - protected function updateWidgetFields(array $dashboards, array $db_dashboards = null): void { + protected function updateWidgetFields(array &$dashboards, array $db_dashboards = null): void { $ins_widget_fields = []; $upd_widget_fields = []; $del_widget_fieldids = []; - foreach ($dashboards as $dashboard) { + foreach ($dashboards as &$dashboard) { if (!array_key_exists('pages', $dashboard)) { continue; } $db_dashboard_pages = ($db_dashboards !== null) ? $db_dashboards[$dashboard['dashboardid']]['pages'] : []; - foreach ($dashboard['pages'] as $dashboard_page) { + foreach ($dashboard['pages'] as &$dashboard_page) { if (!array_key_exists('widgets', $dashboard_page)) { continue; } @@ -756,7 +803,7 @@ abstract class CDashboardGeneral extends CApiService { ? $db_dashboard_pages[$dashboard_page['dashboard_pageid']]['widgets'] : []; - foreach ($dashboard_page['widgets'] as $widget) { + foreach ($dashboard_page['widgets'] as &$widget) { if (!array_key_exists('fields', $widget)) { continue; } @@ -765,48 +812,56 @@ abstract class CDashboardGeneral extends CApiService { ? $db_widgets[$widget['widgetid']]['fields'] : []; - $widget_fields = []; + foreach ($widget['fields'] as &$widget_field) { + $db_widget_field = current( + array_filter($db_widget_fields, + static function (array $db_widget_field) use ($widget_field): bool { + return ($widget_field['type'] == $db_widget_field['type'] + && $widget_field['name'] === $db_widget_field['name']); + } + ) + ); - foreach ($widget['fields'] as $widget_field) { - $widget_field[self::WIDGET_FIELD_TYPE_COLUMNS[$widget_field['type']]] = $widget_field['value']; - $widget_fields[$widget_field['type']][$widget_field['name']][] = $widget_field; - } + if ($db_widget_field) { + $widget_field['widget_fieldid'] = $db_widget_field['widget_fieldid']; - foreach ($db_widget_fields as $db_widget_field) { - if (array_key_exists($db_widget_field['type'], $widget_fields) - && array_key_exists($db_widget_field['name'], $widget_fields[$db_widget_field['type']]) - && $widget_fields[$db_widget_field['type']][$db_widget_field['name']]) { - $widget_field = array_shift( - $widget_fields[$db_widget_field['type']][$db_widget_field['name']] + $upd_widget_field = DB::getUpdatedValues('widget_field', + CArrayHelper::renameKeys($widget_field, [ + 'value' => self::WIDGET_FIELD_TYPE_COLUMNS[$widget_field['type']] + ]), + CArrayHelper::renameKeys($db_widget_field, [ + 'value' => self::WIDGET_FIELD_TYPE_COLUMNS[$db_widget_field['type']] + ]) ); - $upd_widget_field = DB::getUpdatedValues('widget_field', $widget_field, $db_widget_field); - if ($upd_widget_field) { $upd_widget_fields[] = [ 'values' => $upd_widget_field, 'where' => ['widget_fieldid' => $db_widget_field['widget_fieldid']] ]; } + + unset($db_widget_fields[$db_widget_field['widget_fieldid']]); } else { - $del_widget_fieldids[] = $db_widget_field['widget_fieldid']; + $ins_widget_fields[] = ['widgetid' => $widget['widgetid']] + + CArrayHelper::renameKeys($widget_field, [ + 'value' => self::WIDGET_FIELD_TYPE_COLUMNS[$widget_field['type']] + ]); } } + unset($widget_field); - foreach ($widget_fields as $widget_fields) { - foreach ($widget_fields as $widget_fields) { - foreach ($widget_fields as $widget_field) { - $ins_widget_fields[] = ['widgetid' => $widget['widgetid']] + $widget_field; - } - } - } + $del_widget_fieldids += $db_widget_fields; } + unset($widget); } + unset($dashboard_page); } + unset($dashboard); if ($ins_widget_fields) { - DB::insert('widget_field', $ins_widget_fields); + $widget_fieldids = DB::insert('widget_field', $ins_widget_fields); } if ($upd_widget_fields) { @@ -814,8 +869,36 @@ abstract class CDashboardGeneral extends CApiService { } if ($del_widget_fieldids) { - DB::delete('widget_field', ['widget_fieldid' => $del_widget_fieldids]); + DB::delete('widget_field', ['widget_fieldid' => array_keys($del_widget_fieldids)]); + } + + foreach ($dashboards as &$dashboard) { + if (!array_key_exists('pages', $dashboard)) { + continue; + } + + foreach ($dashboard['pages'] as &$dashboard_page) { + if (!array_key_exists('widgets', $dashboard_page)) { + continue; + } + + foreach ($dashboard_page['widgets'] as &$widget) { + if (!array_key_exists('fields', $widget)) { + continue; + } + + foreach ($widget['fields'] as &$widget_field) { + if (!array_key_exists('widget_fieldid', $widget_field)) { + $widget_field['widget_fieldid'] = array_shift($widget_fieldids); + } + } + unset($widget_field); + } + unset($widget); + } + unset($dashboard_page); } + unset($dashboard); } /** |