<?php
$year = $modx->getOption('year');
if ($year) {
return json_encode(array(
'publishedon:>=' => strtotime('1-1-'.$year),
'publishedon:<' => strtotime('1-1-'.($year+1)),
));
} else {
return '';
}&where=`[[inyear? &year=`2015`]]`
Create a snippet named inyear with the following code:
<!--?php $year = $modx--->getOption('year'); if ($year) { return json_encode(array( 'publishedon:>=' => strtotime('1-1-'.$year), 'publishedon:<' => strtotime('1-1-'.($year+1)), )); } else { return ''; }
After this you could call getRessources with
&where=`[[inyear? &year=`2015`]]`
<?php
$year = $modx->getOption('year', $scriptProperties, 1970);
$where = json_decode($modx->getOption('where', $scriptProperties,'[]'), true);
do {
$c = $modx->newQuery('modResource');
$c->where(array_merge($where, array(
'publishedon:>=' => strtotime('1-1-'.$year),
'publishedon:<' => strtotime('1-1-'.($year+1)),
));
$year--;
} while ($year >= 1970 && !$modx->getCount('modResource', $c));
if ($year >= 1970) {
return json_encode(array(
'publishedon:>=' => strtotime('1-1-'.$year),
'publishedon:<' => strtotime('1-1-'.($year+1)),
));
} else {
return '';
}&where=`[[inyear? &year=`2015` &where=`{"parent":1}`]]`'publishedon:>=' => strtotime('-1 year'),@Jako That's a nice solution, but I think the OP just wants the current year and if it comes up empty, the previous year (maybe I'm misunderstanding the goal).
The only fast way I can think of to do that is a custom snippet called within getPage that uses getCount() to check the current year and then does a query with either that or the previous year and uses a Tpl chunk or two for the output with toArray().
It would be a little simpler if you wanted both the current year and the previous year. I think a slightly modified version of your solution would work for that if you didn't need headers for the years and just displayed the pub dates. Even easier yet if you could just use the past year from the current date:
'publishedon:>=' => strtotime('-1 year'),
You could do it with two separate getResources calls and some output filters, but I think it would be a challenge and somewhat slow since you'd probably want to call it uncached. Also, the output filters might not work when nested like that.
$year = $modx->getOption('year', $scriptProperties, strftime('%Y'));&where=`[[inyear? &where=`{"parent":1}`]]`