I’ve tried to go at this every way I can think. My PHP-fu isn’t strong enough to rewrite or even modify getResources or Archivist, so I am coming here. I feel that one or both of these modules is missing a very important functionality.
I have a client who needs a simple "Event Calendar." We have no need for a big calendar display on the site, or a module that stores data outside the normal MODx framework. I created a container for events and created new Documents in that container with a TV Date field "EventDate."
I have tried several different things and I cannot make MODx display a list of these documents with a date separator, for instance:
March 2011
- 3/22 Board Meeting
- 3/25 Company Picnic
April 2011
- 4/1 April Fool’s Day
- 4/22 Board Meeting
And so on.
I have tried the following:
- Looking for options in getResources that will allow me to do this. I have found none.
- Looking for options in Archivist. Apparently Archivist only displays a list of months that have posts, and getArchives only displays a particular month of posts that it gets in a $_REQUEST or $_GET.
- Using the API to write my own snippet to do this. I am having trouble understanding the syntax for doing custom queries; I am really good with raw MySQL but the convenience of an extra abstraction layer is canceled out by the fact that I have to essentially learn a new language. It also doesn’t seem appropriate to reinvent the wheel, in other words, I think there’s a better way than little ol’ me writing a whole new version of getResources to add this one little feature.
- Writing a wrapper snippet for getResources that outputs the header for each month, and attempts to call getResources requesting only the posts whose EventDate falls between (let x = requested month) x/1 00:00:00 and x/31 23:59:59. This fails because TV dates are not stored as timestamps, which prevents me doing math (<= and >=) on them.
- Asking for help on Freenode #modx. Some of the above suggestions came from there, but we ultimately found that we couldn’t get to the end with any of the ideas.
Due to the constraints of my hardcore programming knowledge, I have not been able to modify getResources or Archivist to do this.
So, what am I missing? Is it a fair point that getResources or Archivist should have this functionality? If not, is there any way to write a wrapper to do this?
For reference, here’s the snippet I wrote to wrap getResources - this is as close as I could come - but does not work as I cannot do math on Date TVs.
<?php
$output = "";
if ($months == "" || !is_int($months)) { $months = 4; }
if ($tpl == "") { $output = "The &tpl parameter is required."; return $output; }
$p['sortbyTV'] = "EventDate";
$p['sortdirTV'] = "ASC";
$p['tpl'] = $tpl;
$p['includeTVs'] = 1;
for ($m = 0; $m < $months; $m++)
{
$calcmonth = date("m", strtotime("+$m months"));
$calcyear = date("Y", strtotime("+$m months"));
$output .= "<h2>" . date("F Y", strtotime("+$m months")) . "</h2>";
$p['where'] = array("EventDate:>=" => "$calcyear-$calcmonth-01 00:00:00",
"EventDate:<=" => "$calcyear-$calcmonth-31 23:59:59");
$output .= $modx->runSnippet('getResources', $p);
}
return $output;