-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commits for CO-1871 (filter Matchgrid) and CO-2137 (configura…
…ble columns)
- Loading branch information
Benn Oshrin
committed
Sep 17, 2021
1 parent
01ce3fb
commit 4e5f334
Showing
9 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
| /** | ||
| * COmanage Match Search Filter Trait | ||
| * | ||
| * Portions licensed to the University Corporation for Advanced Internet | ||
| * Development, Inc. ("UCAID") under one or more contributor license agreements. | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * UCAID licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at: | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * @link https://www.internet2.edu/comanage COmanage Project | ||
| * @package match | ||
| * @since COmanage Match v1.0.0 | ||
| * @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
| */ | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace App\Lib\Traits; | ||
|
|
||
| trait SearchFilterTrait { | ||
| // Array (and configuration) of permitted search filters | ||
| private $searchFilters = array(); | ||
|
|
||
| /** | ||
| * Obtain the set of permitted search attributes. | ||
| * | ||
| * @since COmanage Match v1.0.0 | ||
| * @return array Array of permitted search attributes | ||
| */ | ||
|
|
||
| public function getSearchableAttributes() { | ||
| return array_keys($this->searchFilters); | ||
| } | ||
|
|
||
| /** | ||
| * Add a permitted search filters. | ||
| * | ||
| * @since COmanage Match v1.0.0 | ||
| * @param string $attribute Attribute that filtering is permitted on (database name) | ||
| * @param bool $caseSensitive Whether this attribute is case sensitive | ||
| * @param bool $substring Whether substring searching is permitted for this attrimute | ||
| */ | ||
|
|
||
| public function setSearchFilter(string $attribute, bool $caseSensitive=false, bool $substring=true) { | ||
| $this->searchFilters[$attribute] = [ | ||
| 'caseSensitive' => $caseSensitive, | ||
| 'substring' => $substring | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Build a query where() clause for the configured attribute. | ||
| * | ||
| * @since COmanage Match v1.0.0 | ||
| * @param \Cake\ORM\Query $query Cake ORM Query object | ||
| * @param string $attribute Attribute to filter on (database name) | ||
| * @param string $q Value to filter on | ||
| * @return \Cake\ORM\Query Cake ORM Query object | ||
| */ | ||
|
|
||
| public function whereFilter(\Cake\ORM\Query $query, string $attribute, string $q) { | ||
| if(!empty($this->searchFilters[$attribute])) { | ||
| $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)); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| // else not a permitted attribute | ||
|
|
||
| return $query; | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters