From 7faedf0c69c27916dce308290771367e485d3bac Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 15 Nov 2014 11:59:02 +0100
Subject: [PATCH] Uniformize code for search

---
 htdocs/product/composition/card.php | 20 ++++++++++++--------
 htdocs/product/list.php             | 14 +++++---------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/htdocs/product/composition/card.php b/htdocs/product/composition/card.php
index 8c71f29581b..ec616b2f222 100644
--- a/htdocs/product/composition/card.php
+++ b/htdocs/product/composition/card.php
@@ -130,24 +130,28 @@ if ($action == 'search')
 {
 	$current_lang = $langs->getDefaultLang();
 
-	$sql = 'SELECT DISTINCT p.rowid, p.ref, p.label, p.price, p.fk_product_type as type';
+    $sql = 'SELECT DISTINCT p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type,';
+    $sql.= ' p.fk_product_type, p.tms as datem';
 	if (! empty($conf->global->MAIN_MULTILANGS)) $sql.= ', pl.label as labelm, pl.description as descriptionm';
 	$sql.= ' FROM '.MAIN_DB_PREFIX.'product as p';
 	$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_product as cp ON p.rowid = cp.fk_product';
 	if (! empty($conf->global->MAIN_MULTILANGS)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND lang='".($current_lang)."'";
-	$sql.= ' WHERE p.entity IN ('.getEntity("product", 1).')';
+	$sql.= ' WHERE p.entity IN ('.getEntity('product', 1).')';
 	if ($key != "")
 	{
+		// For natural search
+		$params = array('p.ref', 'p.label', 'p.description', 'p.note');
+		// multilang
 		if (! empty($conf->global->MAIN_MULTILANGS))
 		{
-			$sql.= " AND (p.ref LIKE '%".$key."%'";
-			$sql.= " OR pl.label LIKE '%".$key."%')";
+			$params[] = 'pl.label';
+			$params[] = 'pl.description';
+			$params[] = 'pl.note';
 		}
-		else
-		{
-			$sql.= " AND (p.ref LIKE '%".$key."%'";
-			$sql.= " OR p.label LIKE '%".$key."%')";
+		if (! empty($conf->barcode->enabled)) {
+			$params[] = 'p.barcode';
 		}
+		$sql .= natural_search($params, $key);
 	}
 	if (! empty($conf->categorie->enabled) && ! empty($parent) && $parent != -1)
 	{
diff --git a/htdocs/product/list.php b/htdocs/product/list.php
index ebf8b77f722..c199344647e 100644
--- a/htdocs/product/list.php
+++ b/htdocs/product/list.php
@@ -130,25 +130,22 @@ else
 
     $sql = 'SELECT DISTINCT p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type,';
     $sql.= ' p.fk_product_type, p.tms as datem,';
-    $sql.= ' p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte,';
+    $sql.= ' p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock,';
     $sql.= ' MIN(pfp.unitprice) as minsellprice';
-    $sql .= ', p.desiredstock';
     $sql.= ' FROM '.MAIN_DB_PREFIX.'product as p';
     if (! empty($search_categ) || ! empty($catid)) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_product as cp ON p.rowid = cp.fk_product"; // We'll need this table joined to the select in order to filter by categ
    	$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
 	// multilang
-	if ($conf->global->MAIN_MULTILANGS) // si l'option est active
-	{
-		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND pl.lang = '".$langs->getDefaultLang() ."'";
-	}
+	if (! empty($conf->global->MAIN_MULTILANGS)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND pl.lang = '".$langs->getDefaultLang() ."'";
 	$sql.= ' WHERE p.entity IN ('.getEntity('product', 1).')';
 	if ($sall)
 	{
 		// For natural search
 		$params = array('p.ref', 'p.label', 'p.description', 'p.note');
 		// multilang
-		if ($conf->global->MAIN_MULTILANGS) // si l'option est active
+		if (! empty($conf->global->MAIN_MULTILANGS))
 		{
+			$params[] = 'pl.label';
 			$params[] = 'pl.description';
 			$params[] = 'pl.note';
 		}
@@ -169,7 +166,7 @@ else
 	{
 		$params = array('p.label');
 		// multilang
-		if ($conf->global->MAIN_MULTILANGS) // si l'option est active
+		if (! empty($conf->global->MAIN_MULTILANGS))
 		{
 			$params[] = 'pl.label';
 		}
@@ -199,7 +196,6 @@ else
     $sql.= $db->order($sortfield,$sortorder);
     $sql.= $db->plimit($limit + 1, $offset);
 
-    dol_syslog("product:list.php:", LOG_DEBUG);
     $resql = $db->query($sql);
     if ($resql)
     {
-- 
GitLab