From 4e92686c1a7de662840fa160b7348003f5ba817d Mon Sep 17 00:00:00 2001 From: Alexander Guth Date: Sat, 4 May 2019 03:08:12 +0200 Subject: [PATCH] Add support for filters in the RelationController configuration (#4241) Credit to @alxy. Docs: https://github.com/octobercms/docs/pull/368 --- .../backend/behaviors/RelationController.php | 101 ++++++++++++++++-- .../partials/_manage_list.htm | 3 + .../relationcontroller/partials/_view.htm | 4 + modules/backend/widgets/Filter.php | 1 + 4 files changed, 103 insertions(+), 6 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 97d9336bbf..fa44ecbfe5 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -60,11 +60,21 @@ class RelationController extends ControllerBehavior */ protected $viewWidget; + /** + * @var \Backend\Widgets\Filter Reference to the view filter widget. + */ + protected $viewFilterWidget; + /** * @var Backend\Classes\WidgetBase Reference to the widget used for relation management. */ protected $manageWidget; + /** + * @var \Backend\Widgets\Filter Reference to the manage filter widget. + */ + protected $manageFilterWidget; + /** * @var Backend\Classes\WidgetBase Reference to widget for relations with pivot data. */ @@ -254,6 +264,8 @@ public function prepareVars() $this->vars['relationField'] = $this->field; $this->vars['relationType'] = $this->relationType; $this->vars['relationSearchWidget'] = $this->searchWidget; + $this->vars['relationManageFilterWidget'] = $this->manageFilterWidget; + $this->vars['relationViewFilterWidget'] = $this->viewFilterWidget; $this->vars['relationToolbarWidget'] = $this->toolbarWidget; $this->vars['relationManageMode'] = $this->manageMode; $this->vars['relationManageWidget'] = $this->manageWidget; @@ -364,6 +376,19 @@ public function initRelation($model, $field = null) $this->searchWidget->bindToController(); } + /* + * Filter widgets (optional) + */ + if ($this->manageFilterWidget = $this->makeFilterWidget('manage')) { + $this->controller->relationExtendManageFilterWidget($this->manageFilterWidget, $this->field, $this->model); + $this->manageFilterWidget->bindToController(); + } + + if ($this->viewFilterWidget = $this->makeFilterWidget('view')) { + $this->controller->relationExtendViewFilterWidget($this->viewFilterWidget, $this->field, $this->model); + $this->viewFilterWidget->bindToController(); + } + /* * View widget */ @@ -538,6 +563,26 @@ public function relationGetSessionKey($force = false) // Widgets // + /** + * Initialize a filter widget + * + * @param $type string Either 'manage' or 'view' + * @return \Backend\Classes\WidgetBase|null + */ + protected function makeFilterWidget($type) + { + if (!$this->getConfig($type . '[filter]')) { + return null; + } + + $filterConfig = $this->makeConfig($this->getConfig($type . '[filter]')); + $filterConfig->alias = $this->alias . ucfirst($type) . 'Filter'; + $filterWidget = $this->makeWidget('Backend\Widgets\Filter', $filterConfig); + + return $filterWidget; + } + + protected function makeToolbarWidget() { $defaultConfig = []; @@ -715,6 +760,18 @@ protected function makeViewWidget() $searchWidget->setActiveTerm(null); } } + + /* + * Link the Filter Widget to the List Widget + */ + if ($this->viewFilterWidget) { + $this->viewFilterWidget->bindEvent('filter.update', function () use ($widget) { + return $widget->onFilter(); + }); + + // Apply predefined filter values + $widget->addFilter([$this->viewFilterWidget, 'applyAllScopesToQuery']); + } } /* * Single (belongs to, has one) @@ -813,6 +870,18 @@ protected function makeManageWidget() $widget->setSearchTerm($this->searchWidget->getActiveTerm()); } } + + /* + * Link the Filter Widget to the List Widget + */ + if ($this->manageFilterWidget) { + $this->manageFilterWidget->bindEvent('filter.update', function () use ($widget) { + return $widget->onFilter(); + }); + + // Apply predefined filter values + $widget->addFilter([$this->manageFilterWidget, 'applyAllScopesToQuery']); + } } /* * Form @@ -1093,7 +1162,7 @@ public function onRelationManageDelete() */ if ($this->viewMode == 'multi') { if (($checkedIds = post('checked')) && is_array($checkedIds)) { - foreach ($checkedIds as $relationId) { + foreach ($checkedIds as $relationId) { if (!$obj = $this->relationModel->find($relationId)) { continue; } @@ -1299,7 +1368,7 @@ public function onRelationManagePivotUpdate() * Provides an opportunity to manipulate the field configuration. * @param object $config * @param string $field - * @param October\Rain\Database\Model $model + * @param \October\Rain\Database\Model $model */ public function relationExtendConfig($config, $field, $model) { @@ -1309,7 +1378,7 @@ public function relationExtendConfig($config, $field, $model) * Provides an opportunity to manipulate the view widget. * @param Backend\Classes\WidgetBase $widget * @param string $field - * @param October\Rain\Database\Model $model + * @param \October\Rain\Database\Model $model */ public function relationExtendViewWidget($widget, $field, $model) { @@ -1319,7 +1388,7 @@ public function relationExtendViewWidget($widget, $field, $model) * Provides an opportunity to manipulate the manage widget. * @param Backend\Classes\WidgetBase $widget * @param string $field - * @param October\Rain\Database\Model $model + * @param \October\Rain\Database\Model $model */ public function relationExtendManageWidget($widget, $field, $model) { @@ -1329,12 +1398,32 @@ public function relationExtendManageWidget($widget, $field, $model) * Provides an opportunity to manipulate the pivot widget. * @param Backend\Classes\WidgetBase $widget * @param string $field - * @param October\Rain\Database\Model $model + * @param \October\Rain\Database\Model $model */ public function relationExtendPivotWidget($widget, $field, $model) { } + /** + * Provides an opportunity to manipulate the manage filter widget. + * @param \Backend\Widgets\Filter $widget + * @param string $field + * @param \October\Rain\Database\Model $model + */ + public function relationExtendManageFilterWidget($widget, $field, $model) + { + } + + /** + * Provides an opportunity to manipulate the view filter widget. + * @param \Backend\Widgets\Filter $widget + * @param string $field + * @param \October\Rain\Database\Model $model + */ + public function relationExtendViewFilterWidget($widget, $field, $model) + { + } + /** * The view widget is often refreshed when the manage widget makes a change, * you can use this method to inject additional containers when this process @@ -1626,4 +1715,4 @@ public function relationGetManageWidget() { public function relationGetViewWidget() { return $this->viewWidget; } -} +} \ No newline at end of file diff --git a/modules/backend/behaviors/relationcontroller/partials/_manage_list.htm b/modules/backend/behaviors/relationcontroller/partials/_manage_list.htm index 49e71ddbbd..4d24002b31 100644 --- a/modules/backend/behaviors/relationcontroller/partials/_manage_list.htm +++ b/modules/backend/behaviors/relationcontroller/partials/_manage_list.htm @@ -11,6 +11,9 @@