From 2c1d4049be08c850dd79a194993b2cf2cbfe2b80 Mon Sep 17 00:00:00 2001 From: Florian Mortgat Date: Tue, 10 Nov 2020 17:24:17 +0100 Subject: [PATCH] Enable list filtering on date and datetime extrafields --- htdocs/core/class/extrafields.class.php | 60 +++++++++++++++---- .../tpl/extrafields_list_search_input.tpl.php | 4 +- .../tpl/extrafields_list_search_param.tpl.php | 15 +++++ .../tpl/extrafields_list_search_sql.tpl.php | 10 +++- 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 4b6f43bd06e..459a7b1820d 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -948,15 +948,15 @@ class ExtraFields * Return HTML string to put an input field into a page * Code very similar with showInputField of common object * - * @param string $key Key of attribute - * @param string $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value) - * @param string $moreparam To add more parametes on html input tag - * @param string $keysuffix Prefix string to add after name and id of field (can be used to avoid duplicate names) - * @param string $keyprefix Suffix string to add before name and id of field (can be used to avoid duplicate names) - * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric) - * @param int $objectid Current object id - * @param string $extrafieldsobjectkey If defined (for example $object->table_element), use the new method to get extrafields data - * @param string $mode 1=Used for search filters + * @param string $key Key of attribute + * @param string|array $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value); for dates in filter mode, a range array('start'=>, 'end'=>) should be provided + * @param string $moreparam To add more parametes on html input tag + * @param string $keysuffix Prefix string to add after name and id of field (can be used to avoid duplicate names) + * @param string $keyprefix Suffix string to add before name and id of field (can be used to avoid duplicate names) + * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric) + * @param int $objectid Current object id + * @param string $extrafieldsobjectkey If defined (for example $object->table_element), use the new method to get extrafields data + * @param string $mode 1=Used for search filters * @return string */ public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '', $mode = 0) @@ -1069,8 +1069,23 @@ class ExtraFields // Do not show current date when field not required (see selectDate() method) if (!$required && $value == '') $value = '-1'; + if ($mode == 1) { + // search filter on a date extrafield shows two inputs to select a date range + $prefill = array( + 'start' => isset($value['start']) ? $value['start'] : '', + 'end' => isset($value['end']) ? $value['end'] : ''); + $out = '
' + . $langs->trans('From') . ' ' + . $form->selectDate($prefill['start'], $keyprefix . $key . $keysuffix . '_start', 0, 0, 1) + . '
' + . '
' + . $langs->trans('to') . ' ' + . $form->selectDate($prefill['end'], $keyprefix . $key . $keysuffix . '_end', 0, 0, 1) + . '
'; + } else { + $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required, '', 1, (($keyprefix != 'search_' && $keyprefix != 'search_options_') ? 1 : 0), 0, 1); + } // TODO Must also support $moreparam - $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required, '', 1, (($keyprefix != 'search_' && $keyprefix != 'search_options_') ? 1 : 0), 0, 1); } elseif (in_array($type, array('int', 'integer'))) { @@ -2162,9 +2177,28 @@ class ExtraFields if (in_array($key_type, array('date', 'datetime'))) { - if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix."year")) continue; // Value was not provided, we should not set it. - // Clean parameters - $value_key = dol_mktime(GETPOST($keysuffix."options_".$key.$keyprefix."hour", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."min", 'int'), 0, GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int')); + $dateparamname_start = $keysuffix . 'options_' . $key . $keyprefix . '_start'; + $dateparamname_end = $keysuffix . 'options_' . $key . $keyprefix . '_end'; + if (GETPOSTISSET($dateparamname_start . 'year') && GETPOSTISSET($dateparamname_end . 'year')) { + // values provided as a date pair (start date + end date), each date being broken down as year, month, day, etc. + $value_key = array( + 'start' => mktime( + 0, 0, 0, + GETPOST($dateparamname_start . 'month', 'int'), + GETPOST($dateparamname_start . 'day', 'int'), + GETPOST($dateparamname_start . 'year', 'int')), + 'end' => mktime( + 23, 59, 59, + GETPOST($dateparamname_end . 'month', 'int'), + GETPOST($dateparamname_end . 'day', 'int'), + GETPOST($dateparamname_end . 'year', 'int')) + ); + } elseif (GETPOSTISSET($keysuffix."options_".$key.$keyprefix."year")) { + // Clean parameters + $value_key = dol_mktime(GETPOST($keysuffix."options_".$key.$keyprefix."hour", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."min", 'int'), 0, GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int')); + } else { + continue; // Value was not provided, we should not set it. + } } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) { diff --git a/htdocs/core/tpl/extrafields_list_search_input.tpl.php b/htdocs/core/tpl/extrafields_list_search_input.tpl.php index 6e1dae186b1..7fece2efd1d 100644 --- a/htdocs/core/tpl/extrafields_list_search_input.tpl.php +++ b/htdocs/core/tpl/extrafields_list_search_input.tpl.php @@ -36,8 +36,8 @@ if (!empty($extrafieldsobjectkey)) // $extrafieldsobject is the $object->table_e } elseif (in_array($typeofextrafield, array('datetime', 'timestamp'))) { - // TODO - // Use showInputField in a particular manner to have input with a comparison operator, not input for a specific value date-hour-minutes + $morecss = ''; + echo $extrafields->showInputField($key, $search_array_options[$search_options_pattern.$tmpkey], '', '', $search_options_pattern, $morecss, 0, $extrafieldsobjectkey, 1); } else { diff --git a/htdocs/core/tpl/extrafields_list_search_param.tpl.php b/htdocs/core/tpl/extrafields_list_search_param.tpl.php index 6e6d6305106..009d05b30ee 100644 --- a/htdocs/core/tpl/extrafields_list_search_param.tpl.php +++ b/htdocs/core/tpl/extrafields_list_search_param.tpl.php @@ -16,6 +16,21 @@ if (!empty($search_array_options) && is_array($search_array_options)) // $extraf { $crit = $val; $tmpkey = preg_replace('/'.$search_options_pattern.'/', '', $key); + if (is_array($val) && array_key_exists('start', $val) && array_key_exists('end', $val)) { + // date range from list filters is stored as array('start' => , 'end' => ) + // start date + $param .= '&' . $search_options_pattern.$tmpkey.'_startyear' . '=' . intval(dol_print_date($val['start'], '%Y')); + $param .= '&' . $search_options_pattern.$tmpkey.'_startmonth' . '=' . intval(dol_print_date($val['start'], '%m')); + $param .= '&' . $search_options_pattern.$tmpkey.'_startday' . '=' . intval(dol_print_date($val['start'], '%d')); + $param .= '&' . $search_options_pattern.$tmpkey.'_starthour' . '=' . intval(dol_print_date($val['start'], '%H')); + $param .= '&' . $search_options_pattern.$tmpkey.'_startmin' . '=' . intval(dol_print_date($val['start'], '%M')); + // end date + $param .= '&' . $search_options_pattern.$tmpkey.'_endyear' . '=' . intval(dol_print_date($val['end'], '%Y')); + $param .= '&' . $search_options_pattern.$tmpkey.'_endmonth' . '=' . intval(dol_print_date($val['end'], '%m')); + $param .= '&' . $search_options_pattern.$tmpkey.'_endday' . '=' . intval(dol_print_date($val['end'], '%d')); + $param .= '&' . $search_options_pattern.$tmpkey.'_endhour' . '=' . intval(dol_print_date($val['end'], '%H')); + $param .= '&' . $search_options_pattern.$tmpkey.'_endmin' . '=' . intval(dol_print_date($val['end'], '%M')); + } if ($val != '') $param .= '&'.$search_options_pattern.$tmpkey.'='.urlencode($val); } } diff --git a/htdocs/core/tpl/extrafields_list_search_sql.tpl.php b/htdocs/core/tpl/extrafields_list_search_sql.tpl.php index 03d487ee0c0..048ca24020a 100644 --- a/htdocs/core/tpl/extrafields_list_search_sql.tpl.php +++ b/htdocs/core/tpl/extrafields_list_search_sql.tpl.php @@ -23,7 +23,15 @@ if (!empty($extrafieldsobjectkey) && !empty($search_array_options) && is_array($ if ($crit != '' && in_array($typ, array('date', 'datetime', 'timestamp'))) { - $sql .= " AND ".$extrafieldsobjectprefix.$tmpkey." = '".$db->idate($crit)."'"; + if (is_numeric($crit)) { + $sql .= " AND ".$extrafieldsobjectprefix.$tmpkey." = '".$db->idate($crit)."'"; + } elseif (is_array($crit)) { + $sql .= ' AND (' . $extrafieldsobjectprefix.$tmpkey + .' BETWEEN "' + . $db->idate($crit['start']) + . '" AND "' + . $db->idate($crit['end']) . '")'; + } } elseif (in_array($typ, array('boolean'))) {