From 4640e3a4ad8cfaefda4a8fbba39c8c17e55a41e5 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Fri, 27 Sep 2024 14:36:56 +0300 Subject: [PATCH] Filter Petitions by COU --- app/src/Lib/Traits/SearchFilterTrait.php | 25 ++++++++++++++++++++---- app/src/Model/Table/PetitionsTable.php | 25 +++++++++++++++++++++++- app/templates/element/filter/default.php | 2 +- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/src/Lib/Traits/SearchFilterTrait.php b/app/src/Lib/Traits/SearchFilterTrait.php index eeaec4665..42917277e 100644 --- a/app/src/Lib/Traits/SearchFilterTrait.php +++ b/app/src/Lib/Traits/SearchFilterTrait.php @@ -207,9 +207,10 @@ public function expressionsConstructor(Query $query, QueryExpression $exp, strin // Use the `lower` function to apply uniformity for the search 'string' => $exp->like($query->func()->lower([$attributeWithModelPrefix => 'identifier']), strtolower('%' . $search . '%')), - 'integer', + 'select', // AutoviewVar type + 'parent', // AutoviewVar type 'boolean', - 'parent' => $exp->add([$attributeWithModelPrefix => $search]), + 'integer' => $exp->add([$attributeWithModelPrefix => $search]), 'date' => $exp->add([$attributeWithModelPrefix => FrozenTime::parseDate($search, 'y-M-d')]), 'timestamp' => $this->constructDateComparisonClause($exp, $attributeWithModelPrefix, $search), default => $exp->eq($query->func()->lower([$attributeWithModelPrefix => 'identifier']), @@ -238,6 +239,12 @@ public function getSearchableAttributes(string $controller, string $vv_tz=null): $modelname = Inflector::classify(Inflector::underscore($controller)); $filterConfig = $this->getFilterConfig(); + // We get the filter keys and we will force include the fields that we + // have excluded in the filterMetadataFields() method. This way we have a + // method to exclude a field globally but then force its usage when needed through + // configuration + $filterKeys = array_keys($filterConfig); + // Gather up related models defined in the $filterConfig // XXX For now, we'll list these first - but we should probably provide a better way to order these. foreach ($filterConfig as $field => $f) { @@ -274,9 +281,19 @@ public function getSearchableAttributes(string $controller, string $vv_tz=null): ]; } - foreach ($this->filterMetadataFields() as $column => $type) { + // Include meta fields that are defined in the configuration + // FORCE USAGE + $filterMetadatFielsList = $this->filterMetadataFields(); + foreach ($filterKeys as $key) { + if (isset($filterMetadatFielsList['meta'][$key])) { + $filterMetadatFielsList[$key] = $filterMetadatFielsList['meta'][$key]; + } + + } + + foreach ($filterMetadatFielsList as $column => $type) { // If the column is an array, then we are accessing the Metadata fields. Skip - if(is_array($type)) { + if(\is_array($type)) { continue; } diff --git a/app/src/Model/Table/PetitionsTable.php b/app/src/Model/Table/PetitionsTable.php index fda658603..2c0beb9a8 100644 --- a/app/src/Model/Table/PetitionsTable.php +++ b/app/src/Model/Table/PetitionsTable.php @@ -117,9 +117,32 @@ public function initialize(array $config): void { 'statuses' => [ 'type' => 'enum', 'class' => 'PetitionStatusEnum' + ], + 'couIds' => [ + 'type' => 'select', + 'model' => 'Cous' ] ]); - + + $this->setFilterConfig( + [ + 'cou_id' => [ + // We want to keep the default column configuration and add extra functionality. + // Here the extra functionality is additional to select options since the cou_id + // is of type select + // XXX If the extras key is present, no other provided key will be evaluated. The rest + // of the configuration will be expected from the TableMetaTrait::filterMetadataFields() + 'extras' => [ + 'options' => [ + 'isnotnull' => __d('operation','any'), + 'isnull' => __d('operation','none'), + __d('information','table.list', 'COUs') => '@DATA@', + ] + ] + ] + ] + ); + $this->setPermissions([ // Actions that operate over an entity (ie: require an $id) 'entity' => [ diff --git a/app/templates/element/filter/default.php b/app/templates/element/filter/default.php index 40aa654df..dd15a7e16 100644 --- a/app/templates/element/filter/default.php +++ b/app/templates/element/filter/default.php @@ -47,7 +47,7 @@ $label = Inflector::humanize( Inflector::underscore( - $options['label'] ?? $columns[$key]['label'] + strtolower($options['label'] ?? $columns[$key]['label']) ) );