Skip to content

Commit

Permalink
Fix extrafields
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 7, 2018
1 parent c523111 commit 9a0b5fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
33 changes: 22 additions & 11 deletions htdocs/core/class/extrafields.class.php
Expand Up @@ -1941,24 +1941,36 @@ function setOptionalsFromPost($extralabels, &$object, $onlykey='')
/**
* return array_options array of data of extrafields value of object sent by a search form
*
* @param array $extralabels $array of extrafields (@deprecated)
* @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 array|int array_options set or 0 if no value
* @param array|string $extrafieldsobjectkey array of extrafields (old usage) or value of object->table_element (new usage)
* @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 array|int array_options set or 0 if no value
*/
function getOptionalsFromPost($extralabels,$keyprefix='',$keysuffix='')
function getOptionalsFromPost($extrafieldsobjectkey, $keyprefix='', $keysuffix='')
{
global $_POST;

if (is_array($this->attributes[$object->table_element]['label'])) $extralabels=$this->attributes[$object->table_element]['label'];
if (is_string($extrafieldsobjectkey) && is_array($this->attributes[$extrafieldsobjectkey]['label']))
{
$extralabels = $this->attributes[$extrafieldsobjectkey]['label'];
}
else
{
$extralabels = $extrafieldsobjectkey;
}

$array_options = array();
if (is_array($extralabels))
{
$array_options = array();

// Get extra fields
foreach ($extralabels as $key => $value)
{
$key_type = $this->attributes[$object->table_element]['type'][$key];
$key_type = '';
if (is_string($extrafieldsobjectkey))
{
$key_type = $this->attributes[$extrafieldsobjectkey]['type'][$key];
}

if (in_array($key_type,array('date','datetime')))
{
Expand Down Expand Up @@ -1987,8 +1999,7 @@ function getOptionalsFromPost($extralabels,$keyprefix='',$keysuffix='')

return $array_options;
}
else {
return 0;
}

return 0;
}
}
7 changes: 3 additions & 4 deletions htdocs/core/lib/functions.lib.php
Expand Up @@ -7288,7 +7288,8 @@ function dol_getmypid()
* If param $mode is 1, can contains an operator <, > or = like "<10" or ">=100.5 < 1000"
* If param $mode is 2, can contains a list of int id separated by comma like "1,3,4"
* If param $mode is 3, can contains a list of string separated by comma like "a,b,c"
* @param integer $mode 0=value is list of keyword strings, 1=value is a numeric test (Example ">5.5 <10"), 2=value is a list of id separated with comma (Example '1,3,4')
* @param integer $mode 0=value is list of keyword strings, 1=value is a numeric test (Example ">5.5 <10"), 2=value is a list of ID separated with comma (Example '1,3,4')
* 3=value is list of string separated with comma (Example 'text 1,text 2'), 4=value is a list of ID separated with comma (Example '1,3,4') for search into a multiselect string ('1,2')
* @param integer $nofirstand 1=Do not output the first 'AND'
* @return string $res The statement to append to the SQL query
*/
Expand Down Expand Up @@ -7372,11 +7373,9 @@ function natural_search($fields, $value, $mode=0, $nofirstand=0)
else if ($mode == 4)
{
$tmparray=explode(',',trim($crit));

if (count($tmparray))
{
$listofcodes='';

foreach($tmparray as $val)
{
if ($val)
Expand All @@ -7385,7 +7384,7 @@ function natural_search($fields, $value, $mode=0, $nofirstand=0)
$newres .= ' OR '. $field . ' = \'' . $db->escape(trim($val)) . '\'';
$newres .= ' OR '. $field . ' LIKE \'%,' . $db->escape(trim($val)) . '\'';
$newres .= ' OR '. $field . ' LIKE \'%,' . $db->escape(trim($val)) . ',%\'';
$newres .= ')';
$newres .= ')';
$i2++;
}
}
Expand Down
15 changes: 10 additions & 5 deletions htdocs/core/tpl/extrafields_list_search_sql.tpl.php
Expand Up @@ -18,12 +18,17 @@
$tmpkey=preg_replace('/search_options_/','',$key);
$typ=$extrafields->attributes[$extrafieldsobjectkey]['type'][$tmpkey];

$mode_search=0;
if (in_array($typ, array('int','double','real'))) $mode_search=1; // Search on a numeric
if (in_array($typ, array('sellist','link')) && $crit != '0' && $crit != '-1') $mode_search=2; // Search on a foreign key int
if (in_array($typ, array('chkbxlst','checkbox'))) $mode_search=4; // Search on a multiselect field with sql type = text
if ($crit != '' && (! in_array($typ, array('select','sellist')) || $crit != '0') && (! in_array($typ, array('link')) || $crit != '-1'))
if ($crit != '' && in_array($typ, array('date', 'datetime', 'timestamp')))
{
$sql .= " AND ef.".$tmpkey." = '".$db->idate($crit)."'";
}
elseif ($crit != '' && (! in_array($typ, array('select','sellist')) || $crit != '0') && (! in_array($typ, array('link')) || $crit != '-1'))
{
$mode_search=0;
if (in_array($typ, array('int','double','real'))) $mode_search=1; // Search on a numeric
if (in_array($typ, array('sellist','link')) && $crit != '0' && $crit != '-1') $mode_search=2; // Search on a foreign key int
if (in_array($typ, array('chkbxlst','checkbox'))) $mode_search=4; // Search on a multiselect field with sql type = text

$sql .= natural_search('ef.'.$tmpkey, $crit, $mode_search);
}
}
Expand Down
4 changes: 2 additions & 2 deletions htdocs/modulebuilder/template/myobject_list.php
Expand Up @@ -93,8 +93,8 @@
$diroutputmassaction=$conf->mymodule->dir_output . '/temp/massgeneration/'.$user->id;
$hookmanager->initHooks(array('myobjectlist')); // Note that conf->hooks_modules contains array
// Fetch optionals attributes and labels
$extralabels = $extrafields->fetch_name_optionals_label('myobject');
$search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_');
$extralabels = $extrafields->fetch_name_optionals_label('myobject'); // Load $extrafields->attributes['myobject']
$search_array_options=$extrafields->getOptionalsFromPost($object->table_element,'','search_');

// Default sort order (if not yet defined by previous GETPOST)
if (! $sortfield) $sortfield="t.".key($object->fields); // Set here default search field. By default 1st field in definition.
Expand Down

0 comments on commit 9a0b5fb

Please sign in to comment.