From 05c4c5efcae20202932f9085ff867329aaa51770 Mon Sep 17 00:00:00 2001
From: vgilc <vanessa.gil@artica.es>
Date: Mon, 23 Jan 2012 16:28:22 +0000
Subject: [PATCH] 2012-01-23  Vanessa Gil  <vanessa.gil@artica.es>

	* operation/agentes/estado_agente.php
	  include/functions_agents.php: Simplify code estado_agente
	and fix bug in search filter.


git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5404 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |   6 +
 pandora_console/include/functions_agents.php  | 116 ++++++++++++---
 .../operation/agentes/estado_agente.php       | 134 ++++--------------
 3 files changed, 137 insertions(+), 119 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index bbaa9c0ad9..13cb304285 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,9 @@
+2012-01-23  Vanessa Gil  <vanessa.gil@artica.es>
+
+	* operation/agentes/estado_agente.php
+	  include/functions_agents.php: Simplify code estado_agente
+	and fix bug in search filter.
+
 2012-01-23  Ramon Novoa  <rnovoa@artica.es>
 
 	* pandoradb_data.sql, pandoradb.sql,
diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php
index 4a85e632b9..c430418f87 100644
--- a/pandora_console/include/functions_agents.php
+++ b/pandora_console/include/functions_agents.php
@@ -315,7 +315,11 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 	if (! is_array ($filter)) {
 		$filter = array ();
 	}
-	
+
+	if(isset($filter['search'])) {
+	$search = $filter['search'];
+	unset($filter['search']);
+	}
 	
 	if(isset($filter['offset'])) {
 		$offset = $filter['offset'];
@@ -327,6 +331,80 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 		unset($filter['limit']);
 	}
 	
+	$status_sql = ' 1 = 1';
+	if(isset($filter['status'])) {
+		$normal_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo 
+				WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente 
+				AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo 
+				AND tagente_modulo.disabled = 0 AND estado = 0 
+				AND (utimestamp != 0 OR tagente_modulo.id_tipo_modulo IN (21,22,23)) 
+				AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2)) 
+				OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
+				
+		$warning_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo 
+				WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente 
+				AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo 
+				AND tagente_modulo.disabled = 0 AND estado = 2 
+				AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2)) 
+				OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
+				
+		$critical_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo 
+				WHERE tagente.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente 
+				AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo 
+				AND tagente_modulo.disabled = 0 AND estado = 1 
+				AND (utimestamp >= ( UNIX_TIMESTAMP() - (current_interval * 2)) 
+				OR tagente_modulo.id_tipo_modulo IN (21,22,23,100))';
+				
+		$unknown_modules = 'SELECT tagente.id_agente FROM tagente_estado, tagente, tagente_modulo 
+				WHERE tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente 
+				AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo 
+				AND tagente_modulo.disabled = 0 AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23,100) 
+				AND utimestamp < ( UNIX_TIMESTAMP() - (current_interval * 2)) AND utimestamp != 0';
+				
+		$notinit_modules = 'SELECT tagente_estado.id_agente FROM tagente_estado, tagente, tagente_modulo 
+				WHERE tagente.disabled = 0 AND tagente.id_agente = tagente_estado.id_agente 
+				AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo 
+				AND tagente_modulo.disabled = 0 
+				AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23) 
+				AND utimestamp = 0';
+				
+		switch ($filter['status']) {
+			// Normal
+			case 0: 
+				$status_sql = "id_agente IN ($normal_modules) && id_agente NOT IN ($warning_modules) &&
+				 id_agente NOT IN ($critical_modules) && id_agente NOT IN ($unknown_modules)"; //&& id_agente NOT IN ($notinit_modules)";
+				break;
+			// Warning
+			case 1:
+				$status_sql = "id_agente IN ($warning_modules) &&
+				 id_agente NOT IN ($critical_modules)"; //&& id_agente NOT IN ($notinit_modules)";
+				break;
+			// Critical
+			case 2: 
+				$status_sql = "id_agente IN ($critical_modules)";
+				break;
+			// Unknown
+			case 3:	
+				$status_sql = "id_agente IN ($unknown_modules) &&
+				 id_agente NOT IN ($critical_modules) && id_agente NOT IN ($warning_modules)";
+				break;
+			// Not normal
+			case 4:
+				//$status_sql = "id_agente NOT IN ($normal_modules)";
+				$status_sql = "id_agente NOT IN ($normal_modules) || id_agente IN ($warning_modules) ||
+				 id_agente IN ($critical_modules) || id_agente IN ($unknown_modules)";
+				break;
+			// Not init
+			case 5:	
+				$status_sql = "id_agente NOT IN ($warning_modules) &&
+				 id_agente NOT IN ($critical_modules) && id_agente NOT IN ($unknown_modules)";
+				break;
+
+		}
+		unset($filter['status']);
+	} 
+
+	
 	unset($filter['order']);
 
 	$filter_nogroup = $filter;
