Skip to content

Instantly share code, notes, and snippets.

@exside
Created March 2, 2013 21:21
Show Gist options
  • Save exside/5073346 to your computer and use it in GitHub Desktop.
Save exside/5073346 to your computer and use it in GitHub Desktop.
<?php
/**
* getYears
* List up years from a given start untill the current year or given end year.
* Example: [[!getYears? &tpl=`yourChunk` &startYear=`2000`]]
*
* Properties:
* tpl - (Req.) The name of the chunk to use for each year entry
* outerTpl - (Opt.) The name over the outer chunk. Use a [[+wrapper]] placeholder.
* outputSeparator - (Opt.) Separate the output with this contents. Defaults a newline
* toPlaceholder - (Opt.) Assign out to a placeholder instead of returning it
* startYear - (Req.) The year to start from
* endYear - (Opt.) The year to end with. Defaults: current year from date()
* yearKey - (Req.) The name of the GET parameter with the selected year
* reversed - (Opt.) To order the years from closest to furthest. Defaults: true
*
* @author Bert Oost at OostDesign.nl <[email protected]>
*/
$tpl = $modx->getOption('tpl', $scriptProperties, '');
$outerTpl = $modx->getOption('outerTpl', $scriptProperties, '');
$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n");
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
$startYear = $modx->getOption('startYear', $scriptProperties, 0);
$endYear = $modx->getOption('endYear', $scriptProperties, date('Y'));
$yearKey = $modx->getOption('yearKey', $scriptProperties, 'year');
$reversed = (boolean) $modx->getOption('reversed', $scriptProperties, 1);
$output = '';
$year = ($reversed) ? $endYear : $startYear;
$idx = 1;
while((!$reversed && $year <= $endYear) || ($reversed && $year >= $startYear)) {
$phs = array(
'year' => $year,
'idx' => $idx,
'selected' => (isset($_GET[$yearKey]) && !empty($_GET[$yearKey]) && $_GET[$yearKey] == $year) ? 1 : 0,
);
$output .= $modx->getChunk($tpl, $phs).$outputSeparator;
if($reversed) { $year--; } else { $year++; }
$idx++;
}
if(!empty($outerTpl) && !empty($output)) {
$phs = array('yearKey' => $yearKey, 'wrapper' => $output);
$output = $modx->getChunk($outerTpl, $phs).$outputSeparator;
}
if(!empty($toPlaceholder) && !empty($output)) {
$modx->setPlaceholder($toPlaceholder, $output);
return '';
}
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment