blob: 95f537e42d0dc25deac1304935bf99f17b5743c7 [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;
Andrey Andreev56454792012-05-17 14:32:19 +030047
Timothy Warren68f09812012-04-27 10:38:32 -040048 /**
49 * Current local time
50 *
51 * @var int
52 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020053 public $local_time;
Andrey Andreev56454792012-05-17 14:32:19 +030054
Timothy Warren68f09812012-04-27 10:38:32 -040055 /**
56 * Calendar layout template
57 *
58 * @var string
59 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020060 public $template = '';
Andrey Andreev56454792012-05-17 14:32:19 +030061
Timothy Warren68f09812012-04-27 10:38:32 -040062 /**
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';
Andrey Andreev56454792012-05-17 14:32:19 +030068
Timothy Warren68f09812012-04-27 10:38:32 -040069 /**
70 * How to display months
71 *
72 * @var string
73 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020074 public $month_type = 'long';
Andrey Andreev56454792012-05-17 14:32:19 +030075
Timothy Warren68f09812012-04-27 10:38:32 -040076 /**
77 * How to display names of days
78 *
79 * @var string
80 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020081 public $day_type = 'abr';
Andrey Andreev56454792012-05-17 14:32:19 +030082
Timothy Warren68f09812012-04-27 10:38:32 -040083 /**
84 * Whether to show next/prev month links
85 *
86 * @var bool
87 */
Andrey Andreev56454792012-05-17 14:32:19 +030088 public $show_next_prev = FALSE;
89
Timothy Warren68f09812012-04-27 10:38:32 -040090 /**
91 * Url base to use for next/prev month links
92 *
93 * @var bool
94 */
Andrey Andreev56454792012-05-17 14:32:19 +030095 public $next_prev_url = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000096
97 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030098 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000099 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300100 * Loads the calendar language file and sets the default time reference.
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300101 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300102 * @uses CI_Lang::$is_loaded
103 *
104 * @param array $config Calendar options
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300105 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 */
Greg Akera9263282010-11-10 15:26:43 -0600107 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200108 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200110
Greg Aker3a746652011-04-19 10:59:47 -0500111 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
113 $this->CI->lang->load('calendar');
114 }
115
116 $this->local_time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if (count($config) > 0)
119 {
120 $this->initialize($config);
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300123 log_message('debug', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 /**
129 * Initialize the user preferences
130 *
131 * Accepts an associative array as input, containing display preferences
132 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * @param array config preferences
134 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200136 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
138 foreach ($config as $key => $val)
139 {
140 if (isset($this->$key))
141 {
142 $this->$key = $val;
143 }
144 }
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
148
149 /**
150 * Generate the calendar
151 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300152 * @param int the year
153 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @param array the data to be shown in the calendar cells
155 * @return string
156 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200157 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
159 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300160 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200161 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200162 $year = date('Y', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200163 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200164 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200165 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200167 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200168 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200169 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200171 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000172
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300173 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200174 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200175 $month = date('m', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200176 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200177 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200178 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 $month = $adjusted_date['month'];
185 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // Determine the total days in the month
188 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // Set the starting day of the week
191 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300192 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // Set the starting day number
195 $local_date = mktime(12, 0, 0, $month, 1, $year);
196 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300197 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 while ($day > 1)
200 {
201 $day -= 7;
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // Set the current month/year/day
205 // We use this to determine the "today" date
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200206 $cur_year = date('Y', $this->local_time);
207 $cur_month = date('m', $this->local_time);
208 $cur_day = date('j', $this->local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300210 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // Generate the template data array
213 $this->parse_template();
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Barry Mienydd671972010-10-04 16:33:58 +0200215 // Begin building the calendar output
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200216 $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100219 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500221 // Add a trailing slash to the URL if needed
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300222 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200225 $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 +0000226 }
227
228 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100229 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200230
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200231 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
232 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200233
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200234 $out .= $this->temp['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000235
236 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100237 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200238 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 $adjusted_date = $this->adjust_date($month + 1, $year);
240 $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
241 }
242
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200243 $out .= "\n".$this->temp['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200244 // Write the cells containing the days of the week
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200245 .$this->temp['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
247 $day_names = $this->get_day_names();
248
249 for ($i = 0; $i < 7; $i ++)
250 {
251 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
252 }
253
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200254 $out .= "\n".$this->temp['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
256 // Build the main body of the calendar
257 while ($day <= $total_days)
258 {
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200259 $out .= "\n".$this->temp['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
261 for ($i = 0; $i < 7; $i++)
262 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300263 $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 +0200264
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300265 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200266 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300270 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200271 $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200272 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274 else
275 {
276 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300277 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200278 $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $out .= str_replace('{day}', $day, $temp);
280 }
281 }
282 else
283 {
284 // Blank cells
285 $out .= $this->temp['cal_cell_blank'];
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300288 $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 +0000289 $day++;
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200292 $out .= "\n".$this->temp['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
Andrey Andreev56454792012-05-17 14:32:19 +0300295 return $out .= "\n".$this->temp['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100311 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
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
Andrey Andreev56454792012-05-17 14:32:19 +0300320 return ($this->CI->lang->line($month_names[$month]) === FALSE)
321 ? ucfirst(substr($month_names[$month], 4))
322 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Get Day Names
329 *
330 * Returns an array of day names (Sunday, Monday, etc.) based
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300331 * on the type. Options: long, short, abrev
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @return array
335 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200336 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100338 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200339 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Andrey Andreev56454792012-05-17 14:32:19 +0300343 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
346 }
Andrey Andreev56454792012-05-17 14:32:19 +0300347 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
350 }
351 else
352 {
353 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200357 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200358 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200359 $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 +0000360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 return $days;
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // --------------------------------------------------------------------
366
367 /**
368 * Adjust Date
369 *
370 * This function makes sure that we have a valid month/year.
371 * For example, if you submit 13 as the month, the year will
372 * increment and the month will become January.
373 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300374 * @param int the month
375 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @return array
377 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200378 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $date = array();
381
382 $date['month'] = $month;
383 $date['year'] = $year;
384
385 while ($date['month'] > 12)
386 {
387 $date['month'] -= 12;
388 $date['year']++;
389 }
390
391 while ($date['month'] <= 0)
392 {
393 $date['month'] += 12;
394 $date['year']--;
395 }
396
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200397 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $date['month'] = '0'.$date['month'];
400 }
401
402 return $date;
403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // --------------------------------------------------------------------
406
407 /**
408 * Total days in a given month
409 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300410 * @param int the month
411 * @param int the year
412 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200414 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
417
418 if ($month < 1 OR $month > 12)
419 {
420 return 0;
421 }
422
423 // Is the year a leap year?
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300424 if ($month == 2)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100426 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 return 29;
429 }
430 }
431
432 return $days_in_month[$month - 1];
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
436
437 /**
438 * Set Default Template Data
439 *
440 * This is used in the event that the user has not created their own template
441 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300442 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200444 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Andrey Andreev56454792012-05-17 14:32:19 +0300446 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400447 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
448 'heading_row_start' => '<tr>',
449 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
450 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
451 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
452 'heading_row_end' => '</tr>',
453 'week_row_start' => '<tr>',
454 'week_day_cell' => '<td>{week_day}</td>',
455 'week_row_end' => '</tr>',
456 'cal_row_start' => '<tr>',
457 'cal_cell_start' => '<td>',
458 'cal_cell_start_today' => '<td>',
459 'cal_cell_content' => '<a href="{content}">{day}</a>',
460 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
461 'cal_cell_no_content' => '{day}',
462 'cal_cell_no_content_today' => '<strong>{day}</strong>',
463 'cal_cell_blank' => '&nbsp;',
464 'cal_cell_end' => '</td>',
465 'cal_cell_end_today' => '</td>',
466 'cal_row_end' => '</tr>',
467 'table_close' => '</table>'
468 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Parse Template
475 *
476 * Harvests the data within the template {pseudo-variables}
477 * used to display the calendar
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return void
480 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200481 public function parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200482 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 $this->temp = $this->default_template();
Barry Mienydd671972010-10-04 16:33:58 +0200484
Alex Bilbied261b1e2012-06-02 11:12:16 +0100485 if ($this->template === '')
Barry Mienydd671972010-10-04 16:33:58 +0200486 {
487 return;
488 }
489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 $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 +0200491
Derek Jones37f4b9c2011-07-01 17:56:50 -0500492 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 +0000493 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300494 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200496 $this->temp[$val] = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200498 elseif (in_array($val, $today, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200500 $this->temp[$val] = $this->temp[substr($val, 0, -6)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502 }
503 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000504
505}
506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507/* End of file Calendar.php */
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300508/* Location: ./system/libraries/Calendar.php */