diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index f91a106..e64470d 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -55,6 +55,14 @@ class CRUDComponent extends Component $this->Controller->set('data', $data); } } + + public function filtering(): void + { + $filters = !empty($this->Controller->filters) ? $this->Controller->filters : []; + $this->Controller->set('filters', $filters); + $this->Controller->viewBuilder()->setLayout('ajax'); + $this->Controller->render('/genericTemplates/filters'); + } /** * getResponsePayload Returns the adaquate response payload based on the request context diff --git a/src/Controller/OrganisationsController.php b/src/Controller/OrganisationsController.php index a8fc508..8e8cba8 100644 --- a/src/Controller/OrganisationsController.php +++ b/src/Controller/OrganisationsController.php @@ -12,10 +12,13 @@ use Cake\Http\Exception\ForbiddenException; class OrganisationsController extends AppController { + + public $filters = ['name', 'uuid', 'nationality', 'sector', 'type', 'url', 'Alignments.id', 'MetaFields.field', 'MetaFields.value', 'MetaFields.MetaTemplates.name']; + public function index() { $this->CRUD->index([ - 'filters' => ['name', 'uuid', 'nationality', 'sector', 'type', 'url', 'Alignments.id', 'MetaFields.field', 'MetaFields.value', 'MetaFields.MetaTemplates.name'], + 'filters' => $this->filters, 'quickFilters' => [['name' => true], 'uuid', 'nationality', 'sector', 'type', 'url'], 'contextFilters' => [ 'custom' => [ @@ -64,6 +67,11 @@ class OrganisationsController extends AppController $this->set('metaGroup', 'ContactDB'); } + public function filtering() + { + $this->CRUD->filtering(); + } + public function add() { $this->CRUD->add(); diff --git a/templates/Organisations/index.php b/templates/Organisations/index.php index dd3d7b7..da638cb 100644 --- a/templates/Organisations/index.php +++ b/templates/Organisations/index.php @@ -21,10 +21,11 @@ echo $this->element('genericElements/IndexTable/index_table', [ ], [ 'type' => 'search', - 'button' => __('Filter'), + 'button' => __('Search'), 'placeholder' => __('Enter value to search'), 'data' => '', - 'searchKey' => 'value' + 'searchKey' => 'value', + 'allowFilering' => true ] ] ], diff --git a/templates/element/genericElements/ListTopBar/group_context_filters.php b/templates/element/genericElements/ListTopBar/group_context_filters.php index 5a64a74..c7cc3ed 100644 --- a/templates/element/genericElements/ListTopBar/group_context_filters.php +++ b/templates/element/genericElements/ListTopBar/group_context_filters.php @@ -13,7 +13,9 @@ if (!empty($filteringContext['filterCondition'])) { // PHP replaces `.` by `_` when fetching the request parameter $currentFilteringContext = []; foreach ($filteringContext['filterCondition'] as $currentFilteringContextKey => $value) { - $currentFilteringContext[str_replace('.', '_', $currentFilteringContextKey)] = $value; + $currentFilteringContextKey = str_replace('.', '_', $currentFilteringContextKey); + $currentFilteringContextKey = str_replace(' ', '_', $currentFilteringContextKey); + $currentFilteringContext[$currentFilteringContextKey] = $value; } } else { $currentFilteringContext = $filteringContext['filterCondition']; diff --git a/templates/element/genericElements/ListTopBar/group_search.php b/templates/element/genericElements/ListTopBar/group_search.php index 5a47ce2..8e85f39 100644 --- a/templates/element/genericElements/ListTopBar/group_search.php +++ b/templates/element/genericElements/ListTopBar/group_search.php @@ -12,12 +12,32 @@ * - id: element ID for the input field - defaults to quickFilterField */ if (!isset($data['requirement']) || $data['requirement']) { + $filteringButton = ''; + if (!empty($data['allowFilering'])) { + $activeFilters = !empty($activeFilters) ? $activeFilters : []; + $buttonConfig = [ + 'icon' => 'filter', + 'params' => [ + 'title' => __('Filter index'), + 'id' => sprintf('toggleFilterButton-%s', h($tableRandomValue)) + ] + ]; + if (count($activeFilters) > 0) { + $buttonConfig['badge'] = [ + 'variant' => 'light', + 'text' => count($activeFilters), + 'title' => __n('There is {0} active filter', 'There are {0} active filters', count($activeFilters), count($activeFilters)) + ]; + } + $filteringButton = $this->Bootstrap->button($buttonConfig); + } $button = empty($data['button']) && empty($data['fa-icon']) ? '' : sprintf( - '
', + '
%s
', empty($data['data']) ? '' : h($data['data']), h($tableRandomValue), empty($data['fa-icon']) ? '' : sprintf('', h($data['fa-icon'])), - empty($data['button']) ? '' : h($data['button']) + empty($data['button']) ? '' : h($data['button']), + $filteringButton ); if (!empty($data['cancel'])) { $button .= $this->element('/genericElements/ListTopBar/element_simple', array('data' => $data['cancel'])); @@ -45,6 +65,7 @@ var action = 'request->getParam('action') ?>'; var additionalUrlParams = ''; var quickFilter = ; + var activeFilters = ; { + loadingOverlay.hide() + }) + } }); diff --git a/templates/genericTemplates/filters.php b/templates/genericTemplates/filters.php new file mode 100644 index 0000000..44c1b63 --- /dev/null +++ b/templates/genericTemplates/filters.php @@ -0,0 +1,188 @@ +Bootstrap->table( + [ + 'small' => true, + 'striped' => false, + 'hover' => false, + 'tableClass' => ['indexFilteringTable'], + ], + [ + 'fields' => [ + __('Field'), + __('Operator'), + [ + 'labelHtml' => sprintf('%s %s', + __('Value'), + sprintf('', __('Supports strict match and LIKE match with the `%` character. Example: `%.com`')) + ) + ], + __('Action') + ], + 'items' => [] +]); + + +echo $this->Bootstrap->modal([ + 'title' => __('Filtering options for {0}', Inflector::singularize($this->request->getParam('controller'))), + 'size' => 'lg', + 'type' => 'confirm', + 'bodyHtml' => $filteringForm, + 'confirmText' => __('Filter'), + 'confirmFunction' => 'filterIndex(this)' +]); +?> + + \ No newline at end of file