From a359fd6828ff20ed0a64d4dbd51e6a8ed23eabbf Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 3 Feb 2022 12:56:36 +0100 Subject: [PATCH] bulk operation edit agents add secundary groups pandora_enterprise#2152 --- .../godmode/agentes/agent_manager.php | 276 ++---------------- .../godmode/massive/massive_edit_agents.php | 184 +++++++++++- pandora_console/include/functions_html.php | 182 ++++++++++++ .../include/javascript/pandora_agents.js | 169 +++++++++++ 4 files changed, 537 insertions(+), 274 deletions(-) create mode 100644 pandora_console/include/javascript/pandora_agents.js diff --git a/pandora_console/godmode/agentes/agent_manager.php b/pandora_console/godmode/agentes/agent_manager.php index 2d77e7de29..3687b226c9 100644 --- a/pandora_console/godmode/agentes/agent_manager.php +++ b/pandora_console/godmode/agentes/agent_manager.php @@ -188,7 +188,7 @@ if ($disk_conf_delete) { @unlink($filename['conf']); } -echo '
'; +echo ''; // Custom ID. $custom_id_div = '
'; @@ -449,123 +449,14 @@ if (!$new_agent && $alias != '') { echo '
'; if (enterprise_installed()) { - $secondary_groups_selected = enterprise_hook('agents_get_secondary_groups', [$id_agente]); - $adv_secondary_groups_label = '

'.__('Secondary groups').'

'; - $adv_secondary_groups_left = html_print_select_groups( - // Id_user. - // Use the current user to select the groups. - false, - // Privilege. - // ACL permission. - 'AR', - // ReturnAllGroup. - // Not all group. - false, - // Name. - // HTML id. - 'secondary_groups', - // Selected. - // No select any by default. - '', - // Script. - // Javascript onChange code. - '', - // Nothing. - // Do not user no selected value. - false, - // Nothing_value. - // Do not use no selected value. - 0, - // Return. - // Return HTML (not echo). - true, - // Multiple. - // Multiple selection. - true, - // Sort. - // Sorting by default. - true, - // Class. - // CSS classnames (default). - '', - // Disabled. - // Not disabled (default). - false, - // Style. - // Inline styles (default). - 'min-width:170px;', - // Option_style. - // Option style select (default). - false, - // Id_group. - // Do not truncate the users tree (default). - false, - // Keys_field. - // Key to get as value (default). - 'id_grupo', - // Strict_user. - // Not strict user (default). - false, - // Delete_groups. - // Do not show the primary group in this selection. - array_merge( - (empty($secondary_groups_selected['plain']) === false) ? $secondary_groups_selected['plain'] : [], - [$agent['id_grupo']] - ) - // Include_groups. - // Size. - // Simple_multiple_options. - ); - - $adv_secondary_groups_arrows = html_print_input_image( - 'add_secondary', - 'images/darrowright_green.png', - 1, - '', - true, - [ - 'id' => 'right_autorefreshlist', - 'title' => __('Add secondary groups'), - 'onclick' => 'agent_manager_add_secondary_groups(event, '.$id_agente.');', - ] - ).html_print_input_image( - 'remove_secondary', - 'images/darrowleft_green.png', - 1, - '', - true, - [ - 'id' => 'left_autorefreshlist', - 'title' => __('Remove secondary groups'), - 'onclick' => 'agent_manager_remove_secondary_groups(event, '.$id_agente.');', - ] - ); - - $adv_secondary_groups_right .= html_print_select( - // Values. - $secondary_groups_selected['for_select'], - // HTML id. - 'secondary_groups_selected', - // Selected. - '', - // Javascript onChange code. - '', - // Nothing selected. - false, - // Nothing selected. - 0, - // Return HTML (not echo). - true, - // Multiple selection. - true, - // Sort. - true, - // Class. - '', - // Disabled. - false, - // Style. - 'min-width:170px;' + $adv_secondary_groups_label = '
'; + $adv_secondary_groups_label .= '

'; + $adv_secondary_groups_label .= __('Secondary groups'); + $adv_secondary_groups_label .= '

