From 02165a8a4a19db004a8f5f8d063c4629c62a6252 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 23 Apr 2016 13:17:04 +1000 Subject: [PATCH] Introduce a new localized date control Added a helped Backend::DateTime() for rendering the appropriate HTML output as a '.PHP_EOL; + } + } diff --git a/modules/backend/layouts/_head.htm b/modules/backend/layouts/_head.htm index d11d5837b..c2be8adcf 100644 --- a/modules/backend/layouts/_head.htm +++ b/modules/backend/layouts/_head.htm @@ -2,6 +2,8 @@ + + diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index 5743d50a7..3403e9fc4 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -913,13 +913,19 @@ class Lists extends WidgetBase return null; } - $value = $this->validateDateTimeValue($value, $column); + $dateTime = $this->validateDateTimeValue($value, $column); if ($column->format !== null) { - return $value->format($column->format); + $value = $dateTime->format($column->format); + } + else { + $value = $dateTime->toDayDateTimeString(); } - return $value->toDayDateTimeString(); + return Backend::dateTime($dateTime, $value, [ + 'format' => $column->format, + 'formatAlias' => 'dateTimeLongMin' + ]); } /** @@ -931,13 +937,16 @@ class Lists extends WidgetBase return null; } - $value = $this->validateDateTimeValue($value, $column); + $dateTime = $this->validateDateTimeValue($value, $column); - if ($column->format === null) { - $column->format = 'g:i A'; - } + $format = $column->format !== null ? $column->format : 'g:i A'; - return $value->format($column->format); + $value = $dateTime->format($format); + + return Backend::dateTime($dateTime, $value, [ + 'format' => $column->format, + 'formatAlias' => 'time' + ]); } /** @@ -949,13 +958,19 @@ class Lists extends WidgetBase return null; } - $value = $this->validateDateTimeValue($value, $column); + $dateTime = $this->validateDateTimeValue($value, $column); if ($column->format !== null) { - return $value->format($column->format); + $value = $dateTime->format($column->format); + } + else { + $value = $dateTime->toFormattedDateString(); } - return $value->toFormattedDateString(); + return Backend::dateTime($dateTime, $value, [ + 'format' => $column->format, + 'formatAlias' => 'dateLongMin' + ]); } /** @@ -967,9 +982,13 @@ class Lists extends WidgetBase return null; } - $value = $this->validateDateTimeValue($value, $column); + $dateTime = $this->validateDateTimeValue($value, $column); - return DateTimeHelper::timeSince($value); + $value = DateTimeHelper::timeSince($dateTime); + + return Backend::dateTime($dateTime, $value, [ + 'timeSince' => true + ]); } /** @@ -981,9 +1000,13 @@ class Lists extends WidgetBase return null; } - $value = $this->validateDateTimeValue($value, $column); + $dateTime = $this->validateDateTimeValue($value, $column); - return DateTimeHelper::timeTense($value); + $value = DateTimeHelper::timeTense($dateTime); + + return Backend::dateTime($dateTime, $value, [ + 'timeTense' => true + ]); } /** @@ -991,7 +1014,7 @@ class Lists extends WidgetBase */ protected function validateDateTimeValue($value, $column) { - $value = DateTimeHelper::instance()->makeCarbon($value, false); + $value = DateTimeHelper::makeCarbon($value, false); if (!$value instanceof Carbon) { throw new ApplicationException(Lang::get( diff --git a/modules/system/helpers/DateTime.php b/modules/system/helpers/DateTime.php index ee03e8125..bf29521f8 100644 --- a/modules/system/helpers/DateTime.php +++ b/modules/system/helpers/DateTime.php @@ -8,8 +8,6 @@ use InvalidArgumentException; class DateTime { - use \October\Rain\Support\Traits\Singleton; - /** * Returns a human readable time difference from the value to the * current time. Eg: **10 minutes ago** @@ -18,10 +16,7 @@ class DateTime */ public static function timeSince($datetime) { - return self::instance() - ->makeCarbon($datetime) - ->diffForHumans() - ; + return self::makeCarbon($datetime)->diffForHumans(); } /** @@ -33,7 +28,7 @@ class DateTime */ public static function timeTense($datetime) { - $datetime = self::instance()->makeCarbon($datetime); + $datetime = self::makeCarbon($datetime); $yesterday = $datetime->subDays(1); $tomorrow = $datetime->addDays(1); $time = $datetime->format('H:i'); @@ -57,7 +52,7 @@ class DateTime * * @return Carbon\Carbon */ - public function makeCarbon($value, $throwException = true) + public static function makeCarbon($value, $throwException = true) { if ($value instanceof Carbon) { // Do nothing @@ -78,4 +73,60 @@ class DateTime return $value; } + + /** + * Converts a PHP date format to "Moment.js" format. + * @param string $format + * @return string + */ + public static function momentFormat($format) + { + $replacements = [ + 'd' => 'DD', + 'D' => 'ddd', + 'j' => 'D', + 'l' => 'dddd', + 'N' => 'E', + 'S' => 'o', + 'w' => 'e', + 'z' => 'DDD', + 'W' => 'W', + 'F' => 'MMMM', + 'm' => 'MM', + 'M' => 'MMM', + 'n' => 'M', + 't' => '', // no equivalent + 'L' => '', // no equivalent + 'o' => 'YYYY', + 'Y' => 'YYYY', + 'y' => 'YY', + 'a' => 'a', + 'A' => 'A', + 'B' => '', // no equivalent + 'g' => 'h', + 'G' => 'H', + 'h' => 'hh', + 'H' => 'HH', + 'i' => 'mm', + 's' => 'ss', + 'u' => 'SSS', + 'e' => 'zz', // deprecated since version 1.6.0 of moment.js + 'I' => '', // no equivalent + 'O' => '', // no equivalent + 'P' => '', // no equivalent + 'T' => '', // no equivalent + 'Z' => '', // no equivalent + 'c' => '', // no equivalent + 'r' => '', // no equivalent + 'U' => 'X', + ]; + + foreach ($replacements as $from => $to) { + $replacements['\\'.$from] = '['.$from.']'; + } + + $momentFormat = strtr($format, $replacements); + return $momentFormat; + } + }