Skip to content

Instantly share code, notes, and snippets.

@bwente
Created June 2, 2016 17:21
Show Gist options
  • Save bwente/475044a776cc48e6705c5db090a5969e to your computer and use it in GitHub Desktop.
Save bwente/475044a776cc48e6705c5db090a5969e to your computer and use it in GitHub Desktop.
Dynamic form selector for years
<?php
//[[!FormItYearOptions? &name=`rentexpireYear` &futureYears=`3` &pastYears=`1` &selectedOffset=`1`]]
$name = $modx->getOption('name',$scriptProperties,'year');
$futureYears = $modx->getOption('futureYears',$scriptProperties,'5');
$pastYears = $modx->getOption('pastYears',$scriptProperties,'1');
$selectedOffset = $modx->getOption('selectedOffset',$scriptProperties,'1');
$reverse = $modx->getOption('reverse',$scriptProperties,'');
$thisYear = date('Y');
$startYear = ($thisYear - $pastYears);
$endYear = ($thisYear + $futureYears);
$selectYear = ($thisYear + $selectedOffset);
$range = range($startYear, $endYear);
if($reverse){
$range = array_reverse($range);
}
foreach ($range as $year) {
$selected = "";
if($year == $selectYear) { $selected = " selected"; }
$output .= '<option value="'. $year .'" '. $selected . ' [[!+fi.'.$name.':FormItIsSelected=`$year`]]>' . $year . '</option>';
}
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment