From 11dc743dacf5c1934a26b7210696fa95c95490b1 Mon Sep 17 00:00:00 2001 From: Calvo Date: Fri, 28 Jan 2022 10:59:08 +0100 Subject: [PATCH] Planned downtime copy. --- .../agentes/planned_downtime.editor.php | 7 ++- .../include/functions_planned_downtimes.php | 59 ++++++++++++------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index f06faa05f9..7d73a400c3 100644 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -423,10 +423,11 @@ if ($create_downtime || $update_downtime) { if ($downtime_copy) { $result = planned_downtimes_copy($id_downtime); - if ($result === true) { - ui_print_success_message(__('Successfully copied')); + if ($result['id_downtime'] !== false) { + $id_downtime = $result['id_downtime']; + ui_print_success_message($result['success']); } else { - ui_print_error_message(__('Could not be copied')); + ui_print_error_message(__($result['error'])); } } diff --git a/pandora_console/include/functions_planned_downtimes.php b/pandora_console/include/functions_planned_downtimes.php index ecb484060a..fc09a93b90 100644 --- a/pandora_console/include/functions_planned_downtimes.php +++ b/pandora_console/include/functions_planned_downtimes.php @@ -928,12 +928,12 @@ function planned_downtimes_copy($id_downtime) ['id' => $id_downtime] ); - $planned_agents = db_get_row_filter( + $planned_agents = db_get_all_rows_filter( 'tplanned_downtime_agents', ['id_downtime' => $id_downtime] ); - $planned_modules = db_get_row_filter( + $planned_modules = db_get_all_rows_filter( 'tplanned_downtime_modules', ['id_downtime' => $id_downtime] ); @@ -949,22 +949,34 @@ function planned_downtimes_copy($id_downtime) $planned_downtime['name'] = __('Coy of ').$planned_downtime['name']; // Insert new downtime - $result = db_process_sql_insert( + $result['id_downtime'] = db_process_sql_insert( 'tplanned_downtime', $planned_downtime ); if ($result === false) { - return false; + $result['error'] = __('Could not be copied'); + return $result; + } else { + $result['success'] = __('Successfully copied'); } if ($planned_agents !== false) { - // Unser id. - unset($planned_agents['id']); - $result = db_process_sql_insert( - 'tplanned_downtime_agents', - $planned_agents - ); + foreach ($planned_agents as $planned_agent) { + // Unset id. + unset($planned_agent['id']); + // Set id_planned downtime + $planned_agent['id_downtime'] = $result['id_downtime']; + $result['id_agents'][] = db_process_sql_insert( + 'tplanned_downtime_agents', + $planned_agent + ); + + if ($result === false) { + $return['error'] = __('Error adding agents to copied downtime'); + break; + } + } } if ($result === false) { @@ -974,18 +986,21 @@ function planned_downtimes_copy($id_downtime) if ((bool) $planned_agents['all_modules'] === false && $planned_modules !== false ) { - // Unser id. - unset($planned_modules['id']); - $result = db_process_sql_insert( - 'tplanned_downtime_agents', - $planned_agents - ); + foreach ($planned_modules as $planned_module) { + // Unset id. + unset($planned_module['id']); + // Set id_planned downtime + $planned_module['id_downtime'] = $result['id_downtime']; + $result['id_modules'][] = db_process_sql_insert( + 'tplanned_downtime_moduless', + $planned_module + ); + if ($result === false) { + $return['error'] = __('Error adding module to copied downtime'); + break; + } + } } - if ($result === false) { - return false; - } - - return true; - + return $result; }