Skip to content

Commit

Permalink
fix subnavigation issues (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Dec 15, 2024
1 parent 0377da5 commit f4d6889
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
14 changes: 11 additions & 3 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,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');
Expand Down
2 changes: 1 addition & 1 deletion app/src/Model/Table/PersonRolesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 24 additions & 9 deletions app/src/View/Helper/TabHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand All @@ -290,21 +298,28 @@ 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];
}

$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]
};
}

/**
Expand Down

0 comments on commit f4d6889

Please sign in to comment.