From ab05b3eef6705ca2ba6bc8fadbfd184ebb982c6a Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Tue, 12 Sep 2006 11:51:02 +0000 Subject: [PATCH] - change jscalendar-setup url to include lang&dateformat, to force the browser to reload the file, if the user changes his prefs (we had a lot of bug-reports about not working jscal because of this in the past, maybe this should be commited to 1.2 too) - documentation update --- phpgwapi/inc/class.jscalendar.inc.php | 412 +++++++++++++++----------- 1 file changed, 238 insertions(+), 174 deletions(-) diff --git a/phpgwapi/inc/class.jscalendar.inc.php b/phpgwapi/inc/class.jscalendar.inc.php index 38d1c1faac..317cfcd7d4 100644 --- a/phpgwapi/inc/class.jscalendar.inc.php +++ b/phpgwapi/inc/class.jscalendar.inc.php @@ -1,217 +1,281 @@ * - * -------------------------------------------- * - * 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 the * - * Free Software Foundation; either version 2 of the License, or (at your * - * option) any later version. * - \**************************************************************************/ +/** + * generates html with methods representing html-tags or higher widgets + * + * @link http://www.egroupware.org + * @author Ralf Becker + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @version $Id$ + */ - /* $Id$ */ +/**************************************************************************\ +* eGroupWare - API jsCalendar wrapper-class * +* http://www.eGroupWare.org * +* Written by Ralf Becker * +* -------------------------------------------- * +* 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 the * +* Free Software Foundation; either version 2 of the License, or (at your * +* option) any later version. * +\**************************************************************************/ - /*! - @class jscalendar - @author ralfbecker - @abstract wrapper for the jsCalendar - @discussion the constructor load the necessary javascript-files - */ - class jscalendar +/* $Id$ */ + +/*! +@class jscalendar +@author ralfbecker +@abstract wrapper for the jsCalendar +@discussion the constructor load the necessary javascript-files +*/ +/** + * Wrapper for the jsCalendar + * + * The constructor load the necessary javascript-files. + * + * @package api + * @subpackage html + * @access public + */ +class jscalendar +{ + /** + * url to the jscalendar files + * + * @var string + */ + var $jscalendar_url; + /** + * dateformat from the user-prefs + * + * @var string + */ + var $dateformat; + + /** + * Constructor + * + * @param boolean $do_header=true if true, necessary javascript and css gets loaded, only needed for input + * @param string $path='jscalendar' + * @return jscalendar + */ + function jscalendar($do_header=True,$path='jscalendar') { - /*! - @function jscalendar - @syntax jscalendar( $do_header=True ) - @author ralfbecker - @abstract constructor of the class - @param $do_header if true, necessary javascript and css gets loaded, only needed for input - */ - function jscalendar($do_header=True,$path='jscalendar') - { - $this->jscalendar_url = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/js/'.$path; - $this->dateformat = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat']; + $this->jscalendar_url = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/js/'.$path; + $this->dateformat = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat']; - if ($do_header && !strstr($GLOBALS['egw_info']['flags']['java_script'],'jscalendar')) - { - $GLOBALS['egw_info']['flags']['java_script'] .= + if ($do_header && !strstr($GLOBALS['egw_info']['flags']['java_script'],'jscalendar')) + { + $GLOBALS['egw_info']['flags']['java_script'] .= ' - + '; + } + } + + /** + * Creates an inputfield for the jscalendar (returns the necessary html and js) + * + * @param string $name name and id of the input-field (it also names the id of the img $name.'-toggle') + * @param int/string $date date as string or unix timestamp (in server timezone) + * @param int $year=0 if $date is not used + * @param int $month=0 if $date is not used + * @param int $day=0 if $date is not used + * @param string $helpmsg='' a helpmessage for the statusline of the browser + * @param string $options='' any other options to the inputfield + * @param boolean $jsreturn=false + * @return string html + */ + function input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='',$jsreturn=false) + { + //echo "

jscalendar::input(name='$name', date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', day='$day')

\n"; + + if ($date && (is_int($date) || is_numeric($date))) + { + $year = (int)adodb_date('Y',$date); + $month = (int)adodb_date('n',$date); + $day = (int)adodb_date('d',$date); + } + if ($year && $month && $day) + { + $date = adodb_date($this->dateformat,$ts = adodb_mktime(12,0,0,$month,$day,$year)); + if (strpos($this->dateformat,'M') !== False) + { + $short = lang(adodb_date('M',$ts)); // check if we have a translation of the short-cut + if (substr($short,-1) == '*') // if not generate one by truncating the translation of the long name + { + $short = substr(lang(adodb_date('F',$ts)),0,(int) lang('3 number of chars for month-shortcut')); + } + $date = str_replace(adodb_date('M',$ts),$short,$date); } } - - /*! - @function input - @syntax input( $name,$date,$year=0,$month=0,$day=0 ) - @author ralfbecker - @abstract creates an inputfield for the jscalendar (returns the necessary html and js) - @param $name name and id of the input-field (it also names the id of the img $name.'-toggle') - @param $date date as string or unix timestamp (in server timezone) - @param $year,$month,$day if $date is not used - @param $helpmsg a helpmessage for the statusline of the browser - @param $options any other options to the inputfield - */ - function input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='',$jsreturn=false) + if ($helpmsg !== '') { - //echo "

jscalendar::input(name='$name', date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', day='$day')

\n"; + $options .= " onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" . + " onBlur=\"self.status=''; return true;\""; + } - if ($date && (is_int($date) || is_numeric($date))) - { - $year = (int)adodb_date('Y',$date); - $month = (int)adodb_date('n',$date); - $day = (int)adodb_date('d',$date); - } - if ($year && $month && $day) - { - $date = adodb_date($this->dateformat,$ts = adodb_mktime(12,0,0,$month,$day,$year)); - if (strpos($this->dateformat,'M') !== False) - { - $short = lang(adodb_date('M',$ts)); // check if we have a translation of the short-cut - if (substr($short,-1) == '*') // if not generate one by truncating the translation of the long name - { - $short = substr(lang(adodb_date('F',$ts)),0,(int) lang('3 number of chars for month-shortcut')); - } - $date = str_replace(adodb_date('M',$ts),$short,$date); - } - } - if ($helpmsg !== '') - { - $options .= " onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" . - " onBlur=\"self.status=''; return true;\""; - } + if ($jsreturn) + { + $return_array = array( + 'html' => '', + 'js' => 'Calendar.setup({inputField : "'.$name.'",button: "'.$name.'-trigger" });' + ); - if ($jsreturn) - { - $return_array = array( - 'html' => '', - 'js' => 'Calendar.setup({inputField : "'.$name.'",button: "'.$name.'-trigger" });' - ); - - return $return_array; - } - return + return $return_array; + } + return ' '; - } + } - function flat($url,$date=False,$weekUrl=False,$weekTTip=False,$monthUrl=False,$monthTTip=False,$id='calendar-container') + /** + * Flat jscalendar with tooltips and url's for days, weeks and month + * + * @param string $url url to call if user clicks on a date (&date=YYYYmmdd is appended automatically) + * @param string/int $date=null format YYYYmmdd or timestamp + * @param string $weekUrl='' + * @param string $weekTTip='' + * @param string $monthUrl='' + * @param string $monthTTip='' + * @param string $id='calendar-container' + * @return string html + */ + function flat($url,$date=null,$weekUrl='',$weekTTip='',$monthUrl='',$monthTTip='',$id='calendar-container') + { + if ($date) // string if format YYYYmmdd or timestamp { - if ($date) // string if format YYYYmmdd or timestamp - { - $date = is_int($date) ? adodb_date('m/d/Y',$date) : - substr($date,4,2).'/'.substr($date,6,2).'/'.substr($date,0,4); - } - return ' + $date = is_int($date) ? adodb_date('m/d/Y',$date) : + substr($date,4,2).'/'.substr($date,6,2).'/'.substr($date,0,4); + } + return '
'; - } + } - /*! - @function input2date - @syntax input2date( $datestr,$raw='raw',$day='day',$month='month',$year='year' ) - @author ralfbecker - @abstract converts the date-string back to an array with year, month, day and a timestamp - @param $datestr content of the inputfield generated by jscalendar::input() - @param $raw key of the timestamp-field in the returned array or False of no timestamp - @param $day,$month,$year keys for the array, eg. to set mday instead of day - */ - function input2date($datestr,$raw='raw',$day='day',$month='month',$year='year') + /** + * Converts the date-string back to an array with year, month, day and a timestamp + * + * @param string $datestr content of the inputfield generated by jscalendar::input() + * @param boolean/string $raw='raw' key of the timestamp-field in the returned array or False of no timestamp + * @param string $day='day' keys for the array, eg. to set mday instead of day + * @param string $month='month' keys for the array + * @param string $year='year' keys for the array + * @return array/boolean array with the specified keys and values or false if $datestr == '' + */ + function input2date($datestr,$raw='raw',$day='day',$month='month',$year='year') + { + //echo "

jscalendar::input2date('$datestr') ".print_r($fields,True)."

\n"; + if ($datestr === '') { - //echo "

jscalendar::input2date('$datestr') ".print_r($fields,True)."

\n"; - if ($datestr === '') + return False; + } + $fields = split('[./-]',$datestr); + foreach(split('[./-]',$this->dateformat) as $n => $field) + { + if ($field == 'M') { - return False; - } - $fields = split('[./-]',$datestr); - foreach(split('[./-]',$this->dateformat) as $n => $field) - { - if ($field == 'M') + if (!is_numeric($fields[$n])) { - if (!is_numeric($fields[$n])) + $partcial_match = 0; + for($i = 1; $i <= 12; $i++) { - $partcial_match = 0; - for($i = 1; $i <= 12; $i++) + $long_name = lang(adodb_date('F',mktime(12,0,0,$i,1,2000))); + $short_name = lang(adodb_date('M',mktime(12,0,0,$i,1,2000))); // do we have a translation of the short-cut + if (substr($short_name,-1) == '*') // if not generate one by truncating the translation of the long name { - $long_name = lang(adodb_date('F',mktime(12,0,0,$i,1,2000))); - $short_name = lang(adodb_date('M',mktime(12,0,0,$i,1,2000))); // do we have a translation of the short-cut - if (substr($short_name,-1) == '*') // if not generate one by truncating the translation of the long name - { - $short_name = substr($long_name,0,(int) lang('3 number of chars for month-shortcut')); - } - //echo "
checking '".$fields[$n]."' against '$long_name' or '$short_name'"; - if ($fields[$n] == $long_name || $fields[$n] == $short_name) - { - //echo " ==> OK
"; - $fields[$n] = $i; - break; - } - if (strstr($long_name,$fields[$n]) == $long_name) // partcial match => multibyte saver - { - $partcial_match = $i; - } + $short_name = substr($long_name,0,(int) lang('3 number of chars for month-shortcut')); } - if ($i > 12 && $partcial_match) // nothing found, but a partcial match + //echo "
checking '".$fields[$n]."' against '$long_name' or '$short_name'"; + if ($fields[$n] == $long_name || $fields[$n] == $short_name) { - $fields[$n] = $partcial_match; + //echo " ==> OK
"; + $fields[$n] = $i; + break; + } + if (strstr($long_name,$fields[$n]) == $long_name) // partcial match => multibyte saver + { + $partcial_match = $i; } } - $field = 'm'; + if ($i > 12 && $partcial_match) // nothing found, but a partcial match + { + $fields[$n] = $partcial_match; + } } - $date[$field] = (int)$fields[$n]; + $field = 'm'; } - $ret = array( - $year => $date['Y'], - $month => $date['m'], - $day => $date['d'] - ); - if ($raw) - { - $ret[$raw] = adodb_mktime(12,0,0,$date['m'],$date['d'],$date['Y']); - } - //echo "

jscalendar::input2date('$datestr','$raw',$day','$month','$year') = "; print_r($ret); echo "

\n"; - - return $ret; + $date[$field] = (int)$fields[$n]; } + $ret = array( + $year => $date['Y'], + $month => $date['m'], + $day => $date['d'] + ); + if ($raw) + { + $ret[$raw] = adodb_mktime(12,0,0,$date['m'],$date['d'],$date['Y']); + } + //echo "

jscalendar::input2date('$datestr','$raw',$day','$month','$year') = "; print_r($ret); echo "

\n"; + + return $ret; } +} + +if (!function_exists('array_intersect_key')) // php5.1 function +{ + function array_intersect_key($array1,$array2) + { + $intersection = $keys = array(); + foreach(func_get_args() as $arr) + { + $keys[] = array_keys((array)$arr); + } + foreach(call_user_func_array('array_intersect',$keys) as $key) + { + $intersection[$key] = $array1[$key]; + } + return $intersection; + } +}