@@ -336,9 +414,11 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 
 	//If no group specified, get all user groups
 	if (empty ($filter['id_grupo'])) {
+		$all_groups = true;
 		$filter['id_grupo'] = $groups;
 	}
 	elseif (! is_array ($filter['id_grupo'])) {
+		$all_groups = false;
 		//If group is specified but not allowed, return false
 		if (! in_array ($filter['id_grupo'], $groups)) {
 			return false;
@@ -346,6 +426,7 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 		$filter['id_grupo'] = (array) $filter['id_grupo']; //Make an array
 	}
 	else {
+		$all_groups = true;
 		//Check each group specified to the user groups, remove unwanted groups
 		foreach ($filter['id_grupo'] as $key => $id_group) {
 			if (! in_array ($id_group, $groups)) {
@@ -379,26 +460,30 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 
 	$where_nogroup = db_format_array_where_clause_sql ($filter_nogroup, 'AND', '');
 	
-	$sql_extra = enterprise_hook('policies_get_agents_sql_condition');
-	
+	if ($where_nogroup == '') {
+		$where_nogroup = '1 = 1';
+	}
+
 	$extra = false;
-	if($sql_extra != ENTERPRISE_NOT_HOOK) {
-		if (!empty($sql_extra)) {
-			$extra = true;
+
+	$sql_extra = '';
+	if ($all_groups){
+		$where_nogroup = '1 = 1';
+		
+		$sql_extra = enterprise_hook('policies_get_agents_sql_condition');
+		
+		if($sql_extra != ENTERPRISE_NOT_HOOK) {
+			if (!empty($sql_extra)) {
+				$extra = true;
+			}
 		}
 	}
 
 	if($extra) { 
-		if (empty($where_nogroup))
-			$where = sprintf('%s AND (%s)', $where, $sql_extra);	
-		else
-			$where = sprintf('%s AND (%s OR %s)', $where, $where_nogroup, $sql_extra);	
+		$where = sprintf('(%s OR (%s)) AND (%s) AND (%s) %s', $sql_extra, $where, $where_nogroup, $status_sql, $search);	
+	} else {			
+		$where = sprintf('%s AND %s AND (%s) %s', $where, $where_nogroup, $status_sql, $search);
 	}
-	else {
-		if (!empty($where_nogroup))				
-			$where = sprintf('%s AND %s', $where, $where_nogroup);
-	}
-
 	$sql = sprintf('SELECT %s FROM tagente WHERE %s %s', implode(',',$fields), $where, $order);
 
 	switch ($config["dbtype"]) {
@@ -424,7 +509,6 @@ function agents_get_agents ($filter = false, $fields = false, $access = 'AR', $o
 	}
 
 	return $agents;
-	return db_get_all_rows_sql($sql);
 }
 
 /**
diff --git a/pandora_console/operation/agentes/estado_agente.php b/pandora_console/operation/agentes/estado_agente.php
index 2a106d4af4..560fa16d3f 100644
--- a/pandora_console/operation/agentes/estado_agente.php
+++ b/pandora_console/operation/agentes/estado_agente.php
@@ -281,128 +281,56 @@ switch ($sortField) {
 
 $search_sql = '';
 if ($search != ""){
-	$search_sql = " AND ( nombre COLLATE utf8_general_ci LIKE '%$search%' OR direccion LIKE '%$search%') ";
+	$search_sql = " AND ( nombre COLLATE utf8_general_ci LIKE '%$search%' OR direccion LIKE '%$search%' OR comentarios LIKE '%$search%') ";
 }
 
 // Show only selected groups	
 if ($group_id > 0) {
-	$groups = $group_id;
-	$agent_names = agents_get_group_agents ($group_id, $filter, "upper", false, $recursion);
+	$groups = array($group_id);
 	if ($recursion) {
 		$groups = groups_get_id_recursive($group_id, true);
 	}
 }
 else {
 	$groups = array();
-	
-	$user_group = users_get_groups($config["id_user"], "PM");
-	$groups = array_keys($user_group);
-	$user_group = users_get_groups($config["id_user"], "AR");
-	$groups = array_unique(array_merge($groups, array_keys($user_group)));
-	$agent_names = agents_get_group_agents($groups, $filter, "upper");
+	$user_groups = users_get_groups($config["id_user"], "AR");
+	$groups = array_keys($user_groups);
 }
 
 $total_agents = 0;
 $agents = false;
 
-if (! empty ($agent_names)) {
-		$total_agents = agents_get_agents(array ('id_agente' => array_keys ($agent_names),
-			'order' => 'nombre ASC',
-			'disabled' => 0,
-			'id_grupo' => $groups),
-			array ('COUNT(*) as total'));
-		$total_agents = isset ($total_agents[0]['total']) ? $total_agents[0]['total'] : 0;
-		
-		$agents = agents_get_agents(array ('id_agente' => array_keys ($agent_names),
-			'order' => 'nombre ASC',
-			'id_grupo' => $groups,
-		/*	'offset' => (int) get_parameter ('offset'),
-			'limit' => (int) $config['block_size'] */ ),
-			array ('id_agente',
-				'id_grupo',
-				'id_os',
-				'ultimo_contacto',
-				'intervalo',
-				'comentarios description'),
-			'AR',
-			$order);
-}
+$total_agents = agents_get_agents(array (
+	'order' => 'nombre ASC',
+	'disabled' => 0,
+	'id_grupo' => $groups,
+	'search' => $search_sql,
+	'status' => $status),
+	array ('COUNT(*) as total'));
+$total_agents = isset ($total_agents[0]['total']) ? $total_agents[0]['total'] : 0;
+
+$agents = agents_get_agents(array (
+	'order' => 'nombre ASC',
+	'id_grupo' => $groups,
+	'disabled' => 0,
+	'status' => $status,
+	'search' => $search_sql,
+	'offset' => (int) get_parameter ('offset'),
+	'limit' => (int) $config['block_size']  ),
+
+	array ('id_agente',
+		'id_grupo',
+		'id_os',
+		'ultimo_contacto',
+		'intervalo',
+		'comentarios description'),
+	'AR',
+	$order);
 
 if (empty ($agents)) {
 	$agents = array ();
 }
 
-$result_agents = array();
-// This will be use above to simulate pagination
-$i = 0;
-$limit_agents = $offset + $config['block_size'];
-// Filter by agents status
-if ($status != -1){
-	$result_agents_tmp = array();
-	foreach ($agents as $agent_filter){
-		$filter_by_status = false;
-		$agent_stat = reporting_get_agent_module_info ($agent_filter["id_agente"]);	
-		switch ($status){
-				// Normal
-				case 0: 
-						if ($agent_stat['status'] != STATUS_AGENT_OK)
-							$filter_by_status = true;
-						break;
-				// Warning
-				case 1:
-						if ($agent_stat['status'] != STATUS_AGENT_WARNING)
-							$filter_by_status = true;
-						break;
-				// Critical
-				case 2: 
-						if ($agent_stat['status'] != STATUS_AGENT_CRITICAL)
-							$filter_by_status = true;
-						break;
-				// Unknown
-				case 3:
-						if ($agent_stat['status'] != STATUS_AGENT_DOWN)
-							$filter_by_status = true;			
-						break;
-				// Not normal
-				case 4:
-						if ($agent_stat['status'] == STATUS_AGENT_OK)
-							$filter_by_status = true;				
-						break;
-				// Not init
-				case 5:	
-						if ($agent_stat['status'] != STATUS_AGENT_NO_DATA)
-							$filter_by_status = true;
-						break;
-		}
-		
-		// If status is different from filter, don't show agent
-		if (! $filter_by_status)
-			$result_agents_tmp[] = $agent_filter;
-	}
-
-	// Recalculate total agents
-	if (! empty ($result_agents_tmp))
-		$total_agents = count($result_agents_tmp);	
-	else
-		$total_agents = 0;
-	
-	// Simulate pagination
-	foreach ($result_agents_tmp as $agent_filter_off){
-		if ($i >= $offset and $i < $limit_agents)
-			$result_agents[] = $agent_filter_off;
-		$i++;
-	}	
-	
-}
-else {
-	// Simulate pagination
-	foreach ($agents as $agent_filter){
-		if ($i >= $offset and $i < $limit_agents)
-			$result_agents[] = $agent_filter;
-		$i++;
-	}
-}
-
 // Prepare pagination
 ui_pagination ($total_agents, ui_get_url_refresh (array ('group_id' => $group_id, 'recursion' => $recursion, 'search' => $search, 'sort_field' => $sortField, 'sort' => $sort, 'status' => $status)));
 
@@ -457,7 +385,7 @@ $table->data = array ();
 
 $rowPair = true;
 $iterator = 0;
-foreach ($result_agents as $agent) {
+foreach ($agents as $agent) {
 	if ($rowPair)
 		$table->rowclass[$iterator] = 'rowPair';
 	else