Calendar.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Calendar Class
00020  *
00021  * This class enables the creation of calendars
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    Libraries
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/calendar.html
00028  */
00029 class CI_Calendar {
00030 
00031         var $CI;
00032         var $lang;
00033         var $local_time;
00034         var $template           = '';
00035         var $start_day          = 'sunday';
00036         var $month_type         = 'long';
00037         var $day_type           = 'abr';
00038         var $show_next_prev     = FALSE;
00039         var $next_prev_url      = '';
00040 
00041         /**
00042          * Constructor
00043          *
00044          * Loads the calendar language file and sets the default time reference
00045          *
00046          * @access      public
00047          */
00048         function CI_Calendar($config = array())
00049         {               
00050                 $this->CI =& get_instance();
00051                 
00052                 if ( ! in_array('calendar_lang'.EXT, $this->CI->lang->is_loaded, TRUE))
00053                 {
00054                         $this->CI->lang->load('calendar');
00055                 }
00056 
00057                 $this->local_time = time();
00058                 
00059                 if (count($config) > 0)
00060                 {
00061                         $this->initialize($config);
00062                 }
00063                 
00064                 log_message('debug', "Calendar Class Initialized");
00065         }
00066         
00067         // --------------------------------------------------------------------
00068         
00069         /**
00070          * Initialize the user preferences
00071          *
00072          * Accepts an associative array as input, containing display preferences
00073          *
00074          * @access      public
00075          * @param       array   config preferences
00076          * @return      void
00077          */     
00078         function initialize($config = array())
00079         {
00080                 foreach ($config as $key => $val)
00081                 {
00082                         if (isset($this->$key))
00083                         {
00084                                 $this->$key = $val;
00085                         }
00086                 }
00087         }
00088         
00089         // --------------------------------------------------------------------
00090 
00091         /**
00092          * Generate the calendar
00093          *
00094          * @access      public
00095          * @param       integer the year
00096          * @param       integer the month
00097          * @param       array   the data to be shown in the calendar cells
00098          * @return      string
00099          */
00100         function generate($year = '', $month = '', $data = array())
00101         {
00102                 // Set and validate the supplied month/year
00103                 if ($year == '')
00104                         $year  = date("Y", $this->local_time);
00105                         
00106                 if ($month == '')
00107                         $month = date("m", $this->local_time);
00108                         
00109                 if (strlen($year) == 1)
00110                         $year = '200'.$year;
00111                 
00112                 if (strlen($year) == 2)
00113                         $year = '20'.$year;
00114 
00115                 if (strlen($month) == 1)
00116                         $month = '0'.$month;
00117                 
00118                 $adjusted_date = $this->adjust_date($month, $year);
00119                 
00120                 $month  = $adjusted_date['month'];
00121                 $year   = $adjusted_date['year'];
00122                 
00123                 // Determine the total days in the month
00124                 $total_days = $this->get_total_days($month, $year);
00125                                                 
00126                 // Set the starting day of the week
00127                 $start_days     = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
00128                 $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
00129                 
00130                 // Set the starting day number
00131                 $local_date = mktime(12, 0, 0, $month, 1, $year);
00132                 $date = getdate($local_date);
00133                 $day  = $start_day + 1 - $date["wday"];
00134                 
00135                 while ($day > 1)
00136                 {
00137                         $day -= 7;
00138                 }
00139                 
00140                 // Set the current month/year/day
00141                 // We use this to determine the "today" date
00142                 $cur_year       = date("Y", $this->local_time);
00143                 $cur_month      = date("m", $this->local_time);
00144                 $cur_day        = date("j", $this->local_time);
00145                 
00146                 $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
00147         
00148                 // Generate the template data array
00149                 $this->parse_template();
00150         
00151                 // Begin building the calendar output                                           
00152                 $out = $this->temp['table_open'];
00153                 $out .= "\n";   
00154 
00155                 $out .= "\n";           
00156                 $out .= $this->temp['heading_row_start'];
00157                 $out .= "\n";
00158                 
00159                 // "previous" month link
00160                 if ($this->show_next_prev == TRUE)
00161                 {
00162                         // Add a trailing slash to the  URL if needed
00163                         $this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/",  $this->next_prev_url);
00164                 
00165                         $adjusted_date = $this->adjust_date($month - 1, $year);
00166                         $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
00167                         $out .= "\n";
00168                 }
00169 
00170                 // Heading containing the month/year
00171                 $colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
00172                 
00173                 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
00174                 $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)."&nbsp;".$year, $this->temp['heading_title_cell']);
00175                 
00176                 $out .= $this->temp['heading_title_cell'];
00177                 $out .= "\n";
00178 
00179                 // "next" month link
00180                 if ($this->show_next_prev == TRUE)
00181                 {               
00182                         $adjusted_date = $this->adjust_date($month + 1, $year);
00183                         $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
00184                 }
00185 
00186                 $out .= "\n";           
00187                 $out .= $this->temp['heading_row_end'];
00188                 $out .= "\n";
00189 
00190                 // Write the cells containing the days of the week
00191                 $out .= "\n";   
00192                 $out .= $this->temp['week_row_start'];
00193                 $out .= "\n";
00194 
00195                 $day_names = $this->get_day_names();
00196 
00197                 for ($i = 0; $i < 7; $i ++)
00198                 {
00199                         $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
00200                 }
00201 
00202                 $out .= "\n";
00203                 $out .= $this->temp['week_row_end'];
00204                 $out .= "\n";
00205 
00206                 // Build the main body of the calendar
00207                 while ($day <= $total_days)
00208                 {
00209                         $out .= "\n";
00210                         $out .= $this->temp['cal_row_start'];
00211                         $out .= "\n";
00212 
00213                         for ($i = 0; $i < 7; $i++)
00214                         {
00215                                 $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
00216                         
00217                                 if ($day > 0 AND $day <= $total_days)
00218                                 {                                       
00219                                         if (isset($data[$day]))
00220                                         {       
00221                                                 // Cells with content
00222                                                 $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
00223                                                 $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
00224                                         }
00225                                         else
00226                                         {
00227                                                 // Cells with no content
00228                                                 $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
00229                                                 $out .= str_replace('{day}', $day, $temp);
00230                                         }
00231                                 }
00232                                 else
00233                                 {
00234                                         // Blank cells
00235                                         $out .= $this->temp['cal_cell_blank'];
00236                                 }
00237                                 
00238                                 $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];                                             
00239                                 $day++;
00240                         }
00241                         
00242                         $out .= "\n";           
00243                         $out .= $this->temp['cal_row_end'];
00244                         $out .= "\n";           
00245                 }
00246 
00247                 $out .= "\n";           
00248                 $out .= $this->temp['table_close'];
00249 
00250                 return $out;
00251         }
00252         
00253         // --------------------------------------------------------------------
00254 
00255         /**
00256          * Get Month Name
00257          *
00258          * Generates a textual month name based on the numeric
00259          * month provided.
00260          *
00261          * @access      public
00262          * @param       integer the month
00263          * @return      string
00264          */
00265         function get_month_name($month)
00266         {
00267                 if ($this->month_type == 'short')
00268                 {
00269                         $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');
00270                 }
00271                 else
00272                 {
00273                         $month_names = array('01' => 'cal_january', '02' => 'cal_february', '03' => 'cal_march', '04' => 'cal_april', '05' => 'cal_may', '06' => 'cal_june', '07' => 'cal_july', '08' => 'cal_august', '09' => 'cal_september', '10' => 'cal_october', '11' => 'cal_november', '12' => 'cal_december');
00274                 }
00275                 
00276                 $month = $month_names[$month];
00277                 
00278                 if ($this->CI->lang->line($month) === FALSE)
00279                 {
00280                         return ucfirst(str_replace('cal_', '', $month));
00281                 }
00282 
00283                 return $this->CI->lang->line($month);
00284         }
00285         
00286         // --------------------------------------------------------------------
00287 
00288         /**
00289          * Get Day Names
00290          *
00291          * Returns an array of day names (Sunday, Monday, etc.) based
00292          * on the type.  Options: long, short, abrev
00293          *
00294          * @access      public
00295          * @param       string
00296          * @return      array
00297          */
00298         function get_day_names($day_type = '')
00299         {
00300                 if ($day_type != '')
00301                         $this->day_type = $day_type;
00302         
00303                 if ($this->day_type == 'long')
00304                 {
00305                         $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
00306                 }
00307                 elseif ($this->day_type == 'short')
00308                 {
00309                         $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
00310                 }
00311                 else
00312                 {
00313                         $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
00314                 }
00315         
00316                 $days = array();
00317                 foreach ($day_names as $val)
00318                 {                       
00319                         $days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val);
00320                 }
00321         
00322                 return $days;
00323         }
00324         
00325         // --------------------------------------------------------------------
00326 
00327         /**
00328          * Adjust Date
00329          *
00330          * This function makes sure that we have a valid month/year.
00331          * For example, if you submit 13 as the month, the year will
00332          * increment and the month will become January.
00333          *
00334          * @access      public
00335          * @param       integer the month
00336          * @param       integer the year
00337          * @return      array
00338          */
00339         function adjust_date($month, $year)
00340         {
00341                 $date = array();
00342 
00343                 $date['month']  = $month;
00344                 $date['year']   = $year;
00345 
00346                 while ($date['month'] > 12)
00347                 {
00348                         $date['month'] -= 12;
00349                         $date['year']++;
00350                 }
00351 
00352                 while ($date['month'] <= 0)
00353                 {
00354                         $date['month'] += 12;
00355                         $date['year']--;
00356                 }
00357 
00358                 if (strlen($date['month']) == 1)
00359                 {
00360                         $date['month'] = '0'.$date['month'];
00361                 }
00362 
00363                 return $date;
00364         }
00365         
00366         // --------------------------------------------------------------------
00367 
00368         /**
00369          * Total days in a given month
00370          *
00371          * @access      public
00372          * @param       integer the month
00373          * @param       integer the year
00374          * @return      integer
00375          */
00376         function get_total_days($month, $year)
00377         {
00378                 $days_in_month  = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00379 
00380                 if ($month < 1 OR $month > 12)
00381                 {
00382                         return 0;
00383                 }
00384 
00385                 // Is the year a leap year?
00386                 if ($month == 2)
00387                 {
00388                         if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
00389                         {
00390                                 return 29;
00391                         }
00392                 }
00393 
00394                 return $days_in_month[$month - 1];
00395         }
00396         
00397         // --------------------------------------------------------------------
00398 
00399         /**
00400          * Set Default Template Data
00401          *
00402          * This is used in the event that the user has not created their own template
00403          *
00404          * @access      public
00405          * @return array
00406          */
00407         function default_template()
00408         {
00409                 return  array (
00410                                                 'table_open'                            => '<table border="0" cellpadding="4" cellspacing="0">',
00411                                                 'heading_row_start'             => '<tr>',
00412                                                 'heading_previous_cell'         => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
00413                                                 'heading_title_cell'            => '<th colspan="{colspan}">{heading}</th>',
00414                                                 'heading_next_cell'             => '<th><a href="{next_url}">&gt;&gt;</a></th>',
00415                                                 'heading_row_end'                       => '</tr>',
00416                                                 'week_row_start'                        => '<tr>',
00417                                                 'week_day_cell'                         => '<td>{week_day}</td>',
00418                                                 'week_row_end'                          => '</tr>',
00419                                                 'cal_row_start'                         => '<tr>',
00420                                                 'cal_cell_start'                        => '<td>',
00421                                                 'cal_cell_start_today'          => '<td>',
00422                                                 'cal_cell_content'                      => '<a href="{content}">{day}</a>',
00423                                                 'cal_cell_content_today'        => '<a href="{content}"><strong>{day}</strong></a>',
00424                                                 'cal_cell_no_content'           => '{day}',
00425                                                 'cal_cell_no_content_today'     => '<strong>{day}</strong>',
00426                                                 'cal_cell_blank'                        => '&nbsp;',
00427                                                 'cal_cell_end'                          => '</td>',
00428                                                 'cal_cell_end_today'            => '</td>',
00429                                                 'cal_row_end'                           => '</tr>',
00430                                                 'table_close'                           => '</table>'
00431                                         );      
00432         }
00433         
00434         // --------------------------------------------------------------------
00435 
00436         /**
00437          * Parse Template
00438          *
00439          * Harvests the data within the template {pseudo-variables}
00440          * used to display the calendar
00441          *
00442          * @access      public
00443          * @return      void
00444          */
00445         function parse_template()
00446         {
00447                 $this->temp = $this->default_template();
00448         
00449                 if ($this->template == '')
00450                 {
00451                         return;
00452                 }
00453                 
00454                 $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
00455                 
00456                 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)
00457                 {
00458                         if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match))
00459                         {
00460                                 $this->temp[$val] = $match['1'];
00461                         }
00462                         else
00463                         {
00464                                 if (in_array($val, $today, TRUE))
00465                                 {
00466                                         $this->temp[$val] = $this->temp[str_replace('_today', '', $val)];
00467                                 }
00468                         }
00469                 }       
00470         }
00471 
00472 }
00473 
00474 // END CI_Calendar class
00475 
00476 /* End of file Calendar.php */
00477 /* Location: ./system/libraries/Calendar.php */