Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Calendar Class |
| 32 | * |
| 33 | * This class enables the creation of calendars |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/calendar.html |
| 40 | */ |
| 41 | class CI_Calendar { |
| 42 | |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 43 | private $CI; |
| 44 | public $lang; |
| 45 | public $local_time; |
| 46 | public $template = ''; |
| 47 | public $start_day = 'sunday'; |
| 48 | public $month_type = 'long'; |
| 49 | public $day_type = 'abr'; |
| 50 | public $show_next_prev = FALSE; |
| 51 | public $next_prev_url = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Constructor |
| 55 | * |
| 56 | * Loads the calendar language file and sets the default time reference |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 58 | public function __construct($config = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | $this->CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 62 | if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | { |
| 64 | $this->CI->lang->load('calendar'); |
| 65 | } |
| 66 | |
| 67 | $this->local_time = time(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | if (count($config) > 0) |
| 70 | { |
| 71 | $this->initialize($config); |
| 72 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | log_message('debug', "Calendar Class Initialized"); |
| 75 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | /** |
| 80 | * Initialize the user preferences |
| 81 | * |
| 82 | * Accepts an associative array as input, containing display preferences |
| 83 | * |
| 84 | * @access public |
| 85 | * @param array config preferences |
| 86 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 88 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
| 90 | foreach ($config as $key => $val) |
| 91 | { |
| 92 | if (isset($this->$key)) |
| 93 | { |
| 94 | $this->$key = $val; |
| 95 | } |
| 96 | } |
| 97 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Generate the calendar |
| 103 | * |
| 104 | * @access public |
| 105 | * @param integer the year |
| 106 | * @param integer the month |
| 107 | * @param array the data to be shown in the calendar cells |
| 108 | * @return string |
| 109 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 110 | public function generate($year = '', $month = '', $data = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | { |
| 112 | // Set and validate the supplied month/year |
| 113 | if ($year == '') |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 114 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 115 | $year = date('Y', $this->local_time); |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 116 | } |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 117 | elseif (strlen($year) === 1) |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 118 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | $year = '200'.$year; |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 120 | } |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 121 | elseif (strlen($year) === 2) |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 122 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | $year = '20'.$year; |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 124 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 126 | if ($month == '') |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 127 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 128 | $month = date('m', $this->local_time); |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 129 | } |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 130 | elseif (strlen($month) === 1) |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 131 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | $month = '0'.$month; |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 133 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | $adjusted_date = $this->adjust_date($month, $year); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | $month = $adjusted_date['month']; |
| 138 | $year = $adjusted_date['year']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | // Determine the total days in the month |
| 141 | $total_days = $this->get_total_days($month, $year); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | // Set the starting day of the week |
| 144 | $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6); |
| 145 | $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | // Set the starting day number |
| 148 | $local_date = mktime(12, 0, 0, $month, 1, $year); |
| 149 | $date = getdate($local_date); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 150 | $day = $start_day + 1 - $date["wday"]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | while ($day > 1) |
| 153 | { |
| 154 | $day -= 7; |
| 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | // Set the current month/year/day |
| 158 | // We use this to determine the "today" date |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 159 | $cur_year = date('Y', $this->local_time); |
| 160 | $cur_month = date('m', $this->local_time); |
| 161 | $cur_day = date('j', $this->local_time); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | // Generate the template data array |
| 166 | $this->parse_template(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | // Begin building the calendar output |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 169 | $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | // "previous" month link |
| 172 | if ($this->show_next_prev == TRUE) |
| 173 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 174 | // Add a trailing slash to the URL if needed |
| 175 | $this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->next_prev_url); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | $adjusted_date = $this->adjust_date($month - 1, $year); |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 178 | $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell'])."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Heading containing the month/year |
| 182 | $colspan = ($this->show_next_prev == TRUE) ? 5 : 7; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 184 | $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, |
| 185 | str_replace('{heading}', $this->get_month_name($month).' '.$year, $this->temp['heading_title_cell'])); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 187 | $out .= $this->temp['heading_title_cell']."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | |
| 189 | // "next" month link |
| 190 | if ($this->show_next_prev == TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | $adjusted_date = $this->adjust_date($month + 1, $year); |
| 193 | $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']); |
| 194 | } |
| 195 | |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 196 | $out .= "\n".$this->temp['heading_row_end']."\n\n" |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 197 | // Write the cells containing the days of the week |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 198 | .$this->temp['week_row_start']."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
| 200 | $day_names = $this->get_day_names(); |
| 201 | |
| 202 | for ($i = 0; $i < 7; $i ++) |
| 203 | { |
| 204 | $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']); |
| 205 | } |
| 206 | |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 207 | $out .= "\n".$this->temp['week_row_end']."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | |
| 209 | // Build the main body of the calendar |
| 210 | while ($day <= $total_days) |
| 211 | { |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 212 | $out .= "\n".$this->temp['cal_row_start']."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | |
| 214 | for ($i = 0; $i < 7; $i++) |
| 215 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 216 | $out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | if ($day > 0 AND $day <= $total_days) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | if (isset($data[$day])) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // Cells with content |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 223 | $temp = ($is_current_month === TRUE AND $day == $cur_day) ? |
| 224 | $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 225 | $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | { |
| 229 | // Cells with no content |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 230 | $temp = ($is_current_month === TRUE AND $day == $cur_day) ? |
| 231 | $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | $out .= str_replace('{day}', $day, $temp); |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | // Blank cells |
| 238 | $out .= $this->temp['cal_cell_blank']; |
| 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 241 | $out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | $day++; |
| 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 245 | $out .= "\n".$this->temp['cal_row_end']."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 248 | $out .= "\n".$this->temp['table_close']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | |
| 250 | return $out; |
| 251 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | // -------------------------------------------------------------------- |
| 254 | |
| 255 | /** |
| 256 | * Get Month Name |
| 257 | * |
| 258 | * Generates a textual month name based on the numeric |
| 259 | * month provided. |
| 260 | * |
| 261 | * @access public |
| 262 | * @param integer the month |
| 263 | * @return string |
| 264 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 265 | public function get_month_name($month) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
| 267 | if ($this->month_type == 'short') |
| 268 | { |
| 269 | $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'); |
| 270 | } |
| 271 | else |
| 272 | { |
Derek Allard | defaa17 | 2009-08-17 17:24:31 +0000 | [diff] [blame] | 273 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | $month = $month_names[$month]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | if ($this->CI->lang->line($month) === FALSE) |
| 279 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 280 | return ucfirst(substr($month, 4)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return $this->CI->lang->line($month); |
| 284 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | // -------------------------------------------------------------------- |
| 287 | |
| 288 | /** |
| 289 | * Get Day Names |
| 290 | * |
| 291 | * Returns an array of day names (Sunday, Monday, etc.) based |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 292 | * on the type. Options: long, short, abrev |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | * |
| 294 | * @access public |
| 295 | * @param string |
| 296 | * @return array |
| 297 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 298 | public function get_day_names($day_type = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | { |
| 300 | if ($day_type != '') |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 301 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | $this->day_type = $day_type; |
Andrey Andreev | c7dc07e | 2011-12-24 16:48:50 +0200 | [diff] [blame^] | 303 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | if ($this->day_type == 'long') |
| 306 | { |
| 307 | $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'); |
| 308 | } |
| 309 | elseif ($this->day_type == 'short') |
| 310 | { |
| 311 | $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa'); |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | $days = array(); |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 319 | for ($i = 0, $c = count($day_names); $i < $c; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 321 | $days[] = ($this->CI->lang->line('cal_'.$day_names[$i]) === FALSE) ? ucfirst($day_names[$i]) : $this->CI->lang->line('cal_'.$day_names[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | return $days; |
| 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
| 330 | * Adjust Date |
| 331 | * |
| 332 | * This function makes sure that we have a valid month/year. |
| 333 | * For example, if you submit 13 as the month, the year will |
| 334 | * increment and the month will become January. |
| 335 | * |
| 336 | * @access public |
| 337 | * @param integer the month |
| 338 | * @param integer the year |
| 339 | * @return array |
| 340 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 341 | public function adjust_date($month, $year) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | { |
| 343 | $date = array(); |
| 344 | |
| 345 | $date['month'] = $month; |
| 346 | $date['year'] = $year; |
| 347 | |
| 348 | while ($date['month'] > 12) |
| 349 | { |
| 350 | $date['month'] -= 12; |
| 351 | $date['year']++; |
| 352 | } |
| 353 | |
| 354 | while ($date['month'] <= 0) |
| 355 | { |
| 356 | $date['month'] += 12; |
| 357 | $date['year']--; |
| 358 | } |
| 359 | |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 360 | if (strlen($date['month']) === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | { |
| 362 | $date['month'] = '0'.$date['month']; |
| 363 | } |
| 364 | |
| 365 | return $date; |
| 366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | // -------------------------------------------------------------------- |
| 369 | |
| 370 | /** |
| 371 | * Total days in a given month |
| 372 | * |
| 373 | * @access public |
| 374 | * @param integer the month |
| 375 | * @param integer the year |
| 376 | * @return integer |
| 377 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 378 | public function get_total_days($month, $year) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | { |
| 380 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
| 381 | |
| 382 | if ($month < 1 OR $month > 12) |
| 383 | { |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | // Is the year a leap year? |
| 388 | if ($month == 2) |
| 389 | { |
| 390 | if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0)) |
| 391 | { |
| 392 | return 29; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return $days_in_month[$month - 1]; |
| 397 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * Set Default Template Data |
| 403 | * |
| 404 | * This is used in the event that the user has not created their own template |
| 405 | * |
| 406 | * @access public |
| 407 | * @return array |
| 408 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 409 | public function default_template() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 411 | return array ( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 413 | 'heading_row_start' => '<tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | 'heading_previous_cell' => '<th><a href="{previous_url}"><<</a></th>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 415 | 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>', |
| 416 | 'heading_next_cell' => '<th><a href="{next_url}">>></a></th>', |
| 417 | 'heading_row_end' => '</tr>', |
| 418 | 'week_row_start' => '<tr>', |
| 419 | 'week_day_cell' => '<td>{week_day}</td>', |
| 420 | 'week_row_end' => '</tr>', |
| 421 | 'cal_row_start' => '<tr>', |
| 422 | 'cal_cell_start' => '<td>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | 'cal_cell_start_today' => '<td>', |
| 424 | 'cal_cell_content' => '<a href="{content}">{day}</a>', |
| 425 | 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>', |
| 426 | 'cal_cell_no_content' => '{day}', |
| 427 | 'cal_cell_no_content_today' => '<strong>{day}</strong>', |
| 428 | 'cal_cell_blank' => ' ', |
| 429 | 'cal_cell_end' => '</td>', |
| 430 | 'cal_cell_end_today' => '</td>', |
| 431 | 'cal_row_end' => '</tr>', |
| 432 | 'table_close' => '</table>' |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
| 439 | * Parse Template |
| 440 | * |
| 441 | * Harvests the data within the template {pseudo-variables} |
| 442 | * used to display the calendar |
| 443 | * |
| 444 | * @access public |
| 445 | * @return void |
| 446 | */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 447 | public function parse_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | $this->temp = $this->default_template(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | |
| 451 | if ($this->template == '') |
| 452 | { |
| 453 | return; |
| 454 | } |
| 455 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 458 | foreach (array('table_open', 'table_close', 'heading_row_start', 'heading_previous_cell', 'heading_title_cell', 'heading_next_cell', 'heading_row_end', 'week_row_start', 'week_day_cell', 'week_row_end', 'cal_row_start', 'cal_cell_start', 'cal_cell_content', 'cal_cell_no_content', 'cal_cell_blank', 'cal_cell_end', 'cal_row_end', 'cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today') as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | { |
| 460 | if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match)) |
| 461 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 462 | $this->temp[$val] = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 464 | elseif (in_array($val, $today, TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 466 | $this->temp[$val] = $this->temp[substr($val, 0, -6)]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | |
| 471 | } |
| 472 | |
| 473 | // END CI_Calendar class |
| 474 | |
| 475 | /* End of file Calendar.php */ |
Andrey Andreev | e8dfc1b | 2011-12-24 01:22:19 +0200 | [diff] [blame] | 476 | /* Location: ./system/libraries/Calendar.php */ |