Skip to content

Commit

Permalink
Basic filtering works
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis Igoumenos committed May 5, 2022
1 parent f4cb712 commit 4bee7dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
56 changes: 18 additions & 38 deletions app/src/Lib/Traits/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
});
}
}
7 changes: 6 additions & 1 deletion app/templates/element/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,19 @@

// 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];
?>
<button class="top-filters-active-filter deletebutton spin btn btn-default btn-sm" type="button" aria-controls="<?php print $aria_controls; ?>" title="<?= __d('operation', 'clear.filters',[2]); ?>">
<em class="material-icons">cancel</em>
<span class="top-filters-active-filter-title">
<?= $vv_searchable_attributes[$key]['label']; ?>
</span>
<span class="top-filters-active-filter-value">
<?= filter_var($search_params[$key], FILTER_SANITIZE_SPECIAL_CHARS); ?>
<?= filter_var($pvalue, FILTER_SANITIZE_SPECIAL_CHARS); ?>
</span>
</button>
<?php endforeach; ?>
Expand Down

0 comments on commit 4bee7dd

Please sign in to comment.