blob: ec2b7bcc137277c926e1bb88d7a282d0451cbbbf [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Calendar Class
31 *
32 * This class enables the creation of calendars
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/calendar.html
39 */
40class CI_Calendar {
41
Timothy Warren68f09812012-04-27 10:38:32 -040042 /**
43 * Reference to CodeIgniter instance
44 *
45 * @var object
46 */
Andrey Andreev6dbabb52012-03-26 15:41:48 +030047 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030048
Timothy Warren68f09812012-04-27 10:38:32 -040049 /**
50 * Current local time
51 *
52 * @var int
53 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020054 public $local_time;
Andrey Andreev56454792012-05-17 14:32:19 +030055
Timothy Warren68f09812012-04-27 10:38:32 -040056 /**
57 * Calendar layout template
58 *
59 * @var string
60 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020061 public $template = '';
Andrey Andreev56454792012-05-17 14:32:19 +030062
Timothy Warren68f09812012-04-27 10:38:32 -040063 /**
64 * Day of the week to start the calendar on
65 *
66 * @var string
67 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020068 public $start_day = 'sunday';
Andrey Andreev56454792012-05-17 14:32:19 +030069
Timothy Warren68f09812012-04-27 10:38:32 -040070 /**
71 * How to display months
72 *
73 * @var string
74 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020075 public $month_type = 'long';
Andrey Andreev56454792012-05-17 14:32:19 +030076
Timothy Warren68f09812012-04-27 10:38:32 -040077 /**
78 * How to display names of days
79 *
80 * @var string
81 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020082 public $day_type = 'abr';
Andrey Andreev56454792012-05-17 14:32:19 +030083
Timothy Warren68f09812012-04-27 10:38:32 -040084 /**
85 * Whether to show next/prev month links
86 *
87 * @var bool
88 */
Andrey Andreev56454792012-05-17 14:32:19 +030089 public $show_next_prev = FALSE;
90
Timothy Warren68f09812012-04-27 10:38:32 -040091 /**
92 * Url base to use for next/prev month links
93 *
94 * @var bool
95 */
Andrey Andreev56454792012-05-17 14:32:19 +030096 public $next_prev_url = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000097
98 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030099 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300101 * Loads the calendar language file and sets the default time reference.
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300102 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300103 * @uses CI_Lang::$is_loaded
104 *
105 * @param array $config Calendar options
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300106 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 */
Greg Akera9263282010-11-10 15:26:43 -0600108 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200111
Greg Aker3a746652011-04-19 10:59:47 -0500112 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
114 $this->CI->lang->load('calendar');
115 }
116
117 $this->local_time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if (count($config) > 0)
120 {
121 $this->initialize($config);
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300124 log_message('debug', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 /**
130 * Initialize the user preferences
131 *
132 * Accepts an associative array as input, containing display preferences
133 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @param array config preferences
135 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200137 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 foreach ($config as $key => $val)
140 {
141 if (isset($this->$key))
142 {
143 $this->$key = $val;
144 }
145 }
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
149
150 /**
151 * Generate the calendar
152 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300153 * @param int the year
154 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param array the data to be shown in the calendar cells
156 * @return string
157 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200158 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300161 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200162 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200163 $year = date('Y', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200164 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200165 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200168 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200169 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200170 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200172 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300174 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200175 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200176 $month = date('m', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200177 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200178 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200179 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 $month = $adjusted_date['month'];
186 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // Determine the total days in the month
189 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // Set the starting day of the week
192 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300193 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Set the starting day number
196 $local_date = mktime(12, 0, 0, $month, 1, $year);
197 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300198 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 while ($day > 1)
201 {
202 $day -= 7;
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 // Set the current month/year/day
206 // We use this to determine the "today" date
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200207 $cur_year = date('Y', $this->local_time);
208 $cur_month = date('m', $this->local_time);
209 $cur_day = date('j', $this->local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200210
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300211 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // Generate the template data array
214 $this->parse_template();
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Barry Mienydd671972010-10-04 16:33:58 +0200216 // Begin building the calendar output
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200217 $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100220 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200222 // Add a trailing slash to the URL if needed
223 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200226 $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 +0000227 }
228
229 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100230 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200231
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200232 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
233 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200234
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200235 $out .= $this->temp['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
237 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100238 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200239 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $adjusted_date = $this->adjust_date($month + 1, $year);
241 $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
242 }
243
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200244 $out .= "\n".$this->temp['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200245 // Write the cells containing the days of the week
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200246 .$this->temp['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000247
248 $day_names = $this->get_day_names();
249
250 for ($i = 0; $i < 7; $i ++)
251 {
252 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
253 }
254
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200255 $out .= "\n".$this->temp['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
257 // Build the main body of the calendar
258 while ($day <= $total_days)
259 {
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200260 $out .= "\n".$this->temp['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 for ($i = 0; $i < 7; $i++)
263 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300264 $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 +0200265
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300266 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200267 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300271 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200272 $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200273 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275 else
276 {
277 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300278 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200279 $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 $out .= str_replace('{day}', $day, $temp);
281 }
282 }
283 else
284 {
285 // Blank cells
286 $out .= $this->temp['cal_cell_blank'];
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300289 $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 +0000290 $day++;
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200293 $out .= "\n".$this->temp['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295
Andrey Andreev56454792012-05-17 14:32:19 +0300296 return $out .= "\n".$this->temp['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
300
301 /**
302 * Get Month Name
303 *
304 * Generates a textual month name based on the numeric
305 * month provided.
306 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300307 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @return string
309 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200310 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100312 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 $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');
315 }
316 else
317 {
Derek Allarddefaa172009-08-17 17:24:31 +0000318 $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 +0000319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Andrey Andreev56454792012-05-17 14:32:19 +0300321 return ($this->CI->lang->line($month_names[$month]) === FALSE)
322 ? ucfirst(substr($month_names[$month], 4))
323 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Get Day Names
330 *
331 * Returns an array of day names (Sunday, Monday, etc.) based
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300332 * on the type. Options: long, short, abrev
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string
335 * @return array
336 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200337 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100339 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200340 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Andrey Andreev56454792012-05-17 14:32:19 +0300344 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
347 }
Andrey Andreev56454792012-05-17 14:32:19 +0300348 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
351 }
352 else
353 {
354 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200358 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200359 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200360 $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 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 return $days;
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
367
368 /**
369 * Adjust Date
370 *
371 * This function makes sure that we have a valid month/year.
372 * For example, if you submit 13 as the month, the year will
373 * increment and the month will become January.
374 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300375 * @param int the month
376 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @return array
378 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200379 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $date = array();
382
383 $date['month'] = $month;
384 $date['year'] = $year;
385
386 while ($date['month'] > 12)
387 {
388 $date['month'] -= 12;
389 $date['year']++;
390 }
391
392 while ($date['month'] <= 0)
393 {
394 $date['month'] += 12;
395 $date['year']--;
396 }
397
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200398 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 $date['month'] = '0'.$date['month'];
401 }
402
403 return $date;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // --------------------------------------------------------------------
407
408 /**
409 * Total days in a given month
410 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300411 * @param int the month
412 * @param int the year
413 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200415 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
418
419 if ($month < 1 OR $month > 12)
420 {
421 return 0;
422 }
423
424 // Is the year a leap year?
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300425 if ($month == 2)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100427 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 return 29;
430 }
431 }
432
433 return $days_in_month[$month - 1];
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
437
438 /**
439 * Set Default Template Data
440 *
441 * This is used in the event that the user has not created their own template
442 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300443 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200445 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200447 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400448 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
449 'heading_row_start' => '<tr>',
450 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
451 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
452 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
453 'heading_row_end' => '</tr>',
454 'week_row_start' => '<tr>',
455 'week_day_cell' => '<td>{week_day}</td>',
456 'week_row_end' => '</tr>',
457 'cal_row_start' => '<tr>',
458 'cal_cell_start' => '<td>',
459 'cal_cell_start_today' => '<td>',
460 'cal_cell_content' => '<a href="{content}">{day}</a>',
461 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
462 'cal_cell_no_content' => '{day}',
463 'cal_cell_no_content_today' => '<strong>{day}</strong>',
464 'cal_cell_blank' => '&nbsp;',
465 'cal_cell_end' => '</td>',
466 'cal_cell_end_today' => '</td>',
467 'cal_row_end' => '</tr>',
468 'table_close' => '</table>'
469 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Parse Template
476 *
477 * Harvests the data within the template {pseudo-variables}
478 * used to display the calendar
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @return void
481 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200482 public function parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200483 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $this->temp = $this->default_template();
Barry Mienydd671972010-10-04 16:33:58 +0200485
Alex Bilbied261b1e2012-06-02 11:12:16 +0100486 if ($this->template === '')
Barry Mienydd671972010-10-04 16:33:58 +0200487 {
488 return;
489 }
490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $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 +0200492
Andrey Andreev838a9d62012-12-03 14:37:47 +0200493 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 +0000494 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300495 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200497 $this->temp[$val] = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200499 elseif (in_array($val, $today, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200501 $this->temp[$val] = $this->temp[substr($val, 0, -6)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503 }
504 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000505
506}
507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508/* End of file Calendar.php */
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300509/* Location: ./system/libraries/Calendar.php */