From 5f6aaa1d05446a3100f3396c1397f9c3b3bfe853 Mon Sep 17 00:00:00 2001
From: m-lopez-f <miguel.lopez@artica.es>
Date: Thu, 17 Nov 2016 12:49:30 +0100
Subject: [PATCH] Feature: Search by status module in bulk module delete/edit
 actions. Ticket: #4190

---
 .../massive/massive_delete_modules.php        | 28 ++++++++++++++-
 .../godmode/massive/massive_edit_modules.php  | 35 ++++++++++++++++---
 pandora_console/include/functions_agents.php  |  3 ++
 pandora_console/include/javascript/pandora.js | 22 +++++++++++-
 .../operation/agentes/ver_agente.php          | 17 +++++++++
 5 files changed, 99 insertions(+), 6 deletions(-)

diff --git a/pandora_console/godmode/massive/massive_delete_modules.php b/pandora_console/godmode/massive/massive_delete_modules.php
index 95c7499920..adcabc763a 100755
--- a/pandora_console/godmode/massive/massive_delete_modules.php
+++ b/pandora_console/godmode/massive/massive_delete_modules.php
@@ -460,7 +460,19 @@ $table->data['form_agents_2'][1] = html_print_select($status_list,
 	'status_agents', 'selected', '', __('All'), AGENT_STATUS_ALL, true);
 $table->data['form_agents_2'][3] = '';
 
