News Archives by Year/Month ( Tamplate + UDTs )

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Peciura

News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

It looks similar to this archive http://www.alytausmuziejus.lt/naujienos-12/lt/.
If you paste template to empty(new) page you will see how it works. Using pretty urls or on defailt page should add "?" url parameter "&news_date=".
Also it is good idea not to cache page with all this stuff.

Template (page content) looks like this:
{*assign var='gCms' value=''|cmsms*} {*uncomment this line it template does not work with CMSms 1.8+ *}
{news_extreme_date assign=news_start_date extreme='min'}
{news_extreme_date assign=news_end_date extreme='max'}
{assign var='current_time' value='Y-m-d H:i:s'|date}
{assign var='active_year' value=$smarty.get.news_date|substr:0:4}

{assign var='start_year_list' value=$news_start_date|substr:0:4}
{assign var='loop_year_list' value=$news_end_date|substr:0:4}

{********** years ************}
{section name=year_list start=$start_year_list loop="`$loop_year_list+1`"}
{news_date  assign='news_ids' date=`$smarty.section.year_list.index`} {*coment if you want to see links to periods without articles*}
{if !empty($news_ids)} {*coment if you want to see links to periods without articles*}
{capture assign=temp}&news_date={$smarty.section.year_list.index}{/capture}
{if $smarty.section.year_list.index == $active_year}
{$smarty.section.year_list.index}
{else}
{cms_selflink page=$page_alias urlparam=$temp text=$smarty.section.year_list.index}
{/if}
{/if} {*coment if you want to see links to periods without articles*}
{/section}

{********** years end*********}

{********** months and news *******************}
{assign var=mod value=$gCms->modules.FormBuilder.object}
{assign var='temp' value='date_january,date_february,date_march,date_april,date_may,date_june,date_july,date_august,date_september,date_october,date_november,date_december'}
{assign var='months' value=','|explode:$temp}

{if !empty($smarty.get.news_date)}
{********** months *************}
{foreach from=$months name='months' item=month}
{capture assign='news_date'}{$active_year}-{$smarty.foreach.months.iteration|string_format:"%'02u"}{/capture}

