Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active April 27, 2018 19:17
Show Gist options
  • Save stevewithington/ba402e81c2c0e79f94e5 to your computer and use it in GitHub Desktop.
Save stevewithington/ba402e81c2c0e79f94e5 to your computer and use it in GitHub Desktop.
Mura CMS : New CalendarUtility Bean Usage Examples
<cfscript>
// 1) Drop these methods into your Theme or Site contentRenderer.cfc
// 2) Create a component called 'Upcoming Events' and using the Mura tag, call [m]$.getUpcomingEvents(calendarid='Events')[/m]
// 3) You can also control how many months of data you wish to retrieve and the maxItems to return
// For example: [m]$.getUpcomingEvents(calendarid='Events', months=3, maxItems=3)[/m]
// NOTE: The code examples assume you have a 'Calendar' with a Title of 'Events'
public any function getUpcomingEvents(
string calendarid=''
, string feedid = ''
, numeric months=3
, numeric maxitems=50
, string categoryid='#variables.$.event('categoryid')#'
, string tag='#variables.$.event('tag')#'
, string siteid='#variables.$.event('siteid')#'
, string returnformat='string'
) {
var local = {};
local.util = variables.$.getCalendarUtility();
arguments.months = arguments.months < 1 ? 1 : arguments.months;
arguments.maxitems = !arguments.maxitems ? 100000 : arguments.maxitems;
if ( !Len(arguments.calendarid) && !Len(arguments.feedid) ) {
Throw(type='MissingArguments', errorCode='0', message='Missing arguments for getUpcomingEvents()', detail='You must pass in either a calendarID, or feedID to getUpcomingEvents()');
}
// If CalendarID
if ( Len(arguments.calendarid) ) {
local.bean = IsValid('uuid', arguments.calendarid)
? variables.$.getBean('content').loadBy(contentid=arguments.calendarid, siteid=arguments.siteid)
: variables.$.getBean('content').loadBy(title=arguments.calendarid, siteid=arguments.siteid);
// invalid ID
if ( local.bean.getIsNew() ) {
Throw(type='InvalidCalenderID', errorCode='0', message='The CalendarID for getUpcomingEvents() is invalid.', detail='The CalendarID (#arguments.calendarID#) used for getUpcomingEvents() is invalid');
}
// rsItems
local.rsItems = local.util.getCalendarItems(calendarid=local.bean.getContentID(), start=Now(), end=DateAdd('m', arguments.months, Now()), categoryid=arguments.categoryid, tag=arguments.tag, siteid=arguments.siteid);
} else {
// If FeedID
local.feed = IsValid('uuid', arguments.feedid)
? variables.$.getBean('feed').loadBy(feedid=arguments.feedid, siteid=arguments.siteid)
: variables.$.getBean('feed').loadBy(name=arguments.feedid, siteid=arguments.siteid);
// invalid ID
if ( local.feed.getIsNew() ) {
Throw(type='InvalidFeedID', errorCode='0', message='The FeedID for getUpcomingEvents() is invalid.', detail='The FeedID (#arguments.feedID#) used for getUpcomingEvents() is invalid');
}
local.temp = variables.$.getBean('feed').loadBy(feedid=local.feed.getFeedID()).getIterator(from=Now(), to=DateAdd('m', arguments.months, Now()));
// rsItems
local.rsItems = local.util.filterCalendarItems(data=local.temp.getQuery(), maxItems=arguments.maxItems);
}
local.itItems = variables.$.getBean('contentIterator').setQuery(local.rsItems);
savecontent variable='local.str' {
if ( local.itItems.hasNext() ) {
WriteOutput('<div class="mura-index"><div>');
while ( local.itItems.hasNext() ) {
local.item = local.itItems.next();
local.itemDate = ParseDateTime(local.item.getDisplaystart());
WriteOutput('
<dl>
<dt class="date releaseDate">
#LSDateFormat(local.itemDate, 'long')# #LSTimeFormat(local.itemDate, 'long')#
</dt>
<dt class="title">
<a href="#local.item.getURL()#">
#HTMLEditFormat(local.item.getMenuTitle())#
</a>
</dt>');
if ( Len(local.item.getSummary())) {
WriteOutput('
<dd clas="summary">
#local.item.getSummary()#
</dd>
');
}
WriteOutput('
</dl>
');
}
WriteOutput('</div></div>');
} else {
local.nextText = arguments.months == 1 ? 'month' : '#arguments.months# months';
WriteOutput('<div class="alert alert-info">No upcoming events within the next #local.nextText#.</div>');
}
}
switch(arguments.returnformat) {
case 'query' :
return local.rsItems;
break;
case 'iterator' :
return local.itItems;
break;
default :
return local.str;
}
}
// example of how to get an iterator of upcoming events
public any function getMyEvents(calendarid=variables.$.content('contentid'), months=6) {
var local = {};
local.util = variables.$.getCalendarUtility();
local.itItems = local.util.getCalendarItems(
calendarid=arguments.calendarid
, start=Now()
, end=DateAdd('m', arguments.months, Now())
, returnFormat='iterator'
);
return itItems;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment