Made additional changes to allow the import to work and do the right
[eq/.git] / inc / class.jscalendar.inc.php
1 <?php
2         /**
3         * jsCalendar wrapper-class
4         *
5         * @author Ralf Becker <RalfBecker@outdoor-training.de>
6         * @copyright Copyright (C) 2003,2004 Free Software Foundation, Inc. http://www.fsf.org/
7         * @license http://www.fsf.org/licenses/gpl.html GNU General Public License
8         * @package phpgwapi
9         * @subpackage gui
10         * @version $Id: class.jscalendar.inc.php,v 1.1.2.12 2004/05/01 09:24:53 skwashd Exp $
11         */
12
13         /**
14         * jsCalendar wrapper-class
15         *
16         * @package phpgwapi
17         * @subpackage gui
18         * @internal The constructor load the necessary javascript-files
19         */
20 class jscalendar
21 {
22         /*!
23         @function jscalendar
24         @syntax jscalendar( $do_header=True )
25         @author ralfbecker
26         @abstract constructor of the class
27         @param $do_header if true, necessary javascript and css gets loaded, only needed for input
28         */
29         function jscalendar($do_header=True)
30         {
31                 if(!is_object($GLOBALS['phpgw']->js))
32                 {
33                         $GLOBALS['phpgw']->js = createObject('phpgwapi.javascript');
34                 }
35                 $GLOBALS['phpgw']->js->validate_file('jscalendar', 'calendar');
36                 $this->phpgw_js_url = $GLOBALS['phpgw_info']['server']['webserver_url'].'/phpgwapi/js';
37                 $this->dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
38
39                 if ($do_header && !strstr($GLOBALS['phpgw_info']['flags']['css'],'jscalendar'))
40                 {
41                         $GLOBALS['phpgw_info']['flags']['css'] .= "-->\n</style>\n"
42                                 . '<link rel="stylesheet" type="text/css" media="all" href="'
43                                 . $this->phpgw_js_url
44                                 . '/jscalendar/calendar-win2k-cold-1.css" title="win2k-cold-1" />'
45                                 . "\n<style type=\"text/css\">\n<!--\n";
46
47                         $GLOBALS['phpgw_info']['flags']['java_script'] .= "\n"
48                                 . '<script type="text/javascript" src="'
49                                 . $GLOBALS['phpgw']->link('/phpgwapi/js/jscalendar/jscalendar-setup.php')
50                                 ."\">\n</script>\n";
51                 }
52         }
53
54         /*!
55         @function input
56         @syntax input( $name,$date,$year=0,$month=0,$day=0 )
57         @author ralfbecker
58         @abstract creates an inputfield for the jscalendar (returns the necessary html and js)
59         @param $name name and id of the input-field (it also names the id of the img $name.'-toggle')
60         @param $date date as string or unix timestamp (in users localtime)
61         @param $year,$month,$day if $date is not used
62         @param $helpmsg a helpmessage for the statusline of the browser
63         @param $options any other options to the inputfield
64         @param $setup any other calendar setup options
65         */
66         function input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='',$setup='')
67         {
68                 //echo "<p>jscalendar::input(name='$name', date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', day='$day')</p>\n";
69
70                 if ($date && (is_int($date) || is_numeric($date)))
71                 {
72                         $year  = intval($GLOBALS['phpgw']->common->show_date($date,'Y'));
73                         $month = intval($GLOBALS['phpgw']->common->show_date($date,'n'));
74                         $day   = intval($GLOBALS['phpgw']->common->show_date($date,'d'));
75                 }
76                 if ($year && $month && $day)
77                 {
78                         $date = date($this->dateformat,mktime(12,0,0,$month,$day,$year));
79                 }
80                 if ($helpmsg !== '')
81                 {
82                         $options .= " onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" .
83                                 " onBlur=\"self.status=''; return true;\"";
84                 }
85                 return
86                 '<input type="text" id="'.$name.'" name="'.$name.'" size="12" value="'.$date.'"'.$options.'/>
87                 <script type="text/javascript">
88                 document.writeln(\'<img id="'.$name.'-trigger" src="'.$GLOBALS['phpgw']->common->find_image('phpgwapi','cal').'" title="'.lang('Select date').'" style="cursor:pointer; cursor:hand;"/>\');
89                 Calendar.setup(
90                         {
91                                 inputField  : "'.$name.'",
92                                 button      : "'.$name.'-trigger",
93                                 '.$setup.'
94                         }
95                 );
96                 </script>
97                 ';
98         }
99
100         /*!
101         @function input2date
102         @syntax input2date( $datestr,$raw='raw',$day='day',$month='month',$year='year' )
103         @author ralfbecker
104         @abstract converts the date-string back to an array with year, month, day and a timestamp
105         @param $datestr content of the inputfield generated by jscalendar::input()
106         @param $raw key of the timestamp-field in the returned array or False of no timestamp
107         @param $day,$month,$year keys for the array, eg. to set mday instead of day
108         */
109         function input2date($datestr,$raw='raw',$day='day',$month='month',$year='year')
110         {
111                 if ($datestr === '')
112                 {
113                         return False;
114                 }
115                 $fields = split('[./-]',$datestr);
116                 foreach(split('[./-]',$this->dateformat) as $n => $field)
117                 {
118                         $date[$field] = intval($fields[$n]);
119                         if($field == 'M')
120                         {
121                                 for($i=1; $i <=12; $i++)
122                                 {
123                                         if(date('M',mktime(0,0,0,$i,1,2000)) == $fields[$n])
124                                         {
125                                                 $date['m'] = $i;
126                                         }
127                                 }
128                         }
129                 }
130                 $ret = array(
131                         $year  => $date['Y'],
132                         $month => $date['m'],
133                         $day   => $date['d']
134                 );
135                 if ($raw)
136                 {
137                         $ret[$raw] = mktime(12,0,0,$date['m'],$date['d'],$date['Y']);
138                 }
139                 //echo "<p>jscalendar::input2date('$datestr','$raw',$day','$month','$year') = "; print_r($ret); echo "</p>\n";
140
141                 return $ret;
142         }
143 }