From 5f830a48ada905c7e957dbfc5619a81e8cc3f5b9 Mon Sep 17 00:00:00 2001
From: Daniel Maya <daniel.maya@artica.es>
Date: Mon, 5 Sep 2016 13:50:22 +0200
Subject: [PATCH] Added search by multiple ips agents. Ticket #3949.

(cherry picked from commit d39123eb0aea56fcbade1d46d1686a290eccbd8c)
---
 .../operation/agentes/estado_agente.php       | 23 +++++++-
 .../operation/search_agents.getdata.php       | 54 ++++++++++++++++---
 2 files changed, 70 insertions(+), 7 deletions(-)

diff --git a/pandora_console/operation/agentes/estado_agente.php b/pandora_console/operation/agentes/estado_agente.php
index 01388757d4..8ea3e10ee0 100644
--- a/pandora_console/operation/agentes/estado_agente.php
+++ b/pandora_console/operation/agentes/estado_agente.php
@@ -338,7 +338,28 @@ switch ($sortField) {
 
 $search_sql = '';
 if ($search != "") {
-	$search_sql = " AND ( nombre " . $order_collation . " LIKE '%$search%' OR direccion LIKE '%$search%' OR comentarios LIKE '%$search%') ";
+	//$search_sql = " AND ( nombre " . $order_collation . " LIKE '%$search%' OR direccion LIKE '%$search%' OR comentarios LIKE '%$search%') ";
+	$sql = "SELECT DISTINCT taddress_agent.id_agent FROM taddress
+	INNER JOIN taddress_agent ON
+	taddress.id_a = taddress_agent.id_a
+	WHERE taddress.ip LIKE '%$search%'";
+
+	$id = db_get_all_rows_sql($sql);
+	if($id != ''){
+		$aux = $id[0]['id_agent'];
+		$search_sql = " AND ( nombre " . $order_collation . "
+			LIKE '%$search%' OR tagente.id_agente = $aux";
+		if(count($id)>=2){
+			for ($i = 1; $i < count($id); $i++){
+				$aux = $id[$i]['id_agent'];
+				$search_sql .= " OR tagente.id_agente = $aux";
+			}
+		}
+		$search_sql .= ")";
+	}else{
+		$search_sql = " AND ( nombre " . $order_collation . "
+			LIKE '%$search%') ";
+	}
 }
 
 // Show only selected groups
diff --git a/pandora_console/operation/search_agents.getdata.php b/pandora_console/operation/search_agents.getdata.php
index fdbad2e098..3d98ea73db 100644
--- a/pandora_console/operation/search_agents.getdata.php
+++ b/pandora_console/operation/search_agents.getdata.php
@@ -117,6 +117,29 @@ if ($searchAgents) {
 	
 	switch ($config["dbtype"]) {
 		case "mysql":
+		$sql = "SELECT DISTINCT taddress_agent.id_agent FROM taddress
+		INNER JOIN taddress_agent ON
+		taddress.id_a = taddress_agent.id_a
+		WHERE taddress.ip LIKE '%$stringSearchSQL%'";
+
+		$id = db_get_all_rows_sql($sql);
+		if($id != ''){
+			$aux = $id[0]['id_agent'];
+			$search_sql = " t1.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
+							t2.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
+							t1.id_agente = $aux";
+
+			if(count($id)>=2){
+				for ($i = 1; $i < count($id); $i++){
+					$aux = $id[$i]['id_agent'];
+					$search_sql .= " OR t1.id_agente = $aux";
+				}
+			}
+		}else{
+			$search_sql = " t1.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
+							t2.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
+							t1.direccion COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%'";
+		}
 			$sql = "
 				FROM tagente t1
 					INNER JOIN tgrupo t2
@@ -140,14 +163,35 @@ if ($searchAgents) {
 							)
 					)
 					AND (
-						t1.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
-						t2.nombre COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%' OR
-						t1.direccion COLLATE utf8_general_ci LIKE '%%" . $stringSearchSQL . "%%'
+						".$search_sql."
 					)
 			";
 			break;
 		case "postgresql":
 		case "oracle":
+			$sql = "SELECT DISTINCT taddress_agent.id_agent FROM taddress
+			INNER JOIN taddress_agent ON
+			taddress.id_a = taddress_agent.id_a
+			WHERE taddress.ip LIKE '%$stringSearchSQL%'";
+
+			$id = db_get_all_rows_sql($sql);
+			if($id != ''){
+				$aux = $id[0]['id_agent'];
+				$search_sql = " t1.nombre COLLATE utf8_general_ci LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
+								t2.nombre COLLATE utf8_general_ci LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
+								t1.id_agente = $aux";
+
+					if(count($id)>=2){
+						for ($i = 1; $i < count($id); $i++){
+							$aux = $id[$i]['id_agent'];
+							$search_sql .= " OR t1.id_agente = $aux";
+						}
+					}
+			}else{
+				$search_sql = " lower(t1.nombre) LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
+								lower(t2.nombre) LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
+								lower(t1.direccion) LIKE '%%" . strtolower($stringSearchSQL) . "%%'";
+			}
 			$sql = "
 				FROM tagente t1
 					INNER JOIN tgrupo t2
@@ -171,9 +215,7 @@ if ($searchAgents) {
 							)
 					)
 					AND (
-						lower(t1.nombre) LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
-						lower(t2.nombre) LIKE '%%" . strtolower($stringSearchSQL) . "%%' OR
-						lower(t1.direccion) LIKE '%%" . strtolower($stringSearchSQL) . "%%'
+						".$search_sql."
 					)
 			";
 			break;