2003-08-18 01:05:57 +02:00
|
|
|
<?php
|
2006-09-12 13:51:02 +02:00
|
|
|
/**
|
2006-09-12 13:55:54 +02:00
|
|
|
* Wrapper for the jsCalendar
|
2006-09-12 13:51:02 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2009-07-24 12:38:47 +02:00
|
|
|
* @package api
|
|
|
|
* @subpackage html
|
2010-06-04 11:01:15 +02:00
|
|
|
* @version $Id$
|
2006-09-12 13:51:02 +02:00
|
|
|
*/
|
2003-08-18 01:05:57 +02:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for the jsCalendar
|
2008-07-22 16:04:23 +02:00
|
|
|
*
|
2006-09-12 13:55:54 +02:00
|
|
|
* The constructor loads the necessary javascript-files.
|
2006-09-12 13:51:02 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2009-07-24 13:35:34 +02:00
|
|
|
function __construct($do_header=True,$path='jscalendar')
|
2003-08-18 01:05:57 +02:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
$this->jscalendar_url = $GLOBALS['egw_info']['server']['webserver_url'].'/phpgwapi/js/'.$path;
|
|
|
|
$this->dateformat = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
|
2003-12-14 18:07:16 +01:00
|
|
|
|
2009-07-31 14:56:39 +02:00
|
|
|
$args = array_intersect_key($GLOBALS['egw_info']['user']['preferences']['common'],array('lang'=>1,'dateformat'=>1));
|
|
|
|
if (isset($GLOBALS['egw_info']['flags']['currentapp']))
|
|
|
|
{
|
|
|
|
$args['app'] = $GLOBALS['egw_info']['flags']['currentapp'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$args['app'] = 'home'; // home can be granted to anyone.
|
|
|
|
}
|
2009-07-24 13:35:34 +02:00
|
|
|
if ($do_header && (strpos($GLOBALS['egw_info']['flags']['java_script'],'jscalendar')===false))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info']['flags']['java_script'] .= $this->get_javascript();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return javascript needed for jscalendar
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_javascript()
|
|
|
|
{
|
2010-06-04 11:01:15 +02:00
|
|
|
$args = array_intersect_key($GLOBALS['egw_info']['user']['preferences']['common'],array('lang'=>1,'dateformat'=>1));
|
2009-07-24 13:35:34 +02:00
|
|
|
return
|
2005-01-12 01:11:12 +01:00
|
|
|
'<link rel="stylesheet" type="text/css" media="all" href="'.$this->jscalendar_url.'/calendar-blue.css" title="blue" />
|
2004-05-06 18:21:04 +02:00
|
|
|
<script type="text/javascript" src="'.$this->jscalendar_url.'/calendar.js"></script>
|
2010-06-04 11:01:15 +02:00
|
|
|
<script type="text/javascript" src="'.egw::link('/phpgwapi/inc/jscalendar-setup.php',$args,false).'"></script>
|
2003-08-18 01:05:57 +02:00
|
|
|
';
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
2003-08-18 01:05:57 +02:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2009-11-25 21:19:35 +01:00
|
|
|
* @param boolean $useicon=true true: use icon to trigger popup, false: click into input triggers popup
|
2010-01-02 00:04:39 +01:00
|
|
|
* the input is made readonly via javascript to NOT trigger mobile devices to display a keyboard!
|
2006-09-12 13:51:02 +02:00
|
|
|
* @return string html
|
|
|
|
*/
|
2009-11-25 21:19:35 +01:00
|
|
|
function input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='',$jsreturn=false,$useicon=true)
|
2006-09-12 13:51:02 +02:00
|
|
|
{
|
|
|
|
//echo "<p>jscalendar::input(name='$name', date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', day='$day')</p>\n";
|
2003-12-14 18:07:16 +01:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
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));
|
2006-12-07 13:46:08 +01:00
|
|
|
if (strpos($this->dateformat,'M') !== false)
|
2003-12-14 18:07:16 +01:00
|
|
|
{
|
2007-05-08 09:38:36 +02:00
|
|
|
static $substr;
|
|
|
|
if (is_null($substr)) $substr = function_exists('mb_substr') ? 'mb_substr' : 'substr';
|
|
|
|
static $chars_shortcut;
|
|
|
|
if (is_null($chars_shortcut)) $chars_shortcut = (int)lang('3 number of chars for month-shortcut'); // < 0 to take the chars from the end
|
|
|
|
|
2007-05-22 20:02:41 +02:00
|
|
|
$short = lang($m = adodb_date('M',$ts)); // check if we have a translation of the short-cut
|
|
|
|
if ($short == $m || $substr($short,-1) == '*') // if not generate one by truncating the translation of the long name
|
2003-12-14 18:07:16 +01:00
|
|
|
{
|
2008-07-22 16:04:23 +02:00
|
|
|
$short = $chars_shortcut > 0 ? $substr(lang(adodb_date('F',$ts)),0,$chars_shortcut) :
|
2007-05-08 09:38:36 +02:00
|
|
|
$substr(lang(adodb_date('F',$ts)),$chars_shortcut);
|
2003-12-14 18:07:16 +01:00
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
$date = str_replace(adodb_date('M',$ts),$short,$date);
|
2003-12-14 18:07:16 +01:00
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
|
|
|
if ($helpmsg !== '')
|
|
|
|
{
|
|
|
|
$options .= " onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" .
|
|
|
|
" onBlur=\"self.status=''; return true;\"";
|
|
|
|
}
|
2005-11-26 15:25:39 +01:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
if ($jsreturn)
|
|
|
|
{
|
|
|
|
$return_array = array(
|
2009-11-25 21:19:35 +01:00
|
|
|
'html' => '<input type="text" id="'.$name.'" name="'.$name.'" size="10" value="'.htmlspecialchars($date).'"'.$options.'/>'.
|
|
|
|
($useicon ? '<img id="'.$name.'-trigger" src="'.common::find_image('phpgwpai','datepopup').'" title="'.lang('Select date').'" style="cursor:pointer; cursor:hand;"/>' : ''),
|
|
|
|
'js' => 'Calendar.setup({inputField : "'.$name.'"'.($useicon ? ',button: "'.$name.'-trigger"' : '').' });'
|
2009-07-31 14:56:39 +02:00
|
|
|
);
|
2005-11-26 15:25:39 +01:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
return $return_array;
|
|
|
|
}
|
|
|
|
return
|
2009-11-25 21:19:35 +01:00
|
|
|
'<input type="text" id="'.$name.'" name="'.$name.'" size="10" value="'.htmlspecialchars($date).'"'.$options.'/>
|
2010-01-02 00:04:39 +01:00
|
|
|
<script type="text/javascript">'.(!$useicon ? '
|
|
|
|
document.getElementById("'.$name.'").readOnly=true;
|
|
|
|
' : '
|
|
|
|
document.writeln(\'<img id="'.$name.'-trigger" src="'.common::find_image('phpgwapi','datepopup').'" title="'.lang('Select date').'" style="cursor:pointer; cursor:hand;"/>\');').'
|
2006-09-12 13:51:02 +02:00
|
|
|
Calendar.setup(
|
|
|
|
{
|
2009-11-25 21:19:35 +01:00
|
|
|
inputField : "'.$name.'",'.(!$useicon ? '' : '
|
|
|
|
button : "'.$name.'-trigger"').'
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
|
|
|
);
|
2003-08-18 01:05:57 +02:00
|
|
|
</script>
|
|
|
|
';
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
2003-12-14 18:07:16 +01:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
/**
|
|
|
|
* 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')
|
|
|
|
{
|
2010-06-04 11:01:15 +02:00
|
|
|
if (strpos($GLOBALS['egw_info']['flags']['java_script'],'jscalendar') === false)
|
|
|
|
{
|
|
|
|
$javascript = $this->get_javascript();
|
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
if ($date) // string if format YYYYmmdd or timestamp
|
2004-05-06 18:21:04 +02:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
$date = is_int($date) ? adodb_date('m/d/Y',$date) :
|
|
|
|
substr($date,4,2).'/'.substr($date,6,2).'/'.substr($date,0,4);
|
|
|
|
}
|
|
|
|
return '
|
2004-05-06 18:21:04 +02:00
|
|
|
<div id="'.$id.'"></div>
|
2008-07-23 15:59:16 +02:00
|
|
|
'.$javascript.'
|
2004-05-06 18:21:04 +02:00
|
|
|
<script type="text/javascript">
|
2006-09-12 13:51:02 +02:00
|
|
|
function dateChanged(calendar) {
|
2004-05-06 18:21:04 +02:00
|
|
|
'. // Beware that this function is called even if the end-user only
|
2006-09-12 13:51:02 +02:00
|
|
|
// changed the month/year. In order to determine if a date was
|
|
|
|
// clicked you can use the dateClicked property of the calendar:
|
|
|
|
// redirect to $url extended with a &date=YYYYMMDD
|
2004-05-06 18:21:04 +02:00
|
|
|
' if (calendar.dateClicked) {
|
2010-06-04 22:13:21 +02:00
|
|
|
egw_appWindow("calendar").location = "'.$url.'&date=" + calendar.date.print("%Y%m%d");
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
|
|
|
};
|
2010-05-18 11:17:54 +02:00
|
|
|
|
|
|
|
function todayClicked(calendar) {
|
2010-06-04 22:13:21 +02:00
|
|
|
var parts = egw_appWindow("calendar").location.search.split("&");
|
2010-05-18 11:17:54 +02:00
|
|
|
var newsearch = "";
|
|
|
|
var hasdate = false;
|
|
|
|
|
|
|
|
/* Assemble the new search string, if the "date" property is found, replace its value
|
|
|
|
with the current date */
|
|
|
|
for (i = 0; i < parts.length; i++)
|
|
|
|
{
|
|
|
|
var split = parts[i].split("=");
|
|
|
|
if (split[0] && split[0] == "date") {
|
2010-06-04 11:01:15 +02:00
|
|
|
split[1] = "'.egw_time::to('now','Ymd').'";
|
2010-05-18 11:17:54 +02:00
|
|
|
hasdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split[1])
|
|
|
|
newsearch += split[0] + "=" + split[1];
|
|
|
|
else
|
|
|
|
newsearch += split[0];
|
|
|
|
|
|
|
|
if (i < parts.length - 1)
|
|
|
|
newsearch += "&"
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the date property hasn\'t been found, add it to the search string */
|
|
|
|
if (!hasdate) {
|
|
|
|
if (parts.length == 0)
|
|
|
|
newsearch = "?";
|
|
|
|
else
|
|
|
|
newsearch += "&";
|
2010-06-04 11:01:15 +02:00
|
|
|
newsearch += "date='.egw_time::to('now','Ymd').'";
|
2010-05-18 11:17:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-04 22:13:21 +02:00
|
|
|
egw_appWindow("calendar").location.search = newsearch;
|
2010-05-18 11:17:54 +02:00
|
|
|
}
|
|
|
|
|
2004-05-06 18:21:04 +02:00
|
|
|
'.($weekUrl ? '
|
2006-09-12 13:51:02 +02:00
|
|
|
function weekClicked(calendar,weekstart) {
|
2010-06-04 22:13:21 +02:00
|
|
|
egw_appWindow("calendar").location = "'.$weekUrl.'&date=" + weekstart.print("%Y%m%d");
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
2004-05-06 18:21:04 +02:00
|
|
|
' : '').($monthUrl ? '
|
2006-09-12 13:51:02 +02:00
|
|
|
function monthClicked(calendar,monthstart) {
|
2010-06-04 22:13:21 +02:00
|
|
|
egw_appWindow("calendar").location = "'.$monthUrl.'&date=" + monthstart.print("%Y%m%d");
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
2004-05-06 18:21:04 +02:00
|
|
|
' : '').'
|
2008-07-23 15:59:16 +02:00
|
|
|
|
2008-07-22 16:04:23 +02:00
|
|
|
Calendar.setup(
|
|
|
|
{
|
|
|
|
flat : "'.$id.'",
|
|
|
|
flatCallback : dateChanged'.($weekUrl ? ',
|
|
|
|
flatWeekCallback : weekClicked' : '').($weekTTip ? ',
|
|
|
|
flatWeekTTip : "'.addslashes($weekTTip).'"' : '').($monthUrl ? ',
|
|
|
|
flatMonthCallback : monthClicked' : '').($monthTTip ? ',
|
|
|
|
flatMonthTTip : "'.addslashes($monthTTip).'"' : '').($date ? ',
|
2010-05-18 11:17:54 +02:00
|
|
|
flatTodayCallback : todayClicked,
|
2008-07-22 16:04:23 +02:00
|
|
|
date : "'.$date.'"
|
|
|
|
' : '').'
|
|
|
|
}
|
|
|
|
);
|
2008-07-23 15:59:16 +02:00
|
|
|
|
2004-05-06 18:21:04 +02:00
|
|
|
</script>';
|
2006-09-12 13:51:02 +02:00
|
|
|
}
|
2004-05-06 18:21:04 +02:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
/**
|
|
|
|
* 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 "<p>jscalendar::input2date('$datestr') ".print_r($fields,True)."</p>\n";
|
|
|
|
if ($datestr === '')
|
2003-08-18 01:05:57 +02:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
return False;
|
|
|
|
}
|
2009-06-08 18:21:14 +02:00
|
|
|
$fields = preg_split('/[.\\/-]/',$datestr);
|
|
|
|
foreach(preg_split('/[.\\/-]/',$this->dateformat) as $n => $field)
|
2006-09-12 13:51:02 +02:00
|
|
|
{
|
|
|
|
if ($field == 'M')
|
2003-11-03 13:03:17 +01:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
if (!is_numeric($fields[$n]))
|
2003-11-03 13:03:17 +01:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
$partcial_match = 0;
|
|
|
|
for($i = 1; $i <= 12; $i++)
|
2003-11-03 13:03:17 +01:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
$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 "<br>checking '".$fields[$n]."' against '$long_name' or '$short_name'";
|
|
|
|
if ($fields[$n] == $long_name || $fields[$n] == $short_name)
|
2003-11-03 13:03:17 +01:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
//echo " ==> OK<br>";
|
|
|
|
$fields[$n] = $i;
|
|
|
|
break;
|
2004-05-08 13:00:46 +02:00
|
|
|
}
|
2007-04-30 07:42:05 +02:00
|
|
|
if (@strstr($long_name,$fields[$n]) == $long_name) // partcial match => multibyte saver
|
2004-05-08 13:00:46 +02:00
|
|
|
{
|
2006-09-12 13:51:02 +02:00
|
|
|
$partcial_match = $i;
|
2003-11-03 13:03:17 +01:00
|
|
|
}
|
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
if ($i > 12 && $partcial_match) // nothing found, but a partcial match
|
|
|
|
{
|
|
|
|
$fields[$n] = $partcial_match;
|
|
|
|
}
|
2003-11-03 13:03:17 +01:00
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
$field = 'm';
|
2003-12-14 18:07:16 +01:00
|
|
|
}
|
2006-09-12 13:51:02 +02:00
|
|
|
$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 "<p>jscalendar::input2date('$datestr','$raw',$day','$month','$year') = "; print_r($ret); echo "</p>\n";
|
2003-08-18 01:05:57 +02:00
|
|
|
|
2006-09-12 13:51:02 +02:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|