Lasso Soft Inc. > Home

[calendar_grid]

Linkcalendar_grid
AuthorJason Huck
CategoryDate
Version8.x
LicensePublic Domain
Posted20 Jul 2006
Updated05 Sep 2007
More by this author...

Description

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.

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'
	);
]

	
		
	
	
[$myCalendar]
		
[$miniCal]

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.

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' = '
	\
	');
	
	// top label header
	#showheader ? #out += '
			\
	';
	
	#out += '
			\
	';

	// 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 += '
				\
		';

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

	#out += '
			\
	';

	// 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 += '
			\
			';

			#out += '
			\
			';
		/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 = '' + #thisDateNumber + '';
		
		// add the day
		#out += '
				size ? ' class="' + #classes->join(' ') + '"') + '>
					
' + #thisDayLink + '
\ '; // 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 += ' ' + #eventcursor->value->second->find('title') + '\ '; null(#eventcursor->forward); /while; /if; #out += ' \ '; // add a day #thisDay->add( -day=1); /loop; // close out last row #out += '
' + #startdate->format('%B %Y') + '
' + #hdrDate->format(#dayformat) + '
\ '; // return the result return(@#out); /define_tag;

Related Tags

Comments

06 Mar 2014, Shelane French

Fix for daylight savings bug

There is a bug where an hour is added to the date when doing the date add and comparing to the current date causing events to show up a day early on the "spring forward" day. To fix this, change line 278 to while( !#eventcursor->atend && date(#eventcursor->value->first) < (date(#thisDay->format('%D'))->add( -day=1)&)->format('%Q') );

19 Jan 2009, Lieven Gekiere

localisation

Any chance someone could add localisation to this tag ?
I would like to see the day and month names in our own language instead of default English.

05 Sep 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!

02 Aug 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.

15 May 2007, Hoby Van Hoose

v7 version?

Is there any way this could be made for Lasso 7 too?

04 Dec 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.

19 Aug 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!

Please log in to comment

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2014 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft