blob: 4db754f5e38b040cbc5b09966dced4797a676933 [file] [log] [blame]
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Calendar Class
30 *
31 * This class enables the creation of calendars
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/calendar.html
38 */
39class CI_Calendar {
40
Timothy Warren68f09812012-04-27 10:38:32 -040041 /**
42 * Reference to CodeIgniter instance
43 *
44 * @var object
45 */
Andrey Andreev6dbabb52012-03-26 15:41:48 +030046 protected $CI;
Timothy Warren68f09812012-04-27 10:38:32 -040047
48 /**
49 * Current local time
50 *
51 * @var int
52 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020053 public $local_time;
Timothy Warren68f09812012-04-27 10:38:32 -040054
55 /**
56 * Calendar layout template
57 *
58 * @var string
59 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020060 public $template = '';
Timothy Warren68f09812012-04-27 10:38:32 -040061
62 /**
63 * Day of the week to start the calendar on
64 *
65 * @var string
66 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020067 public $start_day = 'sunday';
Timothy Warren68f09812012-04-27 10:38:32 -040068
69 /**
70 * How to display months
71 *
72 * @var string
73 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020074 public $month_type = 'long';
Timothy Warren68f09812012-04-27 10:38:32 -040075
76 /**
77 * How to display names of days
78 *
79 * @var string
80 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020081 public $day_type = 'abr';
Timothy Warren68f09812012-04-27 10:38:32 -040082
83 /**
84 * Whether to show next/prev month links
85 *
86 * @var bool
87 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020088 public $show_next_prev = FALSE;
Timothy Warren68f09812012-04-27 10:38:32 -040089
90 /**
91 * Url base to use for next/prev month links
92 *
93 * @var bool
94 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020095 public $next_prev_url = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000096
97 /**
98 * Constructor
99 *
100 * Loads the calendar language file and sets the default time reference
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300101 *
102 * @param array
103 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 */
Greg Akera9263282010-11-10 15:26:43 -0600105 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200106 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200108
Greg Aker3a746652011-04-19 10:59:47 -0500109 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 $this->CI->lang->load('calendar');
112 }
113
114 $this->local_time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if (count($config) > 0)
117 {
118 $this->initialize($config);
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300121 log_message('debug', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 /**
127 * Initialize the user preferences
128 *
129 * Accepts an associative array as input, containing display preferences
130 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * @param array config preferences
132 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200134 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 foreach ($config as $key => $val)
137 {
138 if (isset($this->$key))
139 {
140 $this->$key = $val;
141 }
142 }
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
146
147 /**
148 * Generate the calendar
149 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300150 * @param int the year
151 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @param array the data to be shown in the calendar cells
153 * @return string
154 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200155 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 // Set and validate the supplied month/year
158 if ($year == '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200159 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200160 $year = date('Y', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200161 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200162 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200163 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200165 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200166 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200169 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000170
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200171 if ($month == '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200172 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200173 $month = date('m', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200174 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200175 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $month = $adjusted_date['month'];
183 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // Determine the total days in the month
186 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // Set the starting day of the week
189 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
190 $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Set the starting day number
193 $local_date = mktime(12, 0, 0, $month, 1, $year);
194 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300195 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 while ($day > 1)
198 {
199 $day -= 7;
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // Set the current month/year/day
203 // We use this to determine the "today" date
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200204 $cur_year = date('Y', $this->local_time);
205 $cur_month = date('m', $this->local_time);
206 $cur_day = date('j', $this->local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200207
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300208 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // Generate the template data array
211 $this->parse_template();
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
Barry Mienydd671972010-10-04 16:33:58 +0200213 // Begin building the calendar output
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200214 $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // "previous" month link
217 if ($this->show_next_prev == TRUE)
218 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500219 // Add a trailing slash to the URL if needed
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300220 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200223 $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell'])."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // Heading containing the month/year
227 $colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200229 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
230 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200231
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200232 $out .= $this->temp['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000233
234 // "next" month link
235 if ($this->show_next_prev == TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $adjusted_date = $this->adjust_date($month + 1, $year);
238 $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
239 }
240
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200241 $out .= "\n".$this->temp['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200242 // Write the cells containing the days of the week
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200243 .$this->temp['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
245 $day_names = $this->get_day_names();
246
247 for ($i = 0; $i < 7; $i ++)
248 {
249 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
250 }
251
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200252 $out .= "\n".$this->temp['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254 // Build the main body of the calendar
255 while ($day <= $total_days)
256 {
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200257 $out .= "\n".$this->temp['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
259 for ($i = 0; $i < 7; $i++)
260 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300261 $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200262
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300263 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200264 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200266 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // Cells with content
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300268 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200269 $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200270 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272 else
273 {
274 // Cells with no content
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300275 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200276 $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $out .= str_replace('{day}', $day, $temp);
278 }
279 }
280 else
281 {
282 // Blank cells
283 $out .= $this->temp['cal_cell_blank'];
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300286 $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 $day++;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200290 $out .= "\n".$this->temp['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200293 $out .= "\n".$this->temp['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000294
295 return $out;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Get Month Name
302 *
303 * Generates a textual month name based on the numeric
304 * month provided.
305 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300306 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return string
308 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200309 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
311 if ($this->month_type == 'short')
312 {
313 $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');
314 }
315 else
316 {
Derek Allarddefaa172009-08-17 17:24:31 +0000317 $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');
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 $month = $month_names[$month];
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 if ($this->CI->lang->line($month) === FALSE)
323 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200324 return ucfirst(substr($month, 4));
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326
327 return $this->CI->lang->line($month);
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
331
332 /**
333 * Get Day Names
334 *
335 * Returns an array of day names (Sunday, Monday, etc.) based
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300336 * on the type. Options: long, short, abrev
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @param string
339 * @return array
340 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200341 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 if ($day_type != '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200344 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 if ($this->day_type == 'long')
349 {
350 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
351 }
352 elseif ($this->day_type == 'short')
353 {
354 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
355 }
356 else
357 {
358 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200362 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200363 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200364 $days[] = ($this->CI->lang->line('cal_'.$day_names[$i]) === FALSE) ? ucfirst($day_names[$i]) : $this->CI->lang->line('cal_'.$day_names[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return $days;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
371
372 /**
373 * Adjust Date
374 *
375 * This function makes sure that we have a valid month/year.
376 * For example, if you submit 13 as the month, the year will
377 * increment and the month will become January.
378 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300379 * @param int the month
380 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return array
382 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200383 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $date = array();
386
387 $date['month'] = $month;
388 $date['year'] = $year;
389
390 while ($date['month'] > 12)
391 {
392 $date['month'] -= 12;
393 $date['year']++;
394 }
395
396 while ($date['month'] <= 0)
397 {
398 $date['month'] += 12;
399 $date['year']--;
400 }
401
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200402 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $date['month'] = '0'.$date['month'];
405 }
406
407 return $date;
408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 // --------------------------------------------------------------------
411
412 /**
413 * Total days in a given month
414 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300415 * @param int the month
416 * @param int the year
417 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200419 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
422
423 if ($month < 1 OR $month > 12)
424 {
425 return 0;
426 }
427
428 // Is the year a leap year?
429 if ($month == 2)
430 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300431 if ($year % 400 == 0 OR ($year % 4 == 0 && $year % 100 != 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 return 29;
434 }
435 }
436
437 return $days_in_month[$month - 1];
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * Set Default Template Data
444 *
445 * This is used in the event that the user has not created their own template
446 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300447 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200449 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500451 return array (
Timothy Warren68f09812012-04-27 10:38:32 -0400452 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
453 'heading_row_start' => '<tr>',
454 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
455 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
456 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
457 'heading_row_end' => '</tr>',
458 'week_row_start' => '<tr>',
459 'week_day_cell' => '<td>{week_day}</td>',
460 'week_row_end' => '</tr>',
461 'cal_row_start' => '<tr>',
462 'cal_cell_start' => '<td>',
463 'cal_cell_start_today' => '<td>',
464 'cal_cell_content' => '<a href="{content}">{day}</a>',
465 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
466 'cal_cell_no_content' => '{day}',
467 'cal_cell_no_content_today' => '<strong>{day}</strong>',
468 'cal_cell_blank' => '&nbsp;',
469 'cal_cell_end' => '</td>',
470 'cal_cell_end_today' => '</td>',
471 'cal_row_end' => '</tr>',
472 'table_close' => '</table>'
473 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Parse Template
480 *
481 * Harvests the data within the template {pseudo-variables}
482 * used to display the calendar
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return void
485 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200486 public function parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200487 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 $this->temp = $this->default_template();
Barry Mienydd671972010-10-04 16:33:58 +0200489
490 if ($this->template == '')
491 {
492 return;
493 }
494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Jones37f4b9c2011-07-01 17:56:50 -0500497 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)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300499 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200501 $this->temp[$val] = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200503 elseif (in_array($val, $today, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200505 $this->temp[$val] = $this->temp[substr($val, 0, -6)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507 }
508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
510}
511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512/* End of file Calendar.php */
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300513/* Location: ./system/libraries/Calendar.php */