upgrade to codeigniter 1.7.2 for f12
[www-register-wizard.git] / libraries / Calendar.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 4.3.2 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2009, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * CodeIgniter Calendar Class
20  *
21  * This class enables the creation of calendars
22  *
23  * @package             CodeIgniter
24  * @subpackage  Libraries
25  * @category    Libraries
26  * @author              ExpressionEngine Dev Team
27  * @link                http://codeigniter.com/user_guide/libraries/calendar.html
28  */
29 class CI_Calendar {
30
31         var $CI;
32         var $lang;
33         var $local_time;
34         var $template           = '';
35         var $start_day          = 'sunday';
36         var $month_type         = 'long';
37         var $day_type           = 'abr';
38         var $show_next_prev     = FALSE;
39         var $next_prev_url      = '';
40
41         /**
42          * Constructor
43          *
44          * Loads the calendar language file and sets the default time reference
45          *
46          * @access      public
47          */
48         function CI_Calendar($config = array())
49         {               
50                 $this->CI =& get_instance();
51                 
52                 if ( ! in_array('calendar_lang'.EXT, $this->CI->lang->is_loaded, TRUE))
53                 {
54                         $this->CI->lang->load('calendar');
55                 }
56
57                 $this->local_time = time();
58                 
59                 if (count($config) > 0)
60                 {
61                         $this->initialize($config);
62                 }
63                 
64                 log_message('debug', "Calendar Class Initialized");
65         }
66         
67         // --------------------------------------------------------------------
68         
69         /**
70          * Initialize the user preferences
71          *
72          * Accepts an associative array as input, containing display preferences
73          *
74          * @access      public
75          * @param       array   config preferences
76          * @return      void
77          */     
78         function initialize($config = array())
79         {
80                 foreach ($config as $key => $val)
81                 {
82                         if (isset($this->$key))
83                         {
84                                 $this->$key = $val;
85                         }
86                 }
87         }
88         
89         // --------------------------------------------------------------------
90
91         /**
92          * Generate the calendar
93          *
94          * @access      public
95          * @param       integer the year
96          * @param       integer the month
97          * @param       array   the data to be shown in the calendar cells
98          * @return      string
99          */
100         function generate($year = '', $month = '', $data = array())
101         {
102                 // Set and validate the supplied month/year
103                 if ($year == '')
104                         $year  = date("Y", $this->local_time);
105                         
106                 if ($month == '')
107                         $month = date("m", $this->local_time);
108                         
109                 if (strlen($year) == 1)
110                         $year = '200'.$year;
111                 
112                 if (strlen($year) == 2)
113                         $year = '20'.$year;
114
115                 if (strlen($month) == 1)
116                         $month = '0'.$month;
117                 
118                 $adjusted_date = $this->adjust_date($month, $year);
119                 
120                 $month  = $adjusted_date['month'];
121                 $year   = $adjusted_date['year'];
122                 
123                 // Determine the total days in the month
124                 $total_days = $this->get_total_days($month, $year);
125                                                 
126                 // Set the starting day of the week
127                 $start_days     = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
128                 $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
129                 
130                 // Set the starting day number
131                 $local_date = mktime(12, 0, 0, $month, 1, $year);
132                 $date = getdate($local_date);
133                 $day  = $start_day + 1 - $date["wday"];
134                 
135                 while ($day > 1)
136                 {
137                         $day -= 7;
138                 }
139                 
140                 // Set the current month/year/day
141                 // We use this to determine the "today" date
142                 $cur_year       = date("Y", $this->local_time);
143                 $cur_month      = date("m", $this->local_time);
144                 $cur_day        = date("j", $this->local_time);
145                 
146                 $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
147         
148                 // Generate the template data array
149                 $this->parse_template();
150         
151                 // Begin building the calendar output                                           
152                 $out = $this->temp['table_open'];
153                 $out .= "\n";   
154
155                 $out .= "\n";           
156                 $out .= $this->temp['heading_row_start'];
157                 $out .= "\n";
158                 
159                 // "previous" month link
160                 if ($this->show_next_prev == TRUE)
161                 {
162                         // Add a trailing slash to the  URL if needed
163                         $this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/",  $this->next_prev_url);
164                 
165                         $adjusted_date = $this->adjust_date($month - 1, $year);
166                         $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
167                         $out .= "\n";
168                 }
169
170                 // Heading containing the month/year
171                 $colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
172                 
173                 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
174                 $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)."&nbsp;".$year, $this->temp['heading_title_cell']);
175                 
176                 $out .= $this->temp['heading_title_cell'];
177                 $out .= "\n";
178
179                 // "next" month link
180                 if ($this->show_next_prev == TRUE)
181                 {               
182                         $adjusted_date = $this->adjust_date($month + 1, $year);
183                         $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
184                 }
185
186                 $out .= "\n";           
187                 $out .= $this->temp['heading_row_end'];
188                 $out .= "\n";
189
190                 // Write the cells containing the days of the week
191                 $out .= "\n";   
192                 $out .= $this->temp['week_row_start'];
193                 $out .= "\n";
194
195                 $day_names = $this->get_day_names();
196
197                 for ($i = 0; $i < 7; $i ++)
198                 {
199                         $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
200                 }
201
202                 $out .= "\n";
203                 $out .= $this->temp['week_row_end'];
204                 $out .= "\n";
205
206                 // Build the main body of the calendar
207                 while ($day <= $total_days)
208                 {
209                         $out .= "\n";
210                         $out .= $this->temp['cal_row_start'];
211                         $out .= "\n";
212
213                         for ($i = 0; $i < 7; $i++)
214                         {
215                                 $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
216                         
217                                 if ($day > 0 AND $day <= $total_days)
218                                 {                                       
219                                         if (isset($data[$day]))
220                                         {       
221                                                 // Cells with content
222                                                 $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
223                                                 $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
224                                         }
225                                         else
226                                         {
227                                                 // Cells with no content
228                                                 $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
229                                                 $out .= str_replace('{day}', $day, $temp);
230                                         }
231                                 }
232                                 else
233                                 {
234                                         // Blank cells
235                                         $out .= $this->temp['cal_cell_blank'];
236                                 }
237                                 
238                                 $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];                                             
239                                 $day++;
240                         }
241                         
242                         $out .= "\n";           
243                         $out .= $this->temp['cal_row_end'];
244                         $out .= "\n";           
245                 }
246
247                 $out .= "\n";           
248                 $out .= $this->temp['table_close'];
249
250                 return $out;
251         }
252         
253         // --------------------------------------------------------------------
254
255         /**
256          * Get Month Name
257          *
258          * Generates a textual month name based on the numeric
259          * month provided.
260          *
261          * @access      public
262          * @param       integer the month
263          * @return      string
264          */
265         function get_month_name($month)
266         {
267                 if ($this->month_type == 'short')
268                 {
269                         $month_names = array('01' => 'cal_jan', '02' => 'cal_feb', '03' => 'cal_mar', '04' => 'cal_apr', '05' => 'cal_may', '06' => 'cal_jun', '07' => 'cal_jul', '08' => 'cal_aug', '09' => 'cal_sep', '10' => 'cal_oct', '11' => 'cal_nov', '12' => 'cal_dec');
270                 }
271                 else
272                 {
273                         $month_names = array('01' => 'cal_january', '02' => 'cal_february', '03' => 'cal_march', '04' => 'cal_april', '05' => 'cal_mayl', '06' => 'cal_june', '07' => 'cal_july', '08' => 'cal_august', '09' => 'cal_september', '10' => 'cal_october', '11' => 'cal_november', '12' => 'cal_december');
274                 }
275                 
276                 $month = $month_names[$month];
277                 
278                 if ($this->CI->lang->line($month) === FALSE)
279                 {
280                         return ucfirst(str_replace('cal_', '', $month));
281                 }
282
283                 return $this->CI->lang->line($month);
284         }
285         
286         // --------------------------------------------------------------------
287
288         /**
289          * Get Day Names
290          *
291          * Returns an array of day names (Sunday, Monday, etc.) based
292          * on the type.  Options: long, short, abrev
293          *
294          * @access      public
295          * @param       string
296          * @return      array
297          */
298         function get_day_names($day_type = '')
299         {
300                 if ($day_type != '')
301                         $this->day_type = $day_type;
302         
303                 if ($this->day_type == 'long')
304                 {
305                         $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
306                 }
307                 elseif ($this->day_type == 'short')
308                 {
309                         $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
310                 }
311                 else
312                 {
313                         $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
314                 }
315         
316                 $days = array();
317                 foreach ($day_names as $val)
318                 {                       
319                         $days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val);
320                 }
321         
322                 return $days;
323         }
324         
325         // --------------------------------------------------------------------
326
327         /**
328          * Adjust Date
329          *
330          * This function makes sure that we have a valid month/year.
331          * For example, if you submit 13 as the month, the year will
332          * increment and the month will become January.
333          *
334          * @access      public
335          * @param       integer the month
336          * @param       integer the year
337          * @return      array
338          */
339         function adjust_date($month, $year)
340         {
341                 $date = array();
342
343                 $date['month']  = $month;
344                 $date['year']   = $year;
345
346                 while ($date['month'] > 12)
347                 {
348                         $date['month'] -= 12;
349                         $date['year']++;
350                 }
351
352                 while ($date['month'] <= 0)
353                 {
354                         $date['month'] += 12;
355                         $date['year']--;
356                 }
357
358                 if (strlen($date['month']) == 1)
359                 {
360                         $date['month'] = '0'.$date['month'];
361                 }
362
363                 return $date;
364         }
365         
366         // --------------------------------------------------------------------
367
368         /**
369          * Total days in a given month
370          *
371          * @access      public
372          * @param       integer the month
373          * @param       integer the year
374          * @return      integer
375          */
376         function get_total_days($month, $year)
377         {
378                 $days_in_month  = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
379
380                 if ($month < 1 OR $month > 12)
381                 {
382                         return 0;
383                 }
384
385                 // Is the year a leap year?
386                 if ($month == 2)
387                 {
388                         if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
389                         {
390                                 return 29;
391                         }
392                 }
393
394                 return $days_in_month[$month - 1];
395         }
396         
397         // --------------------------------------------------------------------
398
399         /**
400          * Set Default Template Data
401          *
402          * This is used in the event that the user has not created their own template
403          *
404          * @access      public
405          * @return array
406          */
407         function default_template()
408         {
409                 return  array (
410                                                 'table_open'                            => '<table border="0" cellpadding="4" cellspacing="0">',
411                                                 'heading_row_start'             => '<tr>',
412                                                 'heading_previous_cell'         => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
413                                                 'heading_title_cell'            => '<th colspan="{colspan}">{heading}</th>',
414                                                 'heading_next_cell'             => '<th><a href="{next_url}">&gt;&gt;</a></th>',
415                                                 'heading_row_end'                       => '</tr>',
416                                                 'week_row_start'                        => '<tr>',
417                                                 'week_day_cell'                         => '<td>{week_day}</td>',
418                                                 'week_row_end'                          => '</tr>',
419                                                 'cal_row_start'                         => '<tr>',
420                                                 'cal_cell_start'                        => '<td>',
421                                                 'cal_cell_start_today'          => '<td>',
422                                                 'cal_cell_content'                      => '<a href="{content}">{day}</a>',
423                                                 'cal_cell_content_today'        => '<a href="{content}"><strong>{day}</strong></a>',
424                                                 'cal_cell_no_content'           => '{day}',
425                                                 'cal_cell_no_content_today'     => '<strong>{day}</strong>',
426                                                 'cal_cell_blank'                        => '&nbsp;',
427                                                 'cal_cell_end'                          => '</td>',
428                                                 'cal_cell_end_today'            => '</td>',
429                                                 'cal_row_end'                           => '</tr>',
430                                                 'table_close'                           => '</table>'
431                                         );      
432         }
433         
434         // --------------------------------------------------------------------
435
436         /**
437          * Parse Template
438          *
439          * Harvests the data within the template {pseudo-variables}
440          * used to display the calendar
441          *
442          * @access      public
443          * @return      void
444          */
445         function parse_template()
446         {
447                 $this->temp = $this->default_template();
448         
449                 if ($this->template == '')
450                 {
451                         return;
452                 }
453                 
454                 $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
455                 
456                 foreach (array('table_open', 'table_close', 'heading_row_start', 'heading_previous_cell', 'heading_title_cell', 'heading_next_cell', 'heading_row_end', 'week_row_start', 'week_day_cell', 'week_row_end', 'cal_row_start', 'cal_cell_start', 'cal_cell_content', 'cal_cell_no_content',  'cal_cell_blank', 'cal_cell_end', 'cal_row_end', 'cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today') as $val)
457                 {
458                         if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match))
459                         {
460                                 $this->temp[$val] = $match['1'];
461                         }
462                         else
463                         {
464                                 if (in_array($val, $today, TRUE))
465                                 {
466                                         $this->temp[$val] = $this->temp[str_replace('_today', '', $val)];
467                                 }
468                         }
469                 }       
470         }
471
472 }
473
474 // END CI_Calendar class
475
476 /* End of file Calendar.php */
477 /* Location: ./system/libraries/Calendar.php */