From e694d6468f741aade51504cb9afdc8991534f165 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 19 Dec 2022 17:03:19 +0100 Subject: [PATCH] 1270 Delete modules on cascade --- .../godmode/agentes/configurar_agente.php | 3 + pandora_console/include/functions_api.php | 8 +++ pandora_console/include/functions_modules.php | 69 +++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 2fd9fa8948..4129e8fcd3 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -2118,6 +2118,9 @@ if ($delete_module) { exit; } + // Before delete the main module, check and delete the childrens from the original module. + module_check_childrens_and_delete($id_borrar_modulo); + // Also call base function to delete modules. modules_delete_agent_module($id_borrar_modulo); diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index ce6387ffc7..c88c911ebb 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -10157,6 +10157,8 @@ function api_set_delete_module($id, $id2, $other, $trash1) } if (!$simulate) { + // Before delete the main module, check and delete the childrens from the original module. + module_check_childrens_and_delete($idAgentModule); $return = modules_delete_agent_module($idAgentModule); } else { $return = true; @@ -10182,6 +10184,8 @@ function api_set_delete_module($id, $id2, $other, $trash1) } if (!$simulate) { + // Before delete the main module, check and delete the childrens from the original module. + module_check_childrens_and_delete($idAgentModule); $return = modules_delete_agent_module($idAgentModule); } else { $return = true; @@ -14887,6 +14891,8 @@ function api_set_delete_cluster($id, $thrash1, $thrast2, $thrash3) foreach ($tcluster_modules_delete_get as $key => $value) { $tcluster_modules_delete_get_values[] = $value['id_agente_modulo']; + // Before delete the main module, check and delete the childrens from the original module. + module_check_childrens_and_delete($value['id_agente_modulo']); } $tcluster_modules_delete = modules_delete_agent_module($tcluster_modules_delete_get_values); @@ -14963,6 +14969,8 @@ function api_set_delete_cluster_item($id, $thrash1, $thrast2, $thrast3) } $delete_module_aa_get = db_process_sql('select id_agente_modulo from tagente_modulo where custom_integer_2 = '.$id); + // Before delete the main module, check and delete the childrens from the original module. + module_check_childrens_and_delete($delete_module_aa_get[0]['id_agente_modulo']); $delete_module_aa_get_result = modules_delete_agent_module($delete_module_aa_get[0]['id_agente_modulo']); $delete_item = db_process_sql('delete from tcluster_item where id = '.$id); diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index 38c31b0146..ba2e76b3ff 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -3982,6 +3982,75 @@ function recursive_get_dt_from_modules_tree(&$f_modules, $modules, $deep) } +/** + * Get the module data from a children + * + * @param integer $id_module Id module + * @return array Children module data + */ +function get_children_module($id_module) +{ + $children_module_data = db_get_all_rows_sql( + 'SELECT * + FROM tagente_modulo + WHERE parent_module_id = '.$id_module + ); + + return $children_module_data; +} + + +function module_check_childrens_and_delete($id_module) +{ + $children_data = get_children_module($id_module); + // Check if exist have a childer + if ($children_data) { + // If have more than 1 children + if (is_array($children_data)) { + foreach ($children_data as $children_module_data) { + if ($children_module_data['parent_module_id']) { + // Search children and delete this module + // Before delete, lets check if exist (Just for cases it's already deleted) + if (modules_check_agentmodule_exists($children_module_data['parent_module_id'])) { + modules_delete_agent_module($children_module_data['parent_module_id']); + } + + module_check_childrens_and_delete($children_module_data['id_agente_modulo']); + } else { + // If haven't children just delete + // Before delete, lets check if exist (Just for cases it's already deleted) + if (modules_check_agentmodule_exists($children_module_data['id_agente_modulo'])) { + modules_delete_agent_module($children_module_data['id_agente_modulo']); + } + } + } + } else { + // If just have 1 children + if ($children_data['parent_module_id']) { + // Before delete, lets check if exist (Just for cases it's already deleted) + if (modules_check_agentmodule_exists($children_data['parent_module_id'])) { + modules_delete_agent_module($children_data['parent_module_id']); + } + + module_check_childrens_and_delete($children_data['id_agente_modulo']); + } else { + // If haven't children just delete + // Before delete, lets check if exist (Just for cases it's already deleted) + if (modules_check_agentmodule_exists($children_data['id_agente_modulo'])) { + modules_delete_agent_module($children_data['id_agente_modulo']); + } + } + } + } else { + // Haven't childrens, so delete + // Before delete, lets check if exist (Just for cases it's already deleted) + if (modules_check_agentmodule_exists($id_module)) { + modules_delete_agent_module($id_module); + } + } +} + + /** * @brief Get the button with the link to open realtime stats into a new window * Only to native (not satellite discovered) snmp modules.