phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] phpgwapi/inc/class.jscalendar.inc.php, 1.5


From: nomail
Subject: [Phpgroupware-cvs] phpgwapi/inc/class.jscalendar.inc.php, 1.5
Date: Thu, 27 May 2004 04:25:50 +0200

Update of /phpgwapi/inc
Added Files:
        Branch: 
          class.jscalendar.inc.php

date: 2004/05/27 02:25:50;  author: skwashd;  state: Exp;  lines: +304 -53

Log Message:
reimport, merged from 16, with upstream update and some new features
=====================================================================
<?php
        /**
        * jsCalendar wrapper-class
        *
        * @author Ralf Becker <address@hidden>
        * @author Dave Hall dave.hall at mbox.com.au
        * @copyright Copyright (C) 2003,2004 Free Software Foundation, Inc. 
http://www.fsf.org/
        * @license http://www.fsf.org/licenses/gpl.html GNU General Public 
License
        * @package phpgwapi
        * @subpackage gui
        * @version $Id: class.jscalendar.inc.php,v 1.5 2004/05/27 02:25:50 
skwashd Exp $
        */

        /**
        * jsCalendar wrapper-class
        *
        * @package phpgwapi
        * @subpackage gui
        * @internal The constructor load the necessary javascript-files
        */
class jscalendar
{
        
        /**
        * @var array $hilite dates to be hilited
        * @internal currently only implemented in flat - skwashd
        */
        var $hilite;

        /**
        * @var bool $hilite_today should today be hilighted?
        * @internal currenly only implemented in flat - skwashd
        */
        var $hilite_today = true;

        /**
        * @var string $onchange url to use when a month/year is changed
        * @internal currently only implemented in flat - skwashd
        */
        var $onchange;

        /**
        * @var string $onclick url to use when a date is clicked
        * @internal currently only implemented in flat - skwashd
        */
        var $onclick;

        /**
        * @var array $hilite dates to be show tooltips
        * @internal currently only implemented in flat - skwashd
        */
        var $tooltips;
        
        /**
        * @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, popup, flat
        */
        function jscalendar($do_header=True)
        {
                if(!is_object($GLOBALS['phpgw']->js))
                {
                        $GLOBALS['phpgw']->js = 
createObject('phpgwapi.javascript');
                }
                $GLOBALS['phpgw']->js->validate_file('jscalendar', 'calendar');
                $this->phpgw_js_url = 
$GLOBALS['phpgw_info']['server']['webserver_url'].'/phpgwapi/js';
                $this->dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];

                if ($do_header && 
!strstr($GLOBALS['phpgw_info']['flags']['css'],'jscalendar'))
                {
                        $GLOBALS['phpgw_info']['flags']['css'] .= "\n   
.holiday: {border: 1px solid #ff00; background-color:#ff0000; 
font-weight:bold;}\n"
                                . "-->\n</style>\n"
                                . '<link rel="stylesheet" type="text/css" 
media="all" href="'
                                . $this->phpgw_js_url
                                . '/jscalendar/calendar-blue.css" title="jscal" 
/>'
                                . "\n<style type=\"text/css\">\n<!--\n";

                        $GLOBALS['phpgw_info']['flags']['java_script'] .= "\n"
                                . '<script type="text/javascript" src="'
                                . 
$GLOBALS['phpgw']->link('/phpgwapi/js/jscalendar/jscalendar-setup.php')
                                ."\">\n</script>\n";
                }
        }

        /**
        * @function flat
        * @syntax input( $name,$date,$year=0,$month=0,$day=0 )
        * @author ralfbecker
        * @abstract creates a &quot;flat&quot; jsclanedar
        * @param $name name and id of the span in which the jscal sits
        * @param $date date as string or unix timestamp (in users localtime)
        * @param $year,$month,$day if $date is not used
        * @return string required js &amp; html
        */
        function flat($name,$date,$year=0,$month=0,$day=0)
        {
                //echo "<p>jscalendar::input(name='$name', 
date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', 
day='$day')</p>\n";

                if ($date && (is_int($date) || is_numeric($date)))
                {
                        $year  = 
intval($GLOBALS['phpgw']->common->show_date($date,'Y'));
                        $month = 
intval($GLOBALS['phpgw']->common->show_date($date,'n'));
                        $day   = 
intval($GLOBALS['phpgw']->common->show_date($date,'d'));
                }
                if ($year && $month && $day)
                {
                        $date = mktime(12,0,0,$month,$day,$year);
                }
                return
                '<div id="'.$name.'-container" style="padding 5px;"></div>
                <script type="text/javascript">
                '.$this->get_hilite($name).'
                '.$this->get_tooltips($name).'
                '.$this->get_changed().'
                
                ' . $name . 'Cal = new Calendar(1, new Date(' . $year . ',' . 
($month - 1) . ',' . $day . '), dateChanged);
                ' . ( (is_array($this->hilite )) ? $name . 
'Cal.setDateStatusHandler(' . $name . 'GetStatus);' : '' ) . '
                ' . ( (is_array($this->tooltips )) ? $name . 
'Cal.setDateToolTipHandler(' . $name . 'GetToolTip);' : '' ) . '
                ' . (!$this->hilite_today ? $name.'Cal["hiliteToday"] = false;' 
