Specifying a date range for CGBlog summary

A user in this thread was trying to figure out how to get CGBlog to show summaries for a particular school year. He had pointed out that CGBlog has a 'year' parameter, which corresponds to a calendar year, and can't be used for a school year.

I have created a UDT and a blog summary template to address this issue and allow an arbitrary date range to be shown in the CGBlog summary

Overview

We will be creating a UDT that captures the start date, the end date, and the number of items you want per page, converts the dates into UNIX Epoch timestamps, creates 'next' and 'previous' URLs and passes this information through smarty variables to the summary template. The summary template determines whether each item should be displayed, based on the date range and current page number.

Usage

This example will show blog summaries for the 2009/2010 school year, 6 per page.

{cgblog_date_range start_date="Sept 1 2009" end_date="July 30 2010" per_page=6}
{CGBlog}

The UDT

Create a User Defined Tag called 'cgblog_date_range'

$start_date = $params['start_date'];
$end_date = $params['end_date'];
$per_page = $params['per_page'];

global $gCms;
$cgblog = $gCms->modules['CGBlog']['object'];
$pagenum = isset($_REQUEST['pagenum']) ? $_REQUEST['pagenum'] : 1;
$base_url = 'index.php?page=' . $gCms->variables['page_name'] . '&pagenum=';
$next_url = $base_url . ($pagenum + 1);
$prev_url = $base_url . ($pagenum - 1);

$smarty->assign('start_date', strtotime($start_date));
$smarty->assign('end_date', strtotime($end_date));
$smarty->assign('per_page', $per_page);
$smarty->assign('next_url', $next_url);
$smarty->assign('prev_url', $prev_url);
$smarty->assign('base_url', $base_url);
$smarty->assign('pagenum', $pagenum);

Part of the above code was inspired by this thread

The Summary Template

Create or edit a summary template and paste the following into it. This example template is based on the default summary template. I have inserted comments into this template to show what I have added and removed.

{if isset($error)}{cgerror}{$error}{/cgerror}{/if}

{* REMOVED THIS SECTION*}

{* ADDED *}
{assign var="counter" value=0}
{assign var="pagenum" value=$pagenum-1}
{* END OF ADDED *}

{foreach from=$items item=entry}

{* ADDED *}
{if $start_date <= $entry->postdate|strtotime && $entry->postdate|strtotime <= $end_date}
{assign var='counter' value=$counter+1}
{if $counter > $per_page*$pagenum && $counter <= $per_page*$pagenum+$per_page}
{* END OF ADDED *}

<div class="CGBlogSummary">
{if $entry->postdate}
<div class="CGBlogSummaryPostdate">
{$entry->postdate|cms_date_format}
 </div>
 {/if}
 
 <div class="CGBlogSummaryLink">
 <a href="{$entry->detail_url}" title="{$entry->title|escape:htmlall}">{$entry->title|escape}</a>
 </div>
 
 {if $entry->categories}
 <div class="CGBlogSummaryCategory">
 {strip}{$category_label}
 {foreach from=$entry->categories item='category'}
 {$category.name}
 {/foreach}
 {/strip}
 </div>
 {/if}
 
 {if $entry->author}
 <div class="CGBlogSummaryAuthor">
 {$author_label}
 {$entry->author}
 </div>
 {/if}
{if $entry->summary}
 <div class="CGBlogSummarySummary">
 {eval var=$entry->summary}
 </div>
 {else if $entry->content}
 <div class="CGBlogSummaryContent">
 {eval var=$entry->content}
 </div>
 {/if}
 
 {if isset($entry->extra)}
 <div class="CGBlogSummaryExtra">
 {eval var=$entry->extra}
 </div>
 {/if}
{if isset($entry->fields)}
{foreach from=$entry->fields item='field'}
<div class="CGBlogSummaryField">
{if $field->type == 'file'}
<img src="{$entry->file_location}/{$field->value}"/>
{else}
{$field->name}: {eval var=$field->value}
{/if}
</div>
{/foreach}
{/if}
</div>

{* ADDED *}
{/if}
{/if}
{* END OF ADDED *}

{/foreach}

{* ADDED *}
{math equation="ceil($counter/$per_page)" assign='num_pages'}
{if $num_pages > 1}
<p>
{if $pagenum > 0}
<a href="{$base_url}1"><< </a><a href="{$prev_url}">< </a>
{/if}
Page {$pagenum+1} of {$num_pages}
{if $pagenum+1 < $num_pages}
<a href="{$next_url}"> ></a><a href="{$base_url}{$num_pages}"> >></a>
{/if}
</p>
{/if}
{* END OF ADDED *}


{* REMOVED SECTION BELOW THIS *}

Comments