Welcome, Guest. Please login or register.
Did you miss your activation email?
06 Oct 2008, 10:34

Login with username, password and session length
Home Chat Help Search Calendar Login Register
Pages: [1]
Print
Author Topic: [SOLVED] Filter News by date (month/year, etc)  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« on: 04 Jul 2008, 06:34 »

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:
{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 Edit: 07 Jul 2008, 09:45 by stopsatgreen » Logged
cyberman
Support Team
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 6800

Location: Dohna / Saxony / Germany


Reality.sys is corrupt. Reboot universe (Y/N)?


WWW
« Reply #1 on: 04 Jul 2008, 08:12 »

Why don't create a category/subcegory for the News-Articles and display it with a category parameter?
Logged

Fr deutschsprachige Anwender / for german speaking users only
http://www.cmsmadesimple.de/ - die deutschsprachige Seite fr CMSms
http://demo.cmsmadesimple.de/ - die Informationen der CMSms-Musterinstallation in deutsch
http://wiki.cmsmadesimple.org/index.php/Main_Page/de - deutschsprachiges CMSms-Wiki
http://dev.cmsmadesimple.org/projects/german/ - deutsche Sprachdateien fr CMSms
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #2 on: 04 Jul 2008, 08:51 »

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:
$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:
{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.
Logged
cyberman
Support Team
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 6800

Location: Dohna / Saxony / Germany


Reality.sys is corrupt. Reboot universe (Y/N)?


WWW
« Reply #3 on: 04 Jul 2008, 10:14 »

a) A way to pass the Content Block value to the UDT; or

Have you tried this?

Code:
{content block='date' assign='item_date'}

A new variable $item_date is available in Smarty and can be read by udt

Code:
$myVar = $smarty->get_template_vars('item_date');
Logged

Fr deutschsprachige Anwender / for german speaking users only
http://www.cmsmadesimple.de/ - die deutschsprachige Seite fr CMSms
http://demo.cmsmadesimple.de/ - die Informationen der CMSms-Musterinstallation in deutsch
http://wiki.cmsmadesimple.org/index.php/Main_Page/de - deutschsprachiges CMSms-Wiki
http://dev.cmsmadesimple.org/projects/german/ - deutsche Sprachdateien fr CMSms
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #4 on: 04 Jul 2008, 10:42 »

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

Code:
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:
{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?
Logged
cyberman
Support Team
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 6800

Location: Dohna / Saxony / Germany


Reality.sys is corrupt. Reboot universe (Y/N)?


WWW
« Reply #5 on: 05 Jul 2008, 04:52 »

echo date('d F Y',strtotime($myVar));

Quote
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:
{content block='Date' oneline='true' wysiwyg='false' assign='item_date'}

So there will be no html tags or similar inside.
Logged

Fr deutschsprachige Anwender / for german speaking users only
http://www.cmsmadesimple.de/ - die deutschsprachige Seite fr CMSms
http://demo.cmsmadesimple.de/ - die Informationen der CMSms-Musterinstallation in deutsch
http://wiki.cmsmadesimple.org/index.php/Main_Page/de - deutschsprachiges CMSms-Wiki
http://dev.cmsmadesimple.org/projects/german/ - deutsche Sprachdateien fr CMSms
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #6 on: 05 Jul 2008, 06:47 »

Quote
Think PHP does not know how to format this

It does; when I pass it as a variable in the UDT:

Code:
{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:
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?
Logged
blast2007
Power Poster
***

Karma: 2
Offline Offline

Posts: 270

Location: Italy



« Reply #7 on: 05 Jul 2008, 09:43 »

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 most asked, working, and ready to apply patches are simply ignored by developers in new release ...

This is frustrating for contributors Sad

Regards
blast
Logged
cyberman
Support Team
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 6800

Location: Dohna / Saxony / Germany


Reality.sys is corrupt. Reboot universe (Y/N)?


WWW
« Reply #8 on: 05 Jul 2008, 11:26 »

Maybe you should join the project  Wink ...
Logged

Fr deutschsprachige Anwender / for german speaking users only
http://www.cmsmadesimple.de/ - die deutschsprachige Seite fr CMSms
http://demo.cmsmadesimple.de/ - die Informationen der CMSms-Musterinstallation in deutsch
http://wiki.cmsmadesimple.org/index.php/Main_Page/de - deutschsprachiges CMSms-Wiki
http://dev.cmsmadesimple.org/projects/german/ - deutsche Sprachdateien fr CMSms
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #9 on: 05 Jul 2008, 11:37 »

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:
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:
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?
Logged
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #10 on: 07 Jul 2008, 04:50 »

Just giving this a bump, as I'd really appreciate a solution.
Logged
cyberman
Support Team
Dev Team Member
Power Poster
*****

Karma: 141
Offline Offline

Posts: 6800

Location: Dohna / Saxony / Germany


Reality.sys is corrupt. Reboot universe (Y/N)?


WWW
« Reply #11 on: 07 Jul 2008, 08:36 »

This works for me

Code:
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.
Logged

Fr deutschsprachige Anwender / for german speaking users only
http://www.cmsmadesimple.de/ - die deutschsprachige Seite fr CMSms
http://demo.cmsmadesimple.de/ - die Informationen der CMSms-Musterinstallation in deutsch
http://wiki.cmsmadesimple.org/index.php/Main_Page/de - deutschsprachiges CMSms-Wiki
http://dev.cmsmadesimple.org/projects/german/ - deutsche Sprachdateien fr CMSms
stopsatgreen
Power Poster
***

Karma: 6
Offline Offline

Posts: 322

Location: London, England


WWW
« Reply #12 on: 07 Jul 2008, 09:44 »

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.
Logged
Pages: [1]
Print
Jump to: