From 4bee7dd39a3098b9252ccc2a38999964842aa925 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Thu, 5 May 2022 20:06:10 +0300 Subject: [PATCH] Basic filtering works --- app/src/Lib/Traits/SearchFilterTrait.php | 56 ++++++++---------------- app/templates/element/filter.php | 7 ++- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/app/src/Lib/Traits/SearchFilterTrait.php b/app/src/Lib/Traits/SearchFilterTrait.php index 616a389e8..967e7fc69 100644 --- a/app/src/Lib/Traits/SearchFilterTrait.php +++ b/app/src/Lib/Traits/SearchFilterTrait.php @@ -51,8 +51,7 @@ public function getSearchableAttributes(): array { 'substring' => ($type === "string"), 'datetime' => ($type === "timestamp"), // todo: Probably the following line is redundant but i am leaving it for now - 'label' => (__d('field', $column) ?? Inflector::humanize($column)), - 'caseSensitive' => true, // hardcoding for now + 'label' => (__d('field', $column) ?? Inflector::humanize($column)) ]; // Not every configuration element is necessary for the search form, and @@ -76,42 +75,23 @@ public function getSearchableAttributes(): array { */ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string $q): object { - if(!empty($this->searchFilters[$attribute])) { - // todo: move caseSensitive into filter block itself - $cs = (isset($this->searchFilters[$attribute]['caseSensitive']) - && $this->searchFilters[$attribute]['caseSensitive']); - - $sub = (isset($this->searchFilters[$attribute]['substring']) - && $this->searchFilters[$attribute]['substring']); - - $search = $q; - - if($sub) { - // Substring - // note, for now at least, a user may infix their own % - $search .= "%"; - } - - if($cs) { - // Case sensitive - $query->where([$attribute => $search]); - } else { - // Case insensitive - $query->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) use ($attribute, $search, $sub) { - $lower = $query->func()->lower([ - // https://book.cakephp.org/3/en/orm/query-builder.html#function-arguments - $attribute => 'identifier' - ]); - if($sub) { - return $exp->like($lower, strtolower($search)); - } else { - return $exp->eq($lower, strtolower($search)); - } - }); - } + // not a permitted attribute + if(empty($this->searchFilters[$attribute])) { + return $query; } - // else not a permitted attribute - - return $query; + + $search = $q; + $sub = false; + if(isset($this->searchFilters[$attribute]['substring']) + && $this->searchFilters[$attribute]['substring']) { + $search = "%" . $search . "%"; + $sub = true; + } + + return $query->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) use ($attribute, $search, $sub) { + $lower = $query->func()->lower([$attribute => 'identifier']); + return ($sub) ? $exp->like($lower, strtolower($search)) + : $exp->eq($lower, strtolower($search)); + }); } } diff --git a/app/templates/element/filter.php b/app/templates/element/filter.php index b919b0ee6..9cc8005c8 100644 --- a/app/templates/element/filter.php +++ b/app/templates/element/filter.php @@ -71,6 +71,11 @@ // We have active filters - not just a sort. $hasActiveFilters = true; + + // The populated variables are in plural while the column names are singular + // Convention: It is a prerequisite that the vvar should be the plural of the column name + $populated_vvar = Cake\Utility\Inflector::pluralize($key); + $pvalue = isset($$populated_vvar) ? $$populated_vvar[ $search_params[$key] ] : $search_params[$key]; ?>