From b29f7eb1606ba4ec51081384570413d2d1460bcc Mon Sep 17 00:00:00 2001
From: Calvo <luis.calvo@artica.es>
Date: Fri, 30 Sep 2022 14:54:16 +0200
Subject: [PATCH 1/3] Fix planned downtime issues

---
 .../agentes/planned_downtime.editor.php       | 100 ++++++++++++------
 .../godmode/agentes/planned_downtime.list.php |  41 ++++++-
 pandora_console/include/javascript/pandora.js |  13 ++-
 .../operation/agentes/ver_agente.php          |   9 +-
 4 files changed, 125 insertions(+), 38 deletions(-)

diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php
index cec18a21ca..cdd5152bd0 100644
--- a/pandora_console/godmode/agentes/planned_downtime.editor.php
+++ b/pandora_console/godmode/agentes/planned_downtime.editor.php
@@ -1104,11 +1104,7 @@ $table->data[1][0] = __('Available agents');
 $table->data[1][1] = html_print_select($agents, 'id_agents[]', -1, '', _('Any'), -2, true, true, true, '', false, 'min-width: 250px;width: 70%;');
 
 
-if ($type_downtime != 'quiet') {
-    echo '<div id="available_modules_selection_mode" style="padding-top:20px;display: none;">';
-} else {
-    echo '<div id="available_modules_selection_mode" style="padding-top:20px">';
-}
+$table->rowid[2] = 'available_modules_selection_mode';
 
 $table->data[2][1] = html_print_select(
     [
@@ -1128,19 +1124,13 @@ $table->data[2][1] = html_print_select(
     'min-width:180px;'
 );
 
-echo '</div>';
 
+$table->rowid[3] = 'available_modules';
 $table->data[3][0] = __('Available modules:').ui_print_help_tip(
     __('Only for type Quiet for downtimes.'),
     true
 );
 
-if ($type_downtime != 'quiet') {
-    echo '<div id="available_modules" style="display: none;">';
-} else {
-    echo '<div id="available_modules" style="">';
-}
-
 $table->data[3][1] = html_print_select(
     [],
     'module[]',
@@ -1155,7 +1145,6 @@ $table->data[3][1] = html_print_select(
     false,
     'min-width: 250px;width: 70%;'
 );
-echo '</div>';
 
 // Print agent table.
 html_print_table($table);
@@ -1184,7 +1173,6 @@ if ($id_downtime > 0) {
 }
 
 echo '</div>';
-html_print_input_hidden('all_agents', implode(',', array_keys($agents)));
 html_print_input_hidden('all_common_modules', '');
 echo '</form>';
 
@@ -1405,12 +1393,18 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
     }
 
     $agents = (array) get_parameter('id_agents');
+    $filter_group = (int) get_parameter('filter_group', 0);
     $module_names = (array) get_parameter('module');
     $modules_selection_mode = (string) get_parameter('modules_selection_mode');
+    $type_downtime = (string) get_parameter('type_downtime', 'quiet');
 
     $all_modules = ($modules_selection_mode === 'all' && (empty($module_names) || (string) $module_names[0] === '0'));
     $all_common_modules = ($modules_selection_mode === 'common' && (empty($module_names) || (string) $module_names[0] === '0'));
 
+    if ($type_downtime === 'disable_agents') {
+        $all_modules = true;
+    }
+
     if ($all_common_modules === true) {
         $module_names = explode(',', get_parameter('all_common_modules'));
     }
@@ -1429,11 +1423,14 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
     } else {
         // If is selected 'Any', get all the agents.
         if (count($agents) === 1 && (int) $agents[0] === -2) {
-            $all_agents = get_parameter('all_agents');
-            $agents = explode(',', $all_agents);
+            $agents = agents_get_agents(
+                ['id_grupo' => $filter_group],
+                'id_agent'
+            );
         }
 
         foreach ($agents as $agent_id) {
+            $agent_id = (int) $agent_id;
             // Check module belongs to the agent.
             if ($modules_selection_mode == 'all' && $all_modules === false) {
                 $check = false;
@@ -1466,17 +1463,40 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
                 continue;
             }
 
-            $values = [
-                'id_downtime' => $id_downtime,
-                'id_agent'    => $agent_id,
-                'all_modules' => $all_modules,
-            ];
-            $result = db_process_sql_insert(
+            // Check if agent is already in downtime.
+            $agent_in_downtime = db_get_value_filter(
+                'id_downtime',
                 'tplanned_downtime_agents',
-                $values
+                [
+                    'id_agent'    => $agent_id,
+                    'id_downtime' => $id_downtime,
+                ]
             );
 
-            if ($result && !$all_modules) {
+            if ($agent_in_downtime !== false) {
+                $values = ['all_modules' => $all_modules];
+
+                $result = db_process_sql_update(
+                    'tplanned_downtime_agents',
+                    $values,
+                    [
+                        'id_downtime' => $id_downtime,
+                        'id_agent'    => $agent_id,
+                    ]
+                );
+            } else {
+                $values = [
+                    'id_downtime' => $id_downtime,
+                    'id_agent'    => $agent_id,
+                    'all_modules' => $all_modules,
+                ];
+                $result = db_process_sql_insert(
+                    'tplanned_downtime_agents',
+                    $values
+                );
+            }
+
+            if ($result !== false && (bool) $all_modules === false) {
                 foreach ($module_names as $module_name) {
                     $module = modules_get_agentmodule_id(
                         $module_name,
@@ -1487,17 +1507,32 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
                         continue;
                     }
 
-                    $values = [
-                        'id_downtime'     => $id_downtime,
-                        'id_agent'        => $agent_id,
-                        'id_agent_module' => $module['id_agente_modulo'],
-                    ];
-                    $result = db_process_sql_insert(
+                     // Check if modules are already in downtime.
+                    $module_in_downtime = db_get_value_filter(
+                        'id_downtime',
                         'tplanned_downtime_modules',
-                        $values
+                        [
+                            'id_downtime'     => $id_downtime,
+                            'id_agent'        => $agent_id,
+                            'id_agent_module' => $module['id_agente_modulo'],
+                        ]
                     );
 
-                    if ($result) {
+                    if ($module_in_downtime !== false) {
+                        continue;
+                    } else {
+                        $values = [
+                            'id_downtime'     => $id_downtime,
+                            'id_agent'        => $agent_id,
+                            'id_agent_module' => $module['id_agente_modulo'],
+                        ];
+                        $result = db_process_sql_insert(
+                            'tplanned_downtime_modules',
+                            $values
+                        );
+                    }
+
+                    if ($result !== false) {
                         $values = ['id_user' => $config['id_user']];
                         $result = db_process_sql_update(
                             'tplanned_downtime',
@@ -1526,6 +1561,7 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
                 $("#available_modules_selection_mode").hide();
                 break;
             case 'quiet':
+            case 'disable_agent_modules':
                 $("#available_modules_selection_mode").show();
                 $("#available_modules").show();
                 break;
diff --git a/pandora_console/godmode/agentes/planned_downtime.list.php b/pandora_console/godmode/agentes/planned_downtime.list.php
index 78f717d7d1..0900ee2aba 100755
--- a/pandora_console/godmode/agentes/planned_downtime.list.php
+++ b/pandora_console/godmode/agentes/planned_downtime.list.php
@@ -137,12 +137,49 @@ if (is_ajax() === true) {
             'filters' => get_parameter('filter', []),
         ];
 
-        $modules = get_agents_modules_planned_dowtime($id, $options);
+        $type_downtime = db_get_value_filter(
+            'type_downtime',
+            'tplanned_downtime',
+            ['id' => $id]
+        );
+
+        if ($type_downtime === 'disable_agents') {
+            $sql = sprintf(
+                'SELECT ta.alias as agent_name
+                    FROM tplanned_downtime_agents tpa JOIN tagente ta
+                    ON tpa.id_agent = ta.id_agente
+                    WHERE tpa.id_downtime = %d',
+                $id
+            );
+            $data = db_get_all_rows_sql($sql);
+
+            if (empty($data) === false) {
+                $data = array_reduce(
+                    $data,
+                    function ($carry, $item) {
+                        global $config;
+                        // Transforms array of arrays $data into an array
+                        // of objects, making a post-process of certain fields.
+                        $tmp = (object) $item;
+
+                        $tmp->agent_name  = io_safe_output($item['agent_name']);
+                        $tmp->module_name   = __('All modules');
+
+                        $carry[] = $tmp;
+                        return $carry;
+                    }
+                );
+            }
+        } else {
+            $data = get_agents_modules_planned_dowtime($id, $options);
+        }
+
+
         $count = get_agents_modules_planned_dowtime($id, $options, $count);
 
         echo json_encode(
             [
-                'data'            => $modules,
+                'data'            => $data,
                 'recordsTotal'    => $count[0]['total'],
                 'recordsFiltered' => $count[0]['total'],
             ]
diff --git a/pandora_console/include/javascript/pandora.js b/pandora_console/include/javascript/pandora.js
index d55be120e6..c8679b4fe0 100644
--- a/pandora_console/include/javascript/pandora.js
+++ b/pandora_console/include/javascript/pandora.js
@@ -196,6 +196,13 @@ function agent_changed_by_multiple_agents(event, id_agent, selected) {
     serialized = "";
   }
 
+  var id_group = null;
+  if (typeof $("#filter_group") !== "undefined") {
+    try {
+      id_group = $("#filter_group").val();
+    } catch (error) {}
+  }
+
   $("#module")
     .prop("disabled", true)
     .empty()
@@ -238,7 +245,8 @@ function agent_changed_by_multiple_agents(event, id_agent, selected) {
       selection_mode: selection_mode,
       serialized: serialized,
       id_server: id_server,
-      status_module: module_status
+      status_module: module_status,
+      id_group: id_group
     },
     function(data) {
       $("#module").empty();
@@ -575,7 +583,8 @@ function module_changed_by_multiple_modules(event, id_module, selected) {
       status_module: status_module,
       "module_name[]": idModules,
       selection_mode: selection_mode,
-      tags: tags_selected
+      tags: tags_selected,
+      id_group: id_group
     },
     function(data) {
       $("#agents").append(
diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php
index 3c4eebb991..9b0e8da11f 100644
--- a/pandora_console/operation/agentes/ver_agente.php
+++ b/pandora_console/operation/agentes/ver_agente.php
@@ -473,6 +473,7 @@ if (is_ajax()) {
         $serialized = get_parameter('serialized', '');
         $id_server = (int) get_parameter('id_server', 0);
         $status_modulo = (int) get_parameter('status_module', -1);
+        $id_group_selected = (int) get_parameter('id_group', 0);
         $metaconsole_server_name = null;
         if (!empty($id_server)) {
             $metaconsole_server_name = db_get_value(
@@ -700,14 +701,18 @@ if (is_ajax()) {
                 // Get all user's groups.
                 $id_group = array_keys(users_get_groups($config['id_user']));
 
-                if (is_array($id_group)) {
+                if (is_array($id_group) && empty($id_group_selected) === true) {
                     $id_group = implode(',', $id_group);
+                } else {
+                    if (in_array($id_group_selected, $id_group) === true) {
+                        $id_group = $id_group_selected;
+                    }
                 }
 
                 $where_tags .= ' AND tagente.id_grupo IN ('.$id_group.')';
 
                 if ($selection_mode == 'common') {
-                    $sql_agent_total = 'SELECT count(*) FROM tagente WHERE disabled=0';
+                    $sql_agent_total = 'SELECT count(*) FROM tagente WHERE disabled=0'.$where_tags;
                     $agent_total = db_get_value_sql($sql_agent_total);
                     $sql = sprintf(
                         "SELECT t1.nombre, t1.id_agente_modulo FROM tagente_modulo t1

From 3f8e0f765a7254a0f632570615fb6e7bf1d8b274 Mon Sep 17 00:00:00 2001
From: Calvo <luis.calvo@pandorafms.com>
Date: Tue, 18 Oct 2022 09:52:08 +0200
Subject: [PATCH 2/3] Fix adding agnents planned downtime

---
 .../godmode/agentes/planned_downtime.editor.php    | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php
index cdd5152bd0..fdb8de3b06 100644
--- a/pandora_console/godmode/agentes/planned_downtime.editor.php
+++ b/pandora_console/godmode/agentes/planned_downtime.editor.php
@@ -1423,9 +1423,19 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
     } else {
         // If is selected 'Any', get all the agents.
         if (count($agents) === 1 && (int) $agents[0] === -2) {
-            $agents = agents_get_agents(
+            $agents = db_get_all_rows_filter(
+                'tagente',
                 ['id_grupo' => $filter_group],
-                'id_agent'
+                'id_agente'
+            );
+
+            $agents = array_reduce(
+                $agents,
+                function ($carry, $item) {
+                    $carry[] = $item['id_agente'];
+
+                    return $carry;
+                }
             );
         }
 

From 11df57cd14d0b324b0147f652f6bf8b348d52cbf Mon Sep 17 00:00:00 2001
From: Calvo <luis.calvo@pandorafms.com>
Date: Tue, 18 Oct 2022 19:55:45 +0200
Subject: [PATCH 3/3] Fix planned downtime agent recursion bugs

---
 .../godmode/agentes/planned_downtime.editor.php      | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php
index fdb8de3b06..ed41aee0ef 100644
--- a/pandora_console/godmode/agentes/planned_downtime.editor.php
+++ b/pandora_console/godmode/agentes/planned_downtime.editor.php
@@ -1397,6 +1397,7 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
     $module_names = (array) get_parameter('module');
     $modules_selection_mode = (string) get_parameter('modules_selection_mode');
     $type_downtime = (string) get_parameter('type_downtime', 'quiet');
+    $recursion = (bool) get_parameter_checkbox('recursion', false);
 
     $all_modules = ($modules_selection_mode === 'all' && (empty($module_names) || (string) $module_names[0] === '0'));
     $all_common_modules = ($modules_selection_mode === 'common' && (empty($module_names) || (string) $module_names[0] === '0'));
@@ -1423,6 +1424,15 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
     } else {
         // If is selected 'Any', get all the agents.
         if (count($agents) === 1 && (int) $agents[0] === -2) {
+            if ($recursion === true) {
+                $filter_group = groups_get_children_ids(
+                    $filter_group,
+                    false,
+                    true,
+                    'AW'
+                );
+            };
+
             $agents = db_get_all_rows_filter(
                 'tagente',
                 ['id_grupo' => $filter_group],
@@ -1886,7 +1896,6 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
         $('input.hasDatepicker[readonly]').disable();
 
         $("#checkbox-recursion").click(function() {
-            recursion = this.checked;
             $("#filter_group").trigger("change");
         });
 
@@ -1896,6 +1905,7 @@ function insert_downtime_agent($id_downtime, $user_groups_ad)
         });
 
         function populate_agents_selector() {
+            recursion = $("#checkbox-recursion").prop('checked');
             jQuery.post ("ajax.php",
                 {"page": "operation/agentes/ver_agente",
                     "get_agents_group_json": 1,