Adds a timetense list column, inspired by FB
Also added |time_since and |time_tense Twig filters
This commit is contained in:
parent
6db2018f28
commit
6b916cb554
|
|
@ -1,4 +1,5 @@
|
||||||
* **Build 297** (2015-09-16)
|
* **Build 297** (2015-09-16)
|
||||||
|
- Added `timetense` list column type (see Backend > Lists docs), along with `|time_since` and `|time_tense` Twig filters.
|
||||||
- Fixed a bug in deferred binding that allowed repeat bindings and ignored add/delete pairs.
|
- Fixed a bug in deferred binding that allowed repeat bindings and ignored add/delete pairs.
|
||||||
|
|
||||||
* **Build 293** (2015-09-07)
|
* **Build 293** (2015-09-07)
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,10 @@
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
maxlength="255" />
|
maxlength="255" />
|
||||||
|
|
||||||
<!-- Submit Login -->
|
<!-- Submit Login -->
|
||||||
<button type="submit" class="btn btn-primary login-button">
|
<button type="submit" class="btn btn-primary login-button">
|
||||||
<?= e(trans('backend::lang.account.login')) ?>
|
<?= e(trans('backend::lang.account.login')) ?>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="oc-icon-lock pull-right forgot-password">
|
<p class="oc-icon-lock pull-right forgot-password">
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use DbDongle;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use October\Rain\Html\Helper as HtmlHelper;
|
use October\Rain\Html\Helper as HtmlHelper;
|
||||||
use October\Rain\Router\Helper as RouterHelper;
|
use October\Rain\Router\Helper as RouterHelper;
|
||||||
|
use System\Helpers\DateTime as DateTimeHelper;
|
||||||
use Backend\Classes\ListColumn;
|
use Backend\Classes\ListColumn;
|
||||||
use Backend\Classes\WidgetBase;
|
use Backend\Classes\WidgetBase;
|
||||||
use ApplicationException;
|
use ApplicationException;
|
||||||
|
|
@ -946,7 +947,21 @@ class Lists extends WidgetBase
|
||||||
|
|
||||||
$value = $this->validateDateTimeValue($value, $column);
|
$value = $this->validateDateTimeValue($value, $column);
|
||||||
|
|
||||||
return $value->diffForHumans();
|
return DateTimeHelper::timeSince($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process as time as current tense (Today at 0:00)
|
||||||
|
*/
|
||||||
|
protected function evalTimetenseTypeValue($record, $column, $value)
|
||||||
|
{
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->validateDateTimeValue($value, $column);
|
||||||
|
|
||||||
|
return DateTimeHelper::timeTense($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -954,9 +969,7 @@ class Lists extends WidgetBase
|
||||||
*/
|
*/
|
||||||
protected function validateDateTimeValue($value, $column)
|
protected function validateDateTimeValue($value, $column)
|
||||||
{
|
{
|
||||||
if ($value instanceof DateTime) {
|
$value = DateTimeHelper::instance()->makeCarbon($value, false);
|
||||||
$value = Carbon::instance($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$value instanceof Carbon) {
|
if (!$value instanceof Carbon) {
|
||||||
throw new ApplicationException(Lang::get(
|
throw new ApplicationException(Lang::get(
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,8 @@ class ServiceProvider extends ModuleServiceProvider
|
||||||
'trans' => ['Lang', 'get'],
|
'trans' => ['Lang', 'get'],
|
||||||
'transchoice' => ['Lang', 'choice'],
|
'transchoice' => ['Lang', 'choice'],
|
||||||
'md' => ['Markdown', 'parse'],
|
'md' => ['Markdown', 'parse'],
|
||||||
|
'time_since' => ['System\Helpers\DateTime', 'timeSince'],
|
||||||
|
'time_tense' => ['System\Helpers\DateTime', 'timeTense'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
<?php namespace System\Helpers;
|
||||||
|
|
||||||
|
use Lang;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
|
use DateTime as PhpDateTime;
|
||||||
|
|
||||||
|
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**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function timeSince($datetime)
|
||||||
|
{
|
||||||
|
return self::instance()
|
||||||
|
->makeCarbon($datetime)
|
||||||
|
->diffForHumans()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns 24-hour time and the day using the grammatical tense
|
||||||
|
* of the current time. Eg: Today at 12:49, Yesterday at 4:00
|
||||||
|
* or 18 Sep 2015 at 14:33.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function timeTense($datetime)
|
||||||
|
{
|
||||||
|
$datetime = self::instance()->makeCarbon($datetime);
|
||||||
|
$yesterday = $datetime->subDays(1);
|
||||||
|
$tomorrow = $datetime->addDays(1);
|
||||||
|
$time = $datetime->format('H:i');
|
||||||
|
$date = $datetime->format('j M Y');
|
||||||
|
|
||||||
|
if ($datetime->isToday()) {
|
||||||
|
$date = 'Today';
|
||||||
|
}
|
||||||
|
elseif ($datetime->isYesterday()) {
|
||||||
|
$date = 'Yesterday';
|
||||||
|
}
|
||||||
|
elseif ($datetime->isTomorrow()) {
|
||||||
|
$date = 'Tomorrow';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $date.' at '.$time;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts mixed inputs to a Carbon object.
|
||||||
|
*
|
||||||
|
* @return Carbon\Carbon
|
||||||
|
*/
|
||||||
|
public function makeCarbon($value, $throwException = true)
|
||||||
|
{
|
||||||
|
if ($value instanceof Carbon) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
elseif ($value instanceof PhpDateTime) {
|
||||||
|
$value = Carbon::instance($value);
|
||||||
|
}
|
||||||
|
elseif (is_numeric($value)) {
|
||||||
|
$value = Carbon::createFromTimestamp($value);
|
||||||
|
}
|
||||||
|
elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
|
||||||
|
$value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$value instanceof Carbon && $throwException) {
|
||||||
|
throw new Exception('Invalid date value supplied to DateTime helper.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue