Skip to content

Commit

Permalink
Add checkbox handling for boolean fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis Igoumenos committed May 5, 2022
1 parent a40a33b commit 67c7ea8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 9 additions & 11 deletions app/src/Lib/Traits/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [];
}

/**
Expand All @@ -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))
Expand Down
27 changes: 25 additions & 2 deletions app/templates/element/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<legend id="top-filters-toggle">
<em class="material-icons">search</em>
<?= __d('operation', 'filter'); ?>


<?php if(!empty($search_params)):?>
<span id="top-filters-active-filters">
Expand Down Expand Up @@ -99,9 +99,13 @@
<div id="top-filters-fields">
<div id="top-filters-fields-subgroups">
<?php
$field_subgroup_columns = array();
$field_booleans_columns = [];

foreach($vv_searchable_attributes as $key => $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
Expand All @@ -126,6 +130,25 @@
}
?>
</div>
<?php if(!empty($field_booleans_columns)): ?>
<div class="top-search-checkboxes input">
<div class="top-search-checkbox-label">Some Title here</div>
<div class="top-search-checkbox-fields">
<?php foreach($field_booleans_columns as $key => $options): ?>
<div class="form-check form-check-inline">
<?php
print $this->Form->label($key);
print $this->Form->checkbox($key, [
'class' => 'form-check-input',
'checked' => $query[$key] ?? 0,
'hiddenField' => false
]);
?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php $rebalanceColumns = ((count($vv_searchable_attributes)) % 2 != 0) ? ' class="tss-rebalance"' : ''; ?>
<div id="top-filters-submit"<?php print $rebalanceColumns ?>>
<?php
Expand Down
3 changes: 3 additions & 0 deletions app/webroot/css/co-responsive.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@
column-gap: 1em;
padding: 0 0.5em;
}
.top-search-checkboxes {
padding: 0 0.5em;
}
#top-filters-submit.tss-rebalance {
margin-top: -3.5em;
position: relative;
Expand Down

0 comments on commit 67c7ea8

Please sign in to comment.