Skip to content

Commit

Permalink
Add support for filters in the RelationController configuration (octo…
Browse files Browse the repository at this point in the history
  • Loading branch information
alxy authored and bennothommo committed May 4, 2019
1 parent 5055c83 commit 4e92686
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
101 changes: 95 additions & 6 deletions modules/backend/behaviors/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -1626,4 +1715,4 @@ public function relationGetManageWidget() {
public function relationGetViewWidget() {
return $this->viewWidget;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ <h4 class="modal-title"><?= e(trans($relationManageTitle, [
<?php if ($relationSearchWidget): ?>
<?= $relationSearchWidget->render() ?>
<?php endif ?>
<?php if ($relationManageFilterWidget): ?>
<?= $relationManageFilterWidget->render() ?>
<?php endif ?>
<?= $relationManageWidget->render() ?>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<?php if ($relationViewFilterWidget): ?>
<?= $relationViewFilterWidget->render() ?>
<?php endif ?>

<?= $relationViewWidget->render() ?>
1 change: 1 addition & 0 deletions modules/backend/widgets/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ protected function makeFilterScope($name, $config)

$scope = new FilterScope($name, $label);
$scope->displayAs($scopeType, $config);
$scope->idPrefix = $this->alias;

/*
* Set scope value
Expand Down

0 comments on commit 4e92686

Please sign in to comment.