blob: 610b427cf2132079b747a3b04227f6264cdcffd1 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 /**
Marcos SF Filho7977e1d2014-01-08 15:34:11 -020099 * Show days of other months
100 *
101 * @var bool
102 */
103 public $show_other_days = FALSE;
104
105 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300106 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300108 * Loads the calendar language file and sets the default time reference.
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300109 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300110 * @uses CI_Lang::$is_loaded
111 *
112 * @param array $config Calendar options
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300113 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 */
Greg Akera9263282010-11-10 15:26:43 -0600115 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200118
Greg Aker3a746652011-04-19 10:59:47 -0500119 if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
121 $this->CI->lang->load('calendar');
122 }
123
124 $this->local_time = time();
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if (count($config) > 0)
127 {
128 $this->initialize($config);
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300131 log_message('debug', 'Calendar Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 /**
137 * Initialize the user preferences
138 *
139 * Accepts an associative array as input, containing display preferences
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param array config preferences
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200142 * @return CI_Calendar
Barry Mienydd671972010-10-04 16:33:58 +0200143 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200144 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 foreach ($config as $key => $val)
147 {
148 if (isset($this->$key))
149 {
150 $this->$key = $val;
151 }
152 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200153
154 // Set the next_prev_url to the controller if required but not defined
Marcos SF Filhoc4f66162014-01-08 18:57:09 -0200155 if ($this->show_next_prev === TRUE && empty($this->next_prev_url))
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200156 {
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200157 $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 -0200158 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200159
160 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // --------------------------------------------------------------------
164
165 /**
166 * Generate the calendar
167 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300168 * @param int the year
169 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param array the data to be shown in the calendar cells
171 * @return string
172 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200173 public function generate($year = '', $month = '', $data = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 // Set and validate the supplied month/year
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300176 if (empty($year))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200177 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200178 $year = date('Y', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200179 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200180 elseif (strlen($year) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200181 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $year = '200'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200183 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200184 elseif (strlen($year) === 2)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200185 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 $year = '20'.$year;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200187 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
Andrey Andreevf51b1fb2012-06-16 20:02:23 +0300189 if (empty($month))
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200190 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200191 $month = date('m', $this->local_time);
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200192 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200193 elseif (strlen($month) === 1)
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200194 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 $month = '0'.$month;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $adjusted_date = $this->adjust_date($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 $month = $adjusted_date['month'];
201 $year = $adjusted_date['year'];
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // Determine the total days in the month
204 $total_days = $this->get_total_days($month, $year);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Set the starting day of the week
207 $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
Andrey Andreev56454792012-05-17 14:32:19 +0300208 $start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // Set the starting day number
211 $local_date = mktime(12, 0, 0, $month, 1, $year);
212 $date = getdate($local_date);
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300213 $day = $start_day + 1 - $date['wday'];
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 while ($day > 1)
216 {
217 $day -= 7;
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // Set the current month/year/day
221 // We use this to determine the "today" date
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200222 $cur_year = date('Y', $this->local_time);
223 $cur_month = date('m', $this->local_time);
224 $cur_day = date('j', $this->local_time);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300226 $is_current_month = ($cur_year == $year && $cur_month == $month);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // Generate the template data array
229 $this->parse_template();
Derek Allard2067d1a2008-11-13 22:59:24 +0000230
Barry Mienydd671972010-10-04 16:33:58 +0200231 // Begin building the calendar output
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200232 $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n";
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // "previous" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100235 if ($this->show_next_prev === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200237 // Add a trailing slash to the URL if needed
238 $this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $adjusted_date = $this->adjust_date($month - 1, $year);
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200241 $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 +0000242 }
243
244 // Heading containing the month/year
Alex Bilbied261b1e2012-06-02 11:12:16 +0100245 $colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
Barry Mienydd671972010-10-04 16:33:58 +0200246
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200247 $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
248 str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
Barry Mienydd671972010-10-04 16:33:58 +0200249
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200250 $out .= $this->temp['heading_title_cell']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000251
252 // "next" month link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100253 if ($this->show_next_prev === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200254 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $adjusted_date = $this->adjust_date($month + 1, $year);
256 $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
257 }
258
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200259 $out .= "\n".$this->temp['heading_row_end']."\n\n"
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200260 // Write the cells containing the days of the week
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200261 .$this->temp['week_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
263 $day_names = $this->get_day_names();
264
265 for ($i = 0; $i < 7; $i ++)
266 {
267 $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
268 }
269
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200270 $out .= "\n".$this->temp['week_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
272 // Build the main body of the calendar
273 while ($day <= $total_days)
274 {
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200275 $out .= "\n".$this->temp['cal_row_start']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000276
277 for ($i = 0; $i < 7; $i++)
278 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300279 if ($day > 0 && $day <= $total_days)
Barry Mienydd671972010-10-04 16:33:58 +0200280 {
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200281 $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if (isset($data[$day]))
Barry Mienydd671972010-10-04 16:33:58 +0200284 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // Cells with content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300286 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200287 $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200288 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290 else
291 {
292 // Cells with no content
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300293 $temp = ($is_current_month === TRUE && $day == $cur_day) ?
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200294 $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 $out .= str_replace('{day}', $day, $temp);
296 }
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200297
298 $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 +0000299 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200300 elseif ($this->show_other_days === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200302 $out .= $this->temp['cal_cell_start_other'];
303
304 if ($day <= 0)
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200305 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200306 // Day of previous month
Marcos SF Filho2e914b72014-01-09 09:20:55 -0200307 $prev_month = $this->adjust_date($month - 1, $year);
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200308 $prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
309 $out .= str_replace('{day}', $prev_month_days + $day, $this->temp['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200310 }
311 else
312 {
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200313 // Day of next month
314 $out .= str_replace('{day}', $day - $total_days, $this->temp['cal_cell_other']);
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200315 }
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200316
317 $out .= $this->temp['cal_cell_end_other'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319 else
320 {
321 // Blank cells
Marcos SF Filhoa593e3e2014-01-08 18:22:12 -0200322 $out .= $this->temp['cal_cell_start'].$this->temp['cal_cell_blank'].$this->temp['cal_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 $day++;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200328 $out .= "\n".$this->temp['cal_row_end']."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
Andrey Andreev56454792012-05-17 14:32:19 +0300331 return $out .= "\n".$this->temp['table_close'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
337 * Get Month Name
338 *
339 * Generates a textual month name based on the numeric
340 * month provided.
341 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300342 * @param int the month
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @return string
344 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200345 public function get_month_name($month)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100347 if ($this->month_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $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');
350 }
351 else
352 {
Derek Allarddefaa172009-08-17 17:24:31 +0000353 $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 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev56454792012-05-17 14:32:19 +0300356 return ($this->CI->lang->line($month_names[$month]) === FALSE)
357 ? ucfirst(substr($month_names[$month], 4))
358 : $this->CI->lang->line($month_names[$month]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Get Day Names
365 *
366 * Returns an array of day names (Sunday, Monday, etc.) based
Derek Jonesa7561252013-07-21 14:21:41 -0700367 * on the type. Options: long, short, abr
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param string
370 * @return array
371 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200372 public function get_day_names($day_type = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100374 if ($day_type !== '')
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200375 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 $this->day_type = $day_type;
Andrey Andreevc7dc07e2011-12-24 16:48:50 +0200377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Andrey Andreev56454792012-05-17 14:32:19 +0300379 if ($this->day_type === 'long')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
382 }
Andrey Andreev56454792012-05-17 14:32:19 +0300383 elseif ($this->day_type === 'short')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
386 }
387 else
388 {
389 $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $days = array();
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200393 for ($i = 0, $c = count($day_names); $i < $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200395 $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 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 return $days;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 // --------------------------------------------------------------------
402
403 /**
404 * Adjust Date
405 *
406 * This function makes sure that we have a valid month/year.
407 * For example, if you submit 13 as the month, the year will
408 * increment and the month will become January.
409 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300410 * @param int the month
411 * @param int the year
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return array
413 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200414 public function adjust_date($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 $date = array();
417
418 $date['month'] = $month;
419 $date['year'] = $year;
420
421 while ($date['month'] > 12)
422 {
423 $date['month'] -= 12;
424 $date['year']++;
425 }
426
427 while ($date['month'] <= 0)
428 {
429 $date['month'] += 12;
430 $date['year']--;
431 }
432
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200433 if (strlen($date['month']) === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 $date['month'] = '0'.$date['month'];
436 }
437
438 return $date;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * Total days in a given month
445 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300446 * @param int the month
447 * @param int the year
448 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200450 public function get_total_days($month, $year)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
453
454 if ($month < 1 OR $month > 12)
455 {
456 return 0;
457 }
458
459 // Is the year a leap year?
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300460 if ($month == 2)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100462 if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 return 29;
465 }
466 }
467
468 return $days_in_month[$month - 1];
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Set Default Template Data
475 *
476 * This is used in the event that the user has not created their own template
477 *
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300478 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200480 public function default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200482 return array(
Timothy Warren68f09812012-04-27 10:38:32 -0400483 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
484 'heading_row_start' => '<tr>',
485 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
486 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
487 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
488 'heading_row_end' => '</tr>',
489 'week_row_start' => '<tr>',
490 'week_day_cell' => '<td>{week_day}</td>',
491 'week_row_end' => '</tr>',
492 'cal_row_start' => '<tr>',
493 'cal_cell_start' => '<td>',
494 'cal_cell_start_today' => '<td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200495 'cal_cell_start_other' => '<td style="color: #666;">',
Timothy Warren68f09812012-04-27 10:38:32 -0400496 'cal_cell_content' => '<a href="{content}">{day}</a>',
497 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
498 'cal_cell_no_content' => '{day}',
499 'cal_cell_no_content_today' => '<strong>{day}</strong>',
500 'cal_cell_blank' => '&nbsp;',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200501 'cal_cell_other' => '{day}',
Timothy Warren68f09812012-04-27 10:38:32 -0400502 'cal_cell_end' => '</td>',
503 'cal_cell_end_today' => '</td>',
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200504 'cal_cell_end_other' => '</td>',
Timothy Warren68f09812012-04-27 10:38:32 -0400505 'cal_row_end' => '</tr>',
506 'table_close' => '</table>'
507 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 // --------------------------------------------------------------------
511
512 /**
513 * Parse Template
514 *
515 * Harvests the data within the template {pseudo-variables}
516 * used to display the calendar
517 *
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200518 * @return CI_Calendar
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 */
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200520 public function parse_template()
Barry Mienydd671972010-10-04 16:33:58 +0200521 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $this->temp = $this->default_template();
Barry Mienydd671972010-10-04 16:33:58 +0200523
Alex Bilbied261b1e2012-06-02 11:12:16 +0100524 if ($this->template === '')
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200526 return $this;
Barry Mienydd671972010-10-04 16:33:58 +0200527 }
528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 $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 +0200530
Marcos SF Filho7977e1d2014-01-08 15:34:11 -0200531 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)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300533 if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200535 $this->temp[$val] = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200537 elseif (in_array($val, $today, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Andrey Andreeve8dfc1b2011-12-24 01:22:19 +0200539 $this->temp[$val] = $this->temp[substr($val, 0, -6)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200542
543 return $this;
Barry Mienydd671972010-10-04 16:33:58 +0200544 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000545
546}
547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548/* End of file Calendar.php */
Andrey Andreev6dbabb52012-03-26 15:41:48 +0300549/* Location: ./system/libraries/Calendar.php */