'; + $adv_secondary_groups_label .= '
'; + $select_agent_secondary = html_print_select_agent_secondary( + $agent, + $id_agente ); // Safe operation mode. @@ -821,19 +712,15 @@ if (enterprise_installed()) { } // General display distribution. -$table_adv_options = $advanced_div.$adv_secondary_groups_label.' -
- '.$adv_secondary_groups_left.' -
-
- '.$adv_secondary_groups_arrows.' -
-
- '.$adv_secondary_groups_right.' -
- -
- '.$table_adv_parent.$table_adv_module_mode.$table_adv_cascade; +$table_adv_options = $advanced_div; +$table_adv_options .= $adv_secondary_groups_label; +$table_adv_options .= $select_agent_secondary; +$table_adv_options .= '
'; + +$table_adv_options .= '
'; +$table_adv_options .= $table_adv_parent; +$table_adv_options .= $table_adv_module_mode; +$table_adv_options .= $table_adv_cascade; if ($new_agent) { // If agent is new, show custom id as old style format. @@ -1110,133 +997,6 @@ ui_require_jquery_file('bgiframe'); } } - function agent_manager_add_secondary_groups (event, id_agent) { - event.preventDefault(); - var primary_value = $("#grupo").val() - // The selected primary value cannot be selected like secondary - if ($("#secondary_groups option:selected[value=" + primary_value + "]").length > 0) { - alert("") - return - } - - // On agent creation PHP will update the secondary groups table (not via AJAX) - if (id_agent == 0) { - agent_manager_add_secondary_groups_ui(); - agent_manager_update_hidden_input_secondary(); - return; - } - - var selected_items = new Array(); - $("#secondary_groups option:selected").each(function(){ - selected_items.push($(this).val()) - }) - - var data = { - page: "godmode/agentes/agent_manager", - id_agent: id_agent, - groups: selected_items, - add_secondary_groups: 1, - } - - // Make the AJAX call to update the secondary groups - $.ajax({ - type: "POST", - url: "ajax.php", - dataType: "html", - data: data, - success: function (data) { - if (data == 1) { - agent_manager_add_secondary_groups_ui(); - } else { - console.error("Error in AJAX call to add secondary groups") - } - }, - error: function (data) { - console.error("Fatal error in AJAX call to add secondary groups") - } - }); - } - - function agent_manager_remove_secondary_groups (event, id_agent) { - event.preventDefault(); - - // On agent creation PHP will update the secondary groups table (not via AJAX) - if (id_agent == 0) { - agent_manager_remove_secondary_groups_ui(); - agent_manager_update_hidden_input_secondary(); - return; - } - - var selected_items = new Array(); - $("#secondary_groups_selected option:selected").each(function(){ - selected_items.push($(this).val()) - }) - - var data = { - page: "godmode/agentes/agent_manager", - id_agent: id_agent, - groups: selected_items, - remove_secondary_groups: 1, - } - - // Make the AJAX call to update the secondary groups - $.ajax({ - type: "POST", - url: "ajax.php", - dataType: "html", - data: data, - success: function (data) { - if (data == 1) { - agent_manager_remove_secondary_groups_ui(); - } else { - console.error("Error in AJAX call to add secondary groups") - } - }, - error: function (data) { - console.error("Fatal error in AJAX call to add secondary groups") - } - }); - } - - // Move from left input to right input - function agent_manager_add_secondary_groups_ui () { - $("#secondary_groups_selected option[value=0]").remove() - $("#secondary_groups option:selected").each(function() { - $(this).remove().appendTo("#secondary_groups_selected") - }) - } - - // Move from right input to left input - function agent_manager_remove_secondary_groups_ui () { - // Remove the groups selected if success - $("#secondary_groups_selected option:selected").each(function(){ - $(this).remove().appendTo("#secondary_groups") - }) - - // Add none if empty select - if ($("#secondary_groups_selected option").length == 0) { - $("#secondary_groups_selected").append($('
'; ui_require_jquery_file('form'); - ui_require_jquery_file('pandora.controls'); ui_require_jquery_file('ajaxqueue'); ui_require_jquery_file('bgiframe'); diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index da12915fc8..9353e34f7f 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -5619,3 +5619,185 @@ function html_print_select_search( echo $output; } } + + +/** + * Print html select for agents secondary. + * + * @param integer $agent Agent. + * @param integer $id_agente Id Agent. + * @param array $options Array options. + * + * @return string Html output. + */ +function html_print_select_agent_secondary($agent, $id_agente, $options=[]) +{ + ui_require_css_file('agent_manager'); + ui_require_javascript_file('pandora_agents'); + + if (empty($options) === '' || isset($options['id_form']) === false) { + $options['id_form'] = 'form_agent'; + } + + if (empty($options) === '' || isset($options['id_form']) === false) { + $options['extra_id'] = ''; + } + + $secondary_groups_selected = enterprise_hook( + 'agents_get_secondary_groups', + [$id_agente] + ); + + $adv_secondary_groups_left = html_print_select_groups( + // Id_user. + // Use the current user to select the groups. + false, + // Privilege. + // ACL permission. + 'AR', + // ReturnAllGroup. + // Not all group. + false, + // Name. + // HTML id. + 'secondary_groups'.$options['extra_id'], + // Selected. + // No select any by default. + '', + // Script. + // Javascript onChange code. + '', + // Nothing. + // Do not user no selected value. + false, + // Nothing_value. + // Do not use no selected value. + 0, + // Return. + // Return HTML (not echo). + true, + // Multiple. + // Multiple selection. + true, + // Sort. + // Sorting by default. + true, + // Class. + // CSS classnames (default). + '', + // Disabled. + // Not disabled (default). + false, + // Style. + // Inline styles (default). + 'min-width:170px;', + // Option_style. + // Option style select (default). + false, + // Id_group. + // Do not truncate the users tree (default). + false, + // Keys_field. + // Key to get as value (default). + 'id_grupo', + // Strict_user. + // Not strict user (default). + false, + // Delete_groups. + // Do not show the primary group in this selection. + array_merge( + (empty($secondary_groups_selected['plain']) === false) ? $secondary_groups_selected['plain'] : [], + [$agent['id_grupo']] + ) + // Include_groups. + // Size. + // Simple_multiple_options. + ); + + $dictionary = base64_encode( + json_encode( + [ + 'primary_group' => __('Primary group cannot be secondary too.'), + 'strNone' => __('None'), + ] + ) + ); + + $adv_secondary_groups_arrows = html_print_input_image( + 'add_secondary', + 'images/darrowright_green.png', + 1, + '', + true, + [ + 'id' => 'right_autorefreshlist'.$options['extra_id'], + 'title' => __('Add secondary groups'), + 'onclick' => 'agent_manager_add_secondary_groups(event, '.$id_agente.',\''.$options['extra_id'].'\', \''.$options['id_form'].'\', \''.$dictionary.'\');', + ] + ); + + $adv_secondary_groups_arrows .= html_print_input_image( + 'remove_secondary', + 'images/darrowleft_green.png', + 1, + '', + true, + [ + 'id' => 'left_autorefreshlist'.$options['extra_id'], + 'title' => __('Remove secondary groups'), + 'onclick' => 'agent_manager_remove_secondary_groups(event, '.$id_agente.',\''.$options['extra_id'].'\', \''.$options['id_form'].'\', \''.$dictionary.'\');', + ] + ); + + $adv_secondary_groups_right .= html_print_select( + // Values. + $secondary_groups_selected['for_select'], + // HTML id. + 'secondary_groups_selected'.$options['extra_id'], + // Selected. + '', + // Javascript onChange code. + '', + // Nothing selected. + false, + // Nothing selected. + 0, + // Return HTML (not echo). + true, + // Multiple selection. + true, + // Sort. + true, + // Class. + '', + // Disabled. + false, + // Style. + 'min-width:170px;' + ); + + $output = ''; + if (isset($options['container']) === true + && $options['container'] === true + ) { + $output = '
'; + } + + $output .= '
'; + $output .= $adv_secondary_groups_left; + $output .= '
'; + $output .= '
'; + $output .= $adv_secondary_groups_arrows; + $output .= '
'; + $output .= '
'; + $output .= $adv_secondary_groups_right; + $output .= '
'; + + if (isset($options['container']) === true + && $options['container'] === true + ) { + $output .= '
'; + } + + return $output; +} diff --git a/pandora_console/include/javascript/pandora_agents.js b/pandora_console/include/javascript/pandora_agents.js new file mode 100644 index 0000000000..a0ce0614a3 --- /dev/null +++ b/pandora_console/include/javascript/pandora_agents.js @@ -0,0 +1,169 @@ +/* globals $ */ +// eslint-disable-next-line no-unused-vars +function agent_manager_add_secondary_groups( + event, + id_agent, + extra_id, + id_form, + dictionary +) { + event.preventDefault(); + var primary_value = $("#grupo").val(); + dictionary = JSON.parse(atob(dictionary)); + // The selected primary value cannot be selected like secondary. + if ( + $( + "#secondary_groups" + + extra_id + + " option:selected[value=" + + primary_value + + "]" + ).length > 0 + ) { + alert(dictionary.primary_group); + return; + } + + // On agent creation PHP will update the secondary groups table (not via AJAX). + if (id_agent == 0) { + agent_manager_add_secondary_groups_ui(extra_id); + agent_manager_update_hidden_input_secondary(id_form, extra_id); + return; + } + + var selected_items = new Array(); + $("#secondary_groups" + extra_id + " option:selected").each(function() { + selected_items.push($(this).val()); + }); + + var data = { + page: "godmode/agentes/agent_manager", + id_agent: id_agent, + groups: selected_items, + add_secondary_groups: 1 + }; + + // Make the AJAX call to update the secondary groups. + $.ajax({ + type: "POST", + url: "ajax.php", + dataType: "html", + data: data, + success: function(data) { + if (data == 1) { + agent_manager_add_secondary_groups_ui(extra_id); + } else { + console.error("Error in AJAX call to add secondary groups"); + } + }, + error: function(data) { + console.error( + "Fatal error in AJAX call to add secondary groups: " + data + ); + } + }); +} + +// eslint-disable-next-line no-unused-vars +function agent_manager_remove_secondary_groups( + event, + id_agent, + extra_id, + id_form, + dictionary +) { + event.preventDefault(); + + dictionary = JSON.parse(atob(dictionary)); + // On agent creation PHP will update the secondary groups table (not via AJAX). + if (id_agent == 0) { + agent_manager_remove_secondary_groups_ui(dictionary.strNone, extra_id); + agent_manager_update_hidden_input_secondary(id_form, extra_id); + return; + } + + var selected_items = new Array(); + $("#secondary_groups_selected" + extra_id + " option:selected").each( + function() { + selected_items.push($(this).val()); + } + ); + + var data = { + page: "godmode/agentes/agent_manager", + id_agent: id_agent, + groups: selected_items, + remove_secondary_groups: 1 + }; + + // Make the AJAX call to update the secondary groups. + $.ajax({ + type: "POST", + url: "ajax.php", + dataType: "html", + data: data, + success: function(data) { + if (data == 1) { + agent_manager_remove_secondary_groups_ui(dictionary.strNone, extra_id); + } else { + console.error("Error in AJAX call to add secondary groups"); + } + }, + error: function(data) { + console.error( + "Fatal error in AJAX call to add secondary groups: " + data + ); + } + }); +} + +// Move from left input to right input. +function agent_manager_add_secondary_groups_ui(extra_id) { + $("#secondary_groups_selected" + extra_id + " option[value=0]").remove(); + $("#secondary_groups" + extra_id + " option:selected").each(function() { + $(this) + .remove() + .appendTo("#secondary_groups_selected" + extra_id); + }); +} + +// Move from right input to left input. +function agent_manager_remove_secondary_groups_ui(strNone, extra_id) { + // Remove the groups selected if success. + $("#secondary_groups_selected" + extra_id + " option:selected").each( + function() { + $(this) + .remove() + .appendTo("#secondary_groups" + extra_id); + } + ); + + // Add none if empty select. + if ($("#secondary_groups_selected" + extra_id + " option").length == 0) { + $("#secondary_groups_selected" + extra_id).append( + $("