blob: d9a336d087e5a28f78b03b2d129f6759a72dcc5e [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
purwandi89f6f1a2011-10-07 19:58:22 +0700100====================== =========== =============================================== ===================================================================
Derek Jonesa7561252013-07-21 14:21:41 -0700101Preference Default Options Description
purwandi89f6f1a2011-10-07 19:58:22 +0700102====================== =========== =============================================== ===================================================================
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.
Derek Jonesa7561252013-07-21 14:21:41 -0700109**day_type** abr long, short, abr Determines what version of the weekday names to use in
purwandi89f6f1a2011-10-07 19:58:22 +0700110 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** None A URL Sets the basepath used in the next/previous calendar links.
114====================== =========== =============================================== ===================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
116
117Showing Next/Previous Month Links
118=================================
119
120To allow your calendar to dynamically increment/decrement via the
121next/previous links requires that you set up your calendar code similar
122to this example::
123
124 $prefs = array (
Derek Jones3ab40a62011-10-05 16:19:27 -0500125 'show_next_prev' => TRUE,
126 'next_prev_url' => 'http://example.com/index.php/calendar/show/'
127 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129 $this->load->library('calendar', $prefs);
130
131 echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
132
133You'll notice a few things about the above example:
134
135- You must set the "show_next_prev" to TRUE.
136- You must supply the URL to the controller containing your calendar in
137 the "next_prev_url" preference.
138- You must supply the "year" and "month" to the calendar generating
139 function via the URI segments where they appear (Note: The calendar
140 class automatically adds the year/month to the base URL you
141 provide.).
142
143Creating a Calendar Template
144============================
145
146By creating a calendar template you have 100% control over the design of
147your calendar. Each component of your calendar will be placed within a
148pair of pseudo-variables as shown here::
149
150 $prefs['template'] = '
151
Derek Jones3ab40a62011-10-05 16:19:27 -0500152 {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Derek Jones3ab40a62011-10-05 16:19:27 -0500154 {heading_row_start}<tr>{/heading_row_start}
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Derek Jones3ab40a62011-10-05 16:19:27 -0500156 {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
157 {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
158 {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Derek Jones3ab40a62011-10-05 16:19:27 -0500160 {heading_row_end}</tr>{/heading_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Derek Jones3ab40a62011-10-05 16:19:27 -0500162 {week_row_start}<tr>{/week_row_start}
163 {week_day_cell}<td>{week_day}</td>{/week_day_cell}
164 {week_row_end}</tr>{/week_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Derek Jones3ab40a62011-10-05 16:19:27 -0500166 {cal_row_start}<tr>{/cal_row_start}
167 {cal_cell_start}<td>{/cal_cell_start}
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones3ab40a62011-10-05 16:19:27 -0500169 {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content}
170 {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Derek Jones3ab40a62011-10-05 16:19:27 -0500172 {cal_cell_no_content}{day}{/cal_cell_no_content}
173 {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Derek Jones3ab40a62011-10-05 16:19:27 -0500175 {cal_cell_blank}&nbsp;{/cal_cell_blank}
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Derek Jones3ab40a62011-10-05 16:19:27 -0500177 {cal_cell_end}</td>{/cal_cell_end}
178 {cal_row_end}</tr>{/cal_row_end}
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Derek Jones3ab40a62011-10-05 16:19:27 -0500180 {table_close}</table>{/table_close}
Derek Jones8ede1a22011-10-05 13:34:52 -0500181 ';
182
183 $this->load->library('calendar', $prefs);
184
Derek Jonesa7561252013-07-21 14:21:41 -0700185 echo $this->calendar->generate();
186
187***************
188Class Reference
189***************
190
191.. class:: CI_Calendar
192
193 .. method:: initialize([$config = array()])
194
195 :param array $config: config preferences
196 :returns: void
197
198 Initializes the Calendaring preferences. Accepts an associative array as input, containing display preferences.
199
200
201 .. method:: generate([$year = ''[, $month = ''[, $data = array()]]])
202
203 :param int $year: the year
204 :param int $month: the month
205 :param array $data: the data to be shown in the calendar cells
206 :returns: string
207
208 Generate the calendar.
209
210
211 .. method:: get_month_name($month)
212
213 :param int $month: the numeric month
214 :returns: string
215
216 Generates a textual month name based on the numeric month provided.
217
218
219 .. method:: get_day_names($day_type = '')
220
221 :param string $day_type: one of 'long', 'short', or 'abr'
222 :returns: array
223
224 Returns an array of day names (Sunday, Monday, etc.) based on the type
225 provided. Options: long, short, abr. If no ``$day_type`` is provided (or
226 if an invalid type is provided) this method will return the "abbreviated"
227 style.
228
229
230 .. method:: adjust_date($month, $year)
231
232 :param int $month: the month
233 :param int $year: the year
234 :returns: array
235
236 This method makes usre that you have a valid month/year. For example, if
237 you submit 13 as the month, the year will increment and the month will
238 become January::
239
240 print_r($this->calendar->adjust_date(13, 2013));
241
242 outputs::
243
244 Array
245 (    
246 [month] => '01'
247 [year] => '2014'
248 )
249
250
251 .. method:: get_total_days($month, $year)
252
253 :param int $month: the month
254 :param int $year: the year
255 :returns: int
256
257 Total days in a given month::
258
259 echo $this->calendar->get_total_days(2, 2012);
260 // 29
261
262
263 .. method:: default_template()
264
265 :returns: array
266
267 Sets the default template. This method is used when you have not created
268 your own template.
269
270
271 .. method:: parse_template()
272
273 :returns: void
274
275 Harvests the data within the template ``{pseudo-variables}`` used to
276 display the calendar.