[Solved] Display list of CGCalendar categories

A place to make for-pay "CMS made simple" job offerings
Post Reply
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am
Location: Brisbane, Australia

[Solved] Display list of CGCalendar categories

Post by gocreative »

Hi,

I'm using CGCalendar to display a graphical calendar. I have several categories set up, and would like the end user to be able to select one of the categories from a drop-down list which refreshes the page and filters the calendar accordingly.

For example, by default the "Events" page on the site loads all calendar items. Let's say the end user selects the drop-down on that page and chooses the category "Brisbane". As soon as they choose this (or if they click "Go" -- I don't care which), the page reloads and only shows events in that category. I figured the best way to do this was to use a session variable but unfortunately I don't have the time to look into outputting the category list, setting a session variable and then using that variable in the page template as a parameter of th CGCalendar module.

This is rather urgent so any help would be greatly appreciated. Please PM me for the website address and other details.

Thanks.
djanelle
New Member
New Member
Posts: 5
Joined: Tue Oct 14, 2008 7:22 pm

Re: [Solved] Display list of CGCalendar categories

Post by djanelle »

I just came to the forum looking for this same answer. It seems like it should be doable - I hope someone replies soon.
gocreative
Power Poster
Power Poster
Posts: 265
Joined: Mon Mar 14, 2011 1:16 am
Location: Brisbane, Australia

Re: [Solved] Display list of CGCalendar categories

Post by gocreative »

Here is the solution (thanks to JohnnyB for creating this):

1. Create a User Defined Tag named "calendar_sel_cat" (as in, select category), with the following code:

Code: Select all

/* *** START Configurable Options **** */
$default_option_text = 'Choose an area'; // Set the option prompt for the default (all) view
$cookie_days = '1'; // Set number of days before cookie expires
/* *** END Configurable Options **** */

// set urls
$config = cmsms()->GetConfig();
$contentops = cmsms()->GetContentOperations();
$content_obj = $contentops->getContentObject();
if ( isset($content_obj) ) { $alias = $content_obj->Alias(); } else {$alias = '';}
$cur_url = $config['root_url'].'/'.$alias;
$cur_url_cat = htmlspecialchars($_GET['selcat']);

// send to Smarty
$smarty = cmsms()->GetSmarty();
if ( $cur_url_cat != '' ) {
$smarty->assign('selcat',$cur_url_cat);
} else {
$smarty->assign('selcat','all');
}

// get categories
$db = cmsms()->GetDb();
$cal = cms_utils::get_module('CGCalendar');
$query = 'SELECT category_id,category_name FROM '.cms_db_prefix().'module_cgcalendar_categories ORDER BY category_order ASC';
$res = $db->GetArray($query);

// category options
echo '<select name="selcat" id="selcat" style="width:auto" class="no-mb">
<option value="">'.$default_option_text.'</option>';
foreach($res as $key=>$value) {
$cat_options = $value["category_name"];
echo '<option value="'.$cat_options.'"';
if ( ($cat_options == $cur_url_cat) || ($cat_options == $_COOKIE['selcat']) ) {echo ' selected="selected"';}
echo '>'.preg_replace('/and/','&',$cat_options).'</option>';
}
echo '</select>';

// create cookie and category onchange Javascript
echo '<__script__ type="text/javascript">
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function OnCategoryChange() {
	current_url = "'.$cur_url.'";
	list = document.getElementById("selcat");
	list.onchange = changeHandler;
	function changeHandler() {
		var get_category = this.value;
		if (get_category != "") {
			window.location = current_url+"&selcat="+get_category;
			createCookie("selcat",get_category,'.$cookie_days.')
		}
		else {
			window.location = current_url;
			createCookie("selcat","all",'.$cookie_days.')
		}
	}
}
window.onload = OnCategoryChange;
</__script>';
2. In your page template (or wherever you want to use it), add the code below.

Note that in my CMS I've defined variables for $events_url (i.e. the full path to the Events page that this module appears on), $events_alias (i.e. the internal page alias of the Events page), and $this_page_url (this is a variabled I've assigned using CGModuleExtensions to grab the current page URL). This helps me adjust my sites easily if the page URL or alias of the Events page changes. I put it in the page template to avoid clients screwing it up, but it could also be in a GCB.

Code: Select all

{calendar_sel_cat}
<strong>Category:</strong><br /> 
{if isset($selcat) AND $selcat != '' AND $selcat != 'all'}
       {assign var='calendar_cat' value=$selcat}
{elseif isset($smarty.cookies.selcat) AND $smarty.cookies.selcat != '' AND $smarty.cookies.selcat != 'all'}
       {assign var='calendar_cat' value=$smarty.cookies.selcat}
{else}
       {assign var='calendar_cat' value='all'}
{/if}
{if isset($calendar_cat) AND $calendar_cat != 'all'}
       {cms_module module="CGCalendar" display='calendar' detailpage=$events_alias category=$calendar_cat}
{else}
       {cms_module module="CGCalendar" display='calendar' detailpage=$events_alias}
{/if}
You may want to find a way to include this module only on a particular page. I wrap the above in:

Code: Select all

{if ($this_page_url eq $events_url) OR ($this_page_url|contains:$events_url)}
       {*the code from above goes here*}
{/if}
Post Reply

Return to “Help Wanted (commercial)”