Skip to content

Commit

Permalink
Initial commits for CO-1871 (filter Matchgrid) and CO-2137 (configura…
Browse files Browse the repository at this point in the history
…ble columns)
  • Loading branch information
Benn Oshrin committed Sep 17, 2021
1 parent 01ce3fb commit 4e5f334
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/config/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"name": {},
"description": {},
"api_name": { "type": "string", "size": 128 },
"index_display": { "type": "boolean" },
"alphanumeric": { "type": "boolean" },
"case_sensitive": { "type": "boolean" },
"invalidates": { "type": "boolean" },
Expand Down
7 changes: 5 additions & 2 deletions app/src/Controller/MatchgridRecordsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public function initialize() {
throw new \RuntimeException(__('match.er.mgid'));
}

// Set the table name for MatchgridRecordsTable
TableRegistry::getTableLocator()->setConfig('MatchgridRecords', ['table' => $obj->prefixed_table_name]);
// Set the table name and Matchgrid ID for MatchgridRecordsTable
TableRegistry::getTableLocator()->setConfig('MatchgridRecords', [
'table' => $obj->prefixed_table_name,
'matchgrid_id' => $this->request->getQuery('matchgrid_id')
]);
}

/**
Expand Down
15 changes: 14 additions & 1 deletion app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,20 @@ public function index() {
&& $table->getIndexContains()) {
$query->contain($table->getIndexContains());
}


// SearchFilterTrait
if(method_exists($table, "getSearchableAttributes")) {
$searchableAttributes = $table->getSearchableAttributes();

if(!empty($searchableAttributes)) {
foreach($searchableAttributes as $attribute) {
if(!empty($this->request->getQuery($attribute))) {
$query = $table->whereFilter($query, $attribute, $this->request->getQuery($attribute));
}
}
}
}

// The Cake documents describe $this->paginate (which worked in Cake 2),
// but it doesn't seem to work in Cake 3/4. So we just use $this->pagination
// ourselves here.
Expand Down
111 changes: 111 additions & 0 deletions app/src/Lib/Traits/SearchFilterTrait.php
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;
}
}
3 changes: 3 additions & 0 deletions app/src/Locale/en_US/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ msgstr "Description"
msgid "match.fd.id"
msgstr "ID"

msgid "match.fd.index_display"
msgstr "Display Field in Matchgrid Index"

msgid "match.fd.invalidates"
msgstr "Invalidates"

Expand Down
7 changes: 7 additions & 0 deletions app/src/Model/Table/AttributesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public function validationDefault(Validator $validator) {
);
$validator->notEmpty('api_name');

$validator->add(
'index_display',
'toggle',
[ 'rule' => [ 'boolean' ] ]
);
$validator->notEmpty('index_display');

$validator->add(
'attribute_group_id',
'content',
Expand Down
19 changes: 19 additions & 0 deletions app/src/Model/Table/MatchgridRecordsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\TableRegistry;

class MatchgridRecordsTable extends Table {
use \App\Lib\Traits\AutoViewVarsTrait;
use \App\Lib\Traits\MatchgridLinkTrait;
use \App\Lib\Traits\PrimaryLinkTrait;
use \App\Lib\Traits\SearchFilterTrait;

/**
* Perform Cake Model initialization.
Expand All @@ -61,6 +63,23 @@ public function initialize(array $config) {
'fields' => ['keyField' => 'label', 'valueField' => 'label']
]
]);

// Use the matchgrid ID to figure out the attribute configuration for
// search filter purposes

$Matchgrid = TableRegistry::get('Matchgrids');

$mgconfig = $Matchgrid->getMatchgridConfig($config['matchgrid_id']);

foreach($mgconfig->attributes as $attr) {
// XXX for now we permit substring on all fields since we don't have a better way to distinguish
$this->setSearchFilter($attr->name, $attr->case_sensitive, true);
}

// Also permit search on SOR, SORID, and Reference ID
$this->setSearchFilter('sor', true, false);
$this->setSearchFilter('sorid', true, false);
$this->setSearchFilter('referenceid', true, false);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/src/Template/Attributes/fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if($action == 'add' || $action == 'edit') {

print $this->Field->control('api_name');

print $this->Field->control('index_display', [], false);

print $this->Field->control('alphanumeric', [], false);
print $this->Field->control('case_sensitive', [], false);
// CO-1762
Expand Down
12 changes: 11 additions & 1 deletion app/src/Template/MatchgridRecords/columns.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ $indexColumns = [
'referenceid' => [
'type' => 'echo'
]
];
];

// Add any configured attributes to the display set
foreach($attributes as $attr) {
if($attr->index_display === true) {
$indexColumns[$attr->name] = [
'label' => $attr->name,
'type' => 'echo'
];
}
}

0 comments on commit 4e5f334

Please sign in to comment.