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.
  • Loading branch information
Ioannis Igoumenos committed Sep 5, 2022
1 parent e658231 commit e1bf2a1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
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
5 changes: 3 additions & 2 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::column_key($modelname, $column, $vv_tz, true)
];

// For the date fields we search ranges
Expand Down
43 changes: 43 additions & 0 deletions app/src/Lib/Util/StringUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,49 @@
namespace App\Lib\Util;

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 column_key($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
return __d('field', $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
29 changes: 1 addition & 28 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 @@ -466,7 +439,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::column_key($modelsName, $col, $vv_tz);
$buttonAttrs['title'] = $label;
$buttonAttrs['data-bs-toggle'] = 'popover';
$buttonAttrs['data-bs-container'] = 'body';
Expand Down

0 comments on commit e1bf2a1

Please sign in to comment.