: '') .'
                ' . $name . 'Cal.create(document.getElementById("' . $name . 
'-container"));
                ' . $name . 'Cal.show(); 

                </script>' . "\n";
        }

        /**
        * function get_changed()
        * @returns string js code for handling date changes
        */
        function get_changed()
        {
                return 'function dateChanged(calendar){' . "\n"
                        . '     var y = calendar.date.getFullYear();' . "\n"
                        . '     var m = calendar.date.getMonth()+1;' . "\n"
                        . '     var d = calendar.date.getDate();' . "\n"
                        . '     if(m < 10) m = "0" + m;' . "\n"
                        . '     if(d < 10) d = "0" + d;' . "\n\n"
                        
                        . '     if (calendar.dateClicked) {' . "\n"
                        . '             window.location = "' . $this->onClick . 
'&date="+y+m+d;' . "\n"
                        . '     }else{' . "\n"
                        . '             window.location = "' . $this->onChange 
. '&date="+y+m+d;' . "\n"
                        . '     }' . "\n"
                        . '}' . "\n";
        }

        /**
        * @function get_hilite()
        * returns a js formatted array of dates which should be hilited and 
supporting code
        * @internal only used by flat atm - skwashd
        * @param string $prefix prepended to function and array name
        * @returns string js array
        */
        function get_hilite($prefix)
        {
                $str = '';
                if(is_array($this->hilite))
                {
                        //echo '<pre>' . $suffix . ' hilite: '; 
print_r($this->hilite); echo '</pre>';
                        $str = 'var ' . $prefix . 'Hilite = new Array();' . 
"\n";
                        foreach($this->hilite as $m => $ds)
                        {
                                $str .= $prefix . "Hilite[$m-1] = new 
Array();\n";
                                foreach($ds as $d => $val)
                                {
                                        $str .= $prefix . "Hilite[$m-1][$d] = 
'$val';\n";
                                }
                        }
                }
                $str .= "\n\n"
                        . 'function '. $prefix.'GetStatus(ign, y, m, d){' . "\n"
                        . '     if(!' . $prefix . 'Hilite[m]) return false;' . 
"\n\n"
                        . '     if(!' . $prefix . 'Hilite[m][d]){' . "\n"
                        . '             return false;' . "\n"
                        . '     }else{' . "\n"
                        . '             return ' . $prefix . 'Hilite[m][d];' . 
"\n"
                        . '     }' . "\n"
                        . "}\n";
                        
                return $str . "\n";
        }

        /**
        * @function get_tooltips()
        * returns a js formatted array of dates which should have tooltips and 
supporting code
        * @internal only used by flat atm - skwashd
        * @param string $prefix prepended to function and array name
        * @returns string js array
        */
        function get_tooltips($prefix)
        {
                $str = '';
                if(is_array($this->tooltips))
                {
                        //echo '<pre>' . $suffix . ' hilite: '; 
print_r($this->hilite); echo '</pre>';
                        $str = 'var ' . $prefix . 'ToolTips = new Array();' . 
"\n";
                        foreach($this->tooltips as $m => $ds)
                        {
                                $str .= $prefix . "ToolTips[$m-1] = new 
Array();\n";
                                foreach($ds as $d => $val)
                                {
                                        $str .= $prefix . "ToolTips[$m-1][$d] = 
\"".$val."\";\n";
                                }
                        }
                }
                $str .= "\n\n"
                        . 'function '. $prefix.'GetToolTip(ign, y, m, d){' . 
"\n"
                        . '     if(!' . $prefix . 'ToolTips[m]) return false;' 
. "\n\n"
                        . '     if(!' . $prefix . 'ToolTips[m][d]){' . "\n"
                        . '             return false;' . "\n"
                        . '     }else{' . "\n"
                        . '             return ' . $prefix . 'ToolTips[m][d];' 
. "\n"
                        . '     }' . "\n"
                        . "}\n";
                        
                return $str . "\n";
        }

        /**
        * @function input
        * @syntax input( $name,$date,$year=0,$month=0,$day=0, $helpmsg='', 
$options='')
        * @author skwashd
        * @abstract wrapper for the new popup method
        * @deprecated
        * @see popup
        * @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 users localtime)
        * @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
        * @return string required js &amp; html
        */
        function 
input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='')
        {
                return 
$this->popup($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='');
        }

        /**
        * @function popup
        * @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 users localtime)
        * @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 
popup($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='')
        {
                //echo "<p>jscalendar::input(name='$name', 
date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', 
day='$day')</p>\n";

                if ($date && (is_int($date) || is_numeric($date)))
                {
                        $year  = 
intval($GLOBALS['phpgw']->common->show_date($date,'Y'));
                        $month = 
intval($GLOBALS['phpgw']->common->show_date($date,'n'));
                        $day   = 
intval($GLOBALS['phpgw']->common->show_date($date,'d'));
                }
                if ($year && $month && $day)
                {
                        $date = 
date($this->dateformat,mktime(12,0,0,$month,$day,$year));
                }
                if ($helpmsg !== '')
                {
                        $options .= " 
onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" .
                                " onBlur=\"self.status=''; return true;\"";
                }
                return
                '<input type="text" id="'.$name.'" name="'.$name.'" size="12" 
value="'.$date.'"'.$options.'/>
                <script type="text/javascript">
                document.writeln(\'<img id="'.$name.'-trigger" 
src="'.$GLOBALS['phpgw']->common->find_image('phpgwapi','cal').'" 
title="'.lang('Select date').'" style="cursor:pointer; cursor:hand;"/>\');
                Calendar.setup(
                        {
                                inputField  : "'.$name.'",
                                button      : "'.$name.'-trigger"
                        }
                );
                </script>
                ';
        }

        /*!
        @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')
        {
                if ($datestr === '')
                {
                        return False;
                }
                $fields = split('[./-]',$datestr);
                foreach(split('[./-]',$this->dateformat) as $n => $field)
                {
                        $date[$field] = intval($fields[$n]);
                        if($field == 'M')
                        {
                                for($i=1; $i <=12; $i++)
                                {
                                        if(date('M',mktime(0,0,0,$i,1,2000)) == 
$fields[$n])
                                        {
                                                $date['m'] = $i;
                                        }
                                }
                        }
                }
                $ret = array(
                        $year  => $date['Y'],
                        $month => $date['m'],
                        $day   => $date['d']
                );
                if ($raw)
                {
                        $ret[$raw] = 
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";

                return $ret;
        }

        /**
        * function set_hilited_dates($dates)
        * highlights dates in the calendar
        * @param array $dates the dates to hilite in the following format 
[m][d] = class
        */
        function set_hilited_dates($dates)
        {
                $this->hilite = $dates;
        }

        /**
        * function set_onchange_link($url, $params)
        * generates a base link for use in the dateChanged call back method of 
the jscal - for when the month is changed
        * works the same as the phpgw->link method
        * @see phpgw::link
        * @param string $url a url relative to the base phpgw install
        * @param array $params the additional paramaters (without the date 
field) 
        */
        function set_onchange_link($url,$params)
        {
                $this->onChange = $GLOBALS['phpgw']->link($url, $params);
        }

        /**
        * function set_onclick_link($url, $params)
        * generates a base link for use in the dateChanged call back method of 
the jscal
        * works the same as the phpgw->link method
        * @see phpgw::link
        * @param string $url a url relative to the base phpgw install
        * @param array $params the additional paramaters (without the date 
field) 
        */
        function set_onclick_link($url, $params)
        {
                $this->onClick = $GLOBALS['phpgw']->link($url, $params);
        }

        /**
        * function set_tooltips($dates)
        * shows tooltips for specified dates in the calendar
        * @param array $dates the dates to show tooltips for in the following 
format [m][d] = 'tooltip text'
        */
        function set_tooltips($dates)
        {
                $this->tooltips = $dates;
        }
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]