From 7e8f100e8e5fbb826a392ba8eead2ff8308aefc9 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Fri, 6 May 2022 19:41:33 +0300 Subject: [PATCH] Prepopulate potential tree parents. Filter using parent id in COU index view. --- app/src/Controller/StandardController.php | 8 ++++++++ app/src/Lib/Traits/SearchFilterTrait.php | 5 +++-- app/src/Model/Table/CousTable.php | 7 +++++++ app/templates/element/javascript.php | 6 +++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index cb154e5e5..eb7ec11cc 100644 --- a/app/src/Controller/StandardController.php +++ b/app/src/Controller/StandardController.php @@ -573,6 +573,14 @@ protected function populateAutoViewVars(object $obj=null) { $this->set($vvar, $query->toArray()); break; + case 'parent': + $modelsName = $this->name; + // $table = the actual table object + $table = $this->$modelsName; + // XXX We assume that all models that load the Tree behavior will + // implement a potentialParents method + $this->set($vvar, $table->potentialParents($this->getCOID())); + break; default: // XXX I18n? and in match? throw new \LogicException('Unknonwn Auto View Var Type {0}', [$avv['type']]); diff --git a/app/src/Lib/Traits/SearchFilterTrait.php b/app/src/Lib/Traits/SearchFilterTrait.php index 7be746641..9f031df0a 100644 --- a/app/src/Lib/Traits/SearchFilterTrait.php +++ b/app/src/Lib/Traits/SearchFilterTrait.php @@ -80,8 +80,9 @@ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string $q $sub = true; } - // Boolean Values - if($this->searchFilters[$attribute]['type'] == 'boolean') { + // Primitive types + $search_types = ['integer', 'boolean']; + if(in_array($this->searchFilters[$attribute]['type'], $search_types, true)) { return $query->where([$attribute => $search]); } diff --git a/app/src/Model/Table/CousTable.php b/app/src/Model/Table/CousTable.php index 1a8122ecb..7a641f17d 100644 --- a/app/src/Model/Table/CousTable.php +++ b/app/src/Model/Table/CousTable.php @@ -86,6 +86,12 @@ public function initialize(array $config): void { 'index' => ['platformAdmin', 'coAdmin'] ] ]); + + $this->setAutoViewVars([ + 'parent_ids' => [ + 'type' => 'parent' + ] + ]); } /** @@ -117,6 +123,7 @@ public function buildTableRules(RulesChecker $rules): RulesChecker { * @param int $id COU ID to determine potential parents of, or null for any (or a new) COU * @param bool $hierarchy Render the hierarchy in the name * @return Array Array of COU IDs and COU Names + * @todo Make a TreeTrait and move the function there */ public function potentialParents(int $coId, int $id=null, bool $hierarchy=false) { diff --git a/app/templates/element/javascript.php b/app/templates/element/javascript.php index 1058a1b6f..201139b51 100644 --- a/app/templates/element/javascript.php +++ b/app/templates/element/javascript.php @@ -127,7 +127,11 @@ e.preventDefault(); e.stopPropagation(); $(this).hide(); - filterId = '#' + $(this).attr("aria-controls"); + // CAKEPHP transforms snake case variables to kebab. As a result + // searching for the initial key will fail + original_value = $(this).attr("aria-controls"); + value_to_snake = original_value.replace(/_/g, "-");; + filterId = '#' + value_to_snake; $(filterId).val(""); $(this).closest('form').submit(); });