[calendar_grid]

Description

Link: [calendar_grid]
Author: Jason Huck
Category: Date
Version: 8.x
License: Public Domain
Posted: Jul. 20, 2006
Updated: Sep. 05, 2007
More by this author...
Generates a calendar grid with lots of options. See parameter usage below. Requires [date_firstdayofweek], [lp_date_leapyear], and [lp_date_monthlastday]. The sample usage includes CSS to demonstrate one way to style the resulting HTML and generate a full-size 2-week calendar along with a 1-month mini-calendar.

Parameters

-id string, optional An HTML element ID to keep multiple grids on the same page separate.
-startdate date, optional The date from which to calculate the grid.
-numweeks integer, optional Number of weeks to display. Defaults to end of current month.
-firstday string, optional 'Sunday' or 'Monday' to start the week.
-dayformat string, optional A valid Lasso date format for day names ('%w', '%A', or '%a').
-daylink string, optional String representing the link where individual day numbers in the grid should point. Replaces %d with each day number.
-showweekends boolean, optional Boolean for whether to show weekends. Defaults to true.
-showheader boolean, optional Boolean for whether to show the current month/year at the top of the grid. Defaults to true.
-events array, optional A pair array of events to display in the grid. See sample usage for the specific format.
-target string, optional The target window (i.e., "_blank") to use for links.

Sample Usage

[//lasso
	// event dates must be in MySQL date format to sort properly
	var('events') = array(
		'2009-03-17 10:30:00' = map('link'='http://www.yahoo.com/', 'title'='This is a test.'),
		'2007-03-17 10:45:00' = map('link'='http://www.apple.com/', 'title'='Second test.'),
		'2008-03-18 15:30:00' = map('link'='http://www.ebay.com/', 'title'='Third test.'),
		'2005-03-19 00:00:00' = map('link'='http://www.amazon.com/', 'title'='Fourth test.'),
		'2009-03-20 11:59:59' = map('link'='http://www.google.com/', 'title'='Fifth test.')
	);

	var('myCalendar') = calendar_grid(
		-id='testcal',
		-startdate=date,
		-numweeks=2,
		-firstday='Monday',
		-daylink='/detail.lasso?day=%d',
		-showweekends=false,
		-showheader=false,
		-dayformat='%A',
		-events=$events
	);

	var('miniCal') = calendar_grid(
		-id='miniCal',
		-startdate=date('7/1/2006'),
		-dayformat='%a'
	);
]
<html>
	<head>
		<style type="text/css">
			.calendar {
				width: 100%;
			}
		
			.calendar * {
				font-family: lucida sans, verdana, sans-serif;
				font-size: 10px;
			}
		
			.calendar * a {
				display: block;
				text-decoration: none;
				margin-bottom: 5px;
			}
		
			.calendar thead {
				background-color: #666;
				color: white;
			}
			
			.calendar thead tr th, .calendar tbody tr td {
				width: 14%;
			}
			
			.calendar tbody tr td {
				border: 1px solid #ccc;
				height: 100px;
				vertical-align: top;
				background-color: #ccc;
			}
			
			.calendar tbody tr td div {
				width: 20px;
				text-align: right;
				float: right;
			}
			
			.calendar * div a {
				font-weight: bold;
			}
			
			.currentMonth {
				background-color: #eee !important;
			}
			
			.today {
				background-color: white !important;
			}
			
			#miniCal tbody tr td {
				height: auto;
			}
		</style>
	</head>
	<body>
[$myCalendar]
		<div style="width: 200px;">
[$miniCal]		
		</div>
	</body>
