chg: [component:CRUD] Improved filtering context to be more flexible

- Support fields to be taken as quick filters
- Support custom quick filters by specifying the conditions
pull/37/head
mokaddem 2020-12-08 15:08:12 +01:00
parent 2de66ff2cf
commit 8cc16b4a69
3 changed files with 46 additions and 21 deletions

View File

@ -42,14 +42,10 @@ class CRUDComponent extends Component
} else { } else {
$this->Controller->loadComponent('Paginator'); $this->Controller->loadComponent('Paginator');
$data = $this->Controller->Paginator->paginate($query); $data = $this->Controller->Paginator->paginate($query);
if (!empty($options['context'])) { if (!empty($options['contextFilters'])) {
$this->setCurrentContext($options, $params); $this->setFilteringContext($options['contextFilters'], $params);
} }
$this->Controller->set('data', $data); $this->Controller->set('data', $data);
if (!empty($options['context'])) {
$contexts = array_merge(['_all'], $this->getAllContexts($options['context']));
$this->Controller->set('contexts', $contexts);
}
} }
} }
@ -380,14 +376,29 @@ class CRUDComponent extends Component
return $query; return $query;
} }
protected function setCurrentContext($options, $params) protected function setFilteringContext($contextFilters, $params)
{ {
foreach ($params as $filter => $filterValue) { $filteringContexts = [];
if ($options['context'] == $filter) { if (!empty($contextFilters['allow_all'])) {
$this->Controller->set('currentContext', $filterValue); $filteringContexts[] = ['label' => __('All')];
break; }
if (!empty($contextFilters['fields'])) {
foreach ($contextFilters['fields'] as $field) {
$contextsFromField = $this->getFilteringContextFromField($field);
foreach ($contextsFromField as $contextFromField) {
$filteringContexts[] = [
'label' => Inflector::humanize($contextFromField),
'filterCondition' => [
$field => $contextFromField
]
];
}
} }
} }
if (!empty($contextFilters['custom'])) {
$filteringContexts = array_merge($filteringContexts, $contextFilters['custom']);
}
$this->Controller->set('filteringContexts', $filteringContexts);
} }
public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void
@ -467,7 +478,7 @@ class CRUDComponent extends Component
} }
} }
private function getAllContexts($context) private function getFilteringContextFromField($context)
{ {
return $this->Table->find()->distinct([$context])->all()->extract($context)->toList(); return $this->Table->find()->distinct([$context])->all()->extract($context)->toList();
} }

View File

@ -9,6 +9,7 @@ use \Cake\Database\Expression\QueryExpression;
class MetaTemplatesController extends AppController class MetaTemplatesController extends AppController
{ {
public function update() public function update()
{ {
if ($this->request->is('post')) { if ($this->request->is('post')) {
@ -33,9 +34,22 @@ class MetaTemplatesController extends AppController
public function index() public function index()
{ {
$this->CRUD->index([ $this->CRUD->index([
'filters' => ['name', 'uuid', 'scope'], 'filters' => ['name', 'uuid', 'scope', 'namespace'],
'quickFilters' => ['name', 'uuid', 'scope'], 'quickFilters' => ['name', 'uuid', 'scope'],
'context' => 'scope', 'contextFilters' => [
'allow_all' => true,
'fields' => ['scope'],
'custom' => [
[
'label' => __('Contact DB'),
'filterCondition' => ['scope' => ['individual', 'organisation']]
],
[
'label' => __('Namespace CNW'),
'filterCondition' => ['namespace' => 'cnw']
],
]
],
'contain' => ['MetaTemplateFields'] 'contain' => ['MetaTemplateFields']
]); ]);
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {

View File

@ -1,18 +1,18 @@
<?php <?php
$contextArray = []; $contextArray = [];
$currentContext = !empty($currentContext) ? $currentContext : '_all'; foreach ($filteringContexts as $filteringContext) {
foreach ($contexts as $context) { $filteringContext['filterCondition'] = empty($filteringContext['filterCondition']) ? [] : $filteringContext['filterCondition'];
$urlParams = [ $urlParams = [
'controller' => $this->request->getParam('controller'), 'controller' => $this->request->getParam('controller'),
'action' => 'index', 'action' => 'index',
'?' => $filteringContext['filterCondition']
]; ];
if ($context != '_all') { $currentQuery = $this->request->getQuery();
$urlParams['?'] = ['scope' => $context]; unset($currentQuery['page'], $currentQuery['limit'], $currentQuery['sort']);
}
$contextArray[] = [ $contextArray[] = [
'active' => $context === $currentContext, 'active' => $currentQuery == $filteringContext['filterCondition'],
'url' => $this->Url->build($urlParams), 'url' => $this->Url->build($urlParams),
'text' => Cake\Utility\Inflector::humanize($context), 'text' => $filteringContext['label'],
]; ];
} }