source: trunk/phpgwapi/inc/class.jscalendar.inc.php @ 5934

Revision 5934, 7.7 KB checked in by marcosw, 12 years ago (diff)

Ticket #2398 - Compatibilizacao com PHP-5.3 em alguns módulos do expresso

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - API jsCalendar wrapper-class                                *
4  * http://www.eGroupWare.org                                                *
5  * Written by Ralf Becker <RalfBecker@outdoor-training.de>                  *
6  * --------------------------------------------                             *
7  *  This program is free software; you can redistribute it and/or modify it *
8  *  under the terms of the GNU General Public License as published by the   *
9  *  Free Software Foundation; either version 2 of the License, or (at your  *
10  *  option) any later version.                                              *
11  \**************************************************************************/
12
13
14        /*!
15        @class jscalendar
16        @author ralfbecker
17        @abstract wrapper for the jsCalendar
18        @discussion 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,$path='jscalendar')
30                {
31                        $this->jscalendar_url = $GLOBALS['phpgw_info']['server']['webserver_url'].'/phpgwapi/js/'.$path;
32                        $this->dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
33                       
34                        if ($do_header && !strpos($GLOBALS['phpgw_info']['flags']['java_script'],'jscalendar'))
35                        {
36                                $GLOBALS['phpgw_info']['flags']['java_script'] .=
37'<link rel="stylesheet" type="text/css" media="all" href="'.$this->jscalendar_url.'/calendar-win2k-cold-1.css" title="" />
38<script type="text/javascript" src="'.$this->jscalendar_url.'/calendar.js"></script>
39<script type="text/javascript" src="'.preg_replace('/[?&]*click_history=[0-9a-f]*/','',$GLOBALS['phpgw']->link('/phpgwapi/inc/jscalendar-setup.php')).'"></script>
40';
41                        }
42                }
43
44                /*!
45                @function input
46                @syntax input( $name,$date,$year=0,$month=0,$day=0 )
47                @author ralfbecker
48                @abstract creates an inputfield for the jscalendar (returns the necessary html and js)
49                @param $name name and id of the input-field (it also names the id of the img $name.'-toggle')
50                @param $date date as string or unix timestamp (in server timezone)
51                @param $year,$month,$day if $date is not used
52                @param $helpmsg a helpmessage for the statusline of the browser
53                @param $options any other options to the inputfield
54                */
55                function input($name,$date,$year=0,$month=0,$day=0,$helpmsg='',$options='')
56                {
57                        //echo "<p>jscalendar::input(name='$name', date='$date'='".date('Y-m-d',$date)."', year='$year', month='$month', day='$day')</p>\n";
58
59                        if ($date && (is_int($date) || is_numeric($date)))
60                        {
61                                $year  = (int)adodb_date('Y',$date);
62                                $month = (int)adodb_date('n',$date);
63                                $day   = (int)adodb_date('d',$date);
64                        }
65                        if ($year && $month && $day)
66                        {
67                                $date = adodb_date($this->dateformat,$ts = adodb_mktime(12,0,0,$month,$day,$year));
68                                if (strpos($this->dateformat,'M') !== False)
69                                {
70                                        $short = lang(adodb_date('M',$ts));     // check if we have a translation of the short-cut
71                                        if (substr($short,-1) == '*')   // if not generate one by truncating the translation of the long name
72                                        {
73                                                $short = substr(lang(adodb_date('F',$ts)),0,(int) lang('3 number of chars for month-shortcut'));
74                                        }
75                                        $date = str_replace(adodb_date('M',$ts),$short,$date);
76                                }
77                        }
78                        if ($helpmsg !== '')
79                        {
80                                $options .= " onFocus=\"self.status='".addslashes($helpmsg)."'; return true;\"" .
81                                " onBlur=\"self.status=''; return true;\"";
82                        }
83                        return
84'<input type="text" id="'.$name.'" name="'.$name.'" size="10" value="'.$date.'"'.$options.'/>
85<script type="text/javascript">
86        document.writeln(\'<img id="'.$name.'-trigger" src="'.$GLOBALS['phpgw']->common->find_image('phpgwpai','datepopup').'" title="'.lang('Select date').'" style="cursor:pointer; cursor:hand;"/>\');
87        Calendar.setup(
88        {
89                inputField  : "'.$name.'",
90                button      : "'.$name.'-trigger"
91        }
92        );
93</script>
94';
95                }
96
97                function flat($url,$date=False,$weekUrl=False,$weekTTip=False,$monthUrl=False,$monthTTip=False,$id='calendar-container')
98                {
99                        if ($date)      // string if format YYYYmmdd or timestamp
100                        {
101                                $date = is_int($date) ? adodb_date('m/d/Y',$date) :
102                                        substr($date,4,2).'/'.substr($date,6,2).'/'.substr($date,0,4);
103                        }
104                        return '
105<div id="'.$id.'"></div>
106
107<script type="text/javascript">
108  function dateChanged(calendar) {
109'.  // Beware that this function is called even if the end-user only
110    // changed the month/year.  In order to determine if a date was
111    // clicked you can use the dateClicked property of the calendar:
112    // redirect to $url extended with a &date=YYYYMMDD
113'    if (calendar.dateClicked) {
114     window.location = "'.$url.'&date=" + calendar.date.print("%Y%m%d");
115    }
116  };
117'.($weekUrl ? '
118  function weekClicked(calendar,weekstart) {
119     window.location = "'.$weekUrl.'&date=" + weekstart.print("%Y%m%d");
120  }
121' : '').($monthUrl ? '
122  function monthClicked(calendar,monthstart) {
123     window.location = "'.$monthUrl.'&date=" + monthstart.print("%Y%m%d");
124  }
125' : '').'
126  Calendar.setup(
127    {
128      flat         : "'.$id.'",
129      flatCallback : dateChanged'.($weekUrl ? ',
130      flatWeekCallback : weekClicked' : '').($weekTTip ? ',
131      flatWeekTTip : "'.addslashes($weekTTip).'"' : '').($monthUrl ? ',
132      flatMonthCallback : monthClicked' : '').($monthTTip ? ',
133      flatMonthTTip : "'.addslashes($monthTTip).'"' : '').($date ? ',
134      date : "'.$date.'"
135' : '').'    }
136  );
137</script>';
138                }
139
140                /*!
141                @function input2date
142                @syntax input2date( $datestr,$raw='raw',$day='day',$month='month',$year='year' )
143                @author ralfbecker
144                @abstract converts the date-string back to an array with year, month, day and a timestamp
145                @param $datestr content of the inputfield generated by jscalendar::input()
146                @param $raw key of the timestamp-field in the returned array or False of no timestamp
147                @param $day,$month,$year keys for the array, eg. to set mday instead of day
148                */
149                function input2date($datestr,$raw='raw',$day='day',$month='month',$year='year')
150                {
151                        //echo "<p>jscalendar::input2date('$datestr') ".print_r($fields,True)."</p>\n";
152                        if ($datestr === '')
153                        {
154                                return False;
155                        }
156                        $fields = split('[./-]',$datestr);
157                        foreach(split('[./-]',$this->dateformat) as $n => $field)
158                        {
159                                if ($field == 'M')
160                                {
161                                        if (!is_numeric($fields[$n]))
162                                        {
163                                                $partcial_match = 0;
164                                                for($i = 1; $i <= 12; $i++)
165                                                {
166                                                        $long_name  = lang(adodb_date('F',mktime(12,0,0,$i,1,2000)));
167                                                        $short_name = lang(adodb_date('M',mktime(12,0,0,$i,1,2000)));   // do we have a translation of the short-cut
168                                                        if (substr($short_name,-1) == '*')      // if not generate one by truncating the translation of the long name
169                                                        {
170                                                                $short_name = substr($long_name,0,(int) lang('3 number of chars for month-shortcut'));
171                                                        }
172                                                        //echo "<br>checking '".$fields[$n]."' against '$long_name' or '$short_name'";
173                                                        if ($fields[$n] == $long_name || $fields[$n] == $short_name)
174                                                        {
175                                                                //echo " ==> OK<br>";
176                                                                $fields[$n] = $i;
177                                                                break;
178                                                        }
179                                                        if (strstr($long_name,$fields[$n]) == $long_name)       // partcial match => multibyte saver
180                                                        {
181                                                                $partcial_match = $i;
182                                                        }
183                                                }
184                                                if ($i > 12 && $partcial_match) // nothing found, but a partcial match
185                                                {
186                                                        $fields[$n] = $partcial_match;
187                                                }
188                                        }
189                                        $field = 'm';
190                                }
191                                $date[$field] = (int)$fields[$n];
192                        }
193                        $ret = array(
194                                $year  => $date['Y'],
195                                $month => $date['m'],
196                                $day   => $date['d']
197                        );
198                        if ($raw)
199                        {
200                                $ret[$raw] = adodb_mktime(12,0,0,$date['m'],$date['d'],$date['Y']);
201                        }
202                        //echo "<p>jscalendar::input2date('$datestr','$raw',$day','$month','$year') = "; print_r($ret); echo "</p>\n";
203
204                        return $ret;
205                }
206        }
Note: See TracBrowser for help on using the repository browser.