Relation controller now applies the defined constraints by default

Relation controller now supports scope and conditions for the manage mode (list)
Logic has been modified across recordfinder and filter that only allows one constraint in this order (1. conditions, 2. scope, 3. defined constraints (where applicable))
Fixes #1203
This commit is contained in:
Samuel Georges 2016-05-17 05:17:40 +10:00
parent 41de93a2cb
commit 3bad70f041
4 changed files with 37 additions and 15 deletions

View File

@ -713,6 +713,25 @@ class RelationController extends ControllerBehavior
$widget = $this->makeWidget('Backend\Widgets\Lists', $config);
/*
* Apply defined constraints
*/
if ($sqlConditions = $this->getConfig('manage[conditions]')) {
$widget->bindEvent('list.extendQueryBefore', function($query) use ($sqlConditions) {
$query->whereRaw($sqlConditions);
});
}
elseif ($scopeMethod = $this->getConfig('manage[scope]')) {
$widget->bindEvent('list.extendQueryBefore', function($query) use ($scopeMethod) {
$query->$scopeMethod();
});
}
else {
$widget->bindEvent('list.extendQueryBefore', function($query) {
$this->relationObject->addDefinedConstraintsToQuery($query);
});
}
/*
* Link the Search Widget to the List Widget
*/

View File

@ -250,15 +250,19 @@ class RecordFinder extends FormWidgetBase
'scope' => $this->searchScope,
]);
if ($scopeMethod = $this->scope) {
if ($sqlConditions = $this->conditions) {
$widget->bindEvent('list.extendQueryBefore', function($query) use ($sqlConditions) {
$query->whereRaw($sqlConditions);
});
}
elseif ($scopeMethod = $this->scope) {
$widget->bindEvent('list.extendQueryBefore', function($query) use ($scopeMethod) {
$query->$scopeMethod();
});
}
if ($sqlConditions = $this->conditions) {
$widget->bindEvent('list.extendQueryBefore', function($query) use ($sqlConditions) {
$query->whereRaw($sqlConditions);
else {
$widget->bindEvent('list.extendQueryBefore', function($query) {
$this->getRelationObject()->addDefinedConstraintsToQuery($query);
});
}

View File

@ -547,16 +547,16 @@ class Filter extends WidgetBase
':before' => $value->copy()->addDay()->addMinutes(-1)->format('Y-m-d H:i:s')
])));
}
/*
* Scope
*/
if ($scopeMethod = $scope->scope) {
elseif ($scopeMethod = $scope->scope) {
$query->$scopeMethod($value);
}
}
break;
case 'daterange':
if (is_array($scope->value) && count($scope->value) > 1) {
list($after, $before) = array_values($scope->value);
@ -574,17 +574,17 @@ class Filter extends WidgetBase
':before' => $before->format('Y-m-d H:i:s')
])));
}
/*
* Scope
*/
if ($scopeMethod = $scope->scope) {
elseif ($scopeMethod = $scope->scope) {
$query->$scopeMethod($after, $before);
}
}
}
break;
default:
$value = is_array($scope->value) ? array_keys($scope->value) : $scope->value;
@ -612,13 +612,14 @@ class Filter extends WidgetBase
$query->whereRaw(DbDongle::parse(strtr($scopeConditions, [':filtered' => $filtered])));
}
/*
* Scope
*/
if ($scopeMethod = $scope->scope) {
elseif ($scopeMethod = $scope->scope) {
$query->$scopeMethod($value);
}
break;
}
return $query;

View File

@ -633,10 +633,8 @@ class Lists extends WidgetBase
protected function defineListColumns()
{
if (!isset($this->columns) || !is_array($this->columns) || !count($this->columns)) {
throw new ApplicationException(Lang::get(
'backend::lang.list.missing_columns',
['class'=>get_class($this->controller)]
));
$class = get_class($this->model instanceof Model ? $this->model : $this->controller);
throw new ApplicationException(Lang::get('backend::lang.list.missing_columns', compact('class')));
}
$this->addColumns($this->columns);