From b016ad5307dfcc4c1c4a8e1b7d4a93a8cce5f0c1 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Thu, 5 May 2022 21:57:43 +0300 Subject: [PATCH] Add checkbox handling for boolean fields --- app/src/Controller/StandardController.php | 1 + app/src/Lib/Traits/SearchFilterTrait.php | 20 ++++++++--------- app/templates/element/filter.php | 27 +++++++++++++++++++++-- app/webroot/css/co-responsive.css | 3 +++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index 8ddb7eeff..cb154e5e5 100644 --- a/app/src/Controller/StandardController.php +++ b/app/src/Controller/StandardController.php @@ -440,6 +440,7 @@ public function index() { $searchableAttributes = $table->getSearchableAttributes(); if(!empty($searchableAttributes)) { + // Here we iterate over the attributes and we add a new where clause for each one foreach(array_keys($searchableAttributes) as $attribute) { if(!empty($this->request->getQuery($attribute))) { $query = $table->whereFilter($query, $attribute, $this->request->getQuery($attribute)); diff --git a/app/src/Lib/Traits/SearchFilterTrait.php b/app/src/Lib/Traits/SearchFilterTrait.php index 967e7fc69..7be746641 100644 --- a/app/src/Lib/Traits/SearchFilterTrait.php +++ b/app/src/Lib/Traits/SearchFilterTrait.php @@ -48,20 +48,13 @@ public function getSearchableAttributes(): array { continue; } $this->searchFilters[$column] = [ - 'substring' => ($type === "string"), - 'datetime' => ($type === "timestamp"), + 'type' => $type, // todo: Probably the following line is redundant but i am leaving it for now 'label' => (__d('field', $column) ?? Inflector::humanize($column)) ]; - - // Not every configuration element is necessary for the search form, and - // some need to be calculated, so we do that work here. - $ret[ $column ] = [ - 'label' => (__d('field', $column) ?? Inflector::humanize($column)) - ]; } - return $ret ?? []; + return $this->searchFilters ?? []; } /** @@ -82,12 +75,17 @@ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string $q $search = $q; $sub = false; - if(isset($this->searchFilters[$attribute]['substring']) - && $this->searchFilters[$attribute]['substring']) { + if( $this->searchFilters[$attribute]['type'] == "string") { $search = "%" . $search . "%"; $sub = true; } + // Boolean Values + if($this->searchFilters[$attribute]['type'] == 'boolean') { + return $query->where([$attribute => $search]); + } + + // String values 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)) diff --git a/app/templates/element/filter.php b/app/templates/element/filter.php index 9cc8005c8..eec0a41bd 100644 --- a/app/templates/element/filter.php +++ b/app/templates/element/filter.php @@ -60,7 +60,7 @@ search - + @@ -99,9 +99,13 @@
$options) { + if($options['type'] == 'boolean') { + $field_booleans_columns[$key] = $options; + continue; + } $formParams = [ 'label' => $options['label'], // The default type is text, but we might convert to select below @@ -126,6 +130,25 @@ } ?>
+ +
+
Some Title here
+
+ $options): ?> +
+ Form->label($key); + print $this->Form->checkbox($key, [ + 'class' => 'form-check-input', + 'checked' => $query[$key] ?? 0, + 'hiddenField' => false + ]); + ?> +
+ +
+
+
>