Year Archives in MT
It's a shame MT doesn't provide year-based archives because some of us have enough entries for them even if Movable Type hasn't been out that long! Here's what my 2001 blogging year looks like. Well, you can create them manually-- and since you only have to do it once a year, it isn't too bad. Click the 'more' link for details...
All you have to do to build a year archive is create an index template for each year. In my case, I created one for 2001 called "2001 Index" and gave it a filename of "past/2001.php". For the template itself, you use a series of "MTCalendar" tags specifying the month attribute for the year and month you want to produce. That means you need 12 MTCalendar tags (and their associated baggage) in all.
Now, while you can write all that MT code manually, I decided to use my PerlScript plugin to do the work. I created a template module I named "year_view.pl" and this is what it contains:
# the goal of this script is to produce a calendar
# view of the current year using MT Calendar tags.
# call this script with a 'year' attribute.
# it will use the current year if no year is given.
my $year = $args{year} || ((gmtime(time))[5]+1900);
# we need the blog object to call format_ts so
# month names match the locale of the blog.
my $blog = MT::Blog->load(<MTBlogID>);
# header
print <<EOT;
<table width="90%" align="center">
<tr><td colspan="3">$year</td></tr>
EOT
# each month is produced by this loop
for ('01'..'12') {
# start a new row for every 3rd month
if ((($_-1) % 3) == 0) {
print qq{<tr valign="top">};
}
# print a single month within a table cell
print "<td>".month("$year$_",$blog)."</td>";
# row closure
if (($_ % 3) == 0) {
print qq{</tr>};
}
}
print <<EOT;
</table>
EOT
sub month {
my ($month, $blog) = @_;
my $date = MT::Util::format_ts("%B %Y",
$month.'01010101',$blog);
# you can customize the format of your calendar by
# changing the HTML below. Note that the 'MT' tags
# have been altered to 'ZZ' instead so they aren't
# evaluated BEFORE the Perl code is executed:
my $cal = <<EOT;
<table border="0" cellspacing="4" cellpadding="0">
<tr>
<th colspan="7"><span
class="calendarhead">$date</span></th>
</tr>
<tr>
<th><span class="calendar">Sun</span></th>
<th><span class="calendar">Mon</span></th>
<th><span class="calendar">Tue</span></th>
<th><span class="calendar">Wed</span></th>
<th><span class="calendar">Thu</span></th>
<th><span class="calendar">Fri</span></th>
<th><span class="calendar">Sat</span></th>
</tr>
<ZZCalendar month="$month">
<ZZCalendarWeekHeader><tr></ZZCalendarWeekHeader>
<td align="center"><span class="calendar">
<ZZCalendarIfEntries>
<ZZEntries lastn="1"><a href="<ZZEntryLink
archive_type="Daily">"><ZZCalendarDay></a>
</ZZEntries></ZZCalendarIfEntries>
<ZZCalendarIfNoEntries><ZZCalendarDay>
</ZZCalendarIfNoEntries>
<ZZCalendarIfBlank> </ZZCalendarIfBlank></span></td>
<ZZCalendarWeekFooter></tr></ZZCalendarWeekFooter>
</ZZCalendar>
</table>
EOT
# this is just a little mechanism to keep the 'MT'
# tags from being evaluated until we actually print
# them out from the PerlScript code...
$cal =~ s/ZZ/MT/g;
$cal;
}
Now, to call this PerlScript code from your template, do something like this:
<MTPerlScript year="2001"> <MTInclude module="year_view.pl"> </MTPerlScript>
That's IT! Now, just create an index template for each year you want to archive, and change the 'year' attribute shown above to match the year of the template you're producing. I would also recommend turning off the 'Rebuild this template automatically when rebuilding index templates' option for prior years but enabling it for the current year.
What happens if you don't have any entries in a particular month? For example, what if I wanted to extend the functionality of this plugin to make MT my day planner?
If I enter in a future date (for example, my upcoming Christmas 2002 trip), will this generate the otherwise empty months between now and then?
If so, count me in!
Thanks for all the wonderful work you've done.
Jason
Take a look at:
http://www.bradchoate.com/past/2002.php
And yes, if I were to create entries and set their 'authored on' date to dates in the future, they would appear on the calendar on their respective days.
Brad,
Would you consider writing a plugin (or instruction with you PerlScript plugin for a Perl-nitwit) for "previous" and "next" facility? As now is only available {MTCalendar month="this"}
I would like to have a three months calendar in my monthly archives.
(If anyone can...)
Thanks for this Year explanation!
Brad,
I'm interested using the PerlScript plugin with this year_view script.
After I chmod the files I am not able to loginto mt.cgi anymore with a 500 Error.
When I remove the plugins it's ok again.
My ISP uses Apache with SuExec wrapper, could this be conflicting with your PerlScript plugin?
BTW
I could not comment on http://www.bradchoate.com/past/000882.php with following error:
An error occurred:
Rebuild failed: Renaming tempfile '/home/www/bradchoate/past/.new' failed: Renaming '/home/www/bradchoate/past/.new' to '/home/www/bradchoate/past/' failed: Is a directory
Commenting at this location seems to work...
Hey, I am wondering about how one might set up the thermal calendar into a year without hand coding it. It needs to have php run after the script makes 12 calendars... but after it sets them up all of the $hot tags are removed so it can't be checked. Do you know if there is a way around this? I don't know perl very well...
Hi Brad. I was searching for a calendar listing and found this site. I've installed your Perl module (thanks!) but see nothing above about where to actually put the year_view.pl file. Is it under extlib/bradchoate/ or elsewhere? MT doesn't seem to be finding it.
Thanks!
Mostly unrelated... but how did you get your calendar to highlight today's date?
Thanks for the great tips!
-Rick
http://rick.978.org/
Brad,
I tried several other alternatives, with "this" archiving and "that" template, but thanks to your perl module, I have my Yearly archives exactly the way I needed them and all with just one simple template and include module. This is great.
Thanks for the great work!
-Jason
I have managed to work the caledar (thanks for the great work btw). I dont suppose you could let me know how I turn the month heading into a link. Bit lost with that one! Much appreciated
Hi Brad, I had a similar problem to Quadsk8 when installing the files for the Year Archives. Alas I am not a coding god so am not too sure how to fix this (or how Quadsk8 fixed it in the end!)
To note was also not too sure which directory to place the year_view.pl file into.
Would really love to use your Year Archives! Hope you can help. Thanks!
Where do I upload each of the files ?
If i copy exactly what u have for the year_view.pl file up there it keeps giving me errors: MT::App::CMS=HASH(0x83189bc) Argument "$mon" isn't numeric in sprintf at lib/MT/Util.pm line 151.
MT::App::CMS=HASH(0x83189bc) Argument "th" isn't numeric in sprintf at lib/MT/Util.pm line 151.
MT::App::CMS=HASH(0x83189bc) Argument "th" isn't numeric in numeric eq (==) at lib/MT/Util.pm line 114.
MT::App::CMS=HASH(0x83189bc) Argument "$mon" isn't numeric in modulus (%) at lib/MT/Util.pm line 30.
MT::App::CMS=HASH(0x83189bc) Argument "th" isn't numeric in subtraction (-) at lib/MT/Util.pm line 38.
MT::App::CMS=HASH(0x83189bc) Argument "$mon" isn't numeric in modulus (%) at lib/MT/Util.pm line 30.
MT::App::CMS=HASH(0x83189bc) Argument "th" isn't numeric in subtraction (-) at lib/MT/Util.pm line 38.
MT::App::CMS=HASH(0x83189bc) Argument "000000-1235959" isn't numeric in numeric lt (
and then if i remove $ everywhere it says:
An error occurred:
Build error in template '2003 Index': Error in tag: Error in tag: Invalid month format: must be YYYYMM
Ok sorry about flooding ure comments but everything is fixed apart from 1 thing, everything looks fine but when you go to http://arvind2100.netfirms.com/blog/archives/2003.html and click a certain date it takes you to e.g. 2003_04_01.html but no such file exists although I have posted on that day, my archives for some reason are stored by their post id ! How do I fix that ???