From e40d73091b0bbea7e8364514be5cf115510b7d84 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 9 Jan 2008 01:57:45 +0000 Subject: [PATCH] new time-since sub-widget --- etemplate/inc/class.date_widget.inc.php | 68 +++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/etemplate/inc/class.date_widget.inc.php b/etemplate/inc/class.date_widget.inc.php index 8e092ad1a9..54d2d786ed 100644 --- a/etemplate/inc/class.date_widget.inc.php +++ b/etemplate/inc/class.date_widget.inc.php @@ -50,6 +50,7 @@ 'date-timeonly' => 'Time', // time 'date-houronly' => 'Hour', // hour 'date-duration' => 'Duration', // duration + 'date-since' => 'Time since', // time past since given time ); var $dateformat; // eg. Y-m-d, d-M-Y var $timeformat; // 12 or 24 @@ -89,14 +90,15 @@ function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl) { $type = $cell['type']; - if ($type == 'date-duration') + switch ($type) { - return $this->pre_process_duration($name,$value,$cell,$readonlys,$extension_data,$tmpl); + case 'date-duration': + return $this->pre_process_duration($name,$value,$cell,$readonlys,$extension_data,$tmpl); } list($data_format,$options,$options2) = explode(',',$cell['size']); if ($type == 'date-houronly' && empty($data_format)) $data_format = 'H'; - $readonly = $cell['readonly'] || $readonlys; + $readonly = $cell['readonly'] || $readonlys || $type == 'date-since'; if (!$readonly) // dont set extension-data on readonly, it's not needed and can conflict with other widgets { @@ -158,6 +160,10 @@ 'i' => (int) adodb_date('i',$value) ); } + if ($type == 'date-since') + { + return $this->pre_process_since($value,$cell); + } $time_0h0 = !(int)$value['H'] && !(int)$value['i']; $timeformat = array(3 => 'H', 4 => 'i'); @@ -446,6 +452,62 @@ return True; // extra Label is ok } + /** + * pre-processing of the time since extension + * + * @param array &$value value / existing content, can be modified + * @param array &$cell array with the widget, can be modified for ui-independent widgets + * @return boolean true if extra label is allowed, false otherwise + */ + function pre_process_since(&$value,&$cell) + { + static $unit2label = array( + 'Y' => 'years', + 'm' => 'month', + 'd' => 'days', + 'H' => 'hours', + 'i' => 'minutes', + 's' => 'seconds', + ); + static $unit2s = array( + 'Y' => 31536000, + 'm' => 2628000, + 'd' => 86400, + 'H' => 3600, + 'i' => 60, + 's' => 1, + ); + $cell = etemplate::empty_cell('label','',array('label'=>$cell['label'])); + + if ((string)$value['Y'] !== '') + { + if (!is_object($GLOBALS['egw']->datetime)) + { + $GLOBALS['egw']->datetime = new egw_datetime(); + } + $now_s = time() + $GLOBALS['egw']->datetime->tz_offset; // time() is server-time and we need a user-time + + $val_s = mktime($value['H'],$value['i'],$value['s'],$value['m'],$value['d'],$value['Y']); + + $diff_s = $now_s - $val_s; + + foreach($unit2s as $unit => $unit_s) + { + if ($diff_s >= $unit_s || $unit == 's') + { + $value = round($diff_s/$unit_s,1).' '.lang($unit2label[$unit]); + break; + } + //echo "

$unit: diff_s=$diff_s >= $unit_s --> continue

\n"; + } + } + else + { + $value = ''; + } + return true; + } + /** * postprocessing method, called after the submission of the form *