Skip to content

Commit

Permalink
pkp#9860 add editorial masthead nav menu item at upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bozana committed May 3, 2024
1 parent 4c027a9 commit 0e951de
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/I9860_EditorialMastheadNavMenuItem.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I9860_EditorialMastheadNavMenuItem
*
* @brief Add editorial masthead navigation menu item under about menu item in the primary navigation menu.
*/

namespace PKP\migration\upgrade\v3_5_0;

use APP\core\Services;
use PKP\db\DAORegistry;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;
use PKP\navigationMenu\NavigationMenuItem;
use PKP\navigationMenu\NavigationMenuItemAssignmentDAO;
use PKP\navigationMenu\NavigationMenuItemDAO;

class I9860_EditorialMastheadNavMenuItem extends Migration
{
/**
* Run the migration.
*/
public function up(): void
{
$navigationMenuItemDao = DAORegistry::getDAO('NavigationMenuItemDAO'); /** @var NavigationMenuItemDAO $navigationMenuItemDao */
$navigationMenuItemAssignmentDao = DAORegistry::getDAO('NavigationMenuItemAssignmentDAO'); /** @var NavigationMenuItemAssignmentDAO $navigationMenuItemAssignmentDao */

$contextIds = Services::get('context')->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();
}
}
5 changes: 3 additions & 2 deletions classes/navigationMenu/NavigationMenuItemAssignmentDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 0e951de

Please sign in to comment.