00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00043
00044
00045
00046
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
00071
00072
00073
00074
00075
00076
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
00093
00094
00095
00096
00097
00098
00099
00100 function generate($year = '', $month = '', $data = array())
00101 {
00102
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
00124 $total_days = $this->get_total_days($month, $year);
00125
00126
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
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
00141
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
00149 $this->parse_template();
00150
00151
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
00160 if ($this->show_next_prev == TRUE)
00161 {
00162
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
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)." ".$year, $this->temp['heading_title_cell']);
00175
00176 $out .= $this->temp['heading_title_cell'];
00177 $out .= "\n";
00178
00179
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
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
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
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
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
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
00257
00258
00259
00260
00261
00262
00263
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
00290
00291
00292
00293
00294
00295
00296
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
00329
00330
00331
00332
00333
00334
00335
00336
00337
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
00370
00371
00372
00373
00374
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
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
00401
00402
00403
00404
00405
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}"><<</a></th>',
00413 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
00414 'heading_next_cell' => '<th><a href="{next_url}">>></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' => ' ',
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
00438
00439
00440
00441
00442
00443
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
00475
00476
00477