blob: 7bdcdca09c9c937ec56bb8819eb533ba8b948a3b [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 *
Andrew021f8ed2014-02-09 16:31:22 -060059 * @var mixed
Andrew227a5d72014-02-09 15:32:44 -060060 */
61 public $template = '';
Andrey Andreev9bf3cf22014-02-18 16:39:46 +020062
Andrew227a5d72014-02-09 15:32:44 -060063 /**
64 * Replacements array for template
65 *
Andrewe0333db2014-02-08 01:24:00 -060066 * @var array
Timothy Warren68f09812012-04-27 10:38:32 -040067 */
Andrew227a5d72014-02-09 15:32:44 -060068 public $replacements = array();
Andrey Andreev56454792012-05-17 14:32:19 +030069
Timothy Warren68f09812012-04-27 10:38:32 -040070 /**
71 * Day of the week to start the calendar on
72 *
73 * @var string
74 */
Andrew227a5d72014-02-09 15:32:44 -060075 public $start_day = 'sunday';
Andrey Andreev56454792012-05-17 14:32:19 +030076
Timothy Warren68f09812012-04-27 10:38:32 -040077 /**
78 * How to display months
79 *
80 * @var string
81 */
Andrew227a5d72014-02-09 15:32:44 -060082 public $month_type = 'long';
Andrey Andreev56454792012-05-17 14:32:19 +030083
Timothy Warren68f09812012-04-27 10:38:32 -040084 /**
85 * How to display names of days
86 *
87 * @var string
88 */
Andrew227a5d72014-02-09 15:32:44 -060089 public $day_type = 'abr';
Andrey Andreev56454792012-05-17 14:32:19 +030090
Timothy Warren68f09812012-04-27 10:38:32 -040091 /**
92 * Whether to show next/prev month links
93 *
94 * @var bool
95 */
Andrew227a5d72014-02-09 15:32:44 -060096 public $show_next_prev = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030097
Timothy Warren68f09812012-04-27 10:38:32 -040098 /**
99 * Url base to use for next/prev month links
100 *
101 * @var bool
102 */
Andrew227a5d72014-02-09 15:32:44 -0600103 public $next_prev_url = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
105 /**
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200106 * Show days of other months
107 *
108 * @var bool
109 */
110 public $show_other_days = FALSE;
111
112 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300113 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300115 * Loads the calendar language file and sets the default time reference.
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300116 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300117 * @uses CI_Lang::$is_loaded
118 *
119 * @param array $config Calendar options
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300120 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 */
Greg Akera9263282010-11-10 15:26:43 -0600122 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200125
Greg Aker3a746652011-04-19 10:59:47 -0500126 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 $this->CI->lang->load('calendar');
129 }
130
131 $this->local_time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 if (count($config) > 0)
134 {
135 $this->initialize($config);
136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300138 log_message('debug', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 /**
144 * Initialize the user preferences
145 *
146 * Accepts an associative array as input, containing display preferences
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @param array config preferences
Andrew227a5d72014-02-09 15:32:44 -0600149 * @return CI_Calendar
Barry Mienydd671972010-10-04 16:33:58 +0200150 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200151 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 foreach ($config as $key => $val)
154 {
155 if (isset($this->$key))
156 {
157 $this->$key = $val;
158 }
159 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200160
161 // Set the next_prev_url to the controller if required but not defined
Marcos SF Filhoc4f66162014-01-08 18:57:09 -0200162 if ($this->show_next_prev === TRUE && empty($this->next_prev_url))
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200163 {
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200164 $this->next_prev_url = $this->CI->config->site_url($this->CI->router->class.'/'.$this->CI->router->method);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200165 }
Andrew227a5d72014-02-09 15:32:44 -0600166
167 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
171
172 /**
173 * Generate the calendar
174 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300175 * @param int the year
176 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param array the data to be shown in the calendar cells
178 * @return string
179 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200180 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300183 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200184 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200185 $year = date('Y', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200186 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200187 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200188 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200190 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200191 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200192 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200194 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300196 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200197 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200198 $month = date('m', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200199 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200200 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $month = $adjusted_date['month'];
208 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // Determine the total days in the month
211 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // Set the starting day of the week
214 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300215 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // Set the starting day number
218 $local_date = mktime(12, 0, 0, $month, 1, $year);
219 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300220 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 while ($day > 1)
223 {
224 $day -= 7;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Set the current month/year/day
228 // We use this to determine the "today" date
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200229 $cur_year = date('Y', $this->local_time);
230 $cur_month = date('m', $this->local_time);
231 $cur_day = date('j', $this->local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200232
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300233 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Andrew227a5d72014-02-09 15:32:44 -0600235 // Generate the template data array
236 $this->parse_template();
237
Barry Mienydd671972010-10-04 16:33:58 +0200238 // Begin building the calendar output
Andrew227a5d72014-02-09 15:32:44 -0600239 $out = $this->replacements['table_open']."\n\n".$this->replacements['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100242 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200244 // Add a trailing slash to the URL if needed
245 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600248 $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_previous_cell'])."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250
251 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100252 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200253
Andrew227a5d72014-02-09 15:32:44 -0600254 $this->replacements['heading_title_cell'] = str_replace('{colspan}', $colspan,
255 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->replacements['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrew227a5d72014-02-09 15:32:44 -0600257 $out .= $this->replacements['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
259 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100260 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200261 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $adjusted_date = $this->adjust_date($month + 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600263 $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_next_cell']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265
Andrew227a5d72014-02-09 15:32:44 -0600266 $out .= "\n".$this->replacements['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200267 // Write the cells containing the days of the week
Andrew227a5d72014-02-09 15:32:44 -0600268 .$this->replacements['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
270 $day_names = $this->get_day_names();
271
272 for ($i = 0; $i < 7; $i ++)
273 {
Andrew227a5d72014-02-09 15:32:44 -0600274 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->replacements['week_day_cell']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276
Andrew227a5d72014-02-09 15:32:44 -0600277 $out .= "\n".$this->replacements['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 // Build the main body of the calendar
280 while ($day <= $total_days)
281 {
Andrew227a5d72014-02-09 15:32:44 -0600282 $out .= "\n".$this->replacements['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
284 for ($i = 0; $i < 7; $i++)
285 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300286 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200287 {
Andrew227a5d72014-02-09 15:32:44 -0600288 $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start'];
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200291 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300293 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600294 $this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200295 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 else
298 {
299 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300300 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600301 $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $out .= str_replace('{day}', $day, $temp);
303 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200304
Andrew227a5d72014-02-09 15:32:44 -0600305 $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_end_today'] : $this->replacements['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200307 elseif ($this->show_other_days === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrew227a5d72014-02-09 15:32:44 -0600309 $out .= $this->replacements['cal_cell_start_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200310
311 if ($day <= 0)
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200312 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200313 // Day of previous month
Marcos SF Filho2e914b72014-01-09 09:20:55 -0200314 $prev_month = $this->adjust_date($month - 1, $year);
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200315 $prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
Andrew227a5d72014-02-09 15:32:44 -0600316 $out .= str_replace('{day}', $prev_month_days + $day, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200317 }
318 else
319 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200320 // Day of next month
Andrew227a5d72014-02-09 15:32:44 -0600321 $out .= str_replace('{day}', $day - $total_days, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200322 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200323
Andrew227a5d72014-02-09 15:32:44 -0600324 $out .= $this->replacements['cal_cell_end_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200325 }
326 else
327 {
328 // Blank cells
Andrew227a5d72014-02-09 15:32:44 -0600329 $out .= $this->replacements['cal_cell_start'].$this->replacements['cal_cell_blank'].$this->replacements['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 $day++;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Andrew227a5d72014-02-09 15:32:44 -0600335 $out .= "\n".$this->replacements['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
Andrew227a5d72014-02-09 15:32:44 -0600338 return $out .= "\n".$this->replacements['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
342
343 /**
344 * Get Month Name
345 *
346 * Generates a textual month name based on the numeric
347 * month provided.
348 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300349 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return string
351 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200352 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100354 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $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');
357 }
358 else
359 {
Derek Allarddefaa172009-08-17 17:24:31 +0000360 $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 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Andrey Andreev56454792012-05-17 14:32:19 +0300363 return ($this->CI->lang->line($month_names[$month]) === FALSE)
364 ? ucfirst(substr($month_names[$month], 4))
365 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // --------------------------------------------------------------------
369
370 /**
371 * Get Day Names
372 *
373 * Returns an array of day names (Sunday, Monday, etc.) based
Andrew227a5d72014-02-09 15:32:44 -0600374 * on the type. Options: long, short, abr
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param string
377 * @return array
378 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200379 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100381 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200382 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Andrey Andreev56454792012-05-17 14:32:19 +0300386 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
389 }
Andrey Andreev56454792012-05-17 14:32:19 +0300390 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
393 }
394 else
395 {
396 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200400 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200401 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200402 $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 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 return $days;
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // --------------------------------------------------------------------
409
410 /**
411 * Adjust Date
412 *
413 * This function makes sure that we have a valid month/year.
414 * For example, if you submit 13 as the month, the year will
415 * increment and the month will become January.
416 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300417 * @param int the month
418 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return array
420 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200421 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $date = array();
424
425 $date['month'] = $month;
426 $date['year'] = $year;
427
428 while ($date['month'] > 12)
429 {
430 $date['month'] -= 12;
431 $date['year']++;
432 }
433
434 while ($date['month'] <= 0)
435 {
436 $date['month'] += 12;
437 $date['year']--;
438 }
439
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200440 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 $date['month'] = '0'.$date['month'];
443 }
444
445 return $date;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
451 * Total days in a given month
452 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300453 * @param int the month
454 * @param int the year
455 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200457 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Andrey Andreevbe368a82014-02-20 15:46:05 +0200459 $this->load->helper('date');
460 return days_in_month($month, $year);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
464
465 /**
Andrew227a5d72014-02-09 15:32:44 -0600466 * Set Default Template Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrew227a5d72014-02-09 15:32:44 -0600468 * This is used in the event that the user has not created their own template
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 *
Andrew227a5d72014-02-09 15:32:44 -0600470 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 */
Andrew227a5d72014-02-09 15:32:44 -0600472 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrew227a5d72014-02-09 15:32:44 -0600474 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400475 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
476 'heading_row_start' => '<tr>',
477 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
478 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
479 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
480 'heading_row_end' => '</tr>',
481 'week_row_start' => '<tr>',
482 'week_day_cell' => '<td>{week_day}</td>',
483 'week_row_end' => '</tr>',
484 'cal_row_start' => '<tr>',
485 'cal_cell_start' => '<td>',
486 'cal_cell_start_today' => '<td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200487 'cal_cell_start_other' => '<td style="color: #666;">',
Timothy Warren68f09812012-04-27 10:38:32 -0400488 'cal_cell_content' => '<a href="{content}">{day}</a>',
489 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
490 'cal_cell_no_content' => '{day}',
491 'cal_cell_no_content_today' => '<strong>{day}</strong>',
492 'cal_cell_blank' => '&nbsp;',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200493 'cal_cell_other' => '{day}',
Timothy Warren68f09812012-04-27 10:38:32 -0400494 'cal_cell_end' => '</td>',
495 'cal_cell_end_today' => '</td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200496 'cal_cell_end_other' => '</td>',
Timothy Warren68f09812012-04-27 10:38:32 -0400497 'cal_row_end' => '</tr>',
498 'table_close' => '</table>'
499 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Andrew227a5d72014-02-09 15:32:44 -0600501
502 // --------------------------------------------------------------------
503
504 /**
505 * Parse Template
506 *
507 * Harvests the data within the template {pseudo-variables}
508 * used to display the calendar
509 *
510 * @return CI_Calendar
511 */
512 public function parse_template()
513 {
514 $this->replacements = $this->default_template();
515
Andrew021f8ed2014-02-09 16:31:22 -0600516 if (empty($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600517 {
518 return $this;
519 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200520
Andrew021f8ed2014-02-09 16:31:22 -0600521 if (is_string($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600522 {
523 $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200524
Andrew227a5d72014-02-09 15:32:44 -0600525 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', 'cal_cell_start_other', 'cal_cell_other', 'cal_cell_end_other') as $val)
526 {
527 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
528 {
529 $this->replacements[$val] = $match[1];
530 }
531 elseif (in_array($val, $today, TRUE))
532 {
533 $this->replacements[$val] = $this->replacements[substr($val, 0, -6)];
534 }
535 }
Andrew227a5d72014-02-09 15:32:44 -0600536 }
Andrew021f8ed2014-02-09 16:31:22 -0600537 elseif (is_array($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600538 {
539 $this->replacements = array_merge($this->replacements, $this->template);
540 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200541
Andrew021f8ed2014-02-09 16:31:22 -0600542 return $this;
Andrew227a5d72014-02-09 15:32:44 -0600543 }
544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545}
546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547/* End of file Calendar.php */
Andrew41713aa2014-02-11 02:07:50 -0600548/* Location: ./system/libraries/Calendar.php */