From 0e951de96c1d6f8445e35bdbb333b9563f3efe60 Mon Sep 17 00:00:00 2001 From: Bozana Bokan Date: Tue, 30 Apr 2024 22:22:53 +0200 Subject: [PATCH] pkp/pkp-lib#9860 add editorial masthead nav menu item at upgrade --- .../I9860_EditorialMastheadNavMenuItem.php | 91 +++++++++++++++++++ .../NavigationMenuItemAssignmentDAO.php | 5 +- 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 classes/migration/upgrade/v3_5_0/I9860_EditorialMastheadNavMenuItem.php diff --git a/classes/migration/upgrade/v3_5_0/I9860_EditorialMastheadNavMenuItem.php b/classes/migration/upgrade/v3_5_0/I9860_EditorialMastheadNavMenuItem.php new file mode 100644 index 00000000000..3d8f6c133d1 --- /dev/null +++ b/classes/migration/upgrade/v3_5_0/I9860_EditorialMastheadNavMenuItem.php @@ -0,0 +1,91 @@ +getIds(); + foreach ($contextIds as $contextId) { + // Create and insert new Editorial Masthead navigation menu item + $navigationMenuItem = $navigationMenuItemDao->newDataObject(); + $navigationMenuItem->setTitleLocaleKey('common.editorialMasthead'); + $navigationMenuItem->setContextId($contextId); + $navigationMenuItem->setType(NavigationMenuItem::NMI_TYPE_MASTHEAD); + $editorialMastheadNavMenuItemId = $navigationMenuItemDao->insertObject($navigationMenuItem); + + $mainAboutNavMenuItemId = $mainAboutNavMenuId = null; + // Try to find the About navigation menu item + $aboutNavMenuItems = $navigationMenuItemDao->getByType(NavigationMenuItem::NMI_TYPE_ABOUT, $contextId); + while ($aboutNavMenuItem = $aboutNavMenuItems->next()) { + // Get all assignments for the nav menu item + $aboutNavMenuItemAssignments = $navigationMenuItemAssignmentDao->getByMenuItemId($aboutNavMenuItem->getId()); + while ($aboutNavMenuItemAssignment = $aboutNavMenuItemAssignments->next()) { + // Find the assignment with no parent + if ($aboutNavMenuItemAssignment->getParentId() == 0) { + $mainAboutNavMenuItemId = $aboutNavMenuItemAssignment->getMenuItemId(); + $mainAboutNavMenuId = $aboutNavMenuItemAssignment->getMenuId(); + break 2; + } + } + } + if ($mainAboutNavMenuItemId && $mainAboutNavMenuId) { + // Create new Editorial Masthead nav menu item assignment. The parent is the main About nav menu item with no parent. + $editorialMastheadMenuItemAssignment = $navigationMenuItemAssignmentDao->newDataObject(); + $editorialMastheadMenuItemAssignment->setMenuId($mainAboutNavMenuId); + $editorialMastheadMenuItemAssignment->setMenuItemId($editorialMastheadNavMenuItemId); + $editorialMastheadMenuItemAssignment->setParentId($mainAboutNavMenuItemId); + + // Resequence all main About nav menu children, so that the new Editorial Masthead is on the third place (considering the default About nav menu and its order). + $seq = 0; + $allAboutNavMenuChildren = $navigationMenuItemAssignmentDao->getByMenuIdAndParentId($mainAboutNavMenuId, $mainAboutNavMenuItemId); + while ($aboutNavMenuChild = $allAboutNavMenuChildren->next()) { + if ($seq == 2) { + $editorialMastheadMenuItemAssignment->setSequence($seq); + } else { + $aboutNavMenuChild->setSequence($seq); + $navigationMenuItemAssignmentDao->updateObject($aboutNavMenuChild); + } + $seq++; + } + $navigationMenuItemAssignmentDao->insertObject($editorialMastheadMenuItemAssignment); + } + } + + } + + /** + * Reverse the migration + */ + public function down(): void + { + throw new DowngradeNotSupportedException(); + } +} diff --git a/classes/navigationMenu/NavigationMenuItemAssignmentDAO.php b/classes/navigationMenu/NavigationMenuItemAssignmentDAO.php index 66464b4b17b..6ecba11222a 100644 --- a/classes/navigationMenu/NavigationMenuItemAssignmentDAO.php +++ b/classes/navigationMenu/NavigationMenuItemAssignmentDAO.php @@ -132,7 +132,8 @@ public function getByMenuIdAndParentId($menuId, $parentId) 'SELECT nmh.* FROM navigation_menu_item_assignments as nmh WHERE nmh.navigation_menu_id = ? - AND nmh.parent_id = ?', + AND nmh.parent_id = ? + ORDER BY nmh.seq', [(int) $menuId, (int) $parentId] ); return new DAOResultFactory($result, $this, '_fromRow'); @@ -175,7 +176,7 @@ public function updateObject($navigationMenuItemAssignment) navigation_menu_id = ?, navigation_menu_item_id = ?, parent_id = ?, - seq = ?, + seq = ? WHERE navigation_menu_item_assignment_id = ?', [ (int) $navigationMenuItemAssignment->getMenuId(),