</html>
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
define_tag(
	'grid',
	-namespace='calendar_',
	-opt='id', -type='string',
	-opt='startdate', -type='date', -copy,
	-opt='numweeks', -type='integer',
	-opt='firstday', -type='string', -copy,
	-opt='dayformat', -type='string', -copy,
	-opt='daylink', -type='string', -copy,
	-opt='showweekends', -type='boolean',
	-opt='showheader', -type='boolean',
	-opt='target', -type='string',
	-opt='events', -type='array', -copy,
	-priority='replace',
	-description='Generates a calendar grid.'
);		
	// sort the events array if one was provided
	local_defined('events') ? #events->sort;

	// create an iterator for the events array
	local_defined('events') ? local('eventcursor') = #events->iterator;
	
	// if there's no start date provided, set it to today
	!local_defined('startdate') ? local('startdate' = date);
	
	// retain the current month
	local('currentmonth') = #startdate->month;
	
	// override whatever time is specified in #startdate with 
	// a value that will remain safe from DST issues.
	#startdate->set( -hour=14, -minute=0, -second=0);
	
	// if numweeks is not defined, go to end of current month
	if(!local_defined('numweeks'));
		local('startweek') = integer(#startdate->format('%W'));

		// 'week of year' apparently starts on Monday, so if startdate is a Sunday, add one
		integer(#startdate->format('%w')) == 1 ? #startweek += 1;			

		// find the last day of the current month
		local('endofmonth') = date(#startdate->format('%Y') + '-' + #startdate->format('%m') + '-' + lp_date_monthlastday(#startdate)  + ' 14:00:00');

		// get the week number
		local('endweek') = integer(#endofmonth->format('%W'));
		
		// 'week of year' apparently starts on Monday, so if endofmonth is a Sunday, add one
		integer(#endofmonth->format('%w')) == 1 ? #endweek += 1;			
		
		// subtract the difference and add one to get the correct number of weeks
		local('numweeks') = #endweek - #startweek + 1;		
	/if;
	
	// make sure dayformat is valid
	!local_defined('dayformat') ? local('dayformat' = '%A');
	(: '%w', '%A', '%a') !>> #dayformat ? #dayformat = '%A';
	
	// default to showing weekends
	!local_defined('showweekends') ? local('showweekends' = true);

	// default to showing a header
	!local_defined('showheader') ? local('showheader' = true);

	// make sure firstday value is valid
	!local_defined('firstday') ? local('firstday' = 'Sunday');
	(: 'Sunday', 'Monday') !>> #firstday ? #firstday = 'Sunday';
	!#showweekends ? #firstday = 'Monday';
	local('firstDayNum') = (#firstday == 'Sunday' ? 1 | 2);
			
	// start generating output
	local('out' = '
	<table class="calendar"' + (local_defined('id') ? ' id="' + #id + '"') + '>
		<thead>\
	');
	
	// top label header
	#showheader ? #out += '
			<tr>
				<th colspan="' + (#showweekends ? 7 | 5) + '">' + #startdate->format('%B %Y') + '</th>
			</tr>\
	';
	
	#out += '
			<tr>\
	';

	// find the first day of the week
	#startdate = date_firstdayofweek(
		-date=#startdate, 
		-starton=#firstdayNum
	);

	// day of week headers
	local('hdrDate' = #startdate);
	
	// only loop 5 days if we aren't showing weekends
	loop((#showweekends ? 7 | 5));
		#out += '
				<th>' + #hdrDate->format(#dayformat) + '</th>\
		';

		// add a day
		#hdrDate->add( -day=1);
	/loop;

	#out += '
			</tr>
		</thead>
		<tbody>\
	';

	// weeks
	local(
		'thisDay' = #startdate,
		'thisDayNum' = integer,
		'thisDateNumber' = integer,
		'thisDayLink' = string,
		'classes' = array,
		'daysEvents' = string
	);

	
	// loop 7 days, even if we're not showing weekends
	loop(#numweeks * 7);
		#thisDayNum = integer(#thisDay->format('%w'));
	
		// if we aren't showing weekends, skip them
		if(!#showweekends && (: 1, 7) >> #thisDayNum);
			// still need to add a day
			#thisDay->add( -day=1);			
			loop_continue;
		/if;
	
		// start a new row at the beginning of each week
		if(#thisDayNum == #firstdayNum);
			// close the previous row unless this is the first one
			loop_count > 1 ? #out += '
			</tr>\
			';

			#out += '
			<tr>\
			';
		/if;
					
		// determine which classes to apply
		#classes = array;
		#thisDay->month == #currentmonth ? #classes->insert('currentMonth');
		#thisDay->format('%D') == date->format('%D') ? #classes->insert('today');
		local_defined('events') ? #events->find(match_regexp(#thisDay->format('%Q') + '( [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?'))->size ? #classes->insert('hasEvents');
		
		// create link from individual days
		#thisDateNumber = #thisDay->format('%d');
		#thisDayLink = #thisDateNumber;
		local_defined('daylink') ? #thisDayLink = '<a href="' + string_replace(#daylink, -find='%d', -replace=#thisDay->format('%Q')) + '"' + (local_defined('target') ? ' target="' + #target + '"') + '>' + #thisDateNumber + '</a>';
		
		// add the day
		#out += '
				<td' + (#classes->size ? ' class="' + #classes->join(' ') + '"') + '>
					<div>' + #thisDayLink + '</div>\
		';
		
		// add the day's events
		if(local_defined('events'));
			while( !#eventcursor->atend && date(#eventcursor->value->first) < date(#thisDay->format('%D'))->add( -day=1)& );
				date(#eventcursor->value->first) >= date(#thisDay->format('%D')) ? #out += '
					<a href="' + #eventcursor->value->second->find('link') + '"' + (local_defined('target') ? ' target="' + #target + '"') + '>' + #eventcursor->value->second->find('title') + '</a>\
				';
				
				null(#eventcursor->forward);
			/while;		
		/if;
							
		#out += '
				</td>\	
		';

		// add a day
		#thisDay->add( -day=1);
	/loop;
	
	// close out last row
	#out += '
			</tr>
		</tbody>
	</table>\
	';

	// return the result
	return(@#out);
/define_tag;

 

Related Tags



Comments

09/05/2007, Jason Huck
Bug Fix
Corrected an issue introduced by the new .hasEvents class where the tag would error if no events were passed to it. Thanks to Jolle Carlestam for discovering this!
08/02/2007, Jason Huck
Added .hasEvents class.
Table cells which contain events now include a "hasEvents" class, to make it easier to style them differently via CSS or attach different behavior via JavaScript.
05/15/2007, Hoby Van Hoose
v7 version?
Is there any way this could be made for Lasso 7 too?
12/04/2006, Jason Huck
Added -target param.
Added an optional param, -target, to specify the target for links within the calendar, i.e., "_blank", "_top", etc.
08/19/2006, Jason Huck
Bug Fixes
Added fixes for compatibility with older versions of LP8, some DST related issues, and specific conditions which would case the calculation for number of weeks to be inaccurate. Thanks to Randy Phillips for helping identify and resolve these issues!
Email:


Password:



Newest

Most Popular