From bf720984c06eb25c6cdd404830ed55ac9b2b8ebf Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Wed, 21 Jan 2015 18:56:42 +0100 Subject: [PATCH] Finish the feature of massive operations with tags. TICKET: #1632 --- .../godmode/massive/massive_add_tags.php | 8 +- .../godmode/massive/massive_delete_tags.php | 305 ++++++++++++++++++ pandora_console/include/functions_tags.php | 36 +++ 3 files changed, 345 insertions(+), 4 deletions(-) create mode 100755 pandora_console/godmode/massive/massive_delete_tags.php diff --git a/pandora_console/godmode/massive/massive_add_tags.php b/pandora_console/godmode/massive/massive_add_tags.php index c6ccc45848..25d311d4ec 100755 --- a/pandora_console/godmode/massive/massive_add_tags.php +++ b/pandora_console/godmode/massive/massive_add_tags.php @@ -46,15 +46,15 @@ function process_manage_add ($id_agents, $modules, $id_tags) { $modules_id = array(); - foreach($modules as $module) { - foreach($id_agents as $id_agent) { + foreach ($modules as $module) { + foreach ($id_agents as $id_agent) { $module_id = modules_get_agentmodule_id($module, $id_agent); $modules_id[] = $module_id['id_agente_modulo']; } } if (count($modules) == 1 && $modules[0] == '0') { - foreach($id_agents as $id_agent) { + foreach ($id_agents as $id_agent) { $modules_temp = agents_get_modules($id_agent); foreach ($modules_temp as $id_module => $name_module) { $modules_id[] = $id_module; @@ -65,7 +65,7 @@ function process_manage_add ($id_agents, $modules, $id_tags) { $conttotal = 0; $contsuccess = 0; - foreach($modules_id as $id_module) { + foreach ($modules_id as $id_module) { $err_count = tags_insert_module_tag($id_module, $id_tags); if ($err_count == 0) { diff --git a/pandora_console/godmode/massive/massive_delete_tags.php b/pandora_console/godmode/massive/massive_delete_tags.php new file mode 100755 index 0000000000..52d9ece86e --- /dev/null +++ b/pandora_console/godmode/massive/massive_delete_tags.php @@ -0,0 +1,305 @@ + $id) { + $id_agents[$key] = (int)$id; + } + + $data = db_get_all_rows_sql(" + SELECT nombre + FROM tagente_modulo + WHERE id_agente IN (" . implode(',', $id_agents) . ") + AND id_agente_modulo IN ( + SELECT t1.id_agente_modulo + FROM ttag_module AS t1 + WHERE id_tag = " . $id_tag . " + AND id_policy_module = 0) + GROUP BY nombre;"); + + if (empty($data)) { + echo json_encode(array()); + } + else { + $modules = array(); + foreach ($data as $row) { + $modules[] = $row['nombre']; + } + + echo json_encode($modules); + } + } + else { + echo json_encode(array()); + } + + return; + } + + return; +} + +function process_manage_delete ($id_agents, $modules, $id_tag) { + + if (empty ($id_agents) || $id_agents[0] == 0) { + ui_print_error_message(__('No agents selected')); + return false; + } + + if (empty ($modules) || $modules[0] == "0") { + ui_print_error_message(__('No modules selected')); + return false; + } + + if (empty ($id_tag)) { + ui_print_error_message(__('No tag selected')); + return false; + } + + $modules_id = array(); + foreach ($modules as $module) { + $data = db_get_all_rows_sql(" + SELECT id_agente_modulo + FROM tagente_modulo + WHERE nombre = '" . $module . "' + AND id_agente IN (" . implode(",", $id_agents) . ") + "); + + if (empty($data)) { + $data = array(); + } + + foreach ($data as $row) { + $modules_id[$row['id_agente_modulo']] = $row['id_agente_modulo']; + } + } + + $conttotal = 0; + $contsuccess = 0; + foreach ($modules_id as $id_module) { + $success = tags_remove_tag($id_tag, $id_module); + + if ($success) { + $contsuccess ++; + } + + $conttotal ++; + } + + if ($contsuccess > 0) { + db_pandora_audit("Massive management", "Delete tags", false, false, + ""); + } + else { + db_pandora_audit("Massive management", "Fail try to delete tags", + false, false, ""); + } + + ui_print_result_message ($contsuccess > 0, + __('Successfully deleted') . "(" . $contsuccess . "/" . $conttotal . ")", + __('Could not be deleted')); + +} + +$id_agents = get_parameter ('id_agents'); +$id_tag = (int)get_parameter ('id_tag'); +$modules = get_parameter ('modules'); + +$delete = (bool) get_parameter_post ('delete'); + +if ($delete) { + process_manage_delete ($id_agents, $modules, $id_tag); +} + +$groups = users_get_groups (); +$own_info = get_user_info($config['id_user']); +if (!$own_info['is_admin'] && !check_acl ($config['id_user'], 0, "AW")) + $return_all_group = false; +else + $return_all_group = true; + +$table->id = 'add_table'; +$table->width = '98%'; +$table->data = array (); +$table->style = array (); +$table->style[0] = 'font-weight: bold; vertical-align:top'; +$table->style[2] = 'font-weight: bold; vertical-align:top'; +$table->size = array (); +$table->size[0] = '15%'; +$table->size[1] = '40%'; +$table->size[2] = '15%'; +$table->size[3] = '40%'; + +$table->data = array (); + +$table->data[0][0] = __('Tags'); +$tags = tags_get_all_tags(); +$table->data[0][1] = html_print_select ($tags, + 'id_tag', '', '', '', '', true, false, true, '', false, 'min-width:180px;'); + +$table->data[1][0] = __('Agents'); +$table->data[1][0] .= ''; +$table->data[1][1] = html_print_select (array(), + 'id_agents[]', '', '', '', '', true, true, true, '', false, 'min-width:180px;'); + +$table->data[2][0] = __('Modules'); +$table->data[2][0] .= ''; +$table->data[2][1] = '' . + html_print_select (array(), 'modules[]', '', false, '', '', true, true, false, '', false, 'min-width:180px;'); + + + + + +echo '
'; +html_print_table ($table); + +echo '
'; +html_print_input_hidden ('delete', 1); + +html_print_submit_button (__('Delete'), 'go', false, 'class="sub delete"'); +echo '
'; +echo '
'; + +// TODO: Change to iu_print_error system +echo ''; + +//Hack to translate text "none" in PHP to javascript +echo ''; + +ui_require_jquery_file ('form'); +ui_require_jquery_file ('pandora.controls'); +?> + + diff --git a/pandora_console/include/functions_tags.php b/pandora_console/include/functions_tags.php index 4605cd5cb4..9f9147e7a0 100644 --- a/pandora_console/include/functions_tags.php +++ b/pandora_console/include/functions_tags.php @@ -403,6 +403,14 @@ function tags_delete_tag ($id_tag) { } +function tags_remove_tag($id_tag, $id_module) { + $result = (bool)db_process_sql_delete('ttag_module', + array('id_tag' => $id_tag, + 'id_agente_modulo' => $id_module)); + + return $result; +} + /** * Get tag's total count. * @@ -658,6 +666,34 @@ function tags_get_tags ($ids) { return $tags; } +function tags_get_agents($id_tag, $id_policy_module = 0) { + + $agents = db_get_all_rows_sql(" + SELECT id_agente + FROM tagente + WHERE id_agente IN ( + SELECT t1.id_agente + FROM tagente_modulo AS t1 + WHERE t1.id_agente_modulo IN ( + SELECT t2.id_agente_modulo + FROM ttag_module AS t2 + WHERE id_tag = " . $id_tag . " + AND id_policy_module = " . $id_policy_module . "))"); + + if (empty($agents)) { + return array(); + } + + + $temp = array(); + foreach ($agents as $agent) { + $temp[] = $agent['id_agente']; + } + $agents = $temp; + + return $agents; +} + /** * Give format to tags when go concatened with url. *