From e8f4f8e961d8f68d8bc3bc3753ebc88b678f5406 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Mon, 26 Sep 2022 02:54:19 +0300 Subject: [PATCH] Move column key to utils. Use the function both for Search block field labels and index view column names. (#50, CFM-198) Enable Filtering in Groups.Improve column_key function. Co-authored-by: Ioannis Igoumenos --- app/resources/locales/en_US/enumeration.po | 12 ++++++ app/src/Controller/AppController.php | 2 +- app/src/Controller/StandardController.php | 4 +- app/src/Lib/Traits/SearchFilterTrait.php | 7 ++-- app/src/Lib/Util/StringUtilities.php | 46 ++++++++++++++++++++++ app/src/Model/Table/GroupsTable.php | 5 +++ app/templates/Standard/index.php | 31 +-------------- 7 files changed, 72 insertions(+), 35 deletions(-) diff --git a/app/resources/locales/en_US/enumeration.po b/app/resources/locales/en_US/enumeration.po index a973c25e2..e54973338 100644 --- a/app/resources/locales/en_US/enumeration.po +++ b/app/resources/locales/en_US/enumeration.po @@ -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" diff --git a/app/src/Controller/AppController.php b/app/src/Controller/AppController.php index d5306fa75..c67a31fcf 100644 --- a/app/src/Controller/AppController.php +++ b/app/src/Controller/AppController.php @@ -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(); diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index 9597e105e..184beea8c 100644 --- a/app/src/Controller/StandardController.php +++ b/app/src/Controller/StandardController.php @@ -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)); diff --git a/app/src/Lib/Traits/SearchFilterTrait.php b/app/src/Lib/Traits/SearchFilterTrait.php index 77b1452c5..8135374a8 100644 --- a/app/src/Lib/Traits/SearchFilterTrait.php +++ b/app/src/Lib/Traits/SearchFilterTrait.php @@ -42,7 +42,8 @@ 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)) { @@ -50,7 +51,7 @@ public function getSearchableAttributes(): array { } $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 @@ -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) { diff --git a/app/src/Lib/Util/StringUtilities.php b/app/src/Lib/Util/StringUtilities.php index 45ddaf0e0..8ee21e80b 100644 --- a/app/src/Lib/Util/StringUtilities.php +++ b/app/src/Lib/Util/StringUtilities.php @@ -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 diff --git a/app/src/Model/Table/GroupsTable.php b/app/src/Model/Table/GroupsTable.php index 0ae30ef97..8dc71b91e 100644 --- a/app/src/Model/Table/GroupsTable.php +++ b/app/src/Model/Table/GroupsTable.php @@ -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. @@ -92,6 +93,10 @@ public function initialize(array $config): void { 'statuses' => [ 'type' => 'enum', 'class' => 'SuspendableStatusEnum' + ], + 'group_types' => [ + 'type' => 'enum', + 'class' => 'GroupTypeEnum' ] ]); diff --git a/app/templates/Standard/index.php b/app/templates/Standard/index.php index 656932ec9..2d8fcfdfa 100644 --- a/app/templates/Standard/index.php +++ b/app/templates/Standard/index.php @@ -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); -} ?>
@@ -197,7 +170,7 @@ function _column_key($modelsName, $c, $tz=null) { print ''; } - $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'])) { @@ -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';