Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active July 22, 2016 23:14
Show Gist options
  • Save stevewithington/2efa6ab9d92f574d1d4d to your computer and use it in GitHub Desktop.
Save stevewithington/2efa6ab9d92f574d1d4d to your computer and use it in GitHub Desktop.
Mura CMS : Example of how to retrieve the next "N" upcoming calendar events.
<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()[/m]
// 3) You can also control how many months of data you wish to retrieve and the maxItems to return
// For example: [m]$.getUpcomingEvents(months=3, maxItems=3)[/m]
public any function getUpcomingEvents(
string calendarid='#variables.$.content('contentid')#'
, numeric months=1
, numeric maxitems=6
, string categoryid='#variables.$.event('categoryid')#'
, string tag='#variables.$.event('tag')#'
, string siteid='#variables.$.event('siteid')#'
, string returnformat='query'
) {
var local = {};
arguments.months = arguments.months < 1 ? 1 : arguments.months;
// if 0 entered, then get up to 100,000 records
arguments.maxitems = !arguments.maxitems ? 100000 : arguments.maxitems;
local.util = variables.$.getCalendarUtility();
local.rsItems = local.util.getCalendarItems(calendarid=arguments.calendarid, start=Now(), end=DateAdd('m', arguments.months, Now()));
local.itItems = variables.$.getBean('contentIterator').setQuery(filterCalendarItems(data=local.rsItems, maxItems=arguments.maxItems));
savecontent variable='local.str' {
if ( local.itItems.hasNext() ) {
WriteOutput('<ul>');
while ( local.itItems.hasNext() ) {
local.item = local.itItems.next();
local.itemDate = DateConvert('utc2local', ParseDateTime(local.item.getDisplaystart()));
WriteOutput('
<li>
<small>#LSDateFormat(local.itemDate, 'long')# #LSTimeFormat(local.itemDate, 'long')#</small>
<div>
<a href="#local.item.getURL()#">
#HTMLEditFormat(local.item.getMenuTitle())#
</a>
</div>
</li>
');
}
WriteOutput('</ul>');
} else {
local.nextText = arguments.months == 1 ? 'month' : '#arguments.months# months';
WriteOutput('<p class="alert alert-info">No upcoming events within the next #local.nextText#.</p>');
}
}
switch(arguments.returnformat) {
case 'query' :
return filterCalendarItems(data=local.rsItems, maxItems=arguments.maxItems);
break;
case 'iterator' :
return local.itItems;
break;
default :
return local.str;
}
}
public any function filterCalendarItems(required query data, numeric maxItems=3) {
var qoq = new Query();
qoq.setDBType('query');
qoq.setAttributes(rs=arguments.data, maxRows=arguments.maxItems);
qoq.setSQL('
SELECT *
FROM rs
ORDER BY displaystart ASC
');
return qoq.execute().getResult();
}
</cfscript>
@manking109
Copy link

I get The method 'getCalendarUtility' is not defined when I try to use this.

Manny

@stevewithington
Copy link
Author

@manking109, Are you using the latest version of Mura? This function relies on new functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment