2008-05-14 07:54:11 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2008-05-14 07:54:11 +02:00
|
|
|
/**
|
|
|
|
* Represents a single year field.
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBYear extends DBField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2009-09-30 00:02:35 +02:00
|
|
|
$parts=Array('datatype'=>'year', 'precision'=>4, 'arrayValue'=>$this->arrayValue);
|
2009-02-17 04:54:10 +01:00
|
|
|
$values=Array('type'=>'year', 'parts'=>$parts);
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2008-05-14 07:54:11 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2008-05-14 07:54:11 +02:00
|
|
|
$selectBox = new DropdownField($this->name, $title);
|
|
|
|
$selectBox->setSource($this->getDefaultOptions());
|
|
|
|
return $selectBox;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:04:15 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of default options that can
|
|
|
|
* be used to populate a select box, or compare against
|
|
|
|
* input values. Starts by default at the current year,
|
|
|
|
* and counts back to 1900.
|
|
|
|
*
|
2016-07-01 04:26:18 +02:00
|
|
|
* @param int|bool $start starting date to count down from
|
|
|
|
* @param int|bool $end end date to count down to
|
2008-08-09 07:04:15 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-06-17 08:49:23 +02:00
|
|
|
private function getDefaultOptions($start = null, $end = null) {
|
|
|
|
if (!$start) {
|
|
|
|
$start = (int)date('Y');
|
|
|
|
}
|
|
|
|
if (!$end) {
|
|
|
|
$end = 1900;
|
|
|
|
}
|
2008-05-14 07:54:11 +02:00
|
|
|
$years = array();
|
|
|
|
for($i=$start;$i>=$end;$i--) {
|
2013-12-16 04:20:34 +01:00
|
|
|
$years[$i] = $i;
|
2008-05-14 07:54:11 +02:00
|
|
|
}
|
|
|
|
return $years;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-05-14 07:54:11 +02:00
|
|
|
}
|