blob: fcca163a5b46f3ca05613e8df238b7fb95857f8c [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/calendar.html
50 */
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();
Barry Mienydd671972010-10-04 16:33:58 +0200133
Greg Aker3a746652011-04-19 10:59:47 -0500134 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 $this->CI->lang->load('calendar');
137 }
138
Andrey Andreev6125a272014-02-21 16:58:13 +0200139 empty($config) OR $this->initialize($config);
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev90726b82015-01-20 12:39:22 +0200141 log_message('info', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 /**
147 * Initialize the user preferences
148 *
149 * Accepts an associative array as input, containing display preferences
150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @param array config preferences
Andrew227a5d72014-02-09 15:32:44 -0600152 * @return CI_Calendar
Barry Mienydd671972010-10-04 16:33:58 +0200153 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200154 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 foreach ($config as $key => $val)
157 {
158 if (isset($this->$key))
159 {
160 $this->$key = $val;
161 }
162 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200163
164 // Set the next_prev_url to the controller if required but not defined
Marcos SF Filhoc4f66162014-01-08 18:57:09 -0200165 if ($this->show_next_prev === TRUE && empty($this->next_prev_url))
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200166 {
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200167 $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 -0200168 }
Andrew227a5d72014-02-09 15:32:44 -0600169
170 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // --------------------------------------------------------------------
174
175 /**
176 * Generate the calendar
177 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300178 * @param int the year
179 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @param array the data to be shown in the calendar cells
181 * @return string
182 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200183 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200185 $local_time = time();
186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300188 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200189 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200190 $year = date('Y', $local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200191 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200192 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200195 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200196 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200197 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200199 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300201 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200202 {
Andrey Andreev6125a272014-02-21 16:58:13 +0200203 $month = date('m', $local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200204 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200205 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200206 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $month = $adjusted_date['month'];
213 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // Determine the total days in the month
216 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // Set the starting day of the week
219 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300220 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // Set the starting day number
223 $local_date = mktime(12, 0, 0, $month, 1, $year);
224 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300225 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 while ($day > 1)
228 {
229 $day -= 7;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // Set the current month/year/day
233 // We use this to determine the "today" date
Andrey Andreev6125a272014-02-21 16:58:13 +0200234 $cur_year = date('Y', $local_time);
235 $cur_month = date('m', $local_time);
236 $cur_day = date('j', $local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200237
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300238 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200239
Andrew227a5d72014-02-09 15:32:44 -0600240 // Generate the template data array
241 $this->parse_template();
242
Barry Mienydd671972010-10-04 16:33:58 +0200243 // Begin building the calendar output
Andrew227a5d72014-02-09 15:32:44 -0600244 $out = $this->replacements['table_open']."\n\n".$this->replacements['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100247 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200249 // Add a trailing slash to the URL if needed
250 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600253 $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 +0000254 }
255
256 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100257 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrew227a5d72014-02-09 15:32:44 -0600259 $this->replacements['heading_title_cell'] = str_replace('{colspan}', $colspan,
260 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->replacements['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200261
Andrew227a5d72014-02-09 15:32:44 -0600262 $out .= $this->replacements['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000263
264 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100265 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200266 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $adjusted_date = $this->adjust_date($month + 1, $year);
Andrew227a5d72014-02-09 15:32:44 -0600268 $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 +0000269 }
270
Andrew227a5d72014-02-09 15:32:44 -0600271 $out .= "\n".$this->replacements['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200272 // Write the cells containing the days of the week
Andrew227a5d72014-02-09 15:32:44 -0600273 .$this->replacements['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
275 $day_names = $this->get_day_names();
276
277 for ($i = 0; $i < 7; $i ++)
278 {
Andrew227a5d72014-02-09 15:32:44 -0600279 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->replacements['week_day_cell']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
Andrew227a5d72014-02-09 15:32:44 -0600282 $out .= "\n".$this->replacements['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000283
284 // Build the main body of the calendar
285 while ($day <= $total_days)
286 {
Andrew227a5d72014-02-09 15:32:44 -0600287 $out .= "\n".$this->replacements['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
289 for ($i = 0; $i < 7; $i++)
290 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300291 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200292 {
Andrew227a5d72014-02-09 15:32:44 -0600293 $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 -0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300298 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600299 $this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200300 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 else
303 {
304 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300305 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrew227a5d72014-02-09 15:32:44 -0600306 $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 $out .= str_replace('{day}', $day, $temp);
308 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200309
Andrew227a5d72014-02-09 15:32:44 -0600310 $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 +0000311 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200312 elseif ($this->show_other_days === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrew227a5d72014-02-09 15:32:44 -0600314 $out .= $this->replacements['cal_cell_start_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200315
316 if ($day <= 0)
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200317 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200318 // Day of previous month
Marcos SF Filho2e914b72014-01-09 09:20:55 -0200319 $prev_month = $this->adjust_date($month - 1, $year);
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200320 $prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
Andrew227a5d72014-02-09 15:32:44 -0600321 $out .= str_replace('{day}', $prev_month_days + $day, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200322 }
323 else
324 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200325 // Day of next month
Andrew227a5d72014-02-09 15:32:44 -0600326 $out .= str_replace('{day}', $day - $total_days, $this->replacements['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200327 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200328
Andrew227a5d72014-02-09 15:32:44 -0600329 $out .= $this->replacements['cal_cell_end_other'];
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200330 }
331 else
332 {
333 // Blank cells
Andrew227a5d72014-02-09 15:32:44 -0600334 $out .= $this->replacements['cal_cell_start'].$this->replacements['cal_cell_blank'].$this->replacements['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 $day++;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Andrew227a5d72014-02-09 15:32:44 -0600340 $out .= "\n".$this->replacements['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
Andrew227a5d72014-02-09 15:32:44 -0600343 return $out .= "\n".$this->replacements['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Get Month Name
350 *
351 * Generates a textual month name based on the numeric
352 * month provided.
353 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300354 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return string
356 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200357 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100359 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 $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');
362 }
363 else
364 {
Derek Allarddefaa172009-08-17 17:24:31 +0000365 $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 +0000366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Andrey Andreev56454792012-05-17 14:32:19 +0300368 return ($this->CI->lang->line($month_names[$month]) === FALSE)
369 ? ucfirst(substr($month_names[$month], 4))
370 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // --------------------------------------------------------------------
374
375 /**
376 * Get Day Names
377 *
378 * Returns an array of day names (Sunday, Monday, etc.) based
Andrew227a5d72014-02-09 15:32:44 -0600379 * on the type. Options: long, short, abr
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string
382 * @return array
383 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200384 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100386 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200387 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Andrey Andreev56454792012-05-17 14:32:19 +0300391 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
394 }
Andrey Andreev56454792012-05-17 14:32:19 +0300395 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
398 }
399 else
400 {
401 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200405 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200406 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200407 $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 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $days;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * Adjust Date
417 *
418 * This function makes sure that we have a valid month/year.
419 * For example, if you submit 13 as the month, the year will
420 * increment and the month will become January.
421 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300422 * @param int the month
423 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return array
425 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200426 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 $date = array();
429
430 $date['month'] = $month;
431 $date['year'] = $year;
432
433 while ($date['month'] > 12)
434 {
435 $date['month'] -= 12;
436 $date['year']++;
437 }
438
439 while ($date['month'] <= 0)
440 {
441 $date['month'] += 12;
442 $date['year']--;
443 }
444
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200445 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 $date['month'] = '0'.$date['month'];
448 }
449
450 return $date;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // --------------------------------------------------------------------
454
455 /**
456 * Total days in a given month
457 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300458 * @param int the month
459 * @param int the year
460 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200462 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreev083a2262014-02-20 16:04:58 +0200464 $this->CI->load->helper('date');
Andrey Andreevbe368a82014-02-20 15:46:05 +0200465 return days_in_month($month, $year);
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
Andrew227a5d72014-02-09 15:32:44 -0600471 * Set Default Template Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 *
Andrew227a5d72014-02-09 15:32:44 -0600473 * This is used in the event that the user has not created their own template
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 *
Andrew227a5d72014-02-09 15:32:44 -0600475 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 */
Andrew227a5d72014-02-09 15:32:44 -0600477 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Andrew227a5d72014-02-09 15:32:44 -0600479 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400480 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
481 'heading_row_start' => '<tr>',
482 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
483 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
484 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
485 'heading_row_end' => '</tr>',
486 'week_row_start' => '<tr>',
487 'week_day_cell' => '<td>{week_day}</td>',
488 'week_row_end' => '</tr>',
489 'cal_row_start' => '<tr>',
490 'cal_cell_start' => '<td>',
491 'cal_cell_start_today' => '<td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200492 'cal_cell_start_other' => '<td style="color: #666;">',
Timothy Warren68f09812012-04-27 10:38:32 -0400493 'cal_cell_content' => '<a href="{content}">{day}</a>',
494 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
495 'cal_cell_no_content' => '{day}',
496 'cal_cell_no_content_today' => '<strong>{day}</strong>',
497 'cal_cell_blank' => '&nbsp;',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200498 'cal_cell_other' => '{day}',
Timothy Warren68f09812012-04-27 10:38:32 -0400499 'cal_cell_end' => '</td>',
500 'cal_cell_end_today' => '</td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200501 'cal_cell_end_other' => '</td>',
Timothy Warren68f09812012-04-27 10:38:32 -0400502 'cal_row_end' => '</tr>',
503 'table_close' => '</table>'
504 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
Andrew227a5d72014-02-09 15:32:44 -0600506
507 // --------------------------------------------------------------------
508
509 /**
510 * Parse Template
511 *
512 * Harvests the data within the template {pseudo-variables}
513 * used to display the calendar
514 *
515 * @return CI_Calendar
516 */
517 public function parse_template()
518 {
519 $this->replacements = $this->default_template();
520
Andrew021f8ed2014-02-09 16:31:22 -0600521 if (empty($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600522 {
523 return $this;
524 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200525
Andrew021f8ed2014-02-09 16:31:22 -0600526 if (is_string($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600527 {
528 $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 +0200529
Andrew227a5d72014-02-09 15:32:44 -0600530 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)
531 {
532 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
533 {
534 $this->replacements[$val] = $match[1];
535 }
536 elseif (in_array($val, $today, TRUE))
537 {
538 $this->replacements[$val] = $this->replacements[substr($val, 0, -6)];
539 }
540 }
Andrew227a5d72014-02-09 15:32:44 -0600541 }
Andrew021f8ed2014-02-09 16:31:22 -0600542 elseif (is_array($this->template))
Andrew227a5d72014-02-09 15:32:44 -0600543 {
544 $this->replacements = array_merge($this->replacements, $this->template);
545 }
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200546
Andrew021f8ed2014-02-09 16:31:22 -0600547 return $this;
Andrew227a5d72014-02-09 15:32:44 -0600548 }
549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550}
551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552/* End of file Calendar.php */
Andrew41713aa2014-02-11 02:07:50 -0600553/* Location: ./system/libraries/Calendar.php */