- Fix submitting null dates.
- Allow scope functions for date and date range with Carbon instances at 00:00:00 (date / daterange after) and 23:59:59 (daterange before). - Introduced after / before (Y-m-d 00:00:00 / 23:59:59) for date widget. Kept filtered at Y-m-d - Introduced afterDate / beforeDate (Y-m-d) for daterange. Kept after / before at Y-m-d 00:00:00 / 23:59:59
This commit is contained in:
parent
7170c1460e
commit
578319a204
|
|
@ -499,21 +499,56 @@ class Filter extends WidgetBase
|
|||
}
|
||||
|
||||
switch ($scope->type) {
|
||||
case 'datepicker':
|
||||
if ($scope->value instanceof Carbon && $scopeConditions = $scope->conditions) {
|
||||
$query->whereRaw(strtr($scopeConditions, [':filtered' => $scope->value->format('Y-m-d')]));
|
||||
case 'date':
|
||||
if ($scope->value instanceof Carbon) {
|
||||
$value = $scope->value->setTime(0, 0, 0);
|
||||
|
||||
/*
|
||||
* Condition
|
||||
*/
|
||||
if($scopeConditions = $scope->conditions) {
|
||||
$query->whereRaw(DbDongle::parse(strtr($scopeConditions, [
|
||||
':filtered' => $value->format('Y-m-d'),
|
||||
':after' => $value->format('Y-m-d H:i:s'),
|
||||
':before' => $value->copy()->setTime(23, 59, 59)->format('Y-m-d H:i:s')
|
||||
])));
|
||||
}
|
||||
|
||||
/*
|
||||
* Scope
|
||||
*/
|
||||
if ($scopeMethod = $scope->scope) {
|
||||
$query->$scopeMethod($value);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case 'daterangepicker':
|
||||
if (is_array($scope->value) && count($scope->value) > 1 && ($scopeConditions = $scope->conditions)) {
|
||||
case 'daterange':
|
||||
if (is_array($scope->value) && count($scope->value) > 1) {
|
||||
list($after, $before) = array_values($scope->value);
|
||||
|
||||
if($after instanceof Carbon && $before instanceof Carbon) {
|
||||
$query->whereRaw(strtr($scopeConditions, [
|
||||
':after' => $after->format('Y-m-d'),
|
||||
':before' => $before->format('Y-m-d')
|
||||
]));
|
||||
if($after && $after instanceof Carbon && $before && $before instanceof Carbon) {
|
||||
$after->setTime(0, 0, 0);
|
||||
$before->setTime(23, 59, 59);
|
||||
|
||||
/*
|
||||
* Condition
|
||||
*/
|
||||
if($scopeConditions = $scope->conditions) {
|
||||
$query->whereRaw(DbDongle::parse(strtr($scopeConditions, [
|
||||
':afterDate' => $after->format('Y-m-d'),
|
||||
':after' => $after->format('Y-m-d H:i:s'),
|
||||
':beforeDate' => $before->format('Y-m-d'),
|
||||
':before' => $before->format('Y-m-d H:i:s')
|
||||
])));
|
||||
}
|
||||
|
||||
/*
|
||||
* Scope
|
||||
*/
|
||||
if ($scopeMethod = $scope->scope) {
|
||||
$query->$scopeMethod($after, $before);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -693,7 +728,12 @@ class Filter extends WidgetBase
|
|||
}
|
||||
|
||||
foreach ($ajaxDates as $date) {
|
||||
$dates[] = Carbon::createFromFormat('Y-m-d', $date);
|
||||
if(preg_match('/\d{4}-\d{2}-\d{2}/', $date)) {
|
||||
$dates[] = Carbon::createFromFormat('Y-m-d', $date);
|
||||
} else {
|
||||
$dates = [];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
href="javascript:;"
|
||||
data-scope-name="<?= $scope->scopeName ?>"
|
||||
data-scope-data="<?= e(json_encode([
|
||||
'dates' => $scope->value && is_array($scope->value) ? array_map(function($value) { return $value instanceof \Carbon\Carbon ? $value->format('Y-m-d') : null; }, $scope->value) : null,
|
||||
'dates' => $scope->value && is_array($scope->value) ? array_map(function($value) { return $value && $value instanceof \Carbon\Carbon ? $value->format('Y-m-d') : null; }, $scope->value) : null,
|
||||
'minDate' => $scope->minDate ? $scope->minDate : '2000-01-01',
|
||||
'maxDate' => $scope->maxDate ? $scope->maxDate : '2099-12-31',
|
||||
]))
|
||||
?>">
|
||||
<span class="filter-label"><?= e(trans($scope->label)) ?>:</span>
|
||||
<span class="filter-setting"><?= $scope->value && is_array($scope->value) ? join(' → ', array_map(function($value) { return $value instanceof \Carbon\Carbon ? $value->format('Y-m-d') : null; }, $scope->value)) : e(trans('backend::lang.filter.date_all')) ?></span>
|
||||
<span class="filter-setting"><?= $scope->value && is_array($scope->value) ? join(' → ', array_map(function($value) { return $value && $value instanceof \Carbon\Carbon ? $value->format('Y-m-d') : null; }, $scope->value)) : e(trans('backend::lang.filter.date_all')) ?></span>
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -288,14 +288,20 @@
|
|||
FilterWidget.prototype.updateScopeDateSetting = function ($scope, dates) {
|
||||
var self = this,
|
||||
$setting = $scope.find('.filter-setting'),
|
||||
dateFormat = self.getDateFormat()
|
||||
dateFormat = self.getDateFormat(),
|
||||
dateRegex =/\d{4}-\d{2}-\d{2}/,
|
||||
reset = false
|
||||
|
||||
if (dates && dates.length) {
|
||||
if (dates && dates.length && dates[0].match(dateRegex)) {
|
||||
if (dates.length > 1) {
|
||||
var after = moment(dates[0], 'YYYY-MM-DD').format(dateFormat),
|
||||
before = moment(dates[1], 'YYYY-MM-DD').format(dateFormat);
|
||||
if(dates[1].match(dateRegex)) {
|
||||
var after = moment(dates[0], 'YYYY-MM-DD').format(dateFormat),
|
||||
before = moment(dates[1], 'YYYY-MM-DD').format(dateFormat);
|
||||
|
||||
$setting.text(after + ' → ' + before)
|
||||
$setting.text(after + ' → ' + before)
|
||||
} else {
|
||||
reset = true
|
||||
}
|
||||
}
|
||||
else {
|
||||
$setting.text(moment(dates[0], 'YYYY-MM-DD').format(dateFormat))
|
||||
|
|
@ -304,6 +310,10 @@
|
|||
$scope.addClass('active')
|
||||
}
|
||||
else {
|
||||
reset = true
|
||||
}
|
||||
|
||||
if(reset) {
|
||||
$setting.text(this.getLang('filter.dates.all', 'all'));
|
||||
$scope.removeClass('active')
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue