diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 8942160..0ad84be 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -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(); } diff --git a/src/Controller/MetaTemplatesController.php b/src/Controller/MetaTemplatesController.php index c6d4e64..f46b3cb 100644 --- a/src/Controller/MetaTemplatesController.php +++ b/src/Controller/MetaTemplatesController.php @@ -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()) { diff --git a/templates/element/genericElements/ListTopBar/group_context_filters.php b/templates/element/genericElements/ListTopBar/group_context_filters.php index 43a29cb..c32bc66 100644 --- a/templates/element/genericElements/ListTopBar/group_context_filters.php +++ b/templates/element/genericElements/ListTopBar/group_context_filters.php @@ -1,18 +1,18 @@ $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'], ]; }