dolibarr  17.0.4
extrafields_list_search_sql.tpl.php
1 <?php
2 
3 // Protection to avoid direct call of template
4 if (empty($conf) || !is_object($conf)) {
5  print "Error, template page can't be called as URL";
6  exit;
7 }
8 
9 if (empty($extrafieldsobjectkey) && is_object($object)) {
10  $extrafieldsobjectkey = $object->table_element;
11 }
12 
13 // Loop to complete the sql search criterias from extrafields
14 if (!empty($extrafieldsobjectkey) && !empty($search_array_options) && is_array($search_array_options)) { // $extrafieldsobject is the $object->table_element like 'societe', 'socpeople', ...
15  if (empty($extrafieldsobjectprefix)) {
16  $extrafieldsobjectprefix = 'ef.';
17  }
18  if (empty($search_options_pattern)) {
19  $search_options_pattern = 'search_options_';
20  }
21 
22  foreach ($search_array_options as $key => $val) {
23  $crit = $val;
24  $tmpkey = preg_replace('/'.$search_options_pattern.'/', '', $key);
25  $typ = $extrafields->attributes[$extrafieldsobjectkey]['type'][$tmpkey];
26 
27  if ($crit != '' && in_array($typ, array('date', 'datetime', 'timestamp'))) {
28  if (is_numeric($crit)) {
29  if ($typ == 'date') {
30  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31  $crit = dol_get_first_hour($crit);
32  }
33  $sql .= " AND ".$extrafieldsobjectprefix.$tmpkey." = '".$db->idate($crit)."'";
34  } elseif (is_array($crit)) {
35  if ($crit['start'] !== '' && $crit['end'] !== '') {
36  $sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." BETWEEN '". $db->idate($crit['start']). "' AND '".$db->idate($crit['end']) . "')";
37  } elseif ($crit['start'] !== '') {
38  $sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." >= '". $db->idate($crit['start'])."')";
39  } elseif ($crit['end'] !== '') {
40  $sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." <= '". $db->idate($crit['end'])."')";
41  }
42  }
43  } elseif (in_array($typ, array('boolean'))) {
44  if ($crit !== '-1' && $crit !== '') {
45  $sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." = '".$db->escape($crit)."'";
46  if ($crit == '0') {
47  $sql .= " OR ".$extrafieldsobjectprefix.$tmpkey." IS NULL";
48  }
49  $sql .= ")";
50  }
51  } elseif ($crit != '' && (!in_array($typ, array('select', 'sellist', 'select')) || $crit != '0') && (!in_array($typ, array('link')) || $crit != '-1')) {
52  $mode_search = 0;
53  if (in_array($typ, array('int', 'double', 'real', 'price'))) {
54  $mode_search = 1; // Search on a numeric
55  }
56  if (in_array($typ, array('sellist', 'link')) && $crit != '0' && $crit != '-1') {
57  $mode_search = 2; // Search on a foreign key int
58  }
59  if (in_array($typ, array('sellist')) && !is_numeric($crit)) {
60  $mode_search = 0;// Search on a foreign key string
61  }
62  if (in_array($typ, array('chkbxlst', 'checkbox', 'select'))) {
63  $mode_search = 4; // Search on a multiselect field with sql type = text
64  }
65  if (is_array($crit)) {
66  $crit = implode(' ', $crit); // natural_search() expects a string
67  } elseif ($typ === 'select' and is_string($crit) and strpos($crit, ',') === false) {
68  $critSelect = "'".implode("','", array_map(array($db, 'escape'), explode(',', $crit)))."'";
69  $sql .= " AND (".$extrafieldsobjectprefix.$tmpkey." IN (".$db->sanitize($critSelect, 1).") )";
70  continue;
71  }
72  $sql .= natural_search($extrafieldsobjectprefix.$tmpkey, $crit, $mode_search);
73  }
74  }
75 }
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition: date.lib.php:635
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...