From 7bc2e1ffad8e6e5381cfac2375d3360161f26c73 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 4 Mar 2021 10:31:31 +0100 Subject: [PATCH 1/2] FIX Timezone management for datetime with modulebuilder and extrafields --- htdocs/core/actions_addupdatedelete.inc.php | 6 ++-- htdocs/core/class/commonobject.class.php | 28 +++++++++++++--- htdocs/core/class/extrafields.class.php | 37 +++++++++++++-------- htdocs/core/tpl/extrafields_view.tpl.php | 15 +++++++-- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/htdocs/core/actions_addupdatedelete.inc.php b/htdocs/core/actions_addupdatedelete.inc.php index cdca2556729..3bc8f0040cc 100644 --- a/htdocs/core/actions_addupdatedelete.inc.php +++ b/htdocs/core/actions_addupdatedelete.inc.php @@ -71,7 +71,7 @@ if ($action == 'add' && !empty($permissiontoadd)) } elseif ($object->fields[$key]['type'] == 'date') { $value = dol_mktime(12, 0, 0, GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int')); // for date without hour, we use gmt } elseif ($object->fields[$key]['type'] == 'datetime') { - $value = dol_mktime(GETPOST($key.'hour', 'int'), GETPOST($key.'min', 'int'), 0, GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int'), 'tzuserrel'); + $value = dol_mktime(GETPOST($key.'hour', 'int'), GETPOST($key.'min', 'int'), GETPOST($key.'sec', 'int'), GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int'), 'tzuserrel'); } elseif ($object->fields[$key]['type'] == 'duration') { $value = 60 * 60 * GETPOST($key.'hour', 'int') + 60 * GETPOST($key.'min', 'int'); } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) { @@ -158,9 +158,9 @@ if ($action == 'update' && !empty($permissiontoadd)) $value = GETPOST($key, 'restricthtml'); } } elseif ($object->fields[$key]['type'] == 'date') { - $value = dol_mktime(12, 0, 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year')); // for date without hour, we use gmt + $value = dol_mktime(12, 0, 0, GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int')); // for date without hour, we use gmt } elseif ($object->fields[$key]['type'] == 'datetime') { - $value = dol_mktime(GETPOST($key.'hour'), GETPOST($key.'min'), 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'), 'tzuserrel'); + $value = dol_mktime(GETPOST($key.'hour', 'int'), GETPOST($key.'min', 'int'), GETPOST($key.'sec', 'int'), GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int'), 'tzuserrel'); } elseif ($object->fields[$key]['type'] == 'duration') { if (GETPOST($key.'hour', 'int') != '' || GETPOST($key.'min', 'int') != '') { $value = 60 * 60 * GETPOST($key.'hour', 'int') + 60 * GETPOST($key.'min', 'int'); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index bbc7aa39067..af2c902aacb 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -5962,17 +5962,26 @@ abstract class CommonObject } } - if (in_array($type, array('date', 'datetime'))) { + if (in_array($type, array('date'))) { $tmp = explode(',', $size); $newsize = $tmp[0]; - - $showtime = in_array($type, array('datetime')) ? 1 : 0; + $showtime = 0; // Do not show current date when field not required (see selectDate() method) if (!$required && $value == '') $value = '-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('datetime'))) { + $tmp = explode(',', $size); + $newsize = $tmp[0]; + $showtime = 1; + + // Do not show current date when field not required (see selectDate() method) + if (!$required && $value == '') $value = '-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, '', '', '', 1, '', '', 'tzuserrel'); } elseif (in_array($type, array('duration'))) { $out = $form->select_duration($keyprefix.$key.$keysuffix, $value, 0, 'text', 0, 1); } elseif (in_array($type, array('int', 'integer'))) { @@ -6911,14 +6920,23 @@ abstract class CommonObject if ($action == 'selectlines') { $colspan++; } // Convert date into timestamp format (value in memory must be a timestamp) - if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('date', 'datetime'))) + if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('date'))) { $datenotinstring = $this->array_options['options_'.$key]; if (!is_numeric($this->array_options['options_'.$key])) // For backward compatibility { $datenotinstring = $this->db->jdate($datenotinstring); } - $value = (GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix)) ? dol_mktime(GETPOST($keyprefix.'options_'.$key.$keysuffix."hour", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."min", 'int', 3), 0, GETPOST($keyprefix.'options_'.$key.$keysuffix."month", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."day", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."year", 'int', 3)) : $datenotinstring; + $value = (GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix)) ? dol_mktime(12, 0, 0, GETPOST($keyprefix.'options_'.$key.$keysuffix."month", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."day", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."year", 'int', 3)) : $datenotinstring; + } + if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('datetime'))) + { + $datenotinstring = $this->array_options['options_'.$key]; + if (!is_numeric($this->array_options['options_'.$key])) // For backward compatibility + { + $datenotinstring = $this->db->jdate($datenotinstring); + } + $value = (GETPOSTISSET($keyprefix.'options_'.$key.$keysuffix)) ? dol_mktime(GETPOST($keyprefix.'options_'.$key.$keysuffix."hour", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."min", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."sec", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."month", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."day", 'int', 3), GETPOST($keyprefix.'options_'.$key.$keysuffix."year", 'int', 3), 'tzuserrel') : $datenotinstring; } // Convert float submited string into real php numeric (value in memory must be a php numeric) if (in_array($extrafields->attributes[$this->table_element]['type'][$key], array('price', 'double'))) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index fa81515b90d..7dfc1196a16 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1015,17 +1015,26 @@ class ExtraFields } } - if (in_array($type, array('date', 'datetime'))) { + if (in_array($type, array('date'))) { $tmp = explode(',', $size); $newsize = $tmp[0]; - - $showtime = in_array($type, array('datetime')) ? 1 : 0; + $showtime = 0; // Do not show current date when field not required (see selectDate() method) if (!$required && $value == '') $value = '-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('datetime'))) { + $tmp = explode(',', $size); + $newsize = $tmp[0]; + $showtime = 1; + + // Do not show current date when field not required (see selectDate() method) + if (!$required && $value == '') $value = '-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, '', '', '', 1, '', '', 'tzuserrel'); } elseif (in_array($type, array('int', 'integer'))) { $tmp = explode(',', $size); @@ -1562,11 +1571,11 @@ class ExtraFields if ($type == 'date') { $showsize = 10; - $value = dol_print_date($value, 'day'); + $value = dol_print_date($value, 'day'); // For date without hour, date is always GMT for storage and output } elseif ($type == 'datetime') { $showsize = 19; - $value = dol_print_date($value, 'dayhour'); + $value = dol_print_date($value, 'dayhour', 'tzuserrel'); } elseif ($type == 'int') { $showsize = 10; @@ -2014,12 +2023,10 @@ class ExtraFields if (in_array($key_type, array('date'))) { // Clean parameters - // TODO GMT date in memory must be GMT so we should add gm=true in parameters - $value_key = dol_mktime(0, 0, 0, $_POST["options_".$key."month"], $_POST["options_".$key."day"], $_POST["options_".$key."year"]); + $value_key = dol_mktime(12, 0, 0, GETPOST("options_".$key."month", 'int'), GETPOST("options_".$key."day", 'int'), GETPOST("options_".$key."year", 'int')); } elseif (in_array($key_type, array('datetime'))) { // Clean parameters - // TODO GMT date in memory must be GMT so we should add gm=true in parameters - $value_key = dol_mktime($_POST["options_".$key."hour"], $_POST["options_".$key."min"], 0, $_POST["options_".$key."month"], $_POST["options_".$key."day"], $_POST["options_".$key."year"]); + $value_key = dol_mktime(GETPOST("options_".$key."hour", 'int'), GETPOST("options_".$key."min", 'int'), GETPOST("options_".$key."sec", 'int'), GETPOST("options_".$key."month", 'int'), GETPOST("options_".$key."day", 'int'), GETPOST("options_".$key."year", 'int'), 'tzuserrel'); } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) { $value_arr = GETPOST("options_".$key, 'array'); // check if an array if (!empty($value_arr)) { @@ -2086,13 +2093,15 @@ class ExtraFields $key_type = $this->attributes[$extrafieldsobjectkey]['type'][$key]; } - if (in_array($key_type, array('date', 'datetime'))) - { + if (in_array($key_type, array('date'))) { 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')); - } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) - { + $value_key = dol_mktime(12, 0, 0, GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int')); + } elseif (in_array($key_type, array('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'), GETPOST($keysuffix."options_".$key.$keyprefix."sec", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int'), 'tzuserrel'); + } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) { if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix)) continue; // Value was not provided, we should not set it. $value_arr = GETPOST($keysuffix."options_".$key.$keyprefix); // Make sure we get an array even if there's only one checkbox diff --git a/htdocs/core/tpl/extrafields_view.tpl.php b/htdocs/core/tpl/extrafields_view.tpl.php index 9db0ae13824..40ea3e0ce50 100644 --- a/htdocs/core/tpl/extrafields_view.tpl.php +++ b/htdocs/core/tpl/extrafields_view.tpl.php @@ -168,7 +168,7 @@ if (empty($reshook) && is_array($extrafields->attributes[$object->table_element] print ''; // Convert date into timestamp format - if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date', 'datetime'))) + if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('date'))) { $datenotinstring = $object->array_options['options_'.$tmpkeyextra]; // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x'; @@ -177,7 +177,18 @@ if (empty($reshook) && is_array($extrafields->attributes[$object->table_element] $datenotinstring = $db->jdate($datenotinstring); } //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour'); - $value = GETPOSTISSET("options_".$tmpkeyextra) ? dol_mktime(GETPOST("options_".$tmpkeyextra."hour", 'int'), GETPOST("options_".$tmpkeyextra."min", 'int'), 0, GETPOST("options_".$tmpkeyextra."month", 'int'), GETPOST("options_".$tmpkeyextra."day", 'int'), GETPOST("options_".$tmpkeyextra."year", 'int')) : $datenotinstring; + $value = GETPOSTISSET("options_".$tmpkeyextra) ? dol_mktime(12, 0, 0, GETPOST("options_".$tmpkeyextra."month", 'int'), GETPOST("options_".$tmpkeyextra."day", 'int'), GETPOST("options_".$tmpkeyextra."year", 'int')) : $datenotinstring; + } + if (in_array($extrafields->attributes[$object->table_element]['type'][$tmpkeyextra], array('datetime'))) + { + $datenotinstring = $object->array_options['options_'.$tmpkeyextra]; + // print 'X'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.'x'; + if (!is_numeric($object->array_options['options_'.$tmpkeyextra])) // For backward compatibility + { + $datenotinstring = $db->jdate($datenotinstring); + } + //print 'x'.$object->array_options['options_' . $tmpkeyextra].'-'.$datenotinstring.' - '.dol_print_date($datenotinstring, 'dayhour'); + $value = GETPOSTISSET("options_".$tmpkeyextra) ? dol_mktime(GETPOST("options_".$tmpkeyextra."hour", 'int'), GETPOST("options_".$tmpkeyextra."min", 'int'), GETPOST("options_".$tmpkeyextra."sec", 'int'), GETPOST("options_".$tmpkeyextra."month", 'int'), GETPOST("options_".$tmpkeyextra."day", 'int'), GETPOST("options_".$tmpkeyextra."year", 'int'), 'tzuserrel') : $datenotinstring; } //TODO Improve element and rights detection From a04a4806e038619646c1e6a2645f7ce6aed38e80 Mon Sep 17 00:00:00 2001 From: Juanjo Menent Date: Thu, 4 Mar 2021 11:50:46 +0100 Subject: [PATCH 2/2] Fix: lost selecteds items in ticket creation --- htdocs/core/class/html.formticket.class.php | 7 ++++--- htdocs/ticket/list.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/html.formticket.class.php b/htdocs/core/class/html.formticket.class.php index d7cf03c41e0..2bd674e1c99 100644 --- a/htdocs/core/class/html.formticket.class.php +++ b/htdocs/core/class/html.formticket.class.php @@ -2,6 +2,7 @@ /* Copyright (C) 2013-2015 Jean-François FERRY * Copyright (C) 2016 Christophe Battarel * Copyright (C) 2019 Frédéric France + * Copyright (C) 2021 Juanjo Menent * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -503,7 +504,7 @@ class FormTicket print ' selected="selected"'; } elseif ($selected == $id) { print ' selected="selected"'; - } elseif ($arraytypes['use_default'] == "1" && !$empty) { + } elseif ($arraytypes['use_default'] == "1" && !$selected && !$empty) { print ' selected="selected"'; } @@ -598,7 +599,7 @@ class FormTicket print ' selected="selected"'; } elseif ($selected == $id) { print ' selected="selected"'; - } elseif ($arraycategories['use_default'] == "1" && !$empty) { + } elseif ($arraycategories['use_default'] == "1" && !$selected && !$empty) { print ' selected="selected"'; } @@ -699,7 +700,7 @@ class FormTicket print ' selected="selected"'; } elseif ($selected == $id) { print ' selected="selected"'; - } elseif ($arrayseverities['use_default'] == "1" && !$empty) { + } elseif ($arrayseverities['use_default'] == "1" && !$selected && !$empty) { print ' selected="selected"'; } diff --git a/htdocs/ticket/list.php b/htdocs/ticket/list.php index e9b36a2d364..91cefc88d42 100644 --- a/htdocs/ticket/list.php +++ b/htdocs/ticket/list.php @@ -362,7 +362,7 @@ foreach ($search as $key => $val) } if ($search_all) $sql .= natural_search(array_keys($fieldstosearchall), $search_all); if ($search_societe) $sql .= natural_search('s.nom', $search_societe); -if ($search_fk_project) $sql .= natural_search('fk_project', $search_fk_project, 2); +if ($search_fk_project > 0) $sql .= natural_search('fk_project', $search_fk_project, 2); if ($search_date_start) $sql .= " AND t.datec >= '".$db->idate($search_date_start)."'"; if ($search_date_end) $sql .= " AND t.datec <= '".$db->idate($search_date_end)."'"; if ($search_dateread_start) $sql .= " AND t.date_read >= '".$db->idate($search_dateread_start)."'";