3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Core_BAO_Navigation
extends CRM_Core_DAO_Navigation
{
35 // Number of characters in the menu js cache key
36 const CACHE_KEY_STRLEN
= 8;
41 public function __construct() {
42 parent
::__construct();
46 * Update the is_active flag in the db.
49 * Id of the database record.
50 * @param bool $is_active
51 * Value we want to set the is_active field.
54 * true if we found and updated the object, else false
56 public static function setIsActive($id, $is_active) {
57 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active);
61 * Add/update navigation record.
63 * @param array $params Submitted values
65 * @return CRM_Core_DAO_Navigation
68 public static function add(&$params) {
69 $navigation = new CRM_Core_DAO_Navigation();
70 if (empty($params['id'])) {
71 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
72 $params['has_separator'] = CRM_Utils_Array
::value('has_separator', $params, FALSE);
73 $params['domain_id'] = CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
76 if (!isset($params['id']) ||
77 (CRM_Utils_Array
::value('parent_id', $params) != CRM_Utils_Array
::value('current_parent_id', $params))
79 /* re/calculate the weight, if the Parent ID changed OR create new menu */
81 if ($navName = CRM_Utils_Array
::value('name', $params)) {
82 $params['name'] = $navName;
84 elseif ($navLabel = CRM_Utils_Array
::value('label', $params)) {
85 $params['name'] = $navLabel;
88 $params['weight'] = self
::calculateWeight(CRM_Utils_Array
::value('parent_id', $params));
91 if (array_key_exists('permission', $params) && is_array($params['permission'])) {
92 $params['permission'] = implode(',', $params['permission']);
95 $navigation->copyValues($params);
102 * Fetch object based on array of properties.
104 * @param array $params
105 * (reference ) an assoc array of name/value pairs.
106 * @param array $defaults
107 * (reference ) an assoc array to hold the flattened values.
109 * @return CRM_Core_BAO_Navigation|null
110 * object on success, NULL otherwise
112 public static function retrieve(&$params, &$defaults) {
113 $navigation = new CRM_Core_DAO_Navigation();
114 $navigation->copyValues($params);
116 $navigation->domain_id
= CRM_Core_Config
::domainID();
118 if ($navigation->find(TRUE)) {
119 CRM_Core_DAO
::storeValues($navigation, $defaults);
126 * Calculate navigation weight.
128 * @param int $parentID
129 * Parent_id of a menu.
136 public static function calculateWeight($parentID = NULL, $menuID = NULL) {
137 $domainID = CRM_Core_Config
::domainID();
140 // we reset weight for each parent, i.e we start from 1 to n
141 // calculate max weight for top level menus, if parent id is absent
143 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID";
146 // if parent is passed, we need to get max weight for that particular parent
147 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID";
150 $dao = CRM_Core_DAO
::executeQuery($query);
152 return $weight = $weight +
$dao->weight
;
156 * Get formatted menu list.
159 * returns associated array
161 public static function getNavigationList() {
162 $cacheKeyString = "navigationList_" . CRM_Core_Config
::domainID();
165 $config = CRM_Core_Config
::singleton();
167 // check if we can retrieve from database cache
168 $navigations = Civi
::cache('navigation')->get($cacheKeyString);
171 $domainID = CRM_Core_Config
::domainID();
173 SELECT id, label, parent_id, weight, is_active, name
174 FROM civicrm_navigation WHERE domain_id = $domainID";
175 $result = CRM_Core_DAO
::executeQuery($query);
178 while ($result->fetch()) {
179 $pidGroups[$result->parent_id
][$result->label
] = $result->id
;
182 foreach ($pidGroups[''] as $label => $val) {
183 $pidGroups[''][$label] = self
::_getNavigationValue($val, $pidGroups);
187 self
::_getNavigationLabel($pidGroups[''], $navigations);
189 Civi
::cache('navigation')->set($cacheKeyString, $navigations);
195 * Helper function for getNavigationList().
199 * @param array $navigations
201 * @param string $separator
204 public static function _getNavigationLabel($list, &$navigations, $separator = '') {
205 $i18n = CRM_Core_I18n
::singleton();
206 foreach ($list as $label => $val) {
207 if ($label == 'navigation_id') {
210 $translatedLabel = $i18n->crm_translate($label, ['context' => 'menu']);
211 $navigations[is_array($val) ?
$val['navigation_id'] : $val] = "{$separator}{$translatedLabel}";
212 if (is_array($val)) {
213 self
::_getNavigationLabel($val, $navigations, $separator . ' ');
219 * Helper function for getNavigationList().
223 * @param array $pidGroups
228 public static function _getNavigationValue($val, &$pidGroups) {
229 if (array_key_exists($val, $pidGroups)) {
230 $list = ['navigation_id' => $val];
231 foreach ($pidGroups[$val] as $label => $id) {
232 $list[$label] = self
::_getNavigationValue($id, $pidGroups);
234 unset($pidGroups[$val]);
243 * Build navigation tree.
246 * nested array of menus
248 public static function buildNavigationTree() {
249 $domainID = CRM_Core_Config
::domainID();
250 $navigationTree = [];
252 $navigationMenu = new self();
253 $navigationMenu->domain_id
= $domainID;
254 $navigationMenu->orderBy('parent_id, weight');
255 $navigationMenu->find();
257 while ($navigationMenu->fetch()) {
258 $navigationTree[$navigationMenu->id
] = [
260 'label' => $navigationMenu->label
,
261 'name' => $navigationMenu->name
,
262 'url' => $navigationMenu->url
,
263 'icon' => $navigationMenu->icon
,
264 'weight' => $navigationMenu->weight
,
265 'permission' => $navigationMenu->permission
,
266 'operator' => $navigationMenu->permission_operator
,
267 'separator' => $navigationMenu->has_separator
,
268 'parentID' => $navigationMenu->parent_id
,
269 'navID' => $navigationMenu->id
,
270 'active' => $navigationMenu->is_active
,
275 return self
::buildTree($navigationTree);
279 * Convert flat array to nested.
281 * @param array $elements
282 * @param int|null $parentId
286 private static function buildTree($elements, $parentId = NULL) {
289 foreach ($elements as $id => $element) {
290 if ($element['attributes']['parentID'] == $parentId) {
291 $children = self
::buildTree($elements, $id);
293 $element['child'] = $children;
295 $branch[$id] = $element;
303 * buildNavigationTree retreives items in order. We call this function to
304 * ensure that any items added by the hook are also in the correct order.
306 public static function orderByWeight(&$navigations) {
307 // sort each item in navigations by weight
308 usort($navigations, function($a, $b) {
310 // If no weight have been defined for an item put it at the end of the list
311 if (!isset($a['attributes']['weight'])) {
312 $a['attributes']['weight'] = 1000;
314 if (!isset($b['attributes']['weight'])) {
315 $b['attributes']['weight'] = 1000;
317 return $a['attributes']['weight'] - $b['attributes']['weight'];
320 // If any of the $navigations have children, recurse
321 foreach ($navigations as $navigation) {
322 if (isset($navigation['child'])) {
323 self
::orderByWeight($navigation['child']);
329 * Recursively check child menus.
331 * @param array $value
332 * @param string $navigationString
333 * @param array $skipMenuItems
337 public static function recurseNavigation(&$value, &$navigationString, $skipMenuItems) {
338 if (!empty($value['child'])) {
339 $navigationString .= '<ul>';
342 $navigationString .= '</li>';
343 //locate separator after
344 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
345 $navigationString .= '<li class="menu-separator"></li>';
349 if (!empty($value['child'])) {
350 foreach ($value['child'] as $val) {
351 $name = self
::getMenuName($val, $skipMenuItems);
353 //locate separator before
354 if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) {
355 $navigationString .= '<li class="menu-separator"></li>';
357 $removeCharacters = ['/', '!', '&', '*', ' ', '(', ')', '.'];
358 $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name;
359 self
::recurseNavigation($val, $navigationString, $skipMenuItems);
363 if (!empty($value['child'])) {
364 $navigationString .= '</ul></li>';
365 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
366 $navigationString .= '<li class="menu-separator"></li>';
369 return $navigationString;
373 * Given a navigation menu, generate navIDs for any items which are
376 * @param array $nodes
377 * Each key is a numeral; each value is a node in
378 * the menu tree (with keys "child" and "attributes").
380 public static function fixNavigationMenu(&$nodes) {
382 array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) {
383 if ($key === 'navID') {
384 $maxNavID = max($maxNavID, $item);
387 self
::_fixNavigationMenu($nodes, $maxNavID, NULL);
391 * @param array $nodes
392 * Each key is a numeral; each value is a node in
393 * the menu tree (with keys "child" and "attributes").
394 * @param int $maxNavID
395 * @param int $parentID
397 private static function _fixNavigationMenu(&$nodes, &$maxNavID, $parentID) {
398 $origKeys = array_keys($nodes);
399 foreach ($origKeys as $origKey) {
400 if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) {
401 $nodes[$origKey]['attributes']['parentID'] = $parentID;
403 // If no navID, then assign navID and fix key.
404 if (!isset($nodes[$origKey]['attributes']['navID'])) {
405 $newKey = ++
$maxNavID;
406 $nodes[$origKey]['attributes']['navID'] = $newKey;
407 if ($origKey != $newKey) {
408 // If the keys are different, reset the array index to match.
409 $nodes[$newKey] = $nodes[$origKey];
410 unset($nodes[$origKey]);
414 if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) {
415 self
::_fixNavigationMenu($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']);
421 * Check permissions and format menu item as html.
424 * @param array $skipMenuItems
426 * @return bool|string
428 public static function getMenuName(&$value, &$skipMenuItems) {
429 // we need to localise the menu labels (CRM-5456) and don’t
430 // want to use ts() as it would throw the ts-extractor off
431 $i18n = CRM_Core_I18n
::singleton();
433 $name = $i18n->crm_translate($value['attributes']['label'], ['context' => 'menu']);
434 $url = CRM_Utils_Array
::value('url', $value['attributes']);
435 $parentID = CRM_Utils_Array
::value('parentID', $value['attributes']);
436 $navID = CRM_Utils_Array
::value('navID', $value['attributes']);
437 $active = CRM_Utils_Array
::value('active', $value['attributes']);
438 $target = CRM_Utils_Array
::value('target', $value['attributes']);
440 if (in_array($parentID, $skipMenuItems) ||
!$active ||
!self
::checkPermission($value['attributes'])) {
441 $skipMenuItems[] = $navID;
447 $url = self
::makeFullyFormedUrl($url);
451 if (!empty($value['attributes']['icon'])) {
452 $menuIcon = sprintf('<i class="%s"></i>', $value['attributes']['icon']);
453 $name = $menuIcon . $name;
457 $url = CRM_Utils_System
::evalUrl($url);
459 $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>";
462 $name = "<a href=\"{$url}\">{$name}</a>";
470 * Check if a menu item should be visible based on permissions and component.
475 public static function checkPermission($item) {
476 if (!empty($item['permission'])) {
477 $permissions = explode(',', $item['permission']);
478 $operator = CRM_Utils_Array
::value('operator', $item);
479 $hasPermission = FALSE;
480 foreach ($permissions as $key) {
484 //get the component name from permission.
485 $componentName = CRM_Core_Permission
::getComponentName($key);
487 if ($componentName) {
488 if (!in_array($componentName, CRM_Core_Config
::singleton()->enableComponents
) ||
489 !CRM_Core_Permission
::check($key)
492 if ($operator == 'AND') {
497 $hasPermission = TRUE;
500 elseif (!CRM_Core_Permission
::check($key)) {
502 if ($operator == 'AND') {
507 $hasPermission = TRUE;
511 if (empty($showItem) && !$hasPermission) {
519 * Turns relative URLs (like civicrm/foo/bar) into fully-formed
520 * ones (i.e. example.com/wp-admin?q=civicrm/dashboard).
522 * If the URL is already fully-formed, nothing will be done.
528 public static function makeFullyFormedUrl($url) {
529 if (self
::isNotFullyFormedUrl($url)) {
530 //CRM-7656 --make sure to separate out url path from url params,
531 //as we'r going to validate url path across cross-site scripting.
532 $path = parse_url($url, PHP_URL_PATH
);
533 $q = parse_url($url, PHP_URL_QUERY
);
534 $fragment = parse_url($url, PHP_URL_FRAGMENT
);
535 return CRM_Utils_System
::url($path, $q, FALSE, $fragment);
538 if (strpos($url, '&') === FALSE) {
539 return htmlspecialchars($url);
546 * Checks if the given URL is not fully-formed
552 private static function isNotFullyFormedUrl($url) {
553 return substr($url, 0, 4) !== 'http' && $url[0] !== '/' && $url[0] !== '#';
557 * Reset navigation for all contacts or a specified contact.
559 * @param int $contactID
560 * Reset only entries belonging to that contact ID.
564 public static function resetNavigation($contactID = NULL) {
565 $newKey = CRM_Utils_String
::createRandom(self
::CACHE_KEY_STRLEN
, CRM_Utils_String
::ALPHANUMERIC
);
567 $ser = serialize($newKey);
568 $query = "UPDATE civicrm_setting SET value = '$ser' WHERE name='navigation' AND contact_id IS NOT NULL";
569 CRM_Core_DAO
::executeQuery($query);
570 Civi
::cache('navigation')->flush();
571 // reset ACL and System caches
572 CRM_Core_BAO_Cache
::resetCaches();
575 // before inserting check if contact id exists in db
576 // this is to handle weird case when contact id is in session but not in db
577 $contact = new CRM_Contact_DAO_Contact();
578 $contact->id
= $contactID;
579 if ($contact->find(TRUE)) {
580 CRM_Core_BAO_Setting
::setItem(
582 CRM_Core_BAO_Setting
::PERSONAL_PREFERENCES_NAME
,
595 * Process navigation.
597 * @param array $params
598 * Associated array, $_GET.
600 public static function processNavigation(&$params) {
601 $nodeID = (int) str_replace("node_", "", $params['id']);
602 $referenceID = (int) str_replace("node_", "", $params['ref_id']);
603 $position = $params['ps'];
604 $type = $params['type'];
605 $label = CRM_Utils_Array
::value('data', $params);
609 self
::processMove($nodeID, $referenceID, $position);
613 self
::processRename($nodeID, $label);
617 self
::processDelete($nodeID);
621 //reset navigation menus
622 self
::resetNavigation();
623 CRM_Utils_System
::civiExit();
627 * Process move action.
630 * Node that is being moved.
631 * @param $referenceID
632 * Parent id where node is moved. 0 mean no parent.
634 * New position of the nod, it starts with 0 - n.
636 public static function processMove($nodeID, $referenceID, $position) {
637 // based on the new position we need to get the weight of the node after moved node
638 // 1. update the weight of $position + 1 nodes to weight + 1
639 // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved
641 // check if there is parent id, which means node is moved inside existing parent container, so use parent id
642 // to find the correct position else use NULL to get the weights of parent ( $position - 1 )
643 // accordingly set the new parent_id
645 $newParentID = $referenceID;
646 $parentClause = "parent_id = {$referenceID} ";
649 $newParentID = 'NULL';
650 $parentClause = 'parent_id IS NULL';
653 $incrementOtherNodes = TRUE;
654 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
655 $params = [1 => [$position, 'Positive']];
656 $newWeight = CRM_Core_DAO
::singleValueQuery($sql, $params);
658 // this means node is moved to last position, so you need to get the weight of last element + 1
660 // If this is not the first item being added to a parent
662 $lastPosition = $position - 1;
663 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
664 $params = [1 => [$lastPosition, 'Positive']];
665 $newWeight = CRM_Core_DAO
::singleValueQuery($sql, $params);
667 // since last node increment + 1
668 $newWeight = $newWeight +
1;
674 // since this is a last node we don't need to increment other nodes
675 $incrementOtherNodes = FALSE;
678 $transaction = new CRM_Core_Transaction();
680 // now update the existing nodes to weight + 1, if required.
681 if ($incrementOtherNodes) {
682 $query = "UPDATE civicrm_navigation SET weight = weight + 1
683 WHERE {$parentClause} AND weight >= {$newWeight}";
685 CRM_Core_DAO
::executeQuery($query);
688 // finally set the weight of current node
689 $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}";
690 CRM_Core_DAO
::executeQuery($query);
692 $transaction->commit();
696 * Function to process rename action for tree.
701 public static function processRename($nodeID, $label) {
702 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label);
706 * Process delete action for tree.
710 public static function processDelete($nodeID) {
711 $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}";
712 CRM_Core_DAO
::executeQuery($query);
718 * @param array $params
719 * @param array $newParams
720 * New value of params.
722 public static function processUpdate($params, $newParams) {
723 $dao = new CRM_Core_DAO_Navigation();
724 $dao->copyValues($params);
725 if ($dao->find(TRUE)) {
726 $dao->copyValues($newParams);
732 * Rebuild reports menu.
734 * All Contact reports will become sub-items of 'Contact Reports' and so on.
736 * @param int $domain_id
738 public static function rebuildReportsNavigation($domain_id) {
739 $component_to_nav_name = [
740 'CiviContact' => 'Contact Reports',
741 'CiviContribute' => 'Contribution Reports',
742 'CiviMember' => 'Membership Reports',
743 'CiviEvent' => 'Event Reports',
744 'CiviPledge' => 'Pledge Reports',
745 'CiviGrant' => 'Grant Reports',
746 'CiviMail' => 'Mailing Reports',
747 'CiviCampaign' => 'Campaign Reports',
750 // Create or update the top level Reports link.
751 $reports_nav = self
::createOrUpdateTopLevelReportsNavItem($domain_id);
753 // Get all active report instances grouped by component.
754 $components = self
::getAllActiveReportsByComponent($domain_id);
755 foreach ($components as $component_id => $component) {
756 // Create or update the per component reports links.
757 $component_nav_name = $component['name'];
758 if (isset($component_to_nav_name[$component_nav_name])) {
759 $component_nav_name = $component_to_nav_name[$component_nav_name];
761 $permission = "access {$component['name']}";
762 if ($component['name'] === 'CiviContact') {
763 $permission = "administer CiviCRM";
765 elseif ($component['name'] === 'CiviCampaign') {
766 $permission = "access CiviReport";
768 $component_nav = self
::createOrUpdateReportNavItem($component_nav_name, 'civicrm/report/list',
769 "compid={$component_id}&reset=1", $reports_nav->id
, $permission, $domain_id, TRUE);
770 foreach ($component['reports'] as $report_id => $report) {
771 // Create or update the report instance links.
772 $report_nav = self
::createOrUpdateReportNavItem($report['title'], $report['url'], 'reset=1', $component_nav->id
, $report['permission'], $domain_id, FALSE, TRUE);
773 // Update the report instance to include the navigation id.
774 $query = "UPDATE civicrm_report_instance SET navigation_id = %1 WHERE id = %2";
776 1 => [$report_nav->id
, 'Integer'],
777 2 => [$report_id, 'Integer'],
779 CRM_Core_DAO
::executeQuery($query, $params);
783 // Create or update the All Reports link.
784 self
::createOrUpdateReportNavItem('All Reports', 'civicrm/report/list', 'reset=1', $reports_nav->id
, 'access CiviReport', $domain_id, TRUE);
785 // Create or update the My Reports link.
786 self
::createOrUpdateReportNavItem('My Reports', 'civicrm/report/list', 'myreports=1&reset=1', $reports_nav->id
, 'access CiviReport', $domain_id, TRUE);
791 * Create the top level 'Reports' item in the navigation tree.
793 * @param int $domain_id
795 * @return bool|\CRM_Core_DAO
797 public static function createOrUpdateTopLevelReportsNavItem($domain_id) {
800 $dao = new CRM_Core_BAO_Navigation();
801 $dao->name
= 'Reports';
802 $dao->domain_id
= $domain_id;
803 // The first selectAdd clears it - so that we only retrieve the one field.
805 $dao->selectAdd('id');
806 if ($dao->find(TRUE)) {
810 $nav = self
::createReportNavItem('Reports', NULL, NULL, NULL, 'access CiviReport', $id, $domain_id);
815 * Retrieve a navigation item using it's url.
817 * Note that we use LIKE to permit a wildcard as the calling code likely doesn't
818 * care about output params appended.
821 * @param array $url_params
823 * @param int|null $parent_id
824 * Optionally restrict to one parent.
826 * @return bool|\CRM_Core_BAO_Navigation
828 public static function getNavItemByUrl($url, $url_params, $parent_id = NULL) {
829 $nav = new CRM_Core_BAO_Navigation();
830 $nav->parent_id
= $parent_id;
831 $nav->whereAdd("url LIKE '{$url}?{$url_params}'");
833 if ($nav->find(TRUE)) {
840 * Get all active reports, organised by component.
842 * @param int $domain_id
846 public static function getAllActiveReportsByComponent($domain_id) {
849 civicrm_report_instance.id, civicrm_report_instance.title, civicrm_report_instance.permission, civicrm_component.name, civicrm_component.id AS component_id
853 civicrm_option_value ON civicrm_option_value.option_group_id = civicrm_option_group.id AND civicrm_option_group.name = 'report_template'
855 civicrm_report_instance ON civicrm_option_value.value = civicrm_report_instance.report_id
857 civicrm_component ON civicrm_option_value.component_id = civicrm_component.id
859 civicrm_option_value.is_active = 1
861 civicrm_report_instance.domain_id = %1
862 ORDER BY civicrm_option_value.weight";
864 $dao = CRM_Core_DAO
::executeQuery($sql, [
865 1 => [$domain_id, 'Integer'],
868 while ($dao->fetch()) {
869 $component_name = is_null($dao->name
) ?
'CiviContact' : $dao->name
;
870 $component_id = is_null($dao->component_id
) ?
99 : $dao->component_id
;
871 $rows[$component_id]['name'] = $component_name;
872 $rows[$component_id]['reports'][$dao->id
] = [
873 'title' => $dao->title
,
874 'url' => "civicrm/report/instance/{$dao->id}",
875 'permission' => $dao->permission
,
882 * Create or update a navigation item for a report instance.
884 * The function will check whether create or update is required.
886 * @param string $name
888 * @param string $url_params
889 * @param int $parent_id
890 * @param string $permission
891 * @param int $domain_id
893 * @param bool $onlyMatchParentID
894 * If True then do not match with a url that has a different parent
895 * (This is because for top level items there is a risk of 'stealing' rows that normally
896 * live under 'Contact' and intentionally duplicate the report examples.)
897 * @param bool $useWildcard
898 * @return \CRM_Core_DAO_Navigation
900 protected static function createOrUpdateReportNavItem($name, $url, $url_params, $parent_id, $permission,
901 $domain_id, $onlyMatchParentID = FALSE, $useWildcard = TRUE) {
903 $existing_url_params = $useWildcard ?
$url_params . '%' : $url_params;
904 $existing_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, $existing_url_params, ($onlyMatchParentID ?
$parent_id : NULL));
906 $id = $existing_nav->id
;
909 $nav = self
::createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id);
914 * Create a navigation item for a report instance.
916 * @param string $name
918 * @param string $url_params
919 * @param int $parent_id
920 * @param string $permission
922 * @param int $domain_id
923 * ID of domain to create item in.
925 * @return \CRM_Core_DAO_Navigation
927 public static function createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id) {
929 $url = "{$url}?{$url_params}";
933 'label' => ts($name),
935 'parent_id' => $parent_id,
940 'domain_id' => $domain_id,
945 return CRM_Core_BAO_Navigation
::add($params);
953 * @return object|string
955 public static function getCacheKey($cid) {
956 $key = Civi
::service('settings_manager')
957 ->getBagByContact(NULL, $cid)
959 if (strlen($key) !== self
::CACHE_KEY_STRLEN
) {
960 $key = self
::resetNavigation($cid);
966 * Unset menu items for disabled components and non-permissioned users
970 public static function filterByPermission(&$menu) {
971 foreach ($menu as $key => $item) {
973 (array_key_exists('active', $item['attributes']) && !$item['attributes']['active']) ||
974 !CRM_Core_BAO_Navigation
::checkPermission($item['attributes'])
979 if (!empty($item['child'])) {
980 self
::filterByPermission($menu[$key]['child']);
988 public static function buildHomeMenu(&$menu) {
989 foreach ($menu as &$item) {
990 if (CRM_Utils_Array
::value('name', $item['attributes']) === 'Home') {
991 unset($item['attributes']['label'], $item['attributes']['url']);
992 $item['attributes']['icon'] = 'crm-logo-sm';
993 $item['attributes']['attr']['accesskey'] = 'm';
997 'label' => 'CiviCRM Home',
998 'name' => 'CiviCRM Home',
999 'url' => 'civicrm/dashboard?reset=1',
1005 'label' => 'Hide Menu',
1006 'name' => 'Hide Menu',
1007 'url' => '#hidemenu',
1013 'label' => 'Log out',
1014 'name' => 'Log out',
1015 'url' => 'civicrm/logout?reset=1',