CI_Calendar Class Reference

List of all members.


Public Member Functions

 CI_Calendar ($config=array())
 Constructor.
 initialize ($config=array())
 Initialize the user preferences.
 generate ($year= '', $month= '', $data=array())
 Generate the calendar.
 get_month_name ($month)
 Get Month Name.
 get_day_names ($day_type= '')
 Get Day Names.
 adjust_date ($month, $year)
 Adjust Date.
 get_total_days ($month, $year)
 Total days in a given month.
 default_template ()
 Set Default Template Data.
 parse_template ()
 Parse Template.

Public Attributes

 $CI
 $lang
 $local_time
 $template = ''
 $start_day = 'sunday'
 $month_type = 'long'
 $day_type = 'abr'
 $show_next_prev = FALSE
 $next_prev_url = ''

Detailed Description

Definition at line 29 of file Calendar.php.


Member Function Documentation

CI_Calendar::adjust_date ( month,
year 
)

Adjust Date.

This function makes sure that we have a valid month/year. For example, if you submit 13 as the month, the year will increment and the month will become January.

public

Parameters:
integer the month
integer the year
Returns:
array

Definition at line 339 of file Calendar.php.

Referenced by generate().

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         }

Here is the caller graph for this function:

CI_Calendar::CI_Calendar ( config = array()  ) 

Constructor.

Loads the calendar language file and sets the default time reference

public

Definition at line 48 of file Calendar.php.

References $config, get_instance(), initialize(), and log_message().

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         }

Here is the call graph for this function:

CI_Calendar::default_template (  ) 

Set Default Template Data.

This is used in the event that the user has not created their own template

public

Returns:
array

Definition at line 407 of file Calendar.php.

Referenced by parse_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         }

Here is the caller graph for this function:

CI_Calendar::generate ( year = '',
month = '',
data = array() 
)

Generate the calendar.

public

Parameters:
integer the year
integer the month
array the data to be shown in the calendar cells
Returns:
string

Definition at line 100 of file Calendar.php.

References $start_day, adjust_date(), get_day_names(), get_month_name(), get_total_days(), and parse_template().

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         }

Here is the call graph for this function:

CI_Calendar::get_day_names ( day_type = ''  ) 

Get Day Names.

Returns an array of day names (Sunday, Monday, etc.) based on the type. Options: long, short, abrev

public

Parameters:
string 
Returns:
array

Definition at line 298 of file Calendar.php.

References $day_type.

Referenced by generate().

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         }

Here is the caller graph for this function:

CI_Calendar::get_month_name ( month  ) 

Get Month Name.

Generates a textual month name based on the numeric month provided.

public

Parameters:
integer the month
Returns:
string

Definition at line 265 of file Calendar.php.

Referenced by generate().

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         }

Here is the caller graph for this function:

CI_Calendar::get_total_days ( month,
year 
)

Total days in a given month.

public

Parameters:
integer the month
integer the year
Returns:
integer

Definition at line 376 of file Calendar.php.

Referenced by generate().

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         }

Here is the caller graph for this function:

CI_Calendar::initialize ( config = array()  ) 

Initialize the user preferences.

Accepts an associative array as input, containing display preferences

public

Parameters:
array config preferences
Returns:
void

Definition at line 78 of file Calendar.php.

References $config.

Referenced by CI_Calendar().

00079         {
00080                 foreach ($config as $key => $val)
00081                 {
00082                         if (isset($this->$key))
00083                         {
00084                                 $this->$key = $val;
00085                         }
00086                 }
00087         }

Here is the caller graph for this function:

CI_Calendar::parse_template (  ) 

Parse Template.

Harvests the data within the template {pseudo-variables} used to display the calendar

public

Returns:
void

Definition at line 445 of file Calendar.php.

References default_template().

Referenced by generate().

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         }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

CI_Calendar::$CI

Definition at line 31 of file Calendar.php.

CI_Calendar::$day_type = 'abr'

Definition at line 37 of file Calendar.php.

Referenced by get_day_names().

CI_Calendar::$lang

Definition at line 32 of file Calendar.php.

CI_Calendar::$local_time

Definition at line 33 of file Calendar.php.

CI_Calendar::$month_type = 'long'

Definition at line 36 of file Calendar.php.

CI_Calendar::$next_prev_url = ''

Definition at line 39 of file Calendar.php.

CI_Calendar::$show_next_prev = FALSE

Definition at line 38 of file Calendar.php.

CI_Calendar::$start_day = 'sunday'

Definition at line 35 of file Calendar.php.

Referenced by generate().

CI_Calendar::$template = ''

Definition at line 34 of file Calendar.php.


The documentation for this class was generated from the following file: