Merge branch 'ent-2152-Operaciones-masivas-de-grupos-secundarios' into 'develop'

bulk operation edit agents add secundary groups pandora_enterprise#2152

See merge request artica/pandorafms!4677
This commit is contained in:
Daniel Rodriguez 2022-03-11 08:51:33 +00:00
commit a67b6647f7
8 changed files with 791 additions and 319 deletions

View File

@ -191,7 +191,7 @@ if ($disk_conf_delete) {
@unlink($filename['conf']);
}
echo '<form autocomplete="new-password" name="conf_agent" method="post" action="index.php?sec=gagente&sec2=godmode/agentes/configurar_agente">';
echo '<form autocomplete="new-password" name="conf_agent" id="form_agent" method="post" action="index.php?sec=gagente&sec2=godmode/agentes/configurar_agente">';
// Custom ID.
$custom_id_div = '<div class="label_select">';
@ -452,123 +452,14 @@ if (!$new_agent && $alias != '') {
echo '</div>';
if (enterprise_installed()) {
$secondary_groups_selected = enterprise_hook('agents_get_secondary_groups', [$id_agente]);
$adv_secondary_groups_label = '<div class="label_select"><p class="input_label">'.__('Secondary groups').'</p></div>';
$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 = '<div class="label_select">';
$adv_secondary_groups_label .= '<p class="input_label">';
$adv_secondary_groups_label .= __('Secondary groups');
$adv_secondary_groups_label .= '</p>';
$adv_secondary_groups_label .= '</div>';
$select_agent_secondary = html_print_select_agent_secondary(
$agent,
$id_agente
);
// Safe operation mode.
@ -824,19 +715,15 @@ if (enterprise_installed()) {
}
// General display distribution.
$table_adv_options = $advanced_div.$adv_secondary_groups_label.'
<div class="sg_source">
'.$adv_secondary_groups_left.'
</div>
<div class="secondary_group_arrows">
'.$adv_secondary_groups_arrows.'
</div>
<div class="sg_target">
'.$adv_secondary_groups_right.'
</div>
</div>
<div class="agent_av_opt_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 .= '</div>';
$table_adv_options .= '<div class="agent_av_opt_right" >';
$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.
@ -1113,133 +1000,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("<?php echo __('Primary group cannot be secondary too.'); ?>")
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($('<option>',{
value: 0,
text: "<?php echo __('None'); ?>"
}))
}
}
function agent_manager_update_hidden_input_secondary () {
var groups = [];
if(!$('form[name="conf_agent"] #secondary_hidden').length) {
$('form[name="conf_agent"]').append(
'<input name="secondary_hidden" type="hidden" id="secondary_hidden">'
);
}
var groups = new Array();
$("#secondary_groups_selected option").each(function() {
groups.push($(this).val())
})
$("#secondary_hidden").val(groups.join(','));
}
$(document).ready (function() {

View File

@ -985,7 +985,6 @@ if ($update_agent) {
$fields = db_get_all_fields_in_table('tagent_custom_fields');
$secondary_groups = (string) get_parameter('secondary_hidden', '');
if ($fields === false) {
$fields = [];
}

View File

@ -14,7 +14,7 @@
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Copyright (c) 2005-2022 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -47,15 +47,17 @@ require_once $config['homedir'].'/include/functions_gis.php';
require_once $config['homedir'].'/include/functions_users.php';
enterprise_include_once('include/functions_config_agents.php');
if (is_ajax()) {
$get_n_conf_files = (bool) get_parameter('get_n_conf_files');
if ($get_n_conf_files) {
if (is_ajax() === true) {
$get_n_conf_files = (bool) get_parameter('get_n_conf_files', false);
$groups_secondary_selected = (bool) get_parameter('groups_secondary_selected', false);
if ($get_n_conf_files === true) {
$id_agents = get_parameter('id_agents');
$cont = 0;
foreach ($id_agents as $id_agent) {
$name = agents_get_name($id_agent);
$agent_md5 = md5($name);
if (file_exists($config['remote_config'].'/md5/'.$agent_md5.'.md5')) {
if (file_exists($config['remote_config'].'/md5/'.$agent_md5.'.md5') === true) {
$cont++;
}
}
@ -63,6 +65,24 @@ if (is_ajax()) {
echo $cont;
return;
}
if ($groups_secondary_selected === true) {
$groups = get_parameter('groups', []);
$groups_selected = get_parameter('groups_selected', []);
hd($groups, true);
hd($groups_selected, true);
$user_groups = users_get_groups($config['user'], 'AR', false);
$ret = [];
foreach ($user_groups as $id_gr => $name_group) {
if (in_array($id_gr, $groups) === false) {
$ret[$id_gr] = $name_group;
}
}
echo json_encode($ret);
return;
}
}
$update_agents = get_parameter('update_agents', 0);
@ -140,6 +160,9 @@ if ($update_agents) {
$values['safe_mode_module'] = '0';
}
$secondary_groups_added = (array) get_parameter('secondary_groups_added', []);
$secondary_groups_removed = (array) get_parameter('secondary_groups_removed', []);
$fields = db_get_all_fields_in_table('tagent_custom_fields');
if ($fields === false) {
@ -213,7 +236,7 @@ if ($update_agents) {
}
$n_edited = 0;
$result = false;
$result = [];
foreach ($id_agents as $id_agent) {
$old_interval_value = db_get_value_filter('intervalo', 'tagente', ['id_agente' => $id_agent]);
@ -231,20 +254,20 @@ if ($update_agents) {
$values['safe_mode_module'] = $id_module_safe[$id_agent];
}
$result = db_process_sql_update(
$result[$id_agent]['db'] = db_process_sql_update(
'tagente',
$values,
['id_agente' => $id_agent]
);
if ($result && $config['metaconsole_agent_cache'] == 1) {
if ($result[$id_agent]['db'] && $config['metaconsole_agent_cache'] == 1) {
$server_name['server_name'] = db_get_sql('SELECT server_name FROM tagente WHERE id_agente ='.$id_agent);
// Force an update of the agent cache.
$result_metaconsole = agent_update_from_cache($id_agent, $values, $server_name);
}
// Update the configuration files.
if ($result && ($old_interval_value != $values['intervalo']) && !empty($values['intervalo'])) {
if ($result[$id_agent]['db'] && ($old_interval_value != $values['intervalo']) && !empty($values['intervalo'])) {
enterprise_hook(
'config_agents_update_config_token',
[
@ -288,7 +311,7 @@ if ($update_agents) {
if ($old_value === false) {
// Create custom field if not exist.
$result = db_process_sql_insert(
$result[$id_agent]['fields'][$field['id_field']] = db_process_sql_insert(
'tagent_custom_data',
[
'id_field' => $key,
@ -298,7 +321,7 @@ if ($update_agents) {
);
} else {
if ($old_value[0]['description'] !== $value) {
$result = db_process_sql_update(
$result[$id_agent]['fields'][$field['id_field']] = db_process_sql_update(
'tagent_custom_data',
['description' => $value],
[
@ -311,36 +334,160 @@ if ($update_agents) {
}
}
$n_edited += (int) $result;
}
// Create or Remove the secondary groups.
if (empty($secondary_groups_added) === false
|| empty($secondary_groups_removed) === false
) {
$result[$id_agent]['secondary'] = enterprise_hook(
'agents_update_secondary_groups',
[
$id_agent,
$secondary_groups_added,
$secondary_groups_removed,
true,
]
);
}
if ($result !== false) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Update agent '.$id_agent,
false,
false,
json_encode($info)
);
} else {
if (isset($id_agent)) {
if ($result['db'] !== false) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Try to update agent '.$id_agent,
'Update agent '.$id_agent,
false,
false,
json_encode($info)
);
} else {
if (isset($id_agent) === true) {
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
'Try to update agent '.$id_agent,
false,
false,
json_encode($info)
);
}
}
}
$ret = [];
foreach ($result as $id_agent => $item) {
if ($item['db'] !== false) {
$ret['db']['edited'] += 1;
$ret['db']['edited_agent'][] = $id_agent;
} else {
$ret['db']['failed'] += 1;
$ret['db']['failed_agent'][] = $id_agent;
}
ui_print_result_message(
$result !== false,
__('Agents updated successfully (%d)', $n_edited),
__('Agents cannot be updated (maybe there was no field to update)')
);
if (isset($item['fields']) === true
&& empty($item['fields']) === false
) {
foreach ($item['fields'] as $kfield => $vfield) {
if ($vfield !== false) {
$ret['fields'][$id_agent]['edited'] += 1;
$ret['fields'][$id_agent]['edited_field'][] = $kfield;
} else {
$ret['fields'][$id_agent]['failed'] += 1;
$ret['fields'][$id_agent]['failed_field'][] = $kfield;
}
}
}
if (isset($item['secondary']) === true
&& empty($item['secondary']) === false
) {
foreach ($item['secondary'] as $type_action => $values_secondary) {
foreach ($values_secondary as $kgr => $vgr) {
if ($vgr !== false) {
$ret['secondary'][$type_action][$id_agent]['edited'] += 1;
$ret['secondary'][$type_action][$id_agent]['edited_gr'][] = $kgr;
} else {
$ret['secondary'][$type_action][$id_agent]['failed'] += 1;
$ret['secondary'][$type_action][$id_agent]['failed_gr'][] = $kgr;
}
}
}
}
}
foreach ($ret as $type => $ret_val) {
switch ($type) {
case 'db':
if (isset($ret_val['edited']) === true
&& $ret_val['edited'] > 0
) {
ui_print_success_message(
__(
'Agents updated successfully (%d)',
$ret_val['edited'],
implode(
',',
$ret_val['edited_agent']
)
)
);
}
if (isset($ret_val['failed']) === true
&& $ret_val['failed'] > 0
) {
ui_print_error_message(
__(
'Agents cannot be updated (%d), ids (%s)',
$ret_val['failed'],
implode(',', $ret_val['failed_agent'])
)
);
}
break;
case 'fields':
$str = '';
foreach ($ret_val as $kag => $vag) {
if (isset($vag['failed']) === true
&& $vag['failed'] > 0
) {
$str .= __(
'Agent ID: %s cannot be updated custom fields (%s)',
$kag,
implode(',', $vag['failed_field'])
).'<br>';
}
}
if (empty($str) === false) {
ui_print_error_message($str);
}
break;
case 'secondary':
$str = '';
foreach ($ret_val as $type => $values_secondary) {
foreach ($values_secondary as $kag => $vag) {
if (isset($vag['failed']) === true
&& $vag['failed'] > 0
) {
$str .= __(
'Agent ID: %s cannot be updated %s secondary groups (%s)',
$kag,
$type,
implode(',', $vag['failed_gr'])
).'<br>';
}
}
}
if (empty($str) === false) {
ui_print_error_message($str);
}
break;
default:
// Not posible.
break;
}
}
}
$id_group = 0;
@ -526,7 +673,16 @@ $table->data[0][1] .= '<b>'.__('Cascade protection').'</b>'.html_print_select(
true
);
$table->data[0][1] .= '&nbsp;&nbsp;'.__('Module').'&nbsp;'.html_print_select($modules, 'cascade_protection_module', $cascade_protection_module, '', '', 0, true);
$table->data[0][1] .= '&nbsp;&nbsp;'.__('Module').'&nbsp;';
$table->data[0][1] .= html_print_select(
$modules,
'cascade_protection_module',
$cascade_protection_module,
'',
'',
0,
true
);
$table->data[1][0] = __('Group');
$table->data[1][1] = '<div class="w290px inline">';
@ -550,7 +706,17 @@ $table->data[1][1] .= '</div>';
$table->data[2][0] = __('Interval');
$table->data[2][1] = html_print_extended_select_for_time('interval', 0, '', __('No change'), '0', 10, true, 'width: 150px', false);
$table->data[2][1] = html_print_extended_select_for_time(
'interval',
0,
'',
__('No change'),
'0',
10,
true,
'width: 150px',
false
);
$table->data[3][0] = __('OS');
$table->data[3][1] = html_print_select_from_sql(
@ -594,7 +760,14 @@ $table->data[4][1] = html_print_select(
// Description.
$table->data[5][0] = __('Description');
$table->data[5][1] = html_print_input_text('description', $description, '', 45, 255, true);
$table->data[5][1] = html_print_input_text(
'description',
$description,
'',
45,
255,
true
);
html_print_table($table);
unset($table);
@ -627,18 +800,148 @@ $table->data = [];
$table->data[0][0] = __('Custom ID');
$table->data[0][1] = html_print_input_text('custom_id', $custom_id, '', 16, 255, true);
// Secondary Groups.
if (enterprise_installed() === true) {
$groups = users_get_groups($config['id_user'], 'AW', false);
$table->data['secondary_groups_added'][0] = __('Add secondary groups');
$table->data['secondary_groups_added'][1] = html_print_select(
$groups,
'secondary_groups_added[]',
0,
false,
'',
'',
true,
true,
true,
'',
false,
'min-width: 500px; max-width: 500px; max-height: 100px',
false,
false,
false,
'',
false,
false,
false,
false,
true,
true
);
$table->data['secondary_groups_removed'][0] = __('Remove secondary groups');
$table->data['secondary_groups_removed'][1] = html_print_select(
$groups,
'secondary_groups_removed[]',
0,
false,
'',
'',
true,
true,
true,
'',
false,
'min-width: 500px; max-width: 500px; max-height: 100px',
false,
false,
false,
'',
false,
false,
false,
false,
true,
true
);
}
// Learn mode / Normal mode.
$table->data[1][0] = __('Module definition');
$table->data[1][1] = __('No change').' '.html_print_radio_button_extended('mode', -1, '', $mode, false, '', 'class="mrgn_right_40px"', true);
$table->data[1][1] .= __('Learning mode').' '.html_print_radio_button_extended('mode', 1, '', $mode, false, '', 'class="mrgn_right_40px"', true);
$table->data[1][1] .= __('Normal mode').' '.html_print_radio_button_extended('mode', 0, '', $mode, false, '', 'class="mrgn_right_40px"', true);
$table->data[1][1] .= __('Autodisable mode').' '.html_print_radio_button_extended('mode', 2, '', $mode, false, '', 'class="mrgn_right_40px"', true);
$table->data[1][1] = __('No change').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'mode',
-1,
'',
$mode,
false,
'',
'class="mrgn_right_40px"',
true
);
$table->data[1][1] .= __('Learning mode').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'mode',
1,
'',
$mode,
false,
'',
'class="mrgn_right_40px"',
true
);
$table->data[1][1] .= __('Normal mode').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'mode',
0,
'',
$mode,
false,
'',
'class="mrgn_right_40px"',
true
);
$table->data[1][1] .= __('Autodisable mode').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'mode',
2,
'',
$mode,
false,
'',
'class="mrgn_right_40px"',
true
);
// Status (Disabled / Enabled).
$table->data[2][0] = __('Status');
$table->data[2][1] = __('No change').' '.html_print_radio_button_extended('disabled', -1, '', $disabled, false, '', 'class="mrgn_right_40px"', true);
$table->data[2][1] .= __('Disabled').' '.ui_print_help_tip(__('If the remote configuration is enabled, it will also go into standby mode when disabling it.'), true).' '.html_print_radio_button_extended('disabled', 1, '', $disabled, false, '', 'class="mrgn_right_40px"', true);
$table->data[2][1] .= __('Active').' '.html_print_radio_button_extended('disabled', 0, '', $disabled, false, '', 'class="mrgn_right_40px"', true);
$table->data[2][1] = __('No change').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'disabled',
-1,
'',
$disabled,
false,
'',
'class="mrgn_right_40px"',
true
);
$table->data[2][1] .= __('Disabled').' ';
$table->data[1][1] .= ui_print_help_tip(
__('If the remote configuration is enabled, it will also go into standby mode when disabling it.'),
true
).' ';
$table->data[1][1] .= html_print_radio_button_extended(
'disabled',
1,
'',
$disabled,
false,
'',
'class="mrgn_right_40px"',
true
);
$table->data[2][1] .= __('Active').' ';
$table->data[1][1] .= html_print_radio_button_extended(
'disabled',
0,
'',
$disabled,
false,
'',
'class="mrgn_right_40px"',
true
);
// Remote configuration.
$table->data[3][0] = __('Remote configuration');
@ -813,7 +1116,6 @@ attachActionButton('update_agents', 'update', $table->width);
echo '</div></form>';
ui_require_jquery_file('form');
ui_require_jquery_file('pandora.controls');
ui_require_jquery_file('ajaxqueue');
ui_require_jquery_file('bgiframe');
@ -912,25 +1214,26 @@ $(document).ready (function () {
jQuery.each ($("#id_agents option:selected"), function (i, val) {
idAgents.push($(val).val());
});
jQuery.post ("ajax.php",
{"page" : "godmode/massive/massive_edit_agents",
jQuery.post (
"ajax.php",
{
"page" : "godmode/massive/massive_edit_agents",
"get_n_conf_files" : 1,
"id_agents[]" : idAgents
},
function (data, status) {
if (data == 0) {
$("#delete_configurations").attr("style", "display: none");
$("#not_available_configurations").attr("style", "");
}
else {
$("#n_configurations").text(data);
$("#not_available_configurations").attr("style", "display: none");
$("#delete_configurations").attr("style", "");
}
},
"json"
);
},
function (data, status) {
if (data == 0) {
$("#delete_configurations").attr("style", "display: none");
$("#not_available_configurations").attr("style", "");
}
else {
$("#n_configurations").text(data);
$("#not_available_configurations").attr("style", "display: none");
$("#delete_configurations").attr("style", "");
}
},
"json"
);
$("#form_agents").attr("style", "");
if($("#safe_mode_change").val() == 1) {
@ -970,8 +1273,41 @@ $(document).ready (function () {
disabled = 2;
//$("#id_group").trigger("change");
$("#status_agents").change(function() {
$("#id_group").trigger("change");
});
$("#secondary_groups_added").change(
function() {
var groups = $("#secondary_groups_added").val();
var groups_selected = $("#secondary_groups_removed").val();
jQuery.post (
"ajax.php",
{
"page" : "godmode/massive/massive_edit_agents",
"groups_secondary_selected" : 1,
"groups" : groups
},
function (data, status) {
$('#secondary_groups_removed').empty();
$('#secondary_groups_removed').val(null).trigger("change");
if($.type(data) === "object"){
jQuery.each (data, function (id, value) {
option = $("<option></option>").attr("value", id).html(value);
if (inArray(id, groups_selected) === true) {
option.attr("selected", true);
}
$("#secondary_groups_removed").append(option).trigger("change");
});
} else {
option = $("<option></option>").attr("value", '').html('None');
$("#secondary_groups_removed").append(option).trigger("change");
}
},
"json"
);
}
);
});
function changeIcons() {

View File

@ -5734,3 +5734,198 @@ 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['extra_id']) === false) {
$options['extra_id'] = '';
}
if (empty($options) === '' || isset($options['only_select']) === false) {
$options['only_select'] = false;
}
$secondary_groups_selected = enterprise_hook(
'agents_get_secondary_groups',
[$id_agente]
);
$name = 'secondary_groups'.$options['extra_id'];
if ($options['only_select'] === true) {
$name = 'secondary_groups'.$options['extra_id'].'[]';
}
$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.
$name,
// 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.
);
if ($options['only_select'] === false) {
$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 = '<div class="secondary_groups_list">';
}
$output .= '<div class="sg_source">';
$output .= $adv_secondary_groups_left;
$output .= '</div>';
$output .= '<div class="secondary_group_arrows">';
$output .= $adv_secondary_groups_arrows;
$output .= '</div>';
$output .= '<div class="sg_target">';
$output .= $adv_secondary_groups_right;
$output .= '</div>';
if (isset($options['container']) === true
&& $options['container'] === true
) {
$output .= '</div>';
}
} else {
$output .= $adv_secondary_groups_left;
}
return $output;
}

View File

@ -2017,3 +2017,11 @@ function progressBarSvg(option) {
return svg;
}
function inArray(needle, haystack) {
var length = haystack.length;
for (var i = 0; i < length; i++) {
if (haystack[i] == needle) return true;
}
return false;
}

View File

@ -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(
$("<option>", {
value: 0,
text: strNone
})
);
}
}
function agent_manager_update_hidden_input_secondary(id_form, extra_id) {
var groups = [];
if (!$("#" + id_form + " #secondary_hidden" + extra_id).length) {
$("#" + id_form).append(
'<input name="secondary_hidden' +
extra_id +
'" type="hidden" id="secondary_hidden' +
extra_id +
'">'
);
}
$("#secondary_groups_selected" + extra_id + " option").each(function() {
groups.push($(this).val());
});
$("#secondary_hidden" + extra_id).val(groups.join(","));
}

View File

@ -7678,6 +7678,11 @@ div.graph div.legend table {
float: left;
}
#table3-secondary_groups_added .select2-container,
#table3-secondary_groups_removed .select2-container {
width: 100% !important;
}
.ux_console_module {
float: left;
width: 98%;

View File

@ -365,7 +365,7 @@ $data[1] = ui_progress(
($agent['intervalo'] - (strtotime('now') - strtotime($agent['ultimo_contacto']))).' s',
[
'page' => 'operation/agentes/ver_agente',
'interval' => (100 / $agent['intervalo']),
'interval' => (empty($agent['intervalo']) === true) ? 0 : (100 / $agent['intervalo']),
'data' => [
'id_agente' => $id_agente,
'refresh_contact' => 1,