Version: current

     1 <?php
     2 /**
     3  * @package sapphire
     4  * @subpackage model
     5  */
     6
     7 /**
     8  * Represents a single year field.
     9  *
    10  * @package sapphire
    11  * @subpackage model
    12  */
    13 class Year extends DBField {
    14
    15     function requireField() {
    16         $parts=Array('datatype'=>'year', 'precision'=>4, 'arrayValue'=>$this->arrayValue);
    17         $values=Array('type'=>'year', 'parts'=>$parts);
    18         DB::requireField($this->tableName, $this->name, $values);
    19     }
    20
    21     public function scaffoldFormField($title = null, $params = null) {
    22         $selectBox = new DropdownField($this->name, $title);
    23         $selectBox->setSource($this->getDefaultOptions());
    24         return $selectBox;
    25     }
    26
    27     /**
    28      * Returns a list of default options that can
    29      * be used to populate a select box, or compare against
    30      * input values. Starts by default at the current year,
    31      * and counts back to 1900.
    32      *
    33      * @param int $start starting date to count down from
    34      * @param int $end end date to count down to
    35      * @return array
    36      */
    37     private function getDefaultOptions($start=false, $end=false) {
    38         if (!$start) $start = (int)date('Y');
    39         if (!$end) $end = 1900;
    40         $years = array();
    41         for($i=$start;$i>=$end;$i--) {
    42             $years[] = $i;
    43         }
    44         return $years;
    45     }
    46
    47 }
    48 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -