Skip to content

Commit

Permalink
Move column key to utils. Use the function both for Search block fiel…
Browse files Browse the repository at this point in the history
…d labels and index view column names. (COmanage#50, CFM-198)

Enable Filtering in Groups.Improve column_key function.

Co-authored-by: Ioannis Igoumenos <ioigoume@admin.grnet.gr>
  • Loading branch information
Ioannis and Ioannis Igoumenos authored Sep 25, 2022
1 parent 3fd8700 commit e8f4f8e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
12 changes: 12 additions & 0 deletions app/resources/locales/en_US/enumeration.po
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ msgstr "Staff"
msgid "EduPersonAffiliationEnum.student"
msgstr "Student"

msgid "GroupTypeEnum.MA"
msgstr "Active Members"

msgid "GroupTypeEnum.A"
msgstr "Admins"

msgid "GroupTypeEnum.M"
msgstr "All Members"

msgid "GroupTypeEnum.S"
msgstr "Standard"

msgid "LanguageEnum.af"
msgstr "Afrikaans"

Expand Down
2 changes: 1 addition & 1 deletion app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ protected function setTZ() {
// $this->name = Models
$modelsName = $this->name;

// See if we've collected it from the browser in a previous page load. Otherwise
// See if we've collected it from the browser in a previous page load. Otherwise,
// use the system default. If the user set a preferred timezone, we'll catch that below.

$tz = date_default_timezone_get();
Expand Down
4 changes: 2 additions & 2 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ public function index() {

// SearchFilterTrait
if(method_exists($table, "getSearchableAttributes")) {
$searchableAttributes = $table->getSearchableAttributes();
$searchableAttributes = $table->getSearchableAttributes($this->name, $this->viewBuilder()->getVar('vv_tz'));

if(!empty($searchableAttributes)) {
// Here we iterate over the attributes and we add a new where clause for each one
// 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
7 changes: 4 additions & 3 deletions app/src/Lib/Traits/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ trait SearchFilterTrait {
* @return array Array of permitted search attributes and configuration elements needed for display
*/

public function getSearchableAttributes(): array {
public function getSearchableAttributes(string $controller, string $vv_tz=null): array {
$modelname = Inflector::classify(Inflector::underscore($controller));
foreach ($this->filterMetadataFields() as $column => $type) {
// If the column is an array then we are accessing the Metadata fields. Skip
if(is_array($type)) {
continue;
}
$this->searchFilters[$column] = [
'type' => $type,
'label' => (__d('field', $column) ?? Inflector::humanize($column))
'label' => \App\Lib\Util\StringUtilities::columnKey($modelname, $column, $vv_tz, true)
];

// For the date fields we search ranges
Expand Down Expand Up @@ -96,7 +97,7 @@ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string|ar
return $query->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) use ($attribute, $search) {
return $exp->between($attribute, "'" . $search[0] . "'", "'" . $search[1] . "'");
});
// The starts at is non empty. So the data should be greater than the starts_at date
// The starts at is non-empty. So the data should be greater than the starts_at date
} elseif(!empty($search[0])
&& empty($search[1])) {
return $query->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) use ($attribute, $search) {
Expand Down
46 changes: 46 additions & 0 deletions app/src/Lib/Util/StringUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,53 @@

namespace App\Lib\Util;

use \Cake\Utility\Inflector;

class StringUtilities {
/**
* Construct the Column human-readable key
*
* @since COmanage Registry v5.0.0
* @param string $modelsName The name of the Model
* @param string $c The name of the column
* @param string $tz The timezone
* @param boolean $useCustomClMdlLabel Whether to use a custom `Model.column` field entry or rely on the default
* @return string Column friendly name
*/

public static function columnKey($modelsName, $c, $tz=null, $useCustomClMdlLabel=false): string {
if(strpos($c, "_id", strlen($c)-3)) {
// Key is of the form field_id, use .ct label instead
$k = Inflector::camelize(Inflector::pluralize(substr($c, 0, strlen($c)-3)));

return __d('controller', $k, [1]);
}

// Look for a model specific key first
$label = __d('field', $modelsName.'.'.$c);

if($label != $modelsName.'.'.$c && !$useCustomClMdlLabel) {
return $label;
}

if($tz) {
// If there is a timezone aware label, use that
$label = __d('field', $c.'.tz', [$tz]);

if($label != $c.'.tz') {
return $label;
}
}

// XXX for the case of eduPersonAffiliation names we could
// consider the Inflector solution. First underscore and then
// Humanize

// Otherwise look for the general key
$cfield = __d('field', $c);
return ($cfield !== $c) ? $cfield : \Cake\Utility\Inflector::humanize($c);
}

// The following two utilities provide base64 encoding and decoding for
// strings that might contain special characters that could interfere with
// URLs. base64 can generate reserved characters, so we handle those specially
Expand Down
5 changes: 5 additions & 0 deletions app/src/Model/Table/GroupsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class GroupsTable extends Table {
use \App\Lib\Traits\PrimaryLinkTrait;
use \App\Lib\Traits\TableMetaTrait;
use \App\Lib\Traits\ValidationTrait;
use \App\Lib\Traits\SearchFilterTrait;

/**
* Perform Cake Model initialization.
Expand Down Expand Up @@ -92,6 +93,10 @@ public function initialize(array $config): void {
'statuses' => [
'type' => 'enum',
'class' => 'SuspendableStatusEnum'
],
'group_types' => [
'type' => 'enum',
'class' => 'GroupTypeEnum'
]
]);

Expand Down
31 changes: 2 additions & 29 deletions app/templates/Standard/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,6 @@
$linkFilter = [$vv_primary_link => $this->request->getQuery($vv_primary_link)];
}

function _column_key($modelsName, $c, $tz=null) {
if(strpos($c, "_id", strlen($c)-3)) {
// Key is of the form field_id, use .ct label instead
$k = Inflector::camelize(Inflector::pluralize(substr($c, 0, strlen($c)-3)));

return __d('controller', $k, [1]);
}

// Look for a model specific key first
$label = __d('field', $modelsName.'.'.$c);

if($label != $modelsName.'.'.$c) {
return $label;
}

if($tz) {
// If there is a timezone aware label, use that
$label = __d('field', $c.'.tz', [$tz]);

if($label != $c.'.tz') {
return $label;
}
}

// Otherwise look for the general key
return __d('field', $c);
}
?>
<div class="titleNavContainer">
<div class="pageTitle">
Expand Down Expand Up @@ -197,7 +170,7 @@ function _column_key($modelsName, $c, $tz=null) {
print '<span class="row-link-heading">';
}

$label = !empty($cfg['label']) ? $cfg['label'] : _column_key($modelsName, $col, $vv_tz);
$label = !empty($cfg['label']) ? $cfg['label'] : \App\Lib\Util\StringUtilities::columnKey($modelsName, $col, $vv_tz);

if(isset($cfg['sortable']) && $cfg['sortable']) {
if(is_string($cfg['sortable'])) {
Expand Down Expand Up @@ -422,7 +395,7 @@ function _column_key($modelsName, $c, $tz=null) {
} else {
$buttonAttrs['data-bs-content'] = $cfg['button']['popover'];
}
$label = !empty($cfg['label']) ? $cfg['label'] : _column_key($modelsName, $col, $vv_tz);
$label = !empty($cfg['label']) ? $cfg['label'] : \App\Lib\Util\StringUtilities::columnKey($modelsName, $col, $vv_tz);
$buttonAttrs['title'] = $label;
$buttonAttrs['data-bs-toggle'] = 'popover';
$buttonAttrs['data-bs-container'] = 'body';
Expand Down

0 comments on commit e8f4f8e

Please sign in to comment.