{if ($news_date >= $news_start_date && $news_date Lang[/b]($months[$smarty.foreach.months.index])}{/capture}
{if $smarty.get.news_date!=$news_date}
{cms_selflink page=$page_alias  text=$text urlparam="&news_date=$news_date"}
{else}
{$text}
{/if}
{/if} {*coment if you want to see links to periods without articles*}
{/if}
{/foreach}
{********** months end *********}
{********** news ***************}
{news_date  assign='news_ids' date="`$smarty.get.news_date`"  sort_by='create_date' sort_order='asc'}
{assign var=mod value=$gCms->modules.News.object}
{if !empty($news_ids)}

{*might be better option if you need all fields in articles *}
{assign var='news_ids' value=','|explode:$news_ids}
{foreach from=$news_ids item='articleid'}
{News action='detail' detailtemplate='summary' articleid=$articleid}
{module_action_link module='News' action='detail' articleid=$articleid text=$mod->Lang('more') page=$page_alias}
{/foreach}

{*news_ids ids=$news_ids*} {*original way for simple summaries*}

{else}
{$mod->Lang('none')} {*Visitor will this message if there is no articles for this period*}
{/if}
{********** news end ***********}
{/if}
To build it you will need 3 UDTs and module FormBuilder (can be done without it):

news_date

Code: Select all

/*$params['assign']*//*mandatory*/
/*$params['date']*//*mandatory*//*9999-12-32 23:59:59*/
/* $params['sort_by']*//* create_date, modified_date, start_time, end_time, news_extra, news_id, news_title, news_date */
/* $params['sort_order'] */ /* ASC, DESC */
if (!empty($params['assign']) && !empty($params['date'])){

		$params['sort_by'] = trim(strtolower($params['sort_by']));
		switch ($params['sort_by']){
			case 'modified_date':
			case 'news_id':
			case 'news_title':
			case 'news_date':
			case 'start_time':
			case 'end_time':
			case 'news_extra':
			$sort_by = $params['sort_by'];
			break;
			default:
			$sort_by = 'create_date';
			break;
		}

		$params['sort_order'] = trim(strtoupper($params['sort_order']));
		switch ($params['sort_order']){
			case 'ASC' :
			$sort_order = $params['sort_order'];
			break;
			default:
			$sort_order = 'DESC';
			break;
		}

	$params['date'] = substr(trim($params['date']), 0, 19);
	$start_date = substr_replace('0000-00-00 00:00:00', $params['date'], 0, strlen($params['date']));
	$end_date = substr_replace('9999-12-31 23:59:59', $params['date'], 0,  strlen($params['date']));
	$assign = '';
	global $gCms;
	$db =& $gCms->GetDB();
	$query = 'SELECT news_id FROM '.cms_db_prefix()."module_news WHERE news_date >= ? AND news_date <= ? AND status = ? ORDER BY $sort_by $sort_order";
	$array = $db->GetArray($query,array($start_date, $end_date, 'published'));
	
	foreach($array as $row){
		$assign .= $row['news_id'].',';
	}
	$assign = rtrim($assign, ' ,');
	$smarty = $gCms->GetSmarty();
	$smarty->assign($params['assign'], $assign);
}
news_extreme_date

Code: Select all

/*$params['assign']*//*mandatory*/
/*$params['extreme']*//*mandatory*/ /* 'min' | 'max'*/

$extreme = strtoupper(trim($params['extreme']));
if (!empty($params['assign']) && ($extreme == 'MIN' or $extreme == 'MAX')){
	$assign='';
	global $gCms;
	$db =& $gCms->GetDB();
	$query = 'SELECT '.$extreme.'(news_date) FROM '.cms_db_prefix()."module_news ". " WHERE  status = 'published'";
	$assign = $db->GetOne($query);
	$smarty = $gCms->GetSmarty();
	$smarty->assign($params['assign'], $assign);
}
a. To use all available fields call News module multiple times and specify detail template that looks like summary. To create "more..." link you need SimpleSmarty module. Article sort can be done in {news_date}
b. news_ids download attachment from this post http://forum.cmsmadesimple.org/index.ph ... #msg191719
This UDT takes majority of news module parameters - listed at the top of file.

You will need FormBuilder, because i used month names from this module. Months will be in the same language as page.

EDIT: Fixed template bug allowing to show all articles if none was valid for appropriate period. Now links to "empty" periods  are not displayed, or you can choose to display message (e.g. "None")
EDIT: Added example with {News action='detail'   ...   } instead of UDT {news_ids}
Attachments
year_month_filter.png
Last edited by Peciura on Mon Jul 26, 2010 4:04 pm, edited 1 time in total.
Darukascool
Forum Members
Forum Members
Posts: 66
Joined: Thu Apr 23, 2009 3:34 pm

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Darukascool »

Where do I need to insert 3 UDTs? Should it be in new modules or in FormBuilder?
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

First one is a template (already contains UDT calls ) that should go directly to page content.

To find out how to create UDTs refer to http://wiki.cmsmadesimple.org/index.php ... efined_Tag
Last edited by Peciura on Tue Mar 23, 2010 9:09 am, edited 1 time in total.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

Peciura, this is what i am looking for. what a pity that this functionality does not come with news module :(.

but one question: where to paste the code "Template"? in which one? newssummaery template or main template of cmsms?

thank you!
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

Template is page content where you want year-month links and articles to appear. Put it all to new page and you will see.

It is not very efficient way of dealing with archive but it is ok for small site and who know things might change to the better.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

allright, i pasted the first (smarty) code in a global content block (because tinymce injects errors in regular content textareas) and put this in my content area. and now the navigation will be displayed. seems to work!
Attachments
Bildschirmfoto 2010-07-13 um 13.38.51.png
Last edited by nicmare on Tue Jul 13, 2010 12:07 pm, edited 1 time in total.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

well, this works pretty good. thank you very much peciura! this is a great help!
two questions left:
the more link of each filtered result article is empty? why?
its just more

and how can i show custom fields? (images)
Last edited by nicmare on Tue Jul 13, 2010 12:16 pm, edited 1 time in total.
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

the more link of each filtered result article is empty? why?
its just more

and how can i show custom fields? (images)
All stuff you have done just picks right articles up, everything else is News summary template. So check default summary template, or set any other summary template.

Code: Select all

{news_ids ids=$news_ids summarytemplate='template_filter'}
It can take all News summary parameters (at least it did originally).
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

yea: {news_ids ids=$news_ids}

and its loads the default summary template as it should. there i have

Code: Select all

<a href="{$entry->moreurl}">more</a>
but i get

Code: Select all

<a href="">more</a>
???


this is my modified code:

Code: Select all

{news_extreme_date assign=news_start_date extreme='min'}
{news_extreme_date assign=news_end_date extreme='max'}
{assign var='current_time' value='Y-m-d H:i:s'|date}
{assign var='active_year' value=$smarty.get.news_date|substr:0:4}

{assign var='start_year_list' value=$news_start_date|substr:0:4}
{assign var='loop_year_list' value=$news_end_date|substr:0:4}
<div class="newsarchivenavi">
{********** years ************}
<div class="newsarchivetitle">select year and month:</div>
<ul class="newsbyyear">
{section name=year_list start=$start_year_list loop="`$loop_year_list+1`"}
<li>
   {news_date  assign='news_ids' date=`$smarty.section.year_list.index`}   {*coment if you want to see links to periods without articles*}
   {if !empty($news_ids)}            {*coment if you want to see links to periods without articles*}
      {capture assign=temp}?news_date={$smarty.section.year_list.index}{/capture}
      {if $smarty.section.year_list.index == $active_year}
         {$smarty.section.year_list.index}
      {else}
         {cms_selflink page=$page_alias urlparam=$temp text=$smarty.section.year_list.index}
      {/if}
   {/if}               {*coment if you want to see links to periods without articles*}
</li>
{/section}
</ul>

{********** years end*********}

{********** months and news *******************}
{assign var=mod value=$gCms->modules.FormBuilder.object}
{assign var='temp' value='date_january,date_february,date_march,date_april,date_may,date_june,date_july,date_august,date_september,date_october,date_november,date_december'}
{assign var='months' value=','|explode:$temp}

{if !empty($smarty.get.news_date)}
<ul class="newsbymonth">
   {********** months *************}
   {foreach from=$months name='months' item=month}

      {capture assign='news_date'}{$active_year}-{$smarty.foreach.months.iteration|string_format:"%'02u"}{/capture}

      {if ($news_date >= $news_start_date && $news_date <= $news_end_date) || (strpos($news_start_date, $news_date) !== FALSE) || (strpos($news_end_date, $news_date) !== FALSE)}
         {news_date  assign='news_ids' date="`$news_date`"}   {*coment if you want to see links to periods without articles*}
         {if !empty($news_ids)}            {*coment if you want to see links to periods without articles*}
<li>
            {capture assign='text'}{$mod->Lang($months[$smarty.foreach.months.index])}{/capture}
            {if $smarty.get.news_date!=$news_date}
               {cms_selflink page=$page_alias  text=$text urlparam="&news_date=$news_date"}
            {else}
               {$text}
            {/if}
</li>
         {/if}               {*coment if you want to see links to periods without articles*}
      {/if}

   {/foreach}
</ul>
</div>

<div class="newsarchive">
   {********** months end *********}
   {********** news ***************}

   {news_date  assign='news_ids' date="`$smarty.get.news_date`"}
   {if !empty($news_ids)}

      {news_ids ids=$news_ids}
   {else}
      {assign var=mod value=$gCms->modules.News.object}
      {$mod->Lang('none')}            {*Visitor will this message if there is no articles for this period*}
   {/if}
   {********** news end ***********}
</div>
{/if}
Last edited by nicmare on Tue Jul 13, 2010 12:35 pm, edited 1 time in total.
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

Have to see summary template.
I guess variable {$entry} is overwritten by other module (comments ?, ); Can suggest to save url at the top of main summary loop

Code: Select all

{foreach from=$items item=entry}
{assign var='saved_news_url' value=$entry->moreurl}
and then use it

Code: Select all

<a href="{$saved_news_url}">more</a>
To make things clear - put

Code: Select all

<pre>{$entry|var_dump}</pre>
just before

Code: Select all

<a href="{$entry->moreurl}">more</a>
. And post its output.
Last edited by Peciura on Tue Jul 13, 2010 12:55 pm, edited 1 time in total.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

well, this is very confusiung.
i use news at 3 pages. homepage (content_id 15) with summary of news and page 57 with detailview of news and finally the archive at page 65 with summary and detail template.
with print_r in the summary template i get at the homepage and page 57 "entry->link" and "entry->moreurl".

and at the archive, with your script i just get
"entry->link" but without title of article in url!

look at the screenshots please

btw: i am using cmsm 1.6.7
Attachments
Bildschirmfoto 2010-07-13 um 16.08.14.png
Bildschirmfoto 2010-07-13 um 16.08.53.png
Last edited by nicmare on Tue Jul 13, 2010 2:16 pm, edited 1 time in total.
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

I think i have to rewrite that UDT because it is out of date.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

:-[
hmm, but your thread is from 22 Mar 2010. a few things have changed huh?
Peciura

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by Peciura »

No, UDT is simply much older. Do not have time to debug new UDT ATM.
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: News Archives by Year/Month ( Tamplate + UDTs )

Post by nicmare »

wohoo, a spammer  :P
Locked

Return to “Tips and Tricks”