blob: 8fdacf1d75d3b53aeb785ab8f452327e4476ad4a [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#################
2Calendaring Class
3#################
4
5The Calendar class enables you to dynamically create calendars. Your
6calendars can be formatted through the use of a calendar template,
7allowing 100% control over every aspect of its design. In addition, you
8can pass data to your calendar cells.
9
Derek Jonesa7561252013-07-21 14:21:41 -070010.. contents::
11 :local:
12
13.. raw:: html
14
15 <div class="custom-index container"></div>
16
17***************************
18Using the Calendaring Class
19***************************
20
Derek Jones8ede1a22011-10-05 13:34:52 -050021Initializing the Class
22======================
23
24Like most other classes in CodeIgniter, the Calendar class is
25initialized in your controller using the $this->load->library function::
26
27 $this->load->library('calendar');
28
29Once loaded, the Calendar object will be available using::
30
31 $this->calendar
32
33Displaying a Calendar
34=====================
35
36Here is a very simple example showing how you can display a calendar::
37
38 $this->load->library('calendar');
39 echo $this->calendar->generate();
40
41The above code will generate a calendar for the current month/year based
42on your server time. To show a calendar for a specific month and year
43you will pass this information to the calendar generating function::
44
45 $this->load->library('calendar');
46 echo $this->calendar->generate(2006, 6);
47
48The above code will generate a calendar showing the month of June in
492006. The first parameter specifies the year, the second parameter
50specifies the month.
51
52Passing Data to your Calendar Cells
53===================================
54
55To add data to your calendar cells involves creating an associative
56array in which the keys correspond to the days you wish to populate and
57the array value contains the data. The array is passed to the third
58parameter of the calendar generating function. Consider this example::
59
60 $this->load->library('calendar');
61
62 $data = array(
Andrey Andreevc1721f12016-03-07 10:03:58 +020063 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 Andreev9bf3cf22014-02-18 16:39:46 +020067 );
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69 echo $this->calendar->generate(2006, 6, $data);
70
71Using the above example, day numbers 3, 7, 13, and 26 will become links
72pointing 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
79Setting Display Preferences
80===========================
81
82There are seven preferences you can set to control various aspects of
83the calendar. Preferences are set by passing an array of preferences in
84the second parameter of the loading function. Here is an example::
85
Andrey Andreev9bf3cf22014-02-18 16:39:46 +020086 $prefs = array(
87 'start_day' => 'saturday',
88 'month_type' => 'long',
89 'day_type' => 'short'
90 );
Derek Jones8ede1a22011-10-05 13:34:52 -050091
92 $this->load->library('calendar', $prefs);
93
94 echo $this->calendar->generate();
95
96The above code would start the calendar on saturday, use the "long"
97month heading, and the "short" day names. More information regarding
98preferences below.
99
Andrey Andreevea801ab2014-01-20 15:03:43 +0200100====================== ================= ============================================ ===================================================================
101Preference Default Options Description
102====================== ================= ============================================ ===================================================================
Andrew1b6f6922014-02-17 12:55:16 -0600103**template** None None A string or array containing your calendar template.
Andrey Andreevea801ab2014-01-20 15:03:43 +0200104 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 Jones8ede1a22011-10-05 13:34:52 -0500117
118
119Showing Next/Previous Month Links
120=================================
121
122To allow your calendar to dynamically increment/decrement via the
123next/previous links requires that you set up your calendar code similar
124to this example::
125
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200126 $prefs = array(
127 'show_next_prev' => TRUE,
128 'next_prev_url' => 'http://example.com/index.php/calendar/show/'
129 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131 $this->load->library('calendar', $prefs);
132
133 echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
134
135You'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 Filho2e914b72014-01-09 09:20:55 -0200139 the "next_prev_url" preference. If you don't, it will be set to the current
140 *controller/method*.
Derek Jones8ede1a22011-10-05 13:34:52 -0500141- 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
146Creating a Calendar Template
147============================
148
149By creating a calendar template you have 100% control over the design of
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200150your calendar. Using the string method, each component of your calendar
151will be placed within a pair of pseudo-variables as shown here::
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
153 $prefs['template'] = '
154
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200155 {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200157 {heading_row_start}<tr>{/heading_row_start}
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200159 {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</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}">&gt;&gt;</a></th>{/heading_next_cell}
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200163 {heading_row_end}</tr>{/heading_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200165 {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 Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200169 {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 Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200174 {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 Jones8ede1a22011-10-05 13:34:52 -0500176
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200177 {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 Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200180 {cal_cell_blank}&nbsp;{/cal_cell_blank}
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Дмитрий2845dec2015-09-27 07:34:11 +0800182 {cal_cell_other}{day}{/cal_cel_other}
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200183
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200184 {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 Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200189 {table_close}</table>{/table_close}
Derek Jones8ede1a22011-10-05 13:34:52 -0500190 ';
191
192 $this->load->library('calendar', $prefs);
193
Derek Jonesa7561252013-07-21 14:21:41 -0700194 echo $this->calendar->generate();
195
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200196Using the array method, you will pass `key => value` pairs. You can pass as
197many or as few values as you'd like. Omitted keys will use the default values
198inherited in the calendar class.
Andrew1b6f6922014-02-17 12:55:16 -0600199
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200200Example::
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 );
Andrew1b6f6922014-02-17 12:55:16 -0600207
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200208 $this->load->library('calendar', $prefs);
Andrew1b6f6922014-02-17 12:55:16 -0600209
Andrey Andreev9bf3cf22014-02-18 16:39:46 +0200210 echo $this->calendar->generate();
Andrew1b6f6922014-02-17 12:55:16 -0600211
Derek Jonesa7561252013-07-21 14:21:41 -0700212***************
213Class Reference
214***************
215
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200216.. php:class:: CI_Calendar
Derek Jonesa7561252013-07-21 14:21:41 -0700217
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200218 .. php:method:: initialize([$config = array()])
Derek Jonesa7561252013-07-21 14:21:41 -0700219
Andrey Andreev28c2c972014-02-08 04:27:48 +0200220 :param array $config: Configuration parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200221 :returns: CI_Calendar instance (method chaining)
222 :rtype: CI_Calendar
Derek Jonesa7561252013-07-21 14:21:41 -0700223
224 Initializes the Calendaring preferences. Accepts an associative array as input, containing display preferences.
225
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200226 .. php:method:: generate([$year = ''[, $month = ''[, $data = array()]]])
Derek Jonesa7561252013-07-21 14:21:41 -0700227
Andrey Andreev28c2c972014-02-08 04:27:48 +0200228 :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 Jonesa7561252013-07-21 14:21:41 -0700233
234 Generate the calendar.
235
236
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200237 .. php:method:: get_month_name($month)
Derek Jonesa7561252013-07-21 14:21:41 -0700238
Andrey Andreev28c2c972014-02-08 04:27:48 +0200239 :param int $month: Month
240 :returns: Month name
241 :rtype: string
Derek Jonesa7561252013-07-21 14:21:41 -0700242
243 Generates a textual month name based on the numeric month provided.
244
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200245 .. php:method:: get_day_names($day_type = '')
Derek Jonesa7561252013-07-21 14:21:41 -0700246
Andrey Andreev28c2c972014-02-08 04:27:48 +0200247 :param string $day_type: 'long', 'short', or 'abr'
248 :returns: Array of day names
249 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700250
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 Andreevcd3d9db2015-02-02 13:41:01 +0200256 .. php:method:: adjust_date($month, $year)
Derek Jonesa7561252013-07-21 14:21:41 -0700257
Andrey Andreev28c2c972014-02-08 04:27:48 +0200258 :param int $month: Month
259 :param int $year: Year
260 :returns: An associative array containing month and year
261 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700262
Andrey Andreev28c2c972014-02-08 04:27:48 +0200263 This method makes sure that you have a valid month/year. For example, if
Derek Jonesa7561252013-07-21 14:21:41 -0700264 you submit 13 as the month, the year will increment and the month will
265 become January::
266
darwinel871754a2014-02-11 17:34:57 +0100267 print_r($this->calendar->adjust_date(13, 2014));
Derek Jonesa7561252013-07-21 14:21:41 -0700268
269 outputs::
270
271 Array
272 (    
273 [month] => '01'
darwinel871754a2014-02-11 17:34:57 +0100274 [year] => '2015'
Derek Jonesa7561252013-07-21 14:21:41 -0700275 )
276
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200277 .. php:method:: get_total_days($month, $year)
Derek Jonesa7561252013-07-21 14:21:41 -0700278
Andrey Andreev28c2c972014-02-08 04:27:48 +0200279 :param int $month: Month
280 :param int $year: Year
281 :returns: Count of days in the specified month
282 :rtype: int
Derek Jonesa7561252013-07-21 14:21:41 -0700283
284 Total days in a given month::
285
286 echo $this->calendar->get_total_days(2, 2012);
287 // 29
288
Andrey Andreevbe368a82014-02-20 15:46:05 +0200289 .. note:: This method is an alias for :doc:`Date Helper
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200290 <../helpers/date_helper>` function :php:func:`days_in_month()`.
Andrey Andreevbe368a82014-02-20 15:46:05 +0200291
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200292 .. php:method:: default_template()
Derek Jonesa7561252013-07-21 14:21:41 -0700293
Andrey Andreev28c2c972014-02-08 04:27:48 +0200294 :returns: An array of template values
295 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700296
297 Sets the default template. This method is used when you have not created
298 your own template.
299
300
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200301 .. php:method:: parse_template()
Derek Jonesa7561252013-07-21 14:21:41 -0700302
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200303 :returns: CI_Calendar instance (method chaining)
304 :rtype: CI_Calendar
Derek Jonesa7561252013-07-21 14:21:41 -0700305
306 Harvests the data within the template ``{pseudo-variables}`` used to
Дмитрий2845dec2015-09-27 07:34:11 +0800307 display the calendar.