blob: 450350ccfca7ac872936e027b283479deab2311c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Calendar Class
42 *
43 * This class enables the creation of calendars
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/calendar.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_Calendar {
52
Timothy Warren68f09812012-04-27 10:38:32 -040053 /**
Timothy Warren68f09812012-04-27 10:38:32 -040054 * Calendar layout template
55 *
Andrew021f8ed2014-02-09 16:31:22 -060056 * @var mixed
Andrew227a5d72014-02-09 15:32:44 -060057 */
58 public $template = '';
Andrey Andreev9bf3cf22014-02-18 16:39:46 +020059
Andrew227a5d72014-02-09 15:32:44 -060060 /**
61 * Replacements array for template
62 *
Andrewe0333db2014-02-08 01:24:00 -060063 * @var array
Timothy Warren68f09812012-04-27 10:38:32 -040064 */
Andrew227a5d72014-02-09 15:32:44 -060065 public $replacements = array();
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren68f09812012-04-27 10:38:32 -040067 /**
68 * Day of the week to start the calendar on
69 *
70 * @var string
71 */
Andrew227a5d72014-02-09 15:32:44 -060072 public $start_day = 'sunday';
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren68f09812012-04-27 10:38:32 -040074 /**
75 * How to display months
76 *
77 * @var string
78 */
Andrew227a5d72014-02-09 15:32:44 -060079 public $month_type = 'long';
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren68f09812012-04-27 10:38:32 -040081 /**
82 * How to display names of days
83 *
84 * @var string
85 */
Andrew227a5d72014-02-09 15:32:44 -060086 public $day_type = 'abr';
Andrey Andreev56454792012-05-17 14:32:19 +030087
Timothy Warren68f09812012-04-27 10:38:32 -040088 /**
89 * Whether to show next/prev month links
90 *
91 * @var bool
92 */
Andrew227a5d72014-02-09 15:32:44 -060093 public $show_next_prev = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030094
Timothy Warren68f09812012-04-27 10:38:32 -040095 /**
96 * Url base to use for next/prev month links
97 *
98 * @var bool
99 */
Andrew227a5d72014-02-09 15:32:44 -0600100 public $next_prev_url = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000101
102 /**
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200103 * Show days of other months
104 *
105 * @var bool
106 */
107 public $show_other_days = FALSE;
108
Andrey Andreev6125a272014-02-21 16:58:13 +0200109 // --------------------------------------------------------------------
110
111 /**
112 * CI Singleton
113 *
114 * @var object
115 */
116 protected $CI;
117
118 // --------------------------------------------------------------------
119
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200120 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300121 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300123 * Loads the calendar language file and sets the default time reference.
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300124 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300125 * @uses CI_Lang::$is_loaded
126 *
127 * @param array $config Calendar options
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300128 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 */
Greg Akera9263282010-11-10 15:26:43 -0600130 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200131 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 $this->CI =& get_instance();
Andrey Andreev03404892015-03-26 20:58:19 +0200133 $this->CI->lang->load('calendar');
Derek Allard2067d1a2008-11-13 22:59:24 +0000134
Andrey Andreev6125a272014-02-21 16:58:13 +0200135 empty($config) OR $this->initialize($config);
Barry Mienydd671972010-10-04 16:33:58 +0200136
Andrey Andreev90726b82015-01-20 12:39:22 +0200137 log_message('info', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 /**
143 * Initialize the user preferences
144 *
145 * Accepts an associative array as input, containing display preferences
146 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 * @param array config preferences
Andrew227a5d72014-02-09 15:32:44 -0600148 * @return CI_Calendar
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200150 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
152 foreach ($config as $key => $val)
153 {
154 if (isset($this->$key))
155 {
156 $this->$key = $val;
157 }
158 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200159
160 // Set the next_prev_url to the controller if required but not defined
Marcos SF Filhoc4f66162014-01-08 18:57:09 -0200161 if ($this->show_next_prev === TRUE && empty($this->next_prev_url))
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200162 {
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200163 $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 -0200164 }
Andrew227a5d72014-02-09 15:32:44 -0600165
166 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
170
171 /**
172 * Generate the calendar
173 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300174 * @param int the year
175 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param array the data to be shown in the calendar cells
177 * @return string
178 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200179 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200181 $local_time = time();
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300184 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200185 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200186 $year = date('Y', $local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200187 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200188 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200191 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200192 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200195 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300197 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200198 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200199 $month = date('m', $local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200200 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200201 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200202 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 $month = $adjusted_date['month'];
209 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // Determine the total days in the month
212 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // Set the starting day of the week
215 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300216 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // Set the starting day number
219 $local_date = mktime(12, 0, 0, $month, 1, $year);
220 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300221 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 while ($day > 1)
224 {
225 $day -= 7;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // Set the current month/year/day
229 // We use this to determine the "today" date
Andrey Andreev6125a272014-02-21 16:58:13 +0200230 $cur_year = date('Y', $local_time);
231 $cur_month = date('m', $local_time);
232 $cur_day = date('j', $local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300234 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Andrew227a5d72014-02-09 15:32:44 -0600236 // Generate the template data array
237 $this->parse_template();
238
Barry Mienydd671972010-10-04 16:33:58 +0200239 // Begin building the calendar output
Andrew227a5d72014-02-09 15:32:44 -0600240 $out = $this->replacements['table_open']."\n\n".$this->replacements['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100243 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200245 // Add a trailing slash to the URL if needed
246 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600249 $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 +0000250 }
251
252 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100253 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200254
Andrew227a5d72014-02-09 15:32:44 -0600255 $this->replacements['heading_title_cell'] = str_replace('{colspan}', $colspan,
256 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->replacements['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrew227a5d72014-02-09 15:32:44 -0600258 $out .= $this->replacements['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
260 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100261 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200262 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 $adjusted_date = $this->adjust_date($month + 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600264 $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 +0000265 }
266
Andrew227a5d72014-02-09 15:32:44 -0600267 $out .= "\n".$this->replacements['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200268 // Write the cells containing the days of the week
Andrew227a5d72014-02-09 15:32:44 -0600269 .$this->replacements['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
271 $day_names = $this->get_day_names();
272
273 for ($i = 0; $i < 7; $i ++)
274 {
Andrew227a5d72014-02-09 15:32:44 -0600275 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->replacements['week_day_cell']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
277
Andrew227a5d72014-02-09 15:32:44 -0600278 $out .= "\n".$this->replacements['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
280 // Build the main body of the calendar
281 while ($day <= $total_days)
282 {
Andrew227a5d72014-02-09 15:32:44 -0600283 $out .= "\n".$this->replacements['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
285 for ($i = 0; $i < 7; $i++)
286 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300287 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200288 {
Andrew227a5d72014-02-09 15:32:44 -0600289 $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 -0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200292 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300294 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600295 $this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200296 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 else
299 {
300 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300301 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600302 $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 $out .= str_replace('{day}', $day, $temp);
304 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200305
Andrew227a5d72014-02-09 15:32:44 -0600306 $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 +0000307 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200308 elseif ($this->show_other_days === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrew227a5d72014-02-09 15:32:44 -0600310 $out .= $this->replacements['cal_cell_start_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200311
312 if ($day <= 0)
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200313 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200314 // Day of previous month
Marcos SF Filho2e914b72014-01-09 09:20:55 -0200315 $prev_month = $this->adjust_date($month - 1, $year);
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200316 $prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
Andrew227a5d72014-02-09 15:32:44 -0600317 $out .= str_replace('{day}', $prev_month_days + $day, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200318 }
319 else
320 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200321 // Day of next month
Andrew227a5d72014-02-09 15:32:44 -0600322 $out .= str_replace('{day}', $day - $total_days, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200323 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200324
Andrew227a5d72014-02-09 15:32:44 -0600325 $out .= $this->replacements['cal_cell_end_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200326 }
327 else
328 {
329 // Blank cells
Andrew227a5d72014-02-09 15:32:44 -0600330 $out .= $this->replacements['cal_cell_start'].$this->replacements['cal_cell_blank'].$this->replacements['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 $day++;
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Andrew227a5d72014-02-09 15:32:44 -0600336 $out .= "\n".$this->replacements['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
338
Andrew227a5d72014-02-09 15:32:44 -0600339 return $out .= "\n".$this->replacements['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
345 * Get Month Name
346 *
347 * Generates a textual month name based on the numeric
348 * month provided.
349 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300350 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return string
352 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200353 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100355 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 $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');
358 }
359 else
360 {
Derek Allarddefaa172009-08-17 17:24:31 +0000361 $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 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Andrey Andreev56454792012-05-17 14:32:19 +0300364 return ($this->CI->lang->line($month_names[$month]) === FALSE)
365 ? ucfirst(substr($month_names[$month], 4))
366 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
370
371 /**
372 * Get Day Names
373 *
374 * Returns an array of day names (Sunday, Monday, etc.) based
Andrew227a5d72014-02-09 15:32:44 -0600375 * on the type. Options: long, short, abr
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string
378 * @return array
379 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200380 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100382 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200383 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Andrey Andreev56454792012-05-17 14:32:19 +0300387 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
390 }
Andrey Andreev56454792012-05-17 14:32:19 +0300391 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
394 }
395 else
396 {
397 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200401 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200402 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200403 $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 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 return $days;
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
410
411 /**
412 * Adjust Date
413 *
414 * This function makes sure that we have a valid month/year.
415 * For example, if you submit 13 as the month, the year will
416 * increment and the month will become January.
417 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300418 * @param int the month
419 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * @return array
421 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200422 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 $date = array();
425
426 $date['month'] = $month;
427 $date['year'] = $year;
428
429 while ($date['month'] > 12)
430 {
431 $date['month'] -= 12;
432 $date['year']++;
433 }
434
435 while ($date['month'] <= 0)
436 {
437 $date['month'] += 12;
438 $date['year']--;
439 }
440
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200441 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 $date['month'] = '0'.$date['month'];
444 }
445
446 return $date;
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // --------------------------------------------------------------------
450
451 /**
452 * Total days in a given month
453 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300454 * @param int the month
455 * @param int the year
456 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200458 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev083a2262014-02-20 16:04:58 +0200460 $this->CI->load->helper('date');
Andrey Andreevbe368a82014-02-20 15:46:05 +0200461 return days_in_month($month, $year);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
465
466 /**
Andrew227a5d72014-02-09 15:32:44 -0600467 * Set Default Template Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 *
Andrew227a5d72014-02-09 15:32:44 -0600469 * This is used in the event that the user has not created their own template
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 *
Andrew227a5d72014-02-09 15:32:44 -0600471 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 */
Andrew227a5d72014-02-09 15:32:44 -0600473 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrew227a5d72014-02-09 15:32:44 -0600475 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400476 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
477 'heading_row_start' => '<tr>',
478 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
479 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
480 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
481 'heading_row_end' => '</tr>',
482 'week_row_start' => '<tr>',
483 'week_day_cell' => '<td>{week_day}</td>',
484 'week_row_end' => '</tr>',
485 'cal_row_start' => '<tr>',
486 'cal_cell_start' => '<td>',
487 'cal_cell_start_today' => '<td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200488 'cal_cell_start_other' => '<td style="color: #666;">',
Timothy Warren68f09812012-04-27 10:38:32 -0400489 'cal_cell_content' => '<a href="{content}">{day}</a>',
490 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
491 'cal_cell_no_content' => '{day}',
492 'cal_cell_no_content_today' => '<strong>{day}</strong>',
493 'cal_cell_blank' => '&nbsp;',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200494 'cal_cell_other' => '{day}',
Timothy Warren68f09812012-04-27 10:38:32 -0400495 'cal_cell_end' => '</td>',
496 'cal_cell_end_today' => '</td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200497 'cal_cell_end_other' => '</td>',
Timothy Warren68f09812012-04-27 10:38:32 -0400498 'cal_row_end' => '</tr>',
499 'table_close' => '</table>'
500 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Andrew227a5d72014-02-09 15:32:44 -0600502
503 // --------------------------------------------------------------------
504
505 /**
506 * Parse Template
507 *
508 * Harvests the data within the template {pseudo-variables}
509 * used to display the calendar
510 *
511 * @return CI_Calendar
512 */
513 public function parse_template()
514 {
515 $this->replacements = $this->default_template();
516
Andrew021f8ed2014-02-09 16:31:22 -0600517 if (empty($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600518 {
519 return $this;
520 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200521
Andrew021f8ed2014-02-09 16:31:22 -0600522 if (is_string($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600523 {
524 $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 +0200525
Andrew227a5d72014-02-09 15:32:44 -0600526 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)
527 {
528 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
529 {
530 $this->replacements[$val] = $match[1];
531 }
532 elseif (in_array($val, $today, TRUE))
533 {
534 $this->replacements[$val] = $this->replacements[substr($val, 0, -6)];
535 }
536 }
Andrew227a5d72014-02-09 15:32:44 -0600537 }
Andrew021f8ed2014-02-09 16:31:22 -0600538 elseif (is_array($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600539 {
540 $this->replacements = array_merge($this->replacements, $this->template);
541 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200542
Andrew021f8ed2014-02-09 16:31:22 -0600543 return $this;
Andrew227a5d72014-02-09 15:32:44 -0600544 }
545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546}