Page 1 of 1

[CGCalendar] get category names (and colors) in list templat

Posted: Thu Apr 07, 2016 12:59 pm
by 10010110
I would like to show a list of all existing categories (and get their assigned colors in the admin area) on the list template (or any calendar template). As far as I can tell there is only an “events” array where each event reveals the assigned category. I suppose I could loop through all events, retreive their categories, and construct a list with a few conditionals (remember, I only want each category once in that list, so subsequent similar occurrences should be ignored) but that feels a little too intricate.

I know that the editevent template has a “categories” variable/array. Couldn’t that also be made available in the calendar templates? And while I’m at it: Could that “categories” array not contain the color values that are assigned to them in admin?

Re: [CGCalendar] get category names (and colors) in list tem

Posted: Thu Apr 07, 2016 5:58 pm
by paulbaker
Here's a way of doing something similar which should help you.

It generates a key, showing each category within a coloured div. CMSMS V1.

Create a UDT, "get_cgcalendar_categories":

Code: Select all

/**
* Get list of CGCalendar categories and assign to Smarty variable.
*
* @params   string   $params['assign']   Mandatory. Name of variable to assign result
*/

$assign = trim($params['assign']);

if (!empty($assign)){
   
   $gCms = cmsms(); // global $gCms;
   $db = $gCms->GetDB(); // cms_utils :: get_db();
   $smarty = $gCms->GetSmarty(); // cms_utils :: get_smarty();
   $dbresult = FALSE;

   $query = 'SELECT * FROM `'.cms_db_prefix().'module_cgcalendar_categories` ORDER BY `category_order`, `category_name`, `category_id`';
   
   $dbresult = $db->GetArray($query);
   if(!empty($dbresult)){
      $smarty->assign($assign, $dbresult);
   }
}
And in calendar template I have:

Code: Select all

<div class="cal-cat-key">
{* udt call *}
{get_cgcalendar_categories assign='cgc_cats'}
{if !empty($cgc_cats)}
<h3>Key</h3>
{foreach from=$cgc_cats item='cgc_cat'}
<div class="{$cgc_cat.category_name}">{$cgc_cat.category_name}</div>
{/foreach}
{/if}
</div>
My CSS includes styles with the same name as each category; not quite what you want but you should be able to get there now? You should find the colours within the returned array. Perhaps something like {$cgc_cat.category_colour}

Re: [CGCalendar] get category names (and colors) in list tem

Posted: Fri Apr 08, 2016 11:46 am
by 10010110
Thanks. That looks kinda “hacky”, though, directly accessing the database without any API. And even though I’m still using version 1 of the CMS I’m determined to upgrade to version 2 as soon as possible so I’d prefer to have a solution that works there, too.

If nobody else comes up with anything (calguy1000 *wink wink*) I’m going to file a feature request.