Skip to content

Commit

Permalink
API / BUG Fix DBField summary methods
Browse files Browse the repository at this point in the history
Cleanup DBField subclasses
Fixes silverstripe#2929
Fixes silverstripe#1381
Fixes silverstripe#5547
Fixes silverstripe#1751
  • Loading branch information
Damian Mooyman committed Jul 12, 2016
1 parent e3afabe commit f7dd170
Show file tree
Hide file tree
Showing 28 changed files with 839 additions and 603 deletions.
74 changes: 56 additions & 18 deletions ORM/FieldType/DBDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class DBDate extends DBField {
/**
* @config
* @see DBDateTime::nice_format
* @see Time::nice_format
* @see DBTime::nice_format
*/
private static $nice_format = 'd/m/Y';

public function setValue($value, $record = null, $markChanged = true) {
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
Expand Down Expand Up @@ -80,44 +80,56 @@ public function setValue($value, $record = null, $markChanged = true) {

/**
* Returns the date in the format specified by the config value nice_format, or dd/mm/yy by default
*/
*
* @return string
*/
public function Nice() {
if($this->value) return $this->Format($this->config()->nice_format);
return $this->Format($this->config()->nice_format);
}

/**
* Returns the date in US format: “01/18/2006”
*
* @return string
*/
public function NiceUS() {
if($this->value) return $this->Format('m/d/Y');
return $this->Format('m/d/Y');
}

/**
* Returns the year from the given date
*
* @return string
*/
public function Year() {
if($this->value) return $this->Format('Y');
return $this->Format('Y');
}

/**
* Returns the Full day, of the given date.
*
* @return string
*/
public function Day(){
if($this->value) return $this->Format('l');
return $this->Format('l');
}

/**
* Returns a full textual representation of a month, such as January.
*
* @return string
*/
public function Month() {
if($this->value) return $this->Format('F');
return $this->Format('F');
}

/**
* Returns the short version of the month such as Jan
*
* @return string
*/
public function ShortMonth() {
if($this->value) return $this->Format('M');
return $this->Format('M');
}

/**
Expand All @@ -126,25 +138,27 @@ public function ShortMonth() {
* @return string
*/
public function DayOfMonth($includeOrdinal = false) {
if($this->value) {
$format = 'j';
if ($includeOrdinal) $format .= 'S';
return $this->Format($format);
}
}

/**
* Returns the date in the format 24 December 2006
*
* @return string
*/
public function Long() {
if($this->value) return $this->Format('j F Y');
return $this->Format('j F Y');
}

/**
* Returns the date in the format 24 Dec 2006
*
* @return string
*/
public function Full() {
if($this->value) return $this->Format('j M Y');
return $this->Format('j M Y');
}

/**
Expand All @@ -158,6 +172,7 @@ public function Format($format) {
$date = new DateTime($this->value);
return $date->format($format);
}
return null;
}

/**
Expand All @@ -173,6 +188,7 @@ public function FormatI18N($formattingString) {
if($this->value) {
return strftime($formattingString, strtotime($this->value));
}
return null;
}

/**
Expand Down Expand Up @@ -217,12 +233,28 @@ public function RangeString($otherDateObj, $includeOrdinals = false) {
else return "$d1 - $d2 $m1 $y1";
}

/**
* Return string in RFC822 format
*
* @return string
*/
public function Rfc822() {
if($this->value) return date('r', strtotime($this->value));
if($this->value) {
return date('r', strtotime($this->value));
}
return null;
}

/**
* Return date in RFC2822 format
*
* @return string
*/
public function Rfc2822() {
if($this->value) return date('Y-m-d H:i:s', strtotime($this->value));
if($this->value) {
return date('Y-m-d H:i:s', strtotime($this->value));
}
return null;
}

public function Rfc3339() {
Expand All @@ -249,7 +281,9 @@ public function Rfc3339() {
* @return String
*/
public function Ago($includeSeconds = true, $significance = 2) {
if($this->value) {
if(!$this->value) {
return null;
}
$time = DBDatetime::now()->Format('U');
if(strtotime($this->value) == $time || $time > strtotime($this->value)) {
return _t(
Expand All @@ -267,7 +301,6 @@ public function Ago($includeSeconds = true, $significance = 2) {
);
}
}
}

/**
* @param boolean $includeSeconds Show seconds, or just round to "less than a minute".
Expand Down Expand Up @@ -304,7 +337,9 @@ public function TimeDiff($includeSeconds = true, $significance = 2) {
* @return string The resulting formatted period
*/
public function TimeDiffIn($format) {
if(!$this->value) return false;
if(!$this->value) {
return null;
}

$time = DBDatetime::now()->Format('U');
$ago = abs($time - strtotime($this->value));
Expand Down Expand Up @@ -333,6 +368,9 @@ public function TimeDiffIn($format) {
case "years":
$span = round($ago/86400/365);
return ($span != 1) ? "{$span} "._t("Date.YEARS", "years") : "{$span} "._t("Date.YEAR", "year");

default:
throw new \InvalidArgumentException("Invalid format $format");
}
}

Expand Down
26 changes: 16 additions & 10 deletions ORM/FieldType/DBDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Convert;
use Exception;
use DatetimeField;
use InvalidArgumentException;
use Zend_Date;
use TemplateGlobalProvider;
use DateTime;
Expand Down Expand Up @@ -75,42 +76,47 @@ public function setValue($value, $record = null, $markChanged = true) {
/**
* Returns the date and time in the format specified by the config value nice_format, or 'd/m/Y g:ia'
* by default (e.g. '31/01/2014 2:23pm').
*
* @return string Formatted date and time.
*/
public function Nice() {
if($this->value) return $this->Format($this->config()->nice_format);
return $this->Format($this->config()->nice_format);
}

/**
* Returns the date and time (in 24-hour format) using the format string 'd/m/Y H:i' e.g. '28/02/2014 13:32'.
*
* @return string Formatted date and time.
*/
public function Nice24() {
if($this->value) return $this->Format('d/m/Y H:i');
return $this->Format('d/m/Y H:i');
}

/**
* Returns the date using the format string 'd/m/Y' e.g. '28/02/2014'.
*
* @return string Formatted date.
*/
public function Date() {
if($this->value) return $this->Format('d/m/Y');
return $this->Format('d/m/Y');
}

/**
* Returns the time in 12-hour format using the format string 'g:ia' e.g. '1:32pm'.
*
* @return string Formatted time.
*/
public function Time() {
if($this->value) return $this->Format('g:ia');
return $this->Format('g:ia');
}

/**
* Returns the time in 24-hour format using the format string 'H:i' e.g. '13:32'.
*
* @return string Formatted time.
*/
public function Time24() {
if($this->value) return $this->Format('H:i');
return $this->Format('H:i');
}

/**
Expand Down Expand Up @@ -149,7 +155,7 @@ public function requireField() {
* @return string Formatted date and time.
*/
public function URLDatetime() {
if($this->value) return $this->Format('Y-m-d%20H:i:s');
return $this->Format('Y-m-d%20H:i:s');
}

public function scaffoldFormField($title = null, $params = null) {
Expand Down Expand Up @@ -187,7 +193,7 @@ public static function now() {
if(self::$mock_now) {
return self::$mock_now;
} else {
return DBField::create_field('SilverStripe\ORM\FieldType\DBDatetime', date('Y-m-d H:i:s'));
return DBField::create_field('Datetime', date('Y-m-d H:i:s'));
}
}

Expand All @@ -203,9 +209,9 @@ public static function set_mock_now($datetime) {
if($datetime instanceof DBDatetime) {
self::$mock_now = $datetime;
} elseif(is_string($datetime)) {
self::$mock_now = DBField::create_field('SilverStripe\ORM\FieldType\DBDatetime', $datetime);
self::$mock_now = DBField::create_field('Datetime', $datetime);
} else {
throw new Exception('DBDatetime::set_mock_now(): Wrong format: ' . $datetime);
throw new InvalidArgumentException('DBDatetime::set_mock_now(): Wrong format: ' . $datetime);
}
}

Expand All @@ -219,7 +225,7 @@ public static function clear_mock_now() {

public static function get_template_global_variables() {
return array(
'Now' => array('method' => 'now', 'casting' => 'SilverStripe\ORM\FieldType\DBDatetime'),
'Now' => array('method' => 'now', 'casting' => 'Datetime'),
);
}
}
Expand Down
23 changes: 11 additions & 12 deletions ORM/FieldType/DBEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ class DBEnum extends DBString {
* </code>
*
* @param string $name
* @param string|array $enum A string containing a comma separated list of options or an
* array of Vals.
* @param string $default The default option, which is either NULL or one of the
* items in the enumeration.
* @param string|array $enum A string containing a comma separated list of options or an array of Vals.
* @param string $default The default option, which is either NULL or one of the items in the enumeration.
*/
public function __construct($name = null, $enum = NULL, $default = NULL) {
if($enum) {
Expand Down Expand Up @@ -107,10 +105,14 @@ public function requireField() {
/**
* Return a dropdown field suitable for editing this field.
*
* @param string $title
* @param string $name
* @param bool $hasEmpty
* @param string $value
* @param string $emptyString
* @return DropdownField
*/
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null,
$emptyString = null) {
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $emptyString = null) {

if(!$title) {
$title = $this->getName();
Expand All @@ -119,18 +121,15 @@ public function formField($title = null, $name = null, $hasEmpty = false, $value
$name = $this->getName();
}

$field = new DropdownField($name, $title, $this->enumValues(false), $value, $form);
$field = new DropdownField($name, $title, $this->enumValues(false), $value);
if($hasEmpty) {
$field->setEmptyString($emptyString);
}

return $field;
}

/**
* @return DropdownField
*/
public function scaffoldFormField($title = null, $params = null) {
public function scaffoldFormField($title = null) {
return $this->formField($title);
}

Expand All @@ -141,7 +140,7 @@ public function scaffoldFormField($title = null, $params = null) {
*/
public function scaffoldSearchField($title = null) {
$anyText = _t('Enum.ANY', 'Any');
return $this->formField($title, null, true, $anyText, null, "($anyText)");
return $this->formField($title, null, true, $anyText, "($anyText)");
}

/**
Expand Down
Loading

0 comments on commit f7dd170

Please sign in to comment.