Skip to content

CFM-291_filtering_improvements #163

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/resources/locales/en_US/field.po
Expand Up @@ -387,6 +387,9 @@ msgstr "{0} Members"
msgid "Groups.desc.members.active"
msgstr "{0} Active Members"

msgid "Groups.desc.member.me"
msgstr "Groups I\'m a member of"

msgid "Groups.group_type"
msgstr "Group Type"

Expand Down
6 changes: 6 additions & 0 deletions app/src/Controller/StandardController.php
Expand Up @@ -563,6 +563,11 @@ public function index() {
// $tableName = models
$tableName = $table->getTable();

$request = $this->getRequest();
$session = $request->getSession();
$username = $session->read('Auth.external.user');
$IdentifiersTable = $this->getTableLocator()->get('Identifiers');

$query = null;

// PrimaryLinkTrait
Expand Down Expand Up @@ -597,6 +602,7 @@ public function index() {
// Here we iterate over the attributes, and we add a new where clause for each one
foreach($searchableAttributes as $attribute => $options) {
if(!empty($this->request->getQuery($attribute))) {
// $ret['person_id'] = $IdentifiersTable->lookupPerson(identifier: $username, coId: $this->getCOID());
$query = $table->whereFilter($query, $attribute, $this->request->getQuery($attribute));
} elseif (!empty($this->request->getQuery($attribute . "_starts_at"))
|| !empty($this->request->getQuery($attribute . "_ends_at"))) {
Expand Down
21 changes: 9 additions & 12 deletions app/src/Lib/Traits/SearchFilterTrait.php
Expand Up @@ -59,19 +59,16 @@ public function getSearchableAttributes(string $controller, string $vv_tz=null):
$modelname = Inflector::classify(Inflector::underscore($controller));
$filterConfig = $this->getFilterConfig();

// Gather up related models defined in the $filterConfig
// XXX For now, we'll list these first - but we should probably provide a better way to order these.
// Gather related models defined in the $filterConfig
foreach ($filterConfig as $field => $f) {
if($f['type'] == 'relatedModel') {
$fieldName = Inflector::classify(Inflector::underscore($field));
$this->searchFilters[$field] = [
'type' => 'string', // XXX for now - this needs to be looked up.
'label' => \App\Lib\Util\StringUtilities::columnKey($fieldName, $field, $vv_tz, true),
'active' => isset($f['active']) ? $f['active'] : true,
'model' => $f['model'],
'order' => $f['order']
];
}
$fieldName = Inflector::classify(Inflector::underscore($field));
$this->searchFilters[$field] = [
'type' => $f['type'] ?? 'string',
'label' => $f['label'] ?? \App\Lib\Util\StringUtilities::columnKey($fieldName, $field, $vv_tz, true),
'active' => $f['active'] ?? true,
'model' => $f['model'],
'order' => $f['order']
];
}

foreach ($this->filterMetadataFields() as $column => $type) {
Expand Down
12 changes: 10 additions & 2 deletions app/src/Model/Table/GroupsTable.php
Expand Up @@ -145,11 +145,19 @@ public function initialize(array $config): void {

$this->setFilterConfig([
'identifier' => [
'type' => 'relatedModel',
'type' => 'string',
'model' => 'Identifier',
'active' => true,
'order' => 4
]
],
'person_id' => [ // Here the association key is ignored
'label' => __d('field', 'Groups.desc.member.me'),
'type' => 'boolean',
'self' => true, // The self key means that the search will focus on the logged-in user and not everyone
'model' => 'GroupMember',
'active' => true,
'order' => 10
],
]);

$this->setPermissions([
Expand Down
66 changes: 53 additions & 13 deletions app/src/Model/Table/IdentifiersTable.php
Expand Up @@ -181,27 +181,67 @@ public function localAfterSave(\Cake\Event\EventInterface $event, \Cake\Datasour
}

/**
* Look up a Person ID from an identifier and identifier type ID.
* Lookup a Person ID from an identifier and identifier type ID.
* Only active Identifiers can be used for lookups.
*
* @since COmanage Registry v5.0.0
* @param int $typeId Identifier Type ID
* @param string $identifier Identifier
*
* @param int|null $typeId Identifier Type ID
* @param string $identifier Identifier
* @param int|null $coId CO ID
*
* @return int Person ID
* @throws Cake\Datasource\Exception\RecordNotFoundException
* @since COmanage Registry v5.0.0
*/

public function lookupPerson(?int $typeId, string $identifier, ?int $coId): int {
$whereClause = [
'identifier' => $identifier,
'status' => SuspendableStatusEnum::Active,
'person_id IS NOT NULL'
];

if($typeId) {
$whereClause['type_id'] = $typeId;
}

$query = $this->find()
->where($whereClause);

if($coId) {
$query->matching('People', function ($q) use ($coId) {
return $q->where(['People.co_id' => $coId]);
});
}

$id = $query->firstOrFail();

return $id->person_id;
}

/**
* Lookup a Person ID from a login identifier. Only active Identifiers can
* be used for lookups.
*
* @param string $identifier Identifier
* @param int $coId CO ID
*
* @return int|null Person ID or null
* @since COmanage Registry v5.0.0
*/

public function lookupPerson(int $typeId, string $identifier): int {
public function lookupPersonForLogin(string $identifier, int $coId): ?int {
$id = $this->find()
->where([
'identifier' => $identifier,
'type_id' => $typeId,
'status' => SuspendableStatusEnum::Active,
'person_id IS NOT NULL'
'Identifiers.identifier' => $identifier,
'Identifiers.status' => SuspendableStatusEnum::Active,
'Identifiers.login' => true,
'Identifiers.person_id IS NOT NULL'
])
->matching('People', function ($q) use ($coId) {
return $q->where(['People.co_id' => $coId]);
})
->firstOrFail();
return $id->person_id;

return $id->person_id ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/templates/element/filter.php
Expand Up @@ -246,7 +246,7 @@
<div class="filter-boolean <?= empty($options['active']) ? 'filter-inactive' : 'filter-active' ?>">
<div class="form-check form-check-inline">
<?php
print $this->Form->label(!empty($columns[$key]['label']) ? $columns[$key]['label'] : $key);
print $this->Form->label($options['label'] ?? $key);
print $this->Form->checkbox($key, [
'id' => str_replace("_", "-", $key),
'class' => 'form-check-input',
Expand Down