From 22f7aaf56c276228b801684ac9777dac27d52402 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 9 Feb 2005 10:15:49 +0000 Subject: [PATCH] fix for bug [ 1049574 ] Windows does not support dates prior 1970-01-01 using adodb's datelibary instead of the regular php-functions: date, mktime, ... also added input-validation for dates --- etemplate/inc/class.date_widget.inc.php | 84 +++++++++++++++---------- etemplate/setup/phpgw_de.lang | 1 + etemplate/setup/phpgw_en.lang | 1 + 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/etemplate/inc/class.date_widget.inc.php b/etemplate/inc/class.date_widget.inc.php index 768d81e317..e595e1fcbd 100644 --- a/etemplate/inc/class.date_widget.inc.php +++ b/etemplate/inc/class.date_widget.inc.php @@ -12,18 +12,25 @@ /* $Id$ */ - /*! - @class date_widget - @author ralfbecker - @abstract widget that reads a date and/or time - @param Options/$cell['size'] = $format[,$options], - @param $format: ''=timestamp or eg. 'Y-m-d H:i' for 2002-12-31 23:59 - @param $options: &1 = year is int-input not selectbox, &2 = show a [Today] button, \ - &4 = 1min steps for time (default is 5min, with fallback to 1min if value is not in 5min-steps), - &8 = dont show time for readonly and type date-time if time is 0:00, - &16 = prefix r/o display with dow - @discussion This widget is independent of the UI as it only uses etemplate-widgets and has therefor no render-function - */ + /** + * eTemplate extension to input or display date and/or time values + * + * Contains the following widgets: Date, Date+Time, Time, Hour + * + * Supported attributes: format[,options] + * format: ''=timestamp, or eg. 'Y-m-d H:i' for 2002-12-31 23:59 + * options: &1 = year is int-input not selectbox, &2 = show a [Today] button, (html-UI always uses jscal and dont care for &1+&2) + * &4 = 1min steps for time (default is 5min, with fallback to 1min if value is not in 5min-steps), + * &8 = dont show time for readonly and type date-time if time is 0:00, + * &16 = prefix r/o display with dow + * + * This widget is independent of the UI as it only uses etemplate-widgets and has therefor no render-function. + * Uses the adodb datelibary to overcome the windows-limitation to not allow dates before 1970 + * + * @package etemplate + * @author RalfBecker-AT-outdoor-training.de + * @license GPL + */ class date_widget { var $public_functions = array( @@ -36,6 +43,8 @@ 'date-timeonly' => 'Time', // time 'date-houronly' => 'Hour', // hour ); + var $dateformat; // eg. Y-m-d, d-M-Y + var $timeformat; // 12 or 24 function date_widget($ui) { @@ -48,6 +57,7 @@ $this->jscal = &$GLOBALS['phpgw']->jscalendar; } $this->timeformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['timeformat']; + $this->dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']; } function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl) @@ -55,7 +65,10 @@ $type = $cell['type']; list($data_format,$options) = explode(',',$cell['size']); if ($type == 'date-houronly' && empty($data_format)) $data_format = 'H'; - $extension_data = $data_format; + $extension_data = array( + 'data_format' => $data_format, + 'type' => $type, + ); if (!$value) { @@ -88,12 +101,12 @@ else { $value = array( - 'Y' => date('Y',$value), - 'm' => date('m',$value), - 'M' => substr(lang(date('F',$value)),0,3), - 'd' => date('d',$value), - 'H' => date('H',$value), - 'i' => date('i',$value) + 'Y' => adodb_date('Y',$value), + 'm' => adodb_date('m',$value), + 'M' => substr(lang(adodb_date('F',$value)),0,3), + 'd' => adodb_date('d',$value), + 'H' => adodb_date('H',$value), + 'i' => adodb_date('i',$value) ); } $time_0h0 = !(int)$value['H'] && !(int)$value['i']; @@ -109,7 +122,7 @@ } $timeformat += array(5 => 'a'); } - $format = split('[/.-]',$sep=$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']); + $format = split('[/.-]',$this->dateformat); $readonly = $cell['readonly'] || $readonlys; @@ -121,8 +134,8 @@ if ($readonly) // is readonly { $sep = array( - 1 => $sep[1], - 2 => $sep[1], + 1 => $this->dateformat[1], + 2 => $this->dateformat[1], 3 => ' ', 4 => ':' ); @@ -132,7 +145,7 @@ { if (!$n && $options & 16 ) { - $str = lang(date('l',mktime(12,0,0,$value['m'],$value['d'],$value['Y']))).' '; + $str = lang(adodb_date('l',adodb_mktime(12,0,0,$value['m'],$value['d'],$value['Y']))).' '; } $str .= ($str != '' ? $sep[$n] : '') . $value[$format[$n]]; } @@ -209,7 +222,7 @@ if (($js = $tmpl->java_script())) { $dcell['needed'] = True; // to get a button - $dcell['onchange'] = "this.form.elements['$name"."[Y]'].value='".date('Y')."'; this.form.elements['$name"."[m]'].value='".date('n')."';this.form.elements['$name"."[d]'].value='".(0+date('d'))."'; return false;"; + $dcell['onchange'] = "this.form.elements['$name"."[Y]'].value='".adodb_date('Y')."'; this.form.elements['$name"."[m]'].value='".adodb_date('n')."';this.form.elements['$name"."[d]'].value='".(0+adodb_date('d'))."'; return false;"; } $dcell['type'] = $js ? 'button' : 'checkbox'; $row[$tpl->num2chrs(++$i)] = &$dcell; @@ -254,7 +267,7 @@ function post_process($name,&$value,&$extension_data,&$loop,&$tmpl,$value_in) { - //echo "

date_widget::post_process('$name','$extension_data') value="; print_r($value); echo ", value_in="; print_r($value_in); echo "

\n"; + //echo "

date_widget::post_process('$name','$extension_data[type]','$extension_data[data_format]') value="; print_r($value); echo ", value_in="; print_r($value_in); echo "

\n"; if (!isset($value) && !isset($value_in)) { return False; @@ -264,7 +277,7 @@ $set = array('Y','m','d'); foreach($set as $d) { - $value[$d] = date($d); + $value[$d] = adodb_date($d); } } if (isset($value_in['str']) && !empty($value_in['str'])) @@ -282,11 +295,11 @@ { if (!$value['m']) { - $value['m'] = date('m'); + $value['m'] = adodb_date('m'); } if (!$value['Y']) { - $value['Y'] = date('Y'); + $value['Y'] = adodb_date('Y'); } elseif ($value['Y'] < 100) { @@ -305,15 +318,22 @@ $value['H'] += 12; } } - if (empty($extension_data)) + // checking the date is a correct one + if (!checkdate($value['m'],$value['d'],$value['Y'])) { - $value = mktime(intval($value['H']),intval($value['i']),0,$value['m'],$value['d'],$value['Y']); + $GLOBALS['phpgw_info']['etemplate']['validation_errors'][$name] = lang("'%1' is not a valid date !!!", + $GLOBALS['phpgw']->common->dateformatorder($value['Y'],$value['m'],$value['d'],true)); + } + $data_format = $extension_data['data_format']; + if (empty($data_format)) + { + $value = adodb_mktime((int) $value['H'],(int) $value['i'],0,$value['m'],$value['d'],$value['Y']); } else { - for ($n = 0,$str = ''; $n < strlen($extension_data); ++$n) + for ($n = 0,$str = ''; $n < strlen($data_format); ++$n) { - if (strstr('YmdHi',$c = $extension_data[$n])) + if (strstr('YmdHi',$c = $data_format[$n])) { $str .= sprintf($c=='Y'?'%04d':'%02d',$value[$c]); } diff --git a/etemplate/setup/phpgw_de.lang b/etemplate/setup/phpgw_de.lang index 9072e4b54c..c03b0dd56b 100644 --- a/etemplate/setup/phpgw_de.lang +++ b/etemplate/setup/phpgw_de.lang @@ -10,6 +10,7 @@ %s onchange etemplate de %s onChange %s readonly etemplate de %s Schreibschutz '%1' has an invalid format !!! etemplate de '%1' hat ein ungültiges Format !!! +'%1' is not a valid date !!! etemplate de '%1' ist ein ungültiges Datum !!! '%1' is not a valid floatingpoint number !!! etemplate de '%1' ist keine gültige Kommazahl !!! '%1' is not a valid integer !!! etemplate de '%1' ist keine gültige Ganzzahl !!! a pattern to be searched for etemplate de ein Muster nach dem gesucht werden soll diff --git a/etemplate/setup/phpgw_en.lang b/etemplate/setup/phpgw_en.lang index 6b6da7fcc0..9613c4776b 100644 --- a/etemplate/setup/phpgw_en.lang +++ b/etemplate/setup/phpgw_en.lang @@ -10,6 +10,7 @@ %s onchange etemplate en %s onChange %s readonly etemplate en %s readonly '%1' has an invalid format !!! etemplate en '%1' has an invalid format !!! +'%1' is not a valid date !!! etemplate en '%1' is not a valid date !!! '%1' is not a valid floatingpoint number !!! etemplate en '%1' is not a valid floatingpoint number !!! '%1' is not a valid integer !!! etemplate en '%1' is not a valid integer !!! a pattern to be searched for etemplate en a pattern to be searched for