-
+$table->rowclass['form_modules_3'] = '';
+$table->data['form_modules_3'][0] = __('Module Status');
+$table->colspan['form_modules_3'][1] = 2;
+$status_list = array ();
+$status_list[AGENT_MODULE_STATUS_NORMAL] = __('Normal');
+$status_list[AGENT_MODULE_STATUS_WARNING] = __('Warning');
+$status_list[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical');
+$status_list[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown');
+$status_list[AGENT_MODULE_STATUS_NOT_NORMAL] = __('Not normal');
+$status_list[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init');
+$table->data['form_modules_3'][1] = html_print_select($status_list,
+	'status_module', 'selected', '', __('All'), AGENT_MODULE_STATUS_ALL, true);
+$table->data['form_modules_3'][3] = '';
 
 $table->rowstyle['form_modules_2'] = 'vertical-align: top;';
 $table->rowclass['form_modules_2'] = 'select_modules_row select_modules_row_2';
@@ -569,6 +581,10 @@ $(document).ready (function () {
 		if (this.value != '0')
 			params['id_tipo_modulo'] = this.value;
 		
+		var status_module = $('#status_module').val();
+		if (status_module != '-1')
+			params['status_module'] = status_module;
+		
 		$("#module_loading").show ();
 		$("tr#delete_table-edit1, tr#delete_table-edit2").hide ();
 		$("#module_name").attr ("disabled", "disabled")
@@ -712,6 +728,16 @@ $(document).ready (function () {
 			return false;
 		}
 	});
+	
+	$("#status_module").change(function() {
+		selector = $("#form_modules input[name=selection_mode]:checked").val();
+		if(selector == 'agents') {
+			$("#id_agents").trigger("change");
+		}
+		else if(selector == 'modules') {
+			$("#module_type").trigger("change");
+		}
+	});
 });
 /* ]]> */
 </script>
diff --git a/pandora_console/godmode/massive/massive_edit_modules.php b/pandora_console/godmode/massive/massive_edit_modules.php
index 45572cdd46..edf7d0b6dd 100755
--- a/pandora_console/godmode/massive/massive_edit_modules.php
+++ b/pandora_console/godmode/massive/massive_edit_modules.php
@@ -288,6 +288,19 @@ $table->data['form_agents_1'][3] = __('Select all modules of this group') . ' '
 		'', 'style="margin-right: 40px;"', true);
 
 
+$table->rowclass['form_modules_3'] = '';
+$table->data['form_modules_3'][0] = __('Module Status');
+$table->colspan['form_modules_3'][1] = 2;
+$status_list = array ();
+$status_list[AGENT_MODULE_STATUS_NORMAL] = __('Normal');
+$status_list[AGENT_MODULE_STATUS_WARNING] = __('Warning');
+$status_list[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical');
+$status_list[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown');
+$status_list[AGENT_MODULE_STATUS_NOT_NORMAL] = __('Not normal');
+$status_list[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init');
+$table->data['form_modules_3'][1] = html_print_select($status_list,
+	'status_module', 'selected', '', __('All'), AGENT_MODULE_STATUS_ALL, true);
+$table->data['form_modules_3'][3] = '';
 
 $table->rowstyle['form_modules_2'] = 'vertical-align: top;';
 $table->rowclass['form_modules_2'] = 'select_modules_row select_modules_row_2';
@@ -304,10 +317,8 @@ $table->data['form_modules_2'][2] .= html_print_select (
 $table->data['form_modules_2'][3] = html_print_select (array(), 'agents[]',
 	$agents_select, false, __('None'), 0, true, true, false);
 
-
-
 $table->rowclass['form_agents_2'] = 'select_agents_row';
-$table->data['form_agents_2'][0] = __('Status');
+$table->data['form_agents_2'][0] = __('Agent Status');
 $table->colspan['form_agents_2'][1] = 2;
 $status_list = array ();
 $status_list[AGENT_STATUS_NORMAL] = __('Normal');
@@ -684,6 +695,10 @@ $(document).ready (function () {
 		if (this.value != '0')
 			params['id_tipo_modulo'] = this.value;
 		
+		var status_module = $('#status_module').val();
+		if (status_module != '-1')
+			params['status_module'] = status_module;
+		
 		$("#module_loading").show ();
 		$("tr#delete_table-edit1, tr#delete_table-edit2").hide ();
 		$("#module_name").attr ("disabled", "disabled")
@@ -701,6 +716,7 @@ $(document).ready (function () {
 			"json"
 		);
 	});
+	
 	function show_form() {
 		$("td#delete_table-0-1, " +
 			"td#delete_table-edit1-1, " +
@@ -869,7 +885,7 @@ $(document).ready (function () {
 	$("#id_agents").change (show_form);
 	
 	$("#form_edit input[name=selection_mode]").change (function () {
-		selector = this.value;
+		selector = $("#form_edit input[name=selection_mode]:checked").val();
 		clean_lists();
 		
 		if(selector == 'agents') {
@@ -966,6 +982,17 @@ $(document).ready (function () {
 	$("#status_agents").change(function() {
 		$("#groups_select").trigger("change");
 	});
+	
+	$("#status_module").change(function() {
+		
+		selector = $("#form_edit input[name=selection_mode]:checked").val();
+		if(selector == 'agents') {
+			$("#id_agents").trigger("change");
+		}
+		else if(selector == 'modules') {
+			$("#module_type").trigger("change");
+		}
+	});
 });
 /* ]]> */
 </script>
diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php
index 6446d433f7..05b86ae0be 100644
--- a/pandora_console/include/functions_agents.php
+++ b/pandora_console/include/functions_agents.php
@@ -1169,6 +1169,9 @@ function agents_get_modules ($id_agent = null, $details = false,
 							break;
 					}
 				}
+				else if (preg_match('/\bin\b/i',$field)) {
+					array_push ($fields, $field.' '.$value);
+				}
 				else {
 					switch ($config["dbtype"]) {
 						case "mysql":
diff --git a/pandora_console/include/javascript/pandora.js b/pandora_console/include/javascript/pandora.js
index b2f92c5722..45ae05834e 100644
--- a/pandora_console/include/javascript/pandora.js
+++ b/pandora_console/include/javascript/pandora.js
@@ -157,6 +157,15 @@ function agent_changed_by_multiple_agents (event, id_agent, selected) {
 		}
 	}
 	
+	var module_status = -1;
+	if (typeof $("#status_module") !== 'undefined') {
+		try {
+			module_status = $("#status_module").val();
+		}
+		catch (error) {
+		}
+	}
+	
 	// Module name
 	var module_name = $("#text-module_filter").val();
 	
@@ -222,7 +231,8 @@ function agent_changed_by_multiple_agents (event, id_agent, selected) {
 			"name": module_name,
 			"selection_mode": selection_mode,
 			"serialized": serialized,
-			"id_server": id_server
+			"id_server": id_server,
+			"status_module": module_status
 		},
 		function (data) {
 			$('#module').empty ();
@@ -382,6 +392,15 @@ function module_changed_by_multiple_modules (event, id_module, selected) {
 	$('#agents').empty ();
 	$('#agents').append ($('<option></option>').html ("Loading...").attr ("value", 0));
 	
+	var status_module = -1;
+	if (typeof $("#status_module") !== 'undefined') {
+		try {
+			status_module = $("#status_module").val();
+		}
+		catch (error) {
+		}
+	}
+	
 	var selection_mode = $('#agents_selection_mode').val();
 	if(selection_mode == undefined) {
 		selection_mode = 'common';
@@ -390,6 +409,7 @@ function module_changed_by_multiple_modules (event, id_module, selected) {
 	jQuery.post('ajax.php', 
 		{"page": "operation/agentes/ver_agente",
 		"get_agents_json_for_multiple_modules": 1,
+		"status_module": status_module,
 		"module_name[]": idModules,
 		"selection_mode": selection_mode
 		},
diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php
index cb3c0531ff..5b4e16788f 100644
--- a/pandora_console/operation/agentes/ver_agente.php
+++ b/pandora_console/operation/agentes/ver_agente.php
@@ -145,6 +145,7 @@ if (is_ajax ()) {
 	if ($get_agents_json_for_multiple_modules) {
 		$nameModules = get_parameter('module_name');
 		$selection_mode = get_parameter('selection_mode','common');
+		$status_modulo = (int) get_parameter ('status_module', -1);
 		
 		$groups = users_get_groups ($config["id_user"], "AW", false);
 		$group_id_list = ($groups ? join(",",array_keys($groups)):"0");
@@ -155,6 +156,10 @@ if (is_ajax ()) {
 				AND t1.id_grupo IN (' . $group_id_list .')
 				AND t2.nombre IN (\'' . implode('\',\'', $nameModules) . '\')';
 		
+		if ($status_modulo != -1) {
+			$sql .= ' AND t2.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where estado = ' . $status_modulo . ') ';
+		}
+		
 		if ($selection_mode == 'common') {
 			$sql .= 'AND (
 					SELECT count(t3.nombre)
@@ -225,6 +230,7 @@ if (is_ajax ()) {
 		$selection_mode = get_parameter('selection_mode', 'common');
 		$serialized = get_parameter('serialized', '');
 		$id_server = (int) get_parameter('id_server', 0);
+		$status_modulo = (int) get_parameter ('status_module', -1);
 		$metaconsole_server_name = null;
 		if (!empty($id_server)) {
 			$metaconsole_server_name = db_get_value('server_name',
@@ -261,6 +267,11 @@ if (is_ajax ()) {
 			}
 		}
 		
+		if ($status_modulo != -1) {
+			$filter .= ' AND t1.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where estado = ' . $status_modulo . ')';
+		}
+		
+		
 		if (is_metaconsole()) {
 			$result = array();
 			$nameModules = array();
@@ -442,6 +453,7 @@ HAVING count(nombre) = (SELECT count(nombre) FROM tagente_modulo))';
 		$delete_pending = (int) get_parameter ('delete_pending', -1);
 		// Use 0 as not received
 		$id_tipo_modulo = (int) get_parameter ('id_tipo_modulo', 0);
+		$status_modulo = (int) get_parameter ('status_module', -1);
 		
 		// Filter
 		$filter = array();
@@ -454,6 +466,11 @@ HAVING count(nombre) = (SELECT count(nombre) FROM tagente_modulo))';
 		if (empty($filter))
 			$filter = false;
 		
+		if ($status_modulo != -1) {
+			$filter['id_agente_modulo IN'] = ' (SELECT id_agente_modulo FROM tagente_estado where estado = '.$status_modulo.') ';
+		}
+		
+		
 		$get_id_and_name = (bool) get_parameter ('get_id_and_name');
 		$get_distinct_name = (bool) get_parameter ('get_distinct_name');