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 {
$this->Controller->loadComponent('Paginator');
$data = $this->Controller->Paginator->paginate($query);
if (!empty($options['context'])) {
$this->setCurrentContext($options, $params);
if (!empty($options['contextFilters'])) {
$this->setFilteringContext($options['contextFilters'], $params);
}
$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;
}
protected function setCurrentContext($options, $params)
protected function setFilteringContext($contextFilters, $params)
{
foreach ($params as $filter => $filterValue) {
if ($options['context'] == $filter) {
$this->Controller->set('currentContext', $filterValue);
break;
$filteringContexts = [];
if (!empty($contextFilters['allow_all'])) {
$filteringContexts[] = ['label' => __('All')];
}
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
@ -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();
}

View File

@ -9,6 +9,7 @@ use \Cake\Database\Expression\QueryExpression;
class MetaTemplatesController extends AppController
{
public function update()
{
if ($this->request->is('post')) {
@ -33,9 +34,22 @@ class MetaTemplatesController extends AppController
public function index()
{
$this->CRUD->index([
'filters' => ['name', 'uuid', 'scope'],
'filters' => ['name', 'uuid', 'scope', 'namespace'],
'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']
]);
if ($this->ParamHandler->isRest()) {

View File

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