1<?php
2
3// Protection to avoid direct call of template
4if (empty($conf) || !is_object($conf))
5{
6	print "Error, template page can't be called as URL";
7	exit;
8}
9
10if (empty($extrafieldsobjectkey) && is_object($object)) $extrafieldsobjectkey = $object->table_element;
11
12// Loop to complete the sql search criterias from extrafields
13if (!empty($extrafieldsobjectkey) && !empty($search_array_options) && is_array($search_array_options))	// $extrafieldsobject is the $object->table_element like 'societe', 'socpeople', ...
14{
15	if (empty($extrafieldsobjectprefix)) $extrafieldsobjectprefix = 'ef.';
16	if (empty($search_options_pattern)) $search_options_pattern = 'search_options_';
17
18	foreach ($search_array_options as $key => $val)
19	{
20		$crit = $val;
21		$tmpkey = preg_replace('/'.$search_options_pattern.'/', '', $key);
22		$typ = $extrafields->attributes[$extrafieldsobjectkey]['type'][$tmpkey];
23
24		if ($crit != '' && in_array($typ, array('date', 'datetime', 'timestamp')))
25		{
26			if ($typ == 'date'){
27				include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
28				$crit = dol_get_first_hour($crit);
29				$sql .= " AND ".$extrafieldsobjectprefix.$tmpkey." = '".$db->idate($crit)."'";
30			}else {
31				$sql .= " AND ".$extrafieldsobjectprefix.$tmpkey." = '".$db->idate($crit)."'";
32			}
33		} elseif (in_array($typ, array('boolean')))
34		{
35			if ($crit !== '-1' && $crit !== '') {
36				$sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." = '".$db->escape($crit)."'";
37				if ($crit == '0') $sql .= " OR ".$extrafieldsobjectprefix.$tmpkey." IS NULL";
38				$sql .= ")";
39			}
40		} elseif ($crit != '' && (!in_array($typ, array('select', 'sellist')) || $crit != '0') && (!in_array($typ, array('link')) || $crit != '-1'))
41		{
42			$mode_search = 0;
43			if (in_array($typ, array('int', 'double', 'real', 'price'))) $mode_search = 1; // Search on a numeric
44			if (in_array($typ, array('sellist', 'link')) && $crit != '0' && $crit != '-1') $mode_search = 2; // Search on a foreign key int
45			if (in_array($typ, array('sellist')) && !is_numeric($crit)) $mode_search = 0;// Search on a foreign key string
46			if (in_array($typ, array('chkbxlst', 'checkbox'))) $mode_search = 4; // Search on a multiselect field with sql type = text
47			if (is_array($crit)) $crit = implode(' ', $crit); // natural_search() expects a string
48			elseif ($typ === 'select' and is_string($crit) and strpos($crit, ' ') === false) {
49				$sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." = '".$db->escape($crit)."')";
50				continue;
51			}
52			$sql .= natural_search($extrafieldsobjectprefix.$tmpkey, $crit, $mode_search);
53		}
54	}
55}
56