Doing More with the CGCalendar Module for CMS Made Simple

CMS Made Simple Beyond the Basics – Do More with Your Calendar

The most popular and versatile calendar module available for CMS Made Simple is the CGCalendar module which can be installed using the Module Manager or downloaded from the Forge. CMS Made Simple Calendar

The calendar’s versatility can be flexed when modifying its templates using the Smarty Template Engine. Functionality can be added through User Defined Tags to query the calendar module and manipulate the results as needed for a project.

Adding the date suffix

Usually, events listed by the module look similar to this:

St. Patrick's Day - March 3
Good Friday - April 18
Easter - April 20
Cinco De Mayo - May 5
Mother's Day - May 11
Your Birthday Bash - May 16 to May 21

However, we don’t say, “March three.” We say, “March third.” And, our event list can be generated dynamically in the same way by adding a few lines into our List template.

First, capture the event start date as a two digit integer with the leading zero  using %d as the date format (01, 02, 03):

{capture assign='startdate'}{$event.event_date_start|date_format:"%d"}{/capture}

Next, show the event date as a one digit integer without the leading zero using %e as the date format (1, 2, 3):

{$event.event_date_start|date_format:"%e"}

Followed by Smarty/PHP logic to assign the appropriate suffix:

{if preg_match('/(01|21|31)/', $startdate)}st{/if}
{if preg_match('/(02|22)/', $startdate)}nd{/if}
{if preg_match('/(03|23)/', $startdate)}rd{/if}
{if preg_match('/(04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|24|25|26|27|28|29|30)/', $startdate)}th{/if}

The PHP preg_match() looks at a provided string to match something from a regular expression.
Last, set the month name after the date:

{$month_names[$month_number]}

Then your event list will look like:

Mother's Day - May 11th
Your Birthday Bash - May 16th to May 21st

List Event Categories

There is not a template available to show a list of event categories. To do this, a User Defined Tag (UDT) is needed to query the calendar’s categories and then list them. Name the UDT something like, “CalendarCategories”.

// initiate the database
$db = cmsms()->GetDb();

// query the calendar categories
$query = 'SELECT category_id,category_name FROM '.cms_db_prefix().'module_cgcalendar_categories ORDER BY category_order ASC';
$res = $db->GetArray($query);

// show results of query
echo "<ul>";
foreach($res as $key=>$value) {
echo '<li>$value["category_name"]</li>';
}
echo "</ul>";

That was easy. A list of categories is nice to have, but each category should be linked to show events belonging to the chosen category. To do this, a category landing page, and a query string that sets the desired category needs to be added to the UDT. First, at the top of your UDT, set a category event page:

$calendar_url = $config['root_url'].'/calendar';    //set calendar to the page alias for your events

Next, use PHP $_GET to retrieve the chosen category from the URL:

$get_category = htmlspecialchars($_GET['setcat']);

The $get_category variable needs to be sent to Smarty so it can be used inside of a template.

// send to Smarty
$smarty = cmsms()-&gt;GetSmarty();
if ( $get_category != '' ) {
$smarty-&gt;assign('setcat',$get_category);
}

The list of categories from the database now need to be set with the $calendar_url variable with a query string for the category name:

foreach($res as $key=>$value) {
echo '<li><a href="'.$calendar_url.'?setcat='.$value["category_name"].'">$value["category_name"]</a></li>';
}

The links generated should look something like, “http://www.your-cool-site.com/calendar?cat=Birthdays” That alone won’t make a working solution. A template tag will be needed to retrieve the category set in the query string. First, set the UDT in the template Where a list of categories should be located on the page: {CalendarCategories}

Next, get the category query from the URL and set it as the calendar’s category parameter:

{if isset($setcat)}
{cms_module module="CGCalendar" display='calendar' detailpage='calendar' category=$setcat}
{else}
{cms_module module="CGCalendar" display='calendar' detailpage='calendar'}
{/if}

The {else} condition will show the calendar with all categories when a category linked has not been clicked by a visitor.

Do you have a favorite calendar tip or trick? Leave a comment below with questions or suggestions.


Looking for quality CMS Made Simple Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , | Posted under CMS Made Simple | RSS 2.0

Author Spotlight

JohnnyB

JohnnyB

I've been a full-time front-end web developer for nine years working with design and marketing agencies and businesses located all over the USA. CMS Made Simple has been my go-to Content Management System since mid 2006.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>