Page 1 of 1

Sorting CG Calendar by a certain field

Posted: Thu Jan 19, 2017 9:29 pm
by mlestat1131
I am trying to sort CGCalendar events by a certain field, as all the events have the same day and time. This is for a conference when all the events have the same time but different topics etc... When I put them on the backend, the front end shows them as inserted. I need to show by a particular number.

Is there any way to do this?

I have seen this posting, but I am unable to get it to work for some reason...

viewtopic.php?f=7&t=69243

Re: Sorting CG Calendar by a certain field

Posted: Mon Jan 23, 2017 6:25 pm
by mlestat1131
Nevermind, I figured this out...

Thanks
Matthew

Re: Sorting CG Calendar by a certain field

Posted: Mon Jan 23, 2017 7:08 pm
by paulbaker
Would you mind posting your solution to help others who search for the same thing?

Sorting CG Calendar by a certain field (Solution)

Posted: Mon Jan 23, 2017 9:03 pm
by mlestat1131
I was able to get the CG Calendar to sort using the Summary Field. This allowed me to use the summary field as a workshop # on the input of workshops for a conference.

This is the Code
You need to create a UDT with this info in it.
['event_summary'} calls the specific variable within the $events array

Code: Select all

if (!function_exists('do_sort')) {
    function do_sort($a, $b) {
        return strcasecmp($a['event_summary'],$b['event_summary']);
    }
}
$data = $params['data'];
usort($data, 'do_sort');
$smarty->assign('sorted', $data);
from there this is the code in the Calendar List View Template
The {sort} tag calls the array $events you then need to change the {foreach from=$events to {foreach from=$sorted

Code: Select all

{sort data=$events}
  {foreach from=$sorted key=key item=event}
  <div class="calendar-event">
<div class="workshops">
<div class="workshop_time">{$details=$event.event_details|strip_tags|trim}
      {if $event.event_summary != '' && $details != ''}{$event.event_summary}{/if} | {if $event.event_date_start == $event.event_date_end || $event.event_date_end == 0}
      {$event.event_date_start|date_format:"%I %x"}
      {else}
        {if $event.event_date_start|date_format:" %B %e, %Y " == $event.event_date_end|date_format:" %B %e, %Y "}
	   {* event starts and ends on the same day *}
          {fa icon='fa-clock-o'} {$event.event_date_start|date_format:"%l:%M %p"} {$lang.to} {$event.event_date_end|date_format:"%l:%M %p"}
        {/if}
{/if}</div>
<div class="workshop_body">
        <div class="workshop_header">{$event.event_title}</div>
<div class="clear">&nbsp;</div>
{if isset($event.fields)}
  <div class="workshop_presenter">
    {foreach $event.fields as $fieldname => $fieldrec}
      {$fieldname}:&nbsp;{$fieldrec.field_value}<br/>
    {/foreach}
  </div>
{/if}
      {if $details != ''}
       {$event.event_details}
I am not sure if you are able to do the same thing with custom fields, but this solution worked for what I was wanting to do with it.

Re: Sorting CG Calendar by a certain field

Posted: Tue Jan 24, 2017 12:08 am
by paulbaker
Great, thank you