From 2cf10ac05d0c5384371df3c40a79fe4b4764d5f6 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 24 Aug 2015 16:32:43 +0200
Subject: [PATCH] Fix filtering

---
 htdocs/comm/prospect/list.php           | 22 +++++++++++++++++++---
 htdocs/core/class/extrafields.class.php | 19 ++++++++++---------
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/htdocs/comm/prospect/list.php b/htdocs/comm/prospect/list.php
index 0422d65cc9a..b15b3aae9db 100644
--- a/htdocs/comm/prospect/list.php
+++ b/htdocs/comm/prospect/list.php
@@ -160,6 +160,7 @@ $extrafields = new ExtraFields($db);
 
 // fetch optionals attributes and labels
 $extralabels = $extrafields->fetch_name_optionals_label('thirdparty');
+$search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
 
 // Do we click on purge search criteria ?
 if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both test are required to be compatible with all browsers
@@ -174,6 +175,7 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter")) // Both
 	$search_datec="";
 	$search_categ="";
 	$search_status="";
+	$search_array_options=array();
 }
 
 if ($search_status=='') $search_status=1; // always display active customer first
@@ -256,6 +258,18 @@ if ($socname)
 	$sortfield = "s.nom";
 	$sortorder = "ASC";
 }
+// Extra fields
+if (is_array($extrafields->attribute_list) && count($extrafields->attribute_list)) 
+{
+	foreach($extrafields->attribute_list as $key => $val) 
+	{
+	    $crit=GETPOST('search_options_'.$key);
+	    if ($val && $crit != '')
+	    {
+            $sql .= natural_search('ef.'.$key, $crit);
+	    }
+	}
+}
 // Add where from hooks
 $parameters=array();
 $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters);    // Note that $action and $object may have been modified by hook
@@ -269,6 +283,7 @@ if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
 }
 $sql.= " ORDER BY $sortfield $sortorder, s.nom ASC";
 $sql.= $db->plimit($conf->liste_limit+1, $offset);
+//print $sql;
 
 dol_syslog('comm/prospect/list.php', LOG_DEBUG);
 $resql = $db->query($sql);
@@ -434,9 +449,10 @@ if ($resql)
 	   {
 	       if ($val)
 	       {
-                print '<td class="liste_titre">';
-                //print $extrafields->showInputField($key, $array_options[$key], '', '', 'search_', 4);
-                print '</td>';
+	           $crit=$search_array_options['search_options_'.$key];
+	           print '<td class="liste_titre">';
+               print $extrafields->showInputField($key, $crit, '', '', 'search_', 4);
+               print '</td>';
 	       }
 	   }
 	}
diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php
index 5644b3c0137..20a812335a1 100644
--- a/htdocs/core/class/extrafields.class.php
+++ b/htdocs/core/class/extrafields.class.php
@@ -1425,11 +1425,12 @@ class ExtraFields
 	/**
 	 * return array_options array for object by extrafields value (using for data send by forms)
 	 *
-	 * @param   array	$extralabels    $array of extrafields
-	 * @param	string	$keyprefix		Prefix string to add into name and id of field (can be used to avoid duplicate names)
-	 * @return	int						1 if array_options set / 0 if no value
+	 * @param  array   $extralabels    $array of extrafields
+	 * @param  string  $keyprefix      Prefix string to add into name and id of field (can be used to avoid duplicate names)
+	 * @param  string  $keysuffix      Suffix string to add into name and id of field (can be used to avoid duplicate names)
+	 * @return int                     1 if array_options set / 0 if no value
 	 */
-	function getOptionalsFromPost($extralabels,$keyprefix='')
+	function getOptionalsFromPost($extralabels,$keyprefix='',$keysuffix='')
 	{
 		global $_POST;
 
@@ -1444,24 +1445,24 @@ class ExtraFields
 				if (in_array($key_type,array('date','datetime')))
 				{
 					// Clean parameters
-					$value_key=dol_mktime($_POST["options_".$key.$keyprefix."hour"], $_POST["options_".$key.$keyprefix."min"], 0, $_POST["options_".$key.$keyprefix."month"], $_POST["options_".$key.$keyprefix."day"], $_POST["options_".$key.$keyprefix."year"]);
+					$value_key=dol_mktime($_POST[$keysuffix."options_".$key.$keyprefix."hour"], $_POST[$keysuffix."options_".$key.$keyprefix."min"], 0, $_POST[$keysuffix."options_".$key.$keyprefix."month"], $_POST[$keysuffix."options_".$key.$keyprefix."day"], $_POST[$keysuffix."options_".$key.$keyprefix."year"]);
 				}
 				else if (in_array($key_type,array('checkbox')))
 				{
-					$value_arr=GETPOST("options_".$key.$keyprefix);
+					$value_arr=GETPOST($keysuffix."options_".$key.$keyprefix);
 					$value_key=implode($value_arr,',');
 				}
 				else if (in_array($key_type,array('price','double')))
 				{
-					$value_arr=GETPOST("options_".$key.$keyprefix);
+					$value_arr=GETPOST($keysuffix."options_".$key.$keyprefix);
 					$value_key=price2num($value_arr);
 				}
 				else
 				{
-					$value_key=GETPOST("options_".$key.$keyprefix);
+					$value_key=GETPOST($keysuffix."options_".$key.$keyprefix);
 				}
 
-				$array_options["options_".$key]=$value_key;	// No keyprefix here. keyprefix is used only for read.
+				$array_options[$keysuffix."options_".$key]=$value_key;	// No keyprefix here. keyprefix is used only for read.
 			}
 
 			return $array_options;
-- 
GitLab