From db16a797f3059499381112f67bf4d875c305921e Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 22 Mar 2018 13:34:15 +0100 Subject: [PATCH] [Secondary Groups] Assign secondary groups by AJAX on agent edit view --- .../godmode/agentes/agent_manager.php | 125 +++++++++++++++--- .../godmode/alerts/configure_alert_action.php | 4 +- 2 files changed, 105 insertions(+), 24 deletions(-) diff --git a/pandora_console/godmode/agentes/agent_manager.php b/pandora_console/godmode/agentes/agent_manager.php index cfb2379843..c5f41df3ae 100644 --- a/pandora_console/godmode/agentes/agent_manager.php +++ b/pandora_console/godmode/agentes/agent_manager.php @@ -13,6 +13,16 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. + +// Load global vars +enterprise_include ('godmode/agentes/agent_manager.php'); + +require_once ('include/functions_clippy.php'); +require_once ('include/functions_servers.php'); +require_once ('include/functions_gis.php'); +require_once($config['homedir'] . "/include/functions_agents.php"); +require_once ($config['homedir'] . '/include/functions_users.php'); + if (is_ajax ()) { global $config; @@ -88,17 +98,28 @@ if (is_ajax ()) { echo io_json_mb_encode($out); } - + + // And and remove groups use the same function + $add_secondary_groups = get_parameter('add_secondary_groups'); + $remove_secondary_groups = get_parameter('remove_secondary_groups'); + if ($add_secondary_groups || $remove_secondary_groups) { + $id_agent = get_parameter('id_agent'); + $groups_to_add = get_parameter('groups'); + if (enterprise_installed()) { + enterprise_include('include/functions_agents.php'); + $ret = enterprise_hook( + 'agents_update_secondary_groups', + array( + $id_agent, + $add_secondary_groups ? $groups_to_add : array(), + $remove_secondary_groups ? $groups_to_add : array()) + ); + // Echo 0 in case of error. 0 Otherwise. + echo $ret ? 1 : 0; + } + } return; } -// Load global vars -enterprise_include ('godmode/agentes/agent_manager.php'); - -require_once ('include/functions_clippy.php'); -require_once ('include/functions_servers.php'); -require_once ('include/functions_gis.php'); -require_once($config['homedir'] . "/include/functions_agents.php"); -require_once ($config['homedir'] . '/include/functions_users.php'); ui_require_javascript_file('openlayers.pandora'); @@ -331,12 +352,12 @@ if (enterprise_installed()) { $table->data['secondary_groups'][2] = html_print_input_image ('add_secondary', 'images/darrowright.png', 1, '', true, array ( 'title' => __('Add secondary groups'), - 'onclick' => "agent_manager_add_secondary_groups(event);" + 'onclick' => "agent_manager_add_secondary_groups(event, " . $id_agente . ");" )) . '



' . html_print_input_image ('remove_secondary', 'images/darrowleft.png', 1, '', true, array ( 'title' => __('Remove secondary groups'), - 'onclick' => "agent_manager_remove_secondary_groups(event);" + 'onclick' => "agent_manager_remove_secondary_groups(event, " . $id_agente . ");" )); $table->data['secondary_groups'][3] = html_print_select ( @@ -623,7 +644,7 @@ ui_require_jquery_file('bgiframe'); } } - function agent_manager_add_secondary_groups (event) { + 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 @@ -631,25 +652,85 @@ ui_require_jquery_file('bgiframe'); alert("") return } - // Move from one input to the other - $("#secondary_groups_selected option[value=0]").remove() + + var selected_items = new Array(); $("#secondary_groups option:selected").each(function(){ - $(this).remove().appendTo("#secondary_groups_selected") + 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) { + // Move from one input to the other + $("#secondary_groups_selected option[value=0]").remove() + $("#secondary_groups option:selected").each(function() { + $(this).remove().appendTo("#secondary_groups_selected") + }) + } 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) { + function agent_manager_remove_secondary_groups (event, id_agent) { event.preventDefault(); + + var selected_items = new Array(); $("#secondary_groups_selected option:selected").each(function(){ - $(this).remove().appendTo("#secondary_groups") + selected_items.push($(this).val()) }) - if ($("#secondary_groups_selected option").length == 0) { - $("#secondary_groups_selected").append($('