From 3bad70f0416b99aefb2c0ffbf7de2ad224361a77 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 17 May 2016 05:17:40 +1000 Subject: [PATCH] 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 --- .../backend/behaviors/RelationController.php | 19 +++++++++++++++++++ modules/backend/formwidgets/RecordFinder.php | 14 +++++++++----- modules/backend/widgets/Filter.php | 13 +++++++------ modules/backend/widgets/Lists.php | 6 ++---- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 8e066f7b4..37dc405b9 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -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 */ diff --git a/modules/backend/formwidgets/RecordFinder.php b/modules/backend/formwidgets/RecordFinder.php index fb47533ec..ee4fca014 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -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); }); } diff --git a/modules/backend/widgets/Filter.php b/modules/backend/widgets/Filter.php index c6d8e5234..488741cf7 100644 --- a/modules/backend/widgets/Filter.php +++ b/modules/backend/widgets/Filter.php @@ -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; diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index 692266773..43fe14d03 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -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);