3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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.
53 * @return CRM_Core_DAO_Navigation|NULL
54 * DAO object on success, NULL otherwise
56 public static function setIsActive($id, $is_active) {
57 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active);
61 * Get existing / build navigation for CiviCRM Admin Menu.
66 public static function getMenus() {
69 $menu = new CRM_Core_DAO_Menu();
70 $menu->domain_id
= CRM_Core_Config
::domainID();
73 while ($menu->fetch()) {
75 $menus[$menu->path
] = $menu->title
;
82 * Add/update navigation record.
84 * @param array $params Submitted values
86 * @return CRM_Core_DAO_Navigation
89 public static function add(&$params) {
90 $navigation = new CRM_Core_DAO_Navigation();
91 if (empty($params['id'])) {
92 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
93 $params['has_separator'] = CRM_Utils_Array
::value('has_separator', $params, FALSE);
96 if (!isset($params['id']) ||
97 (CRM_Utils_Array
::value('parent_id', $params) != CRM_Utils_Array
::value('current_parent_id', $params))
99 /* re/calculate the weight, if the Parent ID changed OR create new menu */
101 if ($navName = CRM_Utils_Array
::value('name', $params)) {
102 $params['name'] = $navName;
104 elseif ($navLabel = CRM_Utils_Array
::value('label', $params)) {
105 $params['name'] = $navLabel;
108 $params['weight'] = self
::calculateWeight(CRM_Utils_Array
::value('parent_id', $params));
111 if (array_key_exists('permission', $params) && is_array($params['permission'])) {
112 $params['permission'] = implode(',', $params['permission']);
115 $navigation->copyValues($params);
117 $navigation->domain_id
= CRM_Core_Config
::domainID();
124 * Fetch object based on array of properties.
126 * @param array $params
127 * (reference ) an assoc array of name/value pairs.
128 * @param array $defaults
129 * (reference ) an assoc array to hold the flattened values.
131 * @return CRM_Core_BAO_Navigation|null
132 * object on success, NULL otherwise
134 public static function retrieve(&$params, &$defaults) {
135 $navigation = new CRM_Core_DAO_Navigation();
136 $navigation->copyValues($params);
138 $navigation->domain_id
= CRM_Core_Config
::domainID();
140 if ($navigation->find(TRUE)) {
141 CRM_Core_DAO
::storeValues($navigation, $defaults);
148 * Calculate navigation weight.
150 * @param int $parentID
151 * Parent_id of a menu.
158 public static function calculateWeight($parentID = NULL, $menuID = NULL) {
159 $domainID = CRM_Core_Config
::domainID();
162 // we reset weight for each parent, i.e we start from 1 to n
163 // calculate max weight for top level menus, if parent id is absent
165 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID";
168 // if parent is passed, we need to get max weight for that particular parent
169 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID";
172 $dao = CRM_Core_DAO
::executeQuery($query);
174 return $weight = $weight +
$dao->weight
;
178 * Get formatted menu list.
181 * returns associated array
183 public static function getNavigationList() {
184 $cacheKeyString = "navigationList";
187 $config = CRM_Core_Config
::singleton();
189 // check if we can retrieve from database cache
190 $navigations = CRM_Core_BAO_Cache
::getItem('navigation', $cacheKeyString);
193 $domainID = CRM_Core_Config
::domainID();
195 SELECT id, label, parent_id, weight, is_active, name
196 FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY parent_id, weight ASC";
197 $result = CRM_Core_DAO
::executeQuery($query);
199 $pidGroups = array();
200 while ($result->fetch()) {
201 $pidGroups[$result->parent_id
][$result->label
] = $result->id
;
204 foreach ($pidGroups[''] as $label => $val) {
205 $pidGroups[''][$label] = self
::_getNavigationValue($val, $pidGroups);
208 $navigations = array();
209 self
::_getNavigationLabel($pidGroups[''], $navigations);
211 CRM_Core_BAO_Cache
::setItem($navigations, 'navigation', $cacheKeyString);
217 * Helper function for getNavigationList().
221 * @param array $navigations
223 * @param string $separator
226 public static function _getNavigationLabel($list, &$navigations, $separator = '') {
227 $i18n = CRM_Core_I18n
::singleton();
228 foreach ($list as $label => $val) {
229 if ($label == 'navigation_id') {
232 $translatedLabel = $i18n->crm_translate($label, array('context' => 'menu'));
233 $navigations[is_array($val) ?
$val['navigation_id'] : $val] = "{$separator}{$translatedLabel}";
234 if (is_array($val)) {
235 self
::_getNavigationLabel($val, $navigations, $separator . ' ');
241 * Helper function for getNavigationList().
245 * @param array $pidGroups
250 public static function _getNavigationValue($val, &$pidGroups) {
251 if (array_key_exists($val, $pidGroups)) {
252 $list = array('navigation_id' => $val);
253 foreach ($pidGroups[$val] as $label => $id) {
254 $list[$label] = self
::_getNavigationValue($id, $pidGroups);
256 unset($pidGroups[$val]);
265 * Build navigation tree.
268 * nested array of menus
270 public static function buildNavigationTree() {
271 $domainID = CRM_Core_Config
::domainID();
272 $navigationTree = array();
274 // get the list of menus
276 SELECT id, label, url, permission, permission_operator, has_separator, parent_id, is_active, name
277 FROM civicrm_navigation
278 WHERE domain_id = $domainID
279 ORDER BY parent_id, weight";
281 $navigation = CRM_Core_DAO
::executeQuery($query);
282 while ($navigation->fetch()) {
283 $navigationTree[$navigation->id
] = array(
284 'attributes' => array(
285 'label' => $navigation->label
,
286 'name' => $navigation->name
,
287 'url' => $navigation->url
,
288 'permission' => $navigation->permission
,
289 'operator' => $navigation->permission_operator
,
290 'separator' => $navigation->has_separator
,
291 'parentID' => $navigation->parent_id
,
292 'navID' => $navigation->id
,
293 'active' => $navigation->is_active
,
298 return self
::buildTree($navigationTree);
302 * Convert flat array to nested.
304 * @param array $elements
305 * @param int|null $parentId
309 private static function buildTree($elements, $parentId = NULL) {
312 foreach ($elements as $id => $element) {
313 if ($element['attributes']['parentID'] == $parentId) {
314 $children = self
::buildTree($elements, $id);
316 $element['child'] = $children;
318 $branch[$id] = $element;
330 public static function buildNavigation() {
331 $navigations = self
::buildNavigationTree();
332 $navigationString = '';
334 // run the Navigation through a hook so users can modify it
335 CRM_Utils_Hook
::navigationMenu($navigations);
336 self
::fixNavigationMenu($navigations);
338 //skip children menu item if user don't have access to parent menu item
339 $skipMenuItems = array();
340 foreach ($navigations as $key => $value) {
341 // Home is a special case
342 if ($value['attributes']['name'] != 'Home') {
343 $name = self
::getMenuName($value, $skipMenuItems);
346 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) {
347 $navigationString .= '<li class="menu-separator"></li>';
349 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
350 $navigationString .= '<li class="menumain crm-' . str_replace($removeCharacters, '_', $value['attributes']['label']) . '">' . $name;
353 self
::recurseNavigation($value, $navigationString, $skipMenuItems);
356 // clean up - Need to remove empty <ul>'s, this happens when user don't have
357 // permission to access parent
358 $navigationString = str_replace('<ul></ul></li>', '', $navigationString);
360 return $navigationString;
364 * Recursively check child menus.
366 * @param array $value
367 * @param string $navigationString
368 * @param array $skipMenuItems
372 public static function recurseNavigation(&$value, &$navigationString, $skipMenuItems) {
373 if (!empty($value['child'])) {
374 $navigationString .= '<ul>';
377 $navigationString .= '</li>';
378 //locate separator after
379 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
380 $navigationString .= '<li class="menu-separator"></li>';
384 if (!empty($value['child'])) {
385 foreach ($value['child'] as $val) {
386 $name = self
::getMenuName($val, $skipMenuItems);
388 //locate separator before
389 if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) {
390 $navigationString .= '<li class="menu-separator"></li>';
392 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
393 $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name;
394 self
::recurseNavigation($val, $navigationString, $skipMenuItems);
398 if (!empty($value['child'])) {
399 $navigationString .= '</ul></li>';
400 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
401 $navigationString .= '<li class="menu-separator"></li>';
404 return $navigationString;
408 * Given a navigation menu, generate navIDs for any items which are
411 * @param array $nodes
412 * Each key is a numeral; each value is a node in
413 * the menu tree (with keys "child" and "attributes").
415 public static function fixNavigationMenu(&$nodes) {
417 array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) {
418 if ($key === 'navID') {
419 $maxNavID = max($maxNavID, $item);
422 self
::_fixNavigationMenu($nodes, $maxNavID, NULL);
426 * @param array $nodes
427 * Each key is a numeral; each value is a node in
428 * the menu tree (with keys "child" and "attributes").
429 * @param int $maxNavID
430 * @param int $parentID
432 private static function _fixNavigationMenu(&$nodes, &$maxNavID, $parentID) {
433 $origKeys = array_keys($nodes);
434 foreach ($origKeys as $origKey) {
435 if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) {
436 $nodes[$origKey]['attributes']['parentID'] = $parentID;
438 // If no navID, then assign navID and fix key.
439 if (!isset($nodes[$origKey]['attributes']['navID'])) {
440 $newKey = ++
$maxNavID;
441 $nodes[$origKey]['attributes']['navID'] = $newKey;
442 if ($origKey != $newKey) {
443 // If the keys are different, reset the array index to match.
444 $nodes[$newKey] = $nodes[$origKey];
445 unset($nodes[$origKey]);
449 if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) {
450 self
::_fixNavigationMenu($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']);
459 * @param array $skipMenuItems
461 * @return bool|string
463 public static function getMenuName(&$value, &$skipMenuItems) {
464 // we need to localise the menu labels (CRM-5456) and don’t
465 // want to use ts() as it would throw the ts-extractor off
466 $i18n = CRM_Core_I18n
::singleton();
468 $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu'));
469 $url = CRM_Utils_Array
::value('url', $value['attributes']);
470 $permission = CRM_Utils_Array
::value('permission', $value['attributes']);
471 $operator = CRM_Utils_Array
::value('operator', $value['attributes']);
472 $parentID = CRM_Utils_Array
::value('parentID', $value['attributes']);
473 $navID = CRM_Utils_Array
::value('navID', $value['attributes']);
474 $active = CRM_Utils_Array
::value('active', $value['attributes']);
475 $menuName = CRM_Utils_Array
::value('name', $value['attributes']);
476 $target = CRM_Utils_Array
::value('target', $value['attributes']);
478 if (in_array($parentID, $skipMenuItems) ||
!$active) {
479 $skipMenuItems[] = $navID;
483 $config = CRM_Core_Config
::singleton();
486 if (isset($url) && $url) {
487 if (substr($url, 0, 4) !== 'http') {
488 //CRM-7656 --make sure to separate out url path from url params,
489 //as we'r going to validate url path across cross-site scripting.
490 $parsedUrl = parse_url($url);
491 if (empty($parsedUrl['query'])) {
492 $parsedUrl['query'] = NULL;
494 if (empty($parsedUrl['fragment'])) {
495 $parsedUrl['fragment'] = NULL;
497 $url = CRM_Utils_System
::url($parsedUrl['path'], $parsedUrl['query'], FALSE, $parsedUrl['fragment'], TRUE);
499 elseif (strpos($url, '&') === FALSE) {
500 $url = htmlspecialchars($url);
505 static $allComponents;
506 if (!$allComponents) {
507 $allComponents = CRM_Core_Component
::getNames();
510 if (isset($permission) && $permission) {
511 $permissions = explode(',', $permission);
513 $hasPermission = FALSE;
514 foreach ($permissions as $key) {
518 //get the component name from permission.
519 $componentName = CRM_Core_Permission
::getComponentName($key);
521 if ($componentName) {
522 if (!in_array($componentName, $config->enableComponents
) ||
523 !CRM_Core_Permission
::check($key)
526 if ($operator == 'AND') {
527 $skipMenuItems[] = $navID;
532 $hasPermission = TRUE;
535 elseif (!CRM_Core_Permission
::check($key)) {
537 if ($operator == 'AND') {
538 $skipMenuItems[] = $navID;
543 $hasPermission = TRUE;
547 if (!$showItem && !$hasPermission) {
548 $skipMenuItems[] = $navID;
554 $url = CRM_Utils_System
::evalUrl($url);
556 $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>";
559 $name = "<a href=\"{$url}\">{$name}</a>";
567 * Create navigation for CiviCRM Admin Menu.
569 * @param int $contactID
573 * returns navigation html
575 public static function createNavigation($contactID) {
576 $config = CRM_Core_Config
::singleton();
578 $navigation = self
::buildNavigation();
582 //add additional navigation items
583 $logoutURL = CRM_Utils_System
::url('civicrm/logout', 'reset=1');
585 // get home menu from db
586 $homeParams = array('name' => 'Home');
588 $homeIcon = '<span class="crm-logo-sm" ></span>';
589 self
::retrieve($homeParams, $homeNav);
591 list($path, $q) = explode('?', $homeNav['url']);
592 $homeURL = CRM_Utils_System
::url($path, $q);
593 $homeLabel = $homeNav['label'];
594 // CRM-6804 (we need to special-case this as we don’t ts()-tag variables)
595 if ($homeLabel == 'Home') {
596 $homeLabel = ts('CiviCRM Home');
600 $homeURL = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
601 $homeLabel = ts('CiviCRM Home');
603 // Link to hide the menubar
604 $hideLabel = ts('Hide Menu');
607 <li class='menumain crm-link-home'>$homeIcon
608 <ul id='civicrm-home'>
609 <li><a href='$homeURL'>$homeLabel</a></li>
610 <li><a href='#' class='crm-hidemenu'>$hideLabel</a></li>
611 <li><a href='$logoutURL' class='crm-logout-link'>" . ts('Log out') . "</a></li>
613 // <li> tag doesn't need to be closed
615 return $prepandString . $navigation;
619 * Reset navigation for all contacts or a specified contact.
621 * @param int $contactID
622 * Reset only entries belonging to that contact ID.
626 public static function resetNavigation($contactID = NULL) {
627 $newKey = CRM_Utils_String
::createRandom(self
::CACHE_KEY_STRLEN
, CRM_Utils_String
::ALPHANUMERIC
);
629 $query = "UPDATE civicrm_setting SET value = '$newKey' WHERE name='navigation' AND contact_id IS NOT NULL";
630 CRM_Core_DAO
::executeQuery($query);
631 CRM_Core_BAO_Cache
::deleteGroup('navigation');
634 // before inserting check if contact id exists in db
635 // this is to handle weird case when contact id is in session but not in db
636 $contact = new CRM_Contact_DAO_Contact();
637 $contact->id
= $contactID;
638 if ($contact->find(TRUE)) {
639 CRM_Core_BAO_Setting
::setItem(
641 CRM_Core_BAO_Setting
::PERSONAL_PREFERENCES_NAME
,
654 * Process navigation.
656 * @param array $params
657 * Associated array, $_GET.
659 public static function processNavigation(&$params) {
660 $nodeID = (int) str_replace("node_", "", $params['id']);
661 $referenceID = (int) str_replace("node_", "", $params['ref_id']);
662 $position = $params['ps'];
663 $type = $params['type'];
664 $label = CRM_Utils_Array
::value('data', $params);
668 self
::processMove($nodeID, $referenceID, $position);
672 self
::processRename($nodeID, $label);
676 self
::processDelete($nodeID);
680 //reset navigation menus
681 self
::resetNavigation();
682 CRM_Utils_System
::civiExit();
686 * Process move action.
689 * Node that is being moved.
690 * @param $referenceID
691 * Parent id where node is moved. 0 mean no parent.
693 * New position of the nod, it starts with 0 - n.
695 public static function processMove($nodeID, $referenceID, $position) {
696 // based on the new position we need to get the weight of the node after moved node
697 // 1. update the weight of $position + 1 nodes to weight + 1
698 // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved
700 // check if there is parent id, which means node is moved inside existing parent container, so use parent id
701 // to find the correct position else use NULL to get the weights of parent ( $position - 1 )
702 // accordingly set the new parent_id
704 $newParentID = $referenceID;
705 $parentClause = "parent_id = {$referenceID} ";
708 $newParentID = 'NULL';
709 $parentClause = 'parent_id IS NULL';
712 $incrementOtherNodes = TRUE;
713 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
714 $params = array(1 => array($position, 'Positive'));
715 $newWeight = CRM_Core_DAO
::singleValueQuery($sql, $params);
717 // this means node is moved to last position, so you need to get the weight of last element + 1
719 // If this is not the first item being added to a parent
721 $lastPosition = $position - 1;
722 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
723 $params = array(1 => array($lastPosition, 'Positive'));
724 $newWeight = CRM_Core_DAO
::singleValueQuery($sql, $params);
726 // since last node increment + 1
727 $newWeight = $newWeight +
1;
733 // since this is a last node we don't need to increment other nodes
734 $incrementOtherNodes = FALSE;
737 $transaction = new CRM_Core_Transaction();
739 // now update the existing nodes to weight + 1, if required.
740 if ($incrementOtherNodes) {
741 $query = "UPDATE civicrm_navigation SET weight = weight + 1
742 WHERE {$parentClause} AND weight >= {$newWeight}";
744 CRM_Core_DAO
::executeQuery($query);
747 // finally set the weight of current node
748 $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}";
749 CRM_Core_DAO
::executeQuery($query);
751 $transaction->commit();
755 * Function to process rename action for tree.
760 public static function processRename($nodeID, $label) {
761 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label);
765 * Process delete action for tree.
769 public static function processDelete($nodeID) {
770 $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}";
771 CRM_Core_DAO
::executeQuery($query);
777 * @param array $params
778 * @param array $newParams
779 * New value of params.
781 public static function processUpdate($params, $newParams) {
782 $dao = new CRM_Core_DAO_Navigation();
783 $dao->copyValues($params);
784 if ($dao->find(TRUE)) {
785 $dao->copyValues($newParams);
791 * Rebuild reports menu.
793 * All Contact reports will become sub-items of 'Contact Reports' and so on.
795 * @param int $domain_id
797 public static function rebuildReportsNavigation($domain_id) {
798 $component_to_nav_name = array(
799 'CiviContact' => 'Contact Reports',
800 'CiviContribute' => 'Contribution Reports',
801 'CiviMember' => 'Membership Reports',
802 'CiviEvent' => 'Event Reports',
803 'CiviPledge' => 'Pledge Reports',
804 'CiviGrant' => 'Grant Reports',
805 'CiviMail' => 'Mailing Reports',
806 'CiviCampaign' => 'Campaign Reports',
809 // Create or update the top level Reports link.
810 $reports_nav = self
::createOrUpdateTopLevelReportsNavItem($domain_id);
812 // Get all active report instances grouped by component.
813 $components = self
::getAllActiveReportsByComponent($domain_id);
814 foreach ($components as $component_id => $component) {
815 // Create or update the per component reports links.
816 $component_nav_name = $component['name'];
817 if (isset($component_to_nav_name[$component_nav_name])) {
818 $component_nav_name = $component_to_nav_name[$component_nav_name];
820 $permission = "access {$component['name']}";
821 if ($component['name'] === 'CiviContact') {
822 $permission = "administer CiviCRM";
824 elseif ($component['name'] === 'CiviCampaign') {
825 $permission = "access CiviReport";
827 $component_nav = self
::createOrUpdateReportNavItem($component_nav_name, 'civicrm/report/list',
828 "compid={$component_id}&reset=1", $reports_nav->id
, $permission, $domain_id, TRUE);
829 foreach ($component['reports'] as $report_id => $report) {
830 // Create or update the report instance links.
831 $report_nav = self
::createOrUpdateReportNavItem($report['title'], $report['url'], 'reset=1', $component_nav->id
, $report['permission'], $domain_id, FALSE, TRUE);
832 // Update the report instance to include the navigation id.
833 $query = "UPDATE civicrm_report_instance SET navigation_id = %1 WHERE id = %2";
835 1 => array($report_nav->id
, 'Integer'),
836 2 => array($report_id, 'Integer'),
838 CRM_Core_DAO
::executeQuery($query, $params);
842 // Create or update the All Reports link.
843 self
::createOrUpdateReportNavItem('All Reports', 'civicrm/report/list', 'reset=1', $reports_nav->id
, 'access CiviReport', $domain_id, TRUE);
844 // Create or update the My Reports link.
845 self
::createOrUpdateReportNavItem('My Reports', 'civicrm/report/list', 'myreports=1&reset=1', $reports_nav->id
, 'access CiviReport', $domain_id, TRUE);
850 * Create the top level 'Reports' item in the navigation tree.
852 * @param int $domain_id
854 * @return bool|\CRM_Core_DAO
856 static public function createOrUpdateTopLevelReportsNavItem($domain_id) {
859 $dao = new CRM_Core_BAO_Navigation();
860 $dao->name
= 'Reports';
861 $dao->domain_id
= $domain_id;
862 // The first selectAdd clears it - so that we only retrieve the one field.
864 $dao->selectAdd('id');
865 if ($dao->find(TRUE)) {
869 $nav = self
::createReportNavItem('Reports', NULL, NULL, NULL, 'access CiviReport', $id, $domain_id);
874 * Retrieve a navigation item using it's url.
876 * Note that we use LIKE to permit a wildcard as the calling code likely doesn't
877 * care about output params appended.
880 * @param array $url_params
882 * @param int|null $parent_id
883 * Optionally restrict to one parent.
885 * @return bool|\CRM_Core_BAO_Navigation
887 public static function getNavItemByUrl($url, $url_params, $parent_id = NULL) {
888 $nav = new CRM_Core_BAO_Navigation();
889 $nav->parent_id
= $parent_id;
890 $nav->whereAdd("url LIKE '{$url}?{$url_params}'");
892 if ($nav->find(TRUE)) {
899 * Get all active reports, organised by component.
901 * @param int $domain_id
905 public static function getAllActiveReportsByComponent($domain_id) {
908 civicrm_report_instance.id, civicrm_report_instance.title, civicrm_report_instance.permission, civicrm_component.name, civicrm_component.id AS component_id
912 civicrm_option_value ON civicrm_option_value.option_group_id = civicrm_option_group.id AND civicrm_option_group.name = 'report_template'
914 civicrm_report_instance ON civicrm_option_value.value = civicrm_report_instance.report_id
916 civicrm_component ON civicrm_option_value.component_id = civicrm_component.id
918 civicrm_option_value.is_active = 1
920 civicrm_report_instance.domain_id = %1
921 ORDER BY civicrm_option_value.weight";
923 $dao = CRM_Core_DAO
::executeQuery($sql, array(
924 1 => array($domain_id, 'Integer'),
927 while ($dao->fetch()) {
928 $component_name = is_null($dao->name
) ?
'CiviContact' : $dao->name
;
929 $component_id = is_null($dao->component_id
) ?
99 : $dao->component_id
;
930 $rows[$component_id]['name'] = $component_name;
931 $rows[$component_id]['reports'][$dao->id
] = array(
932 'title' => $dao->title
,
933 'url' => "civicrm/report/instance/{$dao->id}",
934 'permission' => $dao->permission
,
941 * Create or update a navigation item for a report instance.
943 * The function will check whether create or update is required.
945 * @param string $name
947 * @param string $url_params
948 * @param int $parent_id
949 * @param string $permission
950 * @param int $domain_id
952 * @param bool $onlyMatchParentID
953 * If True then do not match with a url that has a different parent
954 * (This is because for top level items there is a risk of 'stealing' rows that normally
955 * live under 'Contact' and intentionally duplicate the report examples.)
957 * @return \CRM_Core_DAO_Navigation
959 protected static function createOrUpdateReportNavItem($name, $url, $url_params, $parent_id, $permission,
960 $domain_id, $onlyMatchParentID = FALSE, $useWildcard = TRUE) {
962 $existing_url_params = $useWildcard ?
$url_params . '%' : $url_params;
963 $existing_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, $existing_url_params, ($onlyMatchParentID ?
$parent_id : NULL));
965 $id = $existing_nav->id
;
968 $nav = self
::createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id);
973 * Create a navigation item for a report instance.
975 * @param string $name
977 * @param string $url_params
978 * @param int $parent_id
979 * @param string $permission
981 * @param int $domain_id
982 * ID of domain to create item in.
984 * @return \CRM_Core_DAO_Navigation
986 public static function createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id) {
988 $url = "{$url}?{$url_params}";
992 'label' => ts($name),
994 'parent_id' => $parent_id,
996 'permission' => array(
999 'domain_id' => $domain_id,
1002 $params['id'] = $id;
1004 return CRM_Core_BAO_Navigation
::add($params);
1012 * @return object|string
1014 public static function getCacheKey($cid) {
1015 $key = Civi
::service('settings_manager')
1016 ->getBagByContact(NULL, $cid)
1017 ->get('navigation');
1018 if (strlen($key) !== self
::CACHE_KEY_STRLEN
) {
1019 $key = self
::resetNavigation($cid);