From 216ce61dd84fd85a82d2a016f654c08211ea0dec Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Tue, 1 Oct 2024 11:00:46 +0300 Subject: [PATCH] fix subnavigation issues --- app/src/Controller/StandardController.php | 14 +++++++--- app/src/Model/Table/PersonRolesTable.php | 2 +- app/src/View/Helper/TabHelper.php | 33 ++++++++++++++++------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/app/src/Controller/StandardController.php b/app/src/Controller/StandardController.php index c6f2c3ef..e2ad7dae 100644 --- a/app/src/Controller/StandardController.php +++ b/app/src/Controller/StandardController.php @@ -420,9 +420,17 @@ public function edit(string $id) { // Calculate and set title, supertitle and subtitle [$title, $supertitle, $subtitle] = StringUtilities::entityAndActionToTitle($obj, $modelsName, 'edit'); - $this->set('vv_title', $title); - $this->set('vv_supertitle', $supertitle); - $this->set('vv_subtitle', $subtitle); + // We might have calculated the following values earlier. For example, MVEAController runs before the StandarController + // and makes similar calculations. We will keep the ones calculated before we get here + if ($this->viewBuilder()->getVar('vv_title') === null) { + $this->set('vv_title', $title); + } + if ($this->viewBuilder()->getVar('vv_supertitle') === null) { + $this->set('vv_supertitle', $supertitle); + } + if ($this->viewBuilder()->getVar('vv_subtitle') === null) { + $this->set('vv_subtitle', $subtitle); + } // Let the view render $this->render('/Standard/add-edit-view'); diff --git a/app/src/Model/Table/PersonRolesTable.php b/app/src/Model/Table/PersonRolesTable.php index 69e48110..324b81f0 100644 --- a/app/src/Model/Table/PersonRolesTable.php +++ b/app/src/Model/Table/PersonRolesTable.php @@ -175,7 +175,7 @@ public function initialize(array $config): void { // render in index mode for the same use case/context // XXX edit should go first. 'People' => ['edit', 'view'], - 'PersonRoles' => ['index'], + 'PersonRoles' => ['edit', 'view', 'index'], 'ExternalIdentities' => ['index'], ], // What model will have a counter-badge after the tab title diff --git a/app/src/View/Helper/TabHelper.php b/app/src/View/Helper/TabHelper.php index 948094ce..6eeaa36c 100644 --- a/app/src/View/Helper/TabHelper.php +++ b/app/src/View/Helper/TabHelper.php @@ -102,7 +102,10 @@ public function constructLinkUrl(string $tab, string|int $curId, bool $isNested 'action' => $action ]; - if(\in_array('index', $vv_subnavigation_action[$tab], true)) { + if ( + $action === 'index' + && \in_array('index', $vv_subnavigation_action[$tab], true) + ) { $url['?'] = $linkFilter; } else { $url[] = $curId; @@ -262,9 +265,14 @@ public function getHasManyAssociationModels(string $modelName): \Generator */ public function getLinkFilter(string $tab, int|string $curId): array { - $mainTabList = $this->getView()->get('vv_sub_nav_attributes')['tabs']; + $vv_sub_nav_attributes = $this->getView()->get('vv_sub_nav_attributes'); + $subnav_tabs = $vv_sub_nav_attributes['tabs']; + $subnav_allowed_actions = $vv_sub_nav_attributes['action']; + $vv_action = $this->getView()->get('vv_action'); $fullModelsName = $tab; $modelName = $tab; + $curController = $this->getView()->getRequest()->getParam('controller'); + // We have two use cases. The first one is for the Core models and the second one is for the // plugins. In case we have a plugin we need to retrieve the name from the database if(str_contains($tab, '.Plugin')) { @@ -290,9 +298,9 @@ public function getLinkFilter(string $tab, int|string $curId): array $primary_link = null; if(count($primary_link_list) > 1) { $primary_link = collection($primary_link_list) - ->filter(function($link) use ($mainTabList) { + ->filter(function($link) use ($subnav_tabs) { $linkToClass = StringUtilities::foreignKeyToClassName($link); - return \in_array($linkToClass, $mainTabList, true); + return \in_array($linkToClass, $subnav_tabs, true); })->first(); } else if (\is_array($primary_link_list) && !empty($primary_link_list)) { $primary_link = $primary_link_list[0]; @@ -300,11 +308,18 @@ public function getLinkFilter(string $tab, int|string $curId): array $foreignKey = StringUtilities::classNameToForeignKey($modelName); - // If the primary link is the co_id, it means this is a root element. As a result, we will construct - // the link filter key from the controller itself. If not, we will get the primary link directly. - return ($primary_link === 'co_id') - ? [$foreignKey => $curId] - : [$primary_link => $curId]; + // - If the primary link is the co_id, it means this is a root element. As a result, we will construct + // the link filter key from the controller itself. + // - The action the endpoint url action is edit, and we allow edit for this sub-navigation element we will + // return the id instead of any foreign key + // - We will fallback to the primary link directly. + return match(true) { + $fullModelsName === $curController + && $vv_action === 'edit' + && \in_array($vv_action, $subnav_allowed_actions[$fullModelsName], true) => ['id' => $curId], + $primary_link === 'co_id' => [$foreignKey => $curId], + default => [$primary_link => $curId] + }; } /**