[SOLVED] News Archives by Year/Month

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: News Archives by Year/Month

Post by sam_butler »

blast, are you God?
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

Re: News Archives by Year/Month

Post by blast2007 »

sam_butler wrote: blast, are you God?
???

what do you mean?
Last edited by blast2007 on Thu Jul 24, 2008 12:08 pm, edited 1 time in total.
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: News Archives by Year/Month

Post by sam_butler »

Sorry, haha, I just meant your solution was excellent. I guess British jokes don't work with Italians  :-\

Will install the diff soon and let you know how it goes. Thanks again.
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: News Archives by Year/Month

Post by sam_butler »

Wow, I've been busy. Okay, here's what I did:
1) Installed the patch from the diff (see above) so I can set start/end dates for a news summary;

2) Created a new page with the alias 'news-archive' as a child of 'news';

3) Thought a little bit, then realised the best way to do it without creating a new page for each year would be to pass a variable in the query string...

4) Wrote a UDT called 'news_archive_setup':

Code: Select all

global $gCms;
global $smarty;

if (!isset($_GET['year']) || $_GET['year'] == "") {
 $archiveyear=date('Y');
}
else {
 $archiveyear=$_GET['year'];
}
 $startperiod="{$archiveyear}/01/01";
 $endperiod="{$archiveyear}/12/31";
 $title="News Archive ".$archiveyear;
 
 $smarty->assign('archiveyear', $archiveyear);
 $smarty->assign('startperiod', $startperiod);
 $smarty->assign('endperiod', $endperiod);
 $smarty->assign('detail_title', $title);
5) Didn't quite get it right, had to learn the hard way that you can't concatenate strings in smarty (at least not easily), learned how to assign variables to smarty, and finally came up with the simple code above (I could add some regex to check that what was entered was an integer of four digits, but it's not needed right now);

6) Put the following in the content of my archives page:

Code: Select all

{news_archive_setup}
<h3 class="center">{title} – {$archiveyear}</h3>
<p style="text-align: center;">{anchor anchor="archives" title="News Archives" text="Choose another year"}</p>
{news summarytemplate="archive" detailpage="view-news" startperiod=$startperiod endperiod=$endperiod sortasc="true"}
7) I did this before, but if someone else wants to use the variable 'detail_title' as assigned in the UDT, here's what I have in the of my template:

Code: Select all

{if isset($detail_title)}
  <title>{$detail_title} - {sitename}</title>
{else}
  <title>{title} - {sitename}</title>
{/if}
8 ) Created a sidebar with links to each of the years of the archive (I was going to write something to make this dynamic based on what years there are news items for, but I ran out of time) like this:

Code: Select all

<a title="News Archive 2008" href="{cms_selflink href='news-archive'}?year=2008">2008</a>
The heading of this sidebar appears down the page, so it's anchored as 'archives', which is linked to with the smarty {anchor} in the content above. Also there's an {if} statement in my template to display the news archive sidebar in place of the last three articles if you're on the news summary page or an archive page. (Oh, and I thought after that the easiest way to make the list of links, since I'm doing it with yearly archives, would be to have a UDT to loop through from an arbitrary starting year, in my case 2003, until it reaches date('Y').)

9) Rejoiced and moved on to satisfy the next of my client's desires.

Thanks to all who helped, and I'll accept any suggestions for improving the efficiency of the solution I came up with based on the contribs here. Main thing is it works FINALLY! ::)
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: News Archives by Year/Month

Post by sam_butler »

sam_butler wrote:Oh, and I thought after that the easiest way to make the list of links, since I'm doing it with yearly archives, would be to have a UDT to loop through from an arbitrary starting year, in my case 2003, until it reaches date('Y').
Yeah, I did that just now... UDT called 'news_archive_sidebar' with comments:

Code: Select all

//Execute some smarty code to make the archive link
global $gCms;
$smarty = &$gCms->GetSmarty();
$smarty_data = "{cms_selflink href='news-archive'}"; //this is the archive link
$smarty->_compile_source('temporary template', $smarty_data, $_compiled );
@ob_start();
$smarty->_eval('?>' . $_compiled);
$_contents = @ob_get_contents();
@ob_end_clean();
$archivelink = $_contents;

//Set the first year of archives:
$firstyear=2003;

//Set some variables
$thisyear=date('Y');
$i=$firstyear;

//HTML before the loop
echo "<ul>";

//Loop through the years to this year
while ($i <= $thisyear) {
 echo "<li>"; //HTML at beginning of iteration
 echo "<a title="News Archive {$i}" href="{$archivelink}?year={$i}">{$i}</a>";
 echo "</li>"; //HTML at end of iteration
 ++$i;
}

//HTML after the loop
echo "</ul>";
Might be useful to someone...
Last edited by sam_butler on Thu Jul 31, 2008 4:47 pm, edited 1 time in total.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12709
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: [SOLVED] News Archives by Year/Month

Post by Dr.CSS »

Why don't change it and say for previous users bla bla bla ...?
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: [SOLVED] News Archives by Year/Month

Post by sam_butler »

mark wrote: Why don't change it and say for previous users bla bla bla ...?
Sorry Mark, I didn't get you there - change what?
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

Re: [SOLVED] News Archives by Year/Month

Post by stopsatgreen »

I think he means modify the code example so that people don't copy and paste it before reading your correction.
sam_butler
Forum Members
Forum Members
Posts: 24
Joined: Sun Jul 20, 2008 3:36 pm
Location: Manchester, UK

Re: [SOLVED] News Archives by Year/Month

Post by sam_butler »

Done.
Henry Chinaski
New Member
New Member
Posts: 5
Joined: Thu Sep 25, 2008 3:34 pm

Re: [SOLVED] News Archives by Year/Month

Post by Henry Chinaski »

Interesting post, as this Word Press-type archive is exactly what we're after with bringing locustvalleyfd.com over to CMSms. Before digging into this too much, I was wondering if...

blast2007 wrote: Calguy told me that we have to wait until 1.4 is out before these kind of patches will be integrated in the official module. We hope.
1. Has the patch been integrated with latest version (1.4.1 now)?

2. Has there been any update to the News Menu mod that NaN posted above?

Thanks!
NaN

Re: [SOLVED] News Archives by Year/Month

Post by NaN »

Since there was no response if it works or not I did no updates yet.
I'm just using this in one single page and there it works like it should.
I'm not going to publish it into the forge since I've read that there will be a solution in the new CMS version.
Henry Chinaski
New Member
New Member
Posts: 5
Joined: Thu Sep 25, 2008 3:34 pm

Re: [SOLVED] News Archives by Year/Month

Post by Henry Chinaski »

Hi NaN,

Thanks for the reply. Could you post the link to what you've read regarding a solution in next CMS version?

Thank you!
NaN

Re: [SOLVED] News Archives by Year/Month

Post by NaN »

It was mentioned in the current topic by blast2007 ;)

http://forum.cmsmadesimple.org/index.ph ... #msg116827
Last edited by NaN on Mon Oct 27, 2008 3:21 pm, edited 1 time in total.
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

Re: [SOLVED] News Archives by Year/Month

Post by blast2007 »

mmm....

News module 2.9 (CMSMS 1.5) still misses this most-wanted ready-to-apply patch

...anyone can tell us why?

Regards
blast
oliverseddon
Forum Members
Forum Members
Posts: 89
Joined: Thu Aug 21, 2008 11:47 am

Re: [SOLVED] News Archives by Year/Month

Post by oliverseddon »

Blast,

Any chance of you re-posting the link to the patch? It seems to be a dead link now and I could really do with having this functionality.



Thanks
Post Reply

Return to “Developers Discussion”