[SOLVED] Filter News by date (month/year, etc)

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

[SOLVED] Filter News by date (month/year, etc)

Post by stopsatgreen »

I want to have a series of pages which will display only News Articles from a defined date period (monthly). I've seen there's a patch for the News module floating around but I don't really want to patch my site as it could break in future upgrades.

I came up with a solution that would display Articles based on the page creation date:

Code: Select all

{capture assign='newsletter_date'}{created_date}{/capture}
{foreach from=$items item=entry}
{if $entry->postdate|date_format:'%m' == $newsletter_date|date_format:'%m'}
<!-- Content -->
{/foreach}
But this is dependent on the page being created in the month you want to display; not ideal.

I think I'd have to create a UDT which would display Articles based on an input value (perhaps from a Content Block), but I don't really know where to begin. Can anyone help me out?
Last edited by stopsatgreen on Mon Jul 07, 2008 1:45 pm, edited 1 time in total.
cyberman

Re: Filter News by date (month/year, etc)

Post by cyberman »

Why don't create a category/subcegory for the News-Articles and display it with a category parameter?
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

Because there are already five possible subcategories for each Article, so I'd have to create five new subcategories every month.

I'm getting a bit closer to where I want to be; I've created a UDT which takes a string and returns it as a date:

Code: Select all

$input_date = $params['thismonth'];
echo date('d F Y',strtotime($input_date));
Which I can then input into my News template and filter by month:

Code: Select all

{capture assign='item_date'}{newsletter_date thismonth='June 2008'}{/capture}
{foreach from=$items item=entry}
{if $entry->postdate|date_format:'%m' == $item_date|date_format:'%m'}
<!-- Content -->
{/if}
{/foreach}
What I need now is a way to get the contents of a Content Block as a string and either pass it into the UDT instead of the parameter, or pass it into the News template as a parameter; neither of which I can work out how to do.

Also, with a lot of news stories on each page I feel that this could mean a lot of database queries. Could anyone suggest:

a) A way to pass the Content Block value to the UDT; or
b) A more elegant solution.
cyberman

Re: Filter News by date (month/year, etc)

Post by cyberman »

stopsatgreen wrote: a) A way to pass the Content Block value to the UDT; or
Have you tried this?

Code: Select all

{content block='date' assign='item_date'}
A new variable $item_date is available in Smarty and can be read by udt

Code: Select all

$myVar = $smarty->get_template_vars('item_date');
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

OK, so this is my UDT, called 'newsletter_date':

Code: Select all

global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
In my template I have this:

Code: Select all

{content block='Date' oneline='true' assign='item_date'}
In my page I have the value 'June 2008' in the Date block.

When I call {newsletter_date} in the News template, it returns 01 January 1970. What have I missed?
cyberman

Re: Filter News by date (month/year, etc)

Post by cyberman »

stopsatgreen wrote: echo date('d F Y',strtotime($myVar));
In my page I have the value 'June 2008' in the Date block.
Think PHP does not know how to format this ...

A better option for content block could be

Code: Select all

{content block='Date' oneline='true' wysiwyg='false' assign='item_date'}
So there will be no html tags or similar inside.
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

Think PHP does not know how to format this
It does; when I pass it as a variable in the UDT:

Code: Select all

{newletter_date thismonth='June 2008'}
I get the result I want. But that means creating a new News template every month, so what I need to do is pass the same value from the content block. I think I'm really close, I just want to know why my UDT isn't getting the value from the block:

Code: Select all

global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
Is there something missing or wrong?
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

Re: Filter News by date (month/year, etc)

Post by blast2007 »

stopsatgreen wrote: I've seen there's a patch for the News module floating around but I don't really want to patch my site as it could break in future upgrades.
I can't understand why[url=http://'http://forum.cmsmadesimple.org/index.php/topic,20711.msg102501.html#msg102501']most asked, working[/url], and [url=http://'http://dev.cmsmadesimple.org/tracker/index.php?func=detail&aid=2175&group_id=8&atid=111']ready to apply patches[/url] are simply ignored by developers in new release ...

This is frustrating for contributors :(

Regards
blast
cyberman

Re: Filter News by date (month/year, etc)

Post by cyberman »

Maybe you should join the project  ;) ...
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

Right now I don't really care whether the core is patched or not because it doesn't help me; I want to know if there's something wrong with my UDT which is stopping it from getting the variable from the content block:

Code: Select all

global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo date('d F Y',strtotime($myVar));
The issue is not with the date format, because when I do this:

Code: Select all

global $gCms;
$smarty = &$gCms->GetSmarty();
$myVar = $smarty->get_template_vars('item_date');
echo $myVar;
I get no output. Therefore, the UDT isn't getting the value from the content block. So what am I doing wrong?
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

Just giving this a bump, as I'd really appreciate a solution.
cyberman

Re: Filter News by date (month/year, etc)

Post by cyberman »

This works for me

Code: Select all

global $gCms;
$myVar = $smarty->get_template_vars('item_date');
echo $myVar;
But the problem is where you've called the second content block.

If it should be available in complete template (head and body section) I suggest to insert the call for the second block as the first line of template.
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am

Re: Filter News by date (month/year, etc)

Post by stopsatgreen »

That seems to have done the trick! Moving the {content block='name'} up to the top of the page resolved the issue. Not sure why that should be, but I'm not complaining!

Many thanks for your patience and help on this. When I get a chance, I'll look at adding it to the Wiki somewhere.
Post Reply

Return to “CMSMS Core”