blob: 65a447a3da337d2dea0fcec71f17d5b7d95bfe7d [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(
Derek Jones3ab40a62011-10-05 16:19:27 -050063 3 => 'http://example.com/news/article/2006/03/',
64 7 => 'http://example.com/news/article/2006/07/',
65 13 => 'http://example.com/news/article/2006/13/',
66 26 => 'http://example.com/news/article/2006/26/'
67 );
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
86 $prefs = array (
Derek Jones3ab40a62011-10-05 16:19:27 -050087 '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====================== ================= ============================================ ===================================================================
103**template** None None A string containing your calendar template.
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 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
126 $prefs = array (
Derek Jones3ab40a62011-10-05 16:19:27 -0500127 '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
150your calendar. Each component of your calendar will be placed within a
151pair of pseudo-variables as shown here::
152
153 $prefs['template'] = '
154
Derek Jones3ab40a62011-10-05 16:19:27 -0500155 {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Derek Jones3ab40a62011-10-05 16:19:27 -0500157 {heading_row_start}<tr>{/heading_row_start}
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Derek Jones3ab40a62011-10-05 16:19:27 -0500159 {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
Derek Jones3ab40a62011-10-05 16:19:27 -0500163 {heading_row_end}</tr>{/heading_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Derek Jones3ab40a62011-10-05 16:19:27 -0500165 {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
Derek Jones3ab40a62011-10-05 16:19:27 -0500169 {cal_row_start}<tr>{/cal_row_start}
170 {cal_cell_start}<td>{/cal_cell_start}
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200171 {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
Derek Jones3ab40a62011-10-05 16:19:27 -0500174 {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
Derek Jones3ab40a62011-10-05 16:19:27 -0500177 {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
Derek Jones3ab40a62011-10-05 16:19:27 -0500180 {cal_cell_blank}&nbsp;{/cal_cell_blank}
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200182 {cal_cell_other}{day}{cal_cel_other}
183
Derek Jones3ab40a62011-10-05 16:19:27 -0500184 {cal_cell_end}</td>{/cal_cell_end}
Marcos SF Filho0cd7c922014-01-08 19:38:00 -0200185 {cal_cell_end_today}</td>{/cal_cell_end_today}
186 {cal_cell_end_other}</td>{/cal_cell_end_other}
Derek Jones3ab40a62011-10-05 16:19:27 -0500187 {cal_row_end}</tr>{/cal_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Derek Jones3ab40a62011-10-05 16:19:27 -0500189 {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
196***************
197Class Reference
198***************
199
200.. class:: CI_Calendar
201
202 .. method:: initialize([$config = array()])
203
Andrey Andreev28c2c972014-02-08 04:27:48 +0200204 :param array $config: Configuration parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200205 :returns: CI_Calendar instance (method chaining)
206 :rtype: CI_Calendar
Derek Jonesa7561252013-07-21 14:21:41 -0700207
208 Initializes the Calendaring preferences. Accepts an associative array as input, containing display preferences.
209
Derek Jonesa7561252013-07-21 14:21:41 -0700210 .. method:: generate([$year = ''[, $month = ''[, $data = array()]]])
211
Andrey Andreev28c2c972014-02-08 04:27:48 +0200212 :param int $year: Year
213 :param int $month: Month
214 :param array $data: Data to be shown in the calendar cells
215 :returns: HTML-formatted calendar
216 :rtype: string
Derek Jonesa7561252013-07-21 14:21:41 -0700217
218 Generate the calendar.
219
220
221 .. method:: get_month_name($month)
222
Andrey Andreev28c2c972014-02-08 04:27:48 +0200223 :param int $month: Month
224 :returns: Month name
225 :rtype: string
Derek Jonesa7561252013-07-21 14:21:41 -0700226
227 Generates a textual month name based on the numeric month provided.
228
Derek Jonesa7561252013-07-21 14:21:41 -0700229 .. method:: get_day_names($day_type = '')
230
Andrey Andreev28c2c972014-02-08 04:27:48 +0200231 :param string $day_type: 'long', 'short', or 'abr'
232 :returns: Array of day names
233 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700234
235 Returns an array of day names (Sunday, Monday, etc.) based on the type
236 provided. Options: long, short, abr. If no ``$day_type`` is provided (or
237 if an invalid type is provided) this method will return the "abbreviated"
238 style.
239
Derek Jonesa7561252013-07-21 14:21:41 -0700240 .. method:: adjust_date($month, $year)
241
Andrey Andreev28c2c972014-02-08 04:27:48 +0200242 :param int $month: Month
243 :param int $year: Year
244 :returns: An associative array containing month and year
245 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700246
Andrey Andreev28c2c972014-02-08 04:27:48 +0200247 This method makes sure that you have a valid month/year. For example, if
Derek Jonesa7561252013-07-21 14:21:41 -0700248 you submit 13 as the month, the year will increment and the month will
249 become January::
250
251 print_r($this->calendar->adjust_date(13, 2013));
252
253 outputs::
254
255 Array
256 (    
257 [month] => '01'
258 [year] => '2014'
259 )
260
Derek Jonesa7561252013-07-21 14:21:41 -0700261 .. method:: get_total_days($month, $year)
262
Andrey Andreev28c2c972014-02-08 04:27:48 +0200263 :param int $month: Month
264 :param int $year: Year
265 :returns: Count of days in the specified month
266 :rtype: int
Derek Jonesa7561252013-07-21 14:21:41 -0700267
268 Total days in a given month::
269
270 echo $this->calendar->get_total_days(2, 2012);
271 // 29
272
Derek Jonesa7561252013-07-21 14:21:41 -0700273 .. method:: default_template()
274
Andrey Andreev28c2c972014-02-08 04:27:48 +0200275 :returns: An array of template values
276 :rtype: array
Derek Jonesa7561252013-07-21 14:21:41 -0700277
278 Sets the default template. This method is used when you have not created
279 your own template.
280
281
282 .. method:: parse_template()
283
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200284 :returns: CI_Calendar instance (method chaining)
285 :rtype: CI_Calendar
Derek Jonesa7561252013-07-21 14:21:41 -0700286
287 Harvests the data within the template ``{pseudo-variables}`` used to
288 display the calendar.