Skip to content

Commit

Permalink
PCHR-3429: Sync Navigation.php BAO in hrui
Browse files Browse the repository at this point in the history
  • Loading branch information
davialexandre committed Jun 29, 2018
1 parent 6084770 commit 8ef0664
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions hrui/CRM/Core/BAO/Navigation.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
Expand All @@ -28,7 +28,7 @@
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {

Expand Down Expand Up @@ -172,7 +172,7 @@ public static function getNavigationList() {
$domainID = CRM_Core_Config::domainID();
$query = "
SELECT id, label, parent_id, weight, is_active, name
FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY parent_id, weight ASC";
FROM civicrm_navigation WHERE domain_id = $domainID";
$result = CRM_Core_DAO::executeQuery($query);

$pidGroups = array();
Expand Down Expand Up @@ -262,6 +262,7 @@ public static function buildNavigationTree() {
'name' => $navigationMenu->name,
'url' => $navigationMenu->url,
'icon' => $navigationMenu->icon,
'weight' => $navigationMenu->weight,
'permission' => $navigationMenu->permission,
'operator' => $navigationMenu->permission_operator,
'separator' => $navigationMenu->has_separator,
Expand Down Expand Up @@ -312,6 +313,13 @@ public static function buildNavigation() {
CRM_Utils_Hook::navigationMenu($navigations);
self::fixNavigationMenu($navigations);

// Hooks have added menu items in an arbitrary order. We need to order by
// weight again. I would put this function directly after
// CRM_Utils_Hook::navigationMenu but for some reason, fixNavigationMenu is
// moving items added by hooks on the end of the menu. Hence I do it
// afterwards
self::orderByWeight($navigations);

//skip children menu item if user don't have access to parent menu item
$skipMenuItems = array();
foreach ($navigations as $key => $value) {
Expand All @@ -337,6 +345,32 @@ public static function buildNavigation() {
return $navigationString;
}

/**
* buildNavigationTree retreives items in order. We call this function to
* ensure that any items added by the hook are also in the correct order.
*/
private static function orderByWeight(&$navigations) {
// sort each item in navigations by weight
usort($navigations, function($a, $b) {

// If no weight have been defined for an item put it at the end of the list
if (!isset($a['attributes']['weight'])) {
$a['attributes']['weight'] = 1000;
}
if (!isset($b['attributes']['weight'])) {
$b['attributes']['weight'] = 1000;
}
return $a['attributes']['weight'] - $b['attributes']['weight'];
});

// If any of the $navigations have children, recurse
foreach ($navigations as $navigation) {
if (isset($navigation['child'])) {
self::orderByWeight($navigation['child']);
}
}
}

/**
* Recursively check child menus.
*
Expand Down Expand Up @@ -528,7 +562,7 @@ public static function getMenuName(&$value, &$skipMenuItems) {
}

if (!empty($value['attributes']['icon'])) {
$menuIcon = sprintf('<span class="%s"></span>&nbsp;', $value['attributes']['icon']);
$menuIcon = sprintf('<i class="%s"></i>', $value['attributes']['icon']);
$name = $menuIcon . $name;
}

Expand Down Expand Up @@ -614,7 +648,8 @@ public static function createNavigation($contactID) {
public static function resetNavigation($contactID = NULL) {
$newKey = CRM_Utils_String::createRandom(self::CACHE_KEY_STRLEN, CRM_Utils_String::ALPHANUMERIC);
if (!$contactID) {
$query = "UPDATE civicrm_setting SET value = '$newKey' WHERE name='navigation' AND contact_id IS NOT NULL";
$ser = serialize($newKey);
$query = "UPDATE civicrm_setting SET value = '$ser' WHERE name='navigation' AND contact_id IS NOT NULL";
CRM_Core_DAO::executeQuery($query);
CRM_Core_BAO_Cache::deleteGroup('navigation');
}
Expand Down

0 comments on commit 8ef0664

Please sign in to comment.