Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################# |
| 2 | Calendaring Class |
| 3 | ################# |
| 4 | |
| 5 | The Calendar class enables you to dynamically create calendars. Your |
| 6 | calendars can be formatted through the use of a calendar template, |
| 7 | allowing 100% control over every aspect of its design. In addition, you |
| 8 | can pass data to your calendar cells. |
| 9 | |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 10 | .. contents:: |
| 11 | :local: |
| 12 | |
| 13 | .. raw:: html |
| 14 | |
| 15 | <div class="custom-index container"></div> |
| 16 | |
| 17 | *************************** |
| 18 | Using the Calendaring Class |
| 19 | *************************** |
| 20 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 21 | Initializing the Class |
| 22 | ====================== |
| 23 | |
| 24 | Like most other classes in CodeIgniter, the Calendar class is |
| 25 | initialized in your controller using the $this->load->library function:: |
| 26 | |
| 27 | $this->load->library('calendar'); |
| 28 | |
| 29 | Once loaded, the Calendar object will be available using:: |
| 30 | |
| 31 | $this->calendar |
| 32 | |
| 33 | Displaying a Calendar |
| 34 | ===================== |
| 35 | |
| 36 | Here is a very simple example showing how you can display a calendar:: |
| 37 | |
| 38 | $this->load->library('calendar'); |
| 39 | echo $this->calendar->generate(); |
| 40 | |
| 41 | The above code will generate a calendar for the current month/year based |
| 42 | on your server time. To show a calendar for a specific month and year |
| 43 | you will pass this information to the calendar generating function:: |
| 44 | |
| 45 | $this->load->library('calendar'); |
| 46 | echo $this->calendar->generate(2006, 6); |
| 47 | |
| 48 | The above code will generate a calendar showing the month of June in |
| 49 | 2006. The first parameter specifies the year, the second parameter |
| 50 | specifies the month. |
| 51 | |
| 52 | Passing Data to your Calendar Cells |
| 53 | =================================== |
| 54 | |
| 55 | To add data to your calendar cells involves creating an associative |
| 56 | array in which the keys correspond to the days you wish to populate and |
| 57 | the array value contains the data. The array is passed to the third |
| 58 | parameter of the calendar generating function. Consider this example:: |
| 59 | |
| 60 | $this->load->library('calendar'); |
| 61 | |
| 62 | $data = array( |
Andrey Andreev | c1721f1 | 2016-03-07 10:03:58 +0200 | [diff] [blame] | 63 | 3 => 'http://example.com/news/article/2006/06/03/', |
| 64 | 7 => 'http://example.com/news/article/2006/06/07/', |
| 65 | 13 => 'http://example.com/news/article/2006/06/13/', |
| 66 | 26 => 'http://example.com/news/article/2006/06/26/' |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 67 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 69 | echo $this->calendar->generate(2006, 6, $data); |
| 70 | |
| 71 | Using the above example, day numbers 3, 7, 13, and 26 will become links |
| 72 | pointing to the URLs you've provided. |
| 73 | |
| 74 | .. note:: By default it is assumed that your array will contain links. |
| 75 | In the section that explains the calendar template below you'll see how |
| 76 | you can customize how data passed to your cells is handled so you can |
| 77 | pass different types of information. |
| 78 | |
| 79 | Setting Display Preferences |
| 80 | =========================== |
| 81 | |
| 82 | There are seven preferences you can set to control various aspects of |
| 83 | the calendar. Preferences are set by passing an array of preferences in |
| 84 | the second parameter of the loading function. Here is an example:: |
| 85 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 86 | $prefs = array( |
| 87 | 'start_day' => 'saturday', |
| 88 | 'month_type' => 'long', |
| 89 | 'day_type' => 'short' |
| 90 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
| 92 | $this->load->library('calendar', $prefs); |
| 93 | |
| 94 | echo $this->calendar->generate(); |
| 95 | |
| 96 | The above code would start the calendar on saturday, use the "long" |
| 97 | month heading, and the "short" day names. More information regarding |
| 98 | preferences below. |
| 99 | |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 100 | ====================== ================= ============================================ =================================================================== |
| 101 | Preference Default Options Description |
| 102 | ====================== ================= ============================================ =================================================================== |
Andrew | 1b6f692 | 2014-02-17 12:55:16 -0600 | [diff] [blame] | 103 | **template** None None A string or array containing your calendar template. |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 104 | See the template section below. |
| 105 | **local_time** time() None A Unix timestamp corresponding to the current time. |
| 106 | **start_day** sunday Any week day (sunday, monday, tuesday, etc.) Sets the day of the week the calendar should start on. |
| 107 | **month_type** long long, short Determines what version of the month name to use in the header. |
| 108 | long = January, short = Jan. |
| 109 | **day_type** abr long, short, abr Determines what version of the weekday names to use in |
| 110 | the column headers. long = Sunday, short = Sun, abr = Su. |
| 111 | **show_next_prev** FALSE TRUE/FALSE (boolean) Determines whether to display links allowing you to toggle |
| 112 | to next/previous months. See information on this feature below. |
| 113 | **next_prev_url** controller/method A URL Sets the basepath used in the next/previous calendar links. |
| 114 | **show_other_days** FALSE TRUE/FALSE (boolean) Determines whether to display days of other months that share the |
| 115 | first or last week of the calendar month. |
| 116 | ====================== ================= ============================================ =================================================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
| 118 | |
| 119 | Showing Next/Previous Month Links |
| 120 | ================================= |
| 121 | |
| 122 | To allow your calendar to dynamically increment/decrement via the |
| 123 | next/previous links requires that you set up your calendar code similar |
| 124 | to this example:: |
| 125 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 126 | $prefs = array( |
| 127 | 'show_next_prev' => TRUE, |
| 128 | 'next_prev_url' => 'http://example.com/index.php/calendar/show/' |
| 129 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | $this->load->library('calendar', $prefs); |
| 132 | |
| 133 | echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4)); |
| 134 | |
| 135 | You'll notice a few things about the above example: |
| 136 | |
| 137 | - You must set the "show_next_prev" to TRUE. |
| 138 | - You must supply the URL to the controller containing your calendar in |
Marcos SF Filho | 2e914b7 | 2014-01-09 09:20:55 -0200 | [diff] [blame] | 139 | the "next_prev_url" preference. If you don't, it will be set to the current |
| 140 | *controller/method*. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | - You must supply the "year" and "month" to the calendar generating |
| 142 | function via the URI segments where they appear (Note: The calendar |
| 143 | class automatically adds the year/month to the base URL you |
| 144 | provide.). |
| 145 | |
| 146 | Creating a Calendar Template |
| 147 | ============================ |
| 148 | |
| 149 | By creating a calendar template you have 100% control over the design of |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 150 | your calendar. Using the string method, each component of your calendar |
| 151 | will be placed within a pair of pseudo-variables as shown here:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
| 153 | $prefs['template'] = ' |
| 154 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 155 | {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 157 | {heading_row_start}<tr>{/heading_row_start} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 159 | {heading_previous_cell}<th><a href="{previous_url}"><<</a></th>{/heading_previous_cell} |
| 160 | {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell} |
| 161 | {heading_next_cell}<th><a href="{next_url}">>></a></th>{/heading_next_cell} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 163 | {heading_row_end}</tr>{/heading_row_end} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 165 | {week_row_start}<tr>{/week_row_start} |
| 166 | {week_day_cell}<td>{week_day}</td>{/week_day_cell} |
| 167 | {week_row_end}</tr>{/week_row_end} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 169 | {cal_row_start}<tr>{/cal_row_start} |
| 170 | {cal_cell_start}<td>{/cal_cell_start} |
| 171 | {cal_cell_start_today}<td>{/cal_cell_start_today} |
| 172 | {cal_cell_start_other}<td class="other-month">{/cal_cell_start_other} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 174 | {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content} |
| 175 | {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 177 | {cal_cell_no_content}{day}{/cal_cell_no_content} |
| 178 | {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 180 | {cal_cell_blank} {/cal_cell_blank} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Дмитрий | 2845dec | 2015-09-27 07:34:11 +0800 | [diff] [blame] | 182 | {cal_cell_other}{day}{/cal_cel_other} |
Marcos SF Filho | 0cd7c92 | 2014-01-08 19:38:00 -0200 | [diff] [blame] | 183 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 184 | {cal_cell_end}</td>{/cal_cell_end} |
| 185 | {cal_cell_end_today}</td>{/cal_cell_end_today} |
| 186 | {cal_cell_end_other}</td>{/cal_cell_end_other} |
| 187 | {cal_row_end}</tr>{/cal_row_end} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 189 | {table_close}</table>{/table_close} |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | '; |
| 191 | |
| 192 | $this->load->library('calendar', $prefs); |
| 193 | |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 194 | echo $this->calendar->generate(); |
| 195 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 196 | Using the array method, you will pass `key => value` pairs. You can pass as |
| 197 | many or as few values as you'd like. Omitted keys will use the default values |
| 198 | inherited in the calendar class. |
Andrew | 1b6f692 | 2014-02-17 12:55:16 -0600 | [diff] [blame] | 199 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 200 | Example:: |
| 201 | |
| 202 | $prefs['template'] = array( |
| 203 | 'table_open' => '<table class="calendar">', |
| 204 | 'cal_cell_start' => '<td class="day">', |
| 205 | 'cal_cell_start_today' => '<td class="today">' |
| 206 | ); |
Andrew | 1b6f692 | 2014-02-17 12:55:16 -0600 | [diff] [blame] | 207 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 208 | $this->load->library('calendar', $prefs); |
Andrew | 1b6f692 | 2014-02-17 12:55:16 -0600 | [diff] [blame] | 209 | |
Andrey Andreev | 9bf3cf2 | 2014-02-18 16:39:46 +0200 | [diff] [blame] | 210 | echo $this->calendar->generate(); |
Andrew | 1b6f692 | 2014-02-17 12:55:16 -0600 | [diff] [blame] | 211 | |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 212 | *************** |
| 213 | Class Reference |
| 214 | *************** |
| 215 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 216 | .. php:class:: CI_Calendar |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 217 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 218 | .. php:method:: initialize([$config = array()]) |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 219 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 220 | :param array $config: Configuration parameters |
Andrey Andreev | 6f6102c | 2014-02-08 19:11:40 +0200 | [diff] [blame] | 221 | :returns: CI_Calendar instance (method chaining) |
| 222 | :rtype: CI_Calendar |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 223 | |
| 224 | Initializes the Calendaring preferences. Accepts an associative array as input, containing display preferences. |
| 225 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 226 | .. php:method:: generate([$year = ''[, $month = ''[, $data = array()]]]) |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 227 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 228 | :param int $year: Year |
| 229 | :param int $month: Month |
| 230 | :param array $data: Data to be shown in the calendar cells |
| 231 | :returns: HTML-formatted calendar |
| 232 | :rtype: string |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 233 | |
| 234 | Generate the calendar. |
| 235 | |
| 236 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 237 | .. php:method:: get_month_name($month) |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 238 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 239 | :param int $month: Month |
| 240 | :returns: Month name |
| 241 | :rtype: string |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 242 | |
| 243 | Generates a textual month name based on the numeric month provided. |
| 244 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 245 | .. php:method:: get_day_names($day_type = '') |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 246 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 247 | :param string $day_type: 'long', 'short', or 'abr' |
| 248 | :returns: Array of day names |
| 249 | :rtype: array |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 250 | |
| 251 | Returns an array of day names (Sunday, Monday, etc.) based on the type |
| 252 | provided. Options: long, short, abr. If no ``$day_type`` is provided (or |
| 253 | if an invalid type is provided) this method will return the "abbreviated" |
| 254 | style. |
| 255 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 256 | .. php:method:: adjust_date($month, $year) |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 257 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 258 | :param int $month: Month |
| 259 | :param int $year: Year |
| 260 | :returns: An associative array containing month and year |
| 261 | :rtype: array |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 262 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 263 | This method makes sure that you have a valid month/year. For example, if |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 264 | you submit 13 as the month, the year will increment and the month will |
| 265 | become January:: |
| 266 | |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 267 | print_r($this->calendar->adjust_date(13, 2014)); |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 268 | |
| 269 | outputs:: |
| 270 | |
| 271 | Array |
| 272 | ( |
| 273 | [month] => '01' |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 274 | [year] => '2015' |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 275 | ) |
| 276 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 277 | .. php:method:: get_total_days($month, $year) |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 278 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 279 | :param int $month: Month |
| 280 | :param int $year: Year |
| 281 | :returns: Count of days in the specified month |
| 282 | :rtype: int |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 283 | |
| 284 | Total days in a given month:: |
| 285 | |
| 286 | echo $this->calendar->get_total_days(2, 2012); |
| 287 | // 29 |
| 288 | |
Andrey Andreev | be368a8 | 2014-02-20 15:46:05 +0200 | [diff] [blame] | 289 | .. note:: This method is an alias for :doc:`Date Helper |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 290 | <../helpers/date_helper>` function :php:func:`days_in_month()`. |
Andrey Andreev | be368a8 | 2014-02-20 15:46:05 +0200 | [diff] [blame] | 291 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 292 | .. php:method:: default_template() |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 293 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 294 | :returns: An array of template values |
| 295 | :rtype: array |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 296 | |
| 297 | Sets the default template. This method is used when you have not created |
| 298 | your own template. |
| 299 | |
| 300 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 301 | .. php:method:: parse_template() |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 302 | |
Andrey Andreev | 6f6102c | 2014-02-08 19:11:40 +0200 | [diff] [blame] | 303 | :returns: CI_Calendar instance (method chaining) |
| 304 | :rtype: CI_Calendar |
Derek Jones | a756125 | 2013-07-21 14:21:41 -0700 | [diff] [blame] | 305 | |
| 306 | Harvests the data within the template ``{pseudo-variables}`` used to |
Дмитрий | 2845dec | 2015-09-27 07:34:11 +0800 | [diff] [blame] | 307 | display the calendar. |