Page 1 of 1
[Solved] Can I get all News posts on a specific day?
Posted: Wed Jun 15, 2011 10:59 am
by daik
I have tried to search the forum, the cms ms site and the wide internet, but I have not found anything that seems to answer my question.
The problem
I am converting from an old (completly different) blog system to CMS MS and the News module. I have a fair amount of posts (~2100). I would like to provide a way on my site to access a post by the date it was posted. Now I had a look at the options provided by the News module and I can not seem to find any way to find all posts for a single date?
My naive approach
I am fluent in a number of languages, but unfortunately not PHP. However this could be a good excuse to learn it. My thoughts are to create a module that allows me to find the id's of posts from a specific date. This module would put these ids into a smarty variable which I can then use to include the News module and display the post. Something like
Code: Select all
{mymodule date=$date}
{foreach from=$myvariable item=id}
{news articleid=id}
{foreachelse}
No posts found for {$date}
{/foreach}
I like this as it would make for easy upgrades(no hacks in core modules), but I am not certain it is a good way to achieve this. Indeed it seems like there could be some way to do this already?
Any advice is welcome!
regards
Daniel
Re: Can I get all News articles posted on a specific day?
Posted: Thu Jun 16, 2011 8:58 pm
by vinyl
I'm not sure if you can do this with the module. I don't think so.
With a UDT you can write your own code and do the things you want in there. But even if you end up with a bunch of id's from the news module table I'm not sure if you can call the news module with multiple id's to show in detail mode or in summary mode.
The documentation says:
(optional) articleid="" - This parameter is only applicable to the detail view. It allows specifying which news article to display in detail mode. If the special value -1 is used, the system will display the newest, published, non expired article.
So I guess only one id can be passed, not multiple..
The CGBlog module, which is a fork of the news module, can be given a month to display the posts from:
(optional) month="" - Applicable only to the default action, this parameter can contain an (integer) month, for which all listings should be displayed. This parameter will only work in conjunction with the "year" parameter.
Maybe that is an option?
Re: Can I get all News articles posted on a specific day?
Posted: Fri Jun 17, 2011 11:16 am
by daik
I have plans to use the CGBlog module for other things, and I would like to keep it separate. However if you scrutinise my posted pseudo code you notice I only ever pass a single id into the news module, but I include the news module multiple times in a foreach loop. As I don't hear any really strong arguments against this I think I might just as well give it a go and even if I fail I should come out with some additional knowledge of PHP and CMS MS so it is win either way.
Thanks for your thoughts!
Re: Can I get all News articles posted on a specific day?
Posted: Tue Jun 21, 2011 10:54 am
by daik
I solved this with a tag, and I am pleased to say it works rather well.
My tag is
Code: Select all
function smarty_cms_function_dairydate($params, &$smarty)
{
global $gCms;
$status = $params['status'];
if(!isset($status))
{
return "status not set, it is required";
}
$category = $params['category_id'];
if(!isset($category))
{
return "category_id is not set";
}
$date = $_GET['date'];
if(!isset($date))
{
return "date not passed. Please pass the date query parameter on the form yyyy-mm-dd<br/>";
}
$varname = $params['variable'];
if(!isset($varname))
{
return "variable not set";
}
$db = $gCms->GetDb();
$query = 'SELECT news_id FROM '.cms_db_prefix().'module_news WHERE status = ? AND news_category_id = ? AND news_date >= ? AND news_date <= ?';
$dbresult = $db->Execute( $query, array($status,$category,$date.' 00:00:00',$date.' 23:59:59' ) );
$result = array();
while ($dbresult && $row = $dbresult->FetchRow())
{
array_push($result,$row['news_id']);
}
$smarty->assign($varname,$result);
return '';
}
It has been tested on mysql and I don't think it will work particularly well on postgres. But then again just adjusting the dateformat should do for that. It may work for you, it may not, it works for me, so take inspiration from this.
In my template I do
Code: Select all
{dairydate status="published" category_id="2" variable="ids" }
{foreach from=$ids item=id}
{news articleid="$id" action="detail"}
{foreachelse} No posts found for {$smarty.get.date}
{/foreach}