Merge pull request #11724 from lemacarl/CRM-21779
[civicrm-core.git] / CRM / Core / BAO / Navigation.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33 class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {
34
35 // Number of characters in the menu js cache key
36 const CACHE_KEY_STRLEN = 8;
37
38 /**
39 * Class constructor.
40 */
41 public function __construct() {
42 parent::__construct();
43 }
44
45 /**
46 * Update the is_active flag in the db.
47 *
48 * @param int $id
49 * Id of the database record.
50 * @param bool $is_active
51 * Value we want to set the is_active field.
52 *
53 * @return CRM_Core_DAO_Navigation|NULL
54 * DAO object on success, NULL otherwise
55 */
56 public static function setIsActive($id, $is_active) {
57 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active);
58 }
59
60 /**
61 * Add/update navigation record.
62 *
63 * @param array $params Submitted values
64 *
65 * @return CRM_Core_DAO_Navigation
66 * navigation object
67 */
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 }
74
75 if (!isset($params['id']) ||
76 (CRM_Utils_Array::value('parent_id', $params) != CRM_Utils_Array::value('current_parent_id', $params))
77 ) {
78 /* re/calculate the weight, if the Parent ID changed OR create new menu */
79
80 if ($navName = CRM_Utils_Array::value('name', $params)) {
81 $params['name'] = $navName;
82 }
83 elseif ($navLabel = CRM_Utils_Array::value('label', $params)) {
84 $params['name'] = $navLabel;
85 }
86
87 $params['weight'] = self::calculateWeight(CRM_Utils_Array::value('parent_id', $params));
88 }
89
90 if (array_key_exists('permission', $params) && is_array($params['permission'])) {
91 $params['permission'] = implode(',', $params['permission']);
92 }
93
94 $navigation->copyValues($params);
95
96 $navigation->domain_id = CRM_Core_Config::domainID();
97
98 $navigation->save();
99 return $navigation;
100 }
101
102 /**
103 * Fetch object based on array of properties.
104 *
105 * @param array $params
106 * (reference ) an assoc array of name/value pairs.
107 * @param array $defaults
108 * (reference ) an assoc array to hold the flattened values.
109 *
110 * @return CRM_Core_BAO_Navigation|null
111 * object on success, NULL otherwise
112 */
113 public static function retrieve(&$params, &$defaults) {
114 $navigation = new CRM_Core_DAO_Navigation();
115 $navigation->copyValues($params);
116
117 $navigation->domain_id = CRM_Core_Config::domainID();
118
119 if ($navigation->find(TRUE)) {
120 CRM_Core_DAO::storeValues($navigation, $defaults);
121 return $navigation;
122 }
123 return NULL;
124 }
125
126 /**
127 * Calculate navigation weight.
128 *
129 * @param int $parentID
130 * Parent_id of a menu.
131 * @param int $menuID
132 * Menu id.
133 *
134 * @return int
135 * $weight string
136 */
137 public static function calculateWeight($parentID = NULL, $menuID = NULL) {
138 $domainID = CRM_Core_Config::domainID();
139
140 $weight = 1;
141 // we reset weight for each parent, i.e we start from 1 to n
142 // calculate max weight for top level menus, if parent id is absent
143 if (!$parentID) {
144 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID";
145 }
146 else {
147 // if parent is passed, we need to get max weight for that particular parent
148 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID";
149 }
150
151 $dao = CRM_Core_DAO::executeQuery($query);
152 $dao->fetch();
153 return $weight = $weight + $dao->weight;
154 }
155
156 /**
157 * Get formatted menu list.
158 *
159 * @return array
160 * returns associated array
161 */
162 public static function getNavigationList() {
163 $cacheKeyString = "navigationList";
164 $whereClause = '';
165
166 $config = CRM_Core_Config::singleton();
167
168 // check if we can retrieve from database cache
169 $navigations = CRM_Core_BAO_Cache::getItem('navigation', $cacheKeyString);
170
171 if (!$navigations) {
172 $domainID = CRM_Core_Config::domainID();
173 $query = "
174 SELECT id, label, parent_id, weight, is_active, name
175 FROM civicrm_navigation WHERE domain_id = $domainID";
176 $result = CRM_Core_DAO::executeQuery($query);
177
178 $pidGroups = array();
179 while ($result->fetch()) {
180 $pidGroups[$result->parent_id][$result->label] = $result->id;
181 }
182
183 foreach ($pidGroups[''] as $label => $val) {
184 $pidGroups[''][$label] = self::_getNavigationValue($val, $pidGroups);
185 }
186
187 $navigations = array();
188 self::_getNavigationLabel($pidGroups[''], $navigations);
189
190 CRM_Core_BAO_Cache::setItem($navigations, 'navigation', $cacheKeyString);
191 }
192 return $navigations;
193 }
194
195 /**
196 * Helper function for getNavigationList().
197 *
198 * @param array $list
199 * Menu info.
200 * @param array $navigations
201 * Navigation menus.
202 * @param string $separator
203 * Menu separator.
204 */
205 public static function _getNavigationLabel($list, &$navigations, $separator = '') {
206 $i18n = CRM_Core_I18n::singleton();
207 foreach ($list as $label => $val) {
208 if ($label == 'navigation_id') {
209 continue;
210 }
211 $translatedLabel = $i18n->crm_translate($label, array('context' => 'menu'));
212 $navigations[is_array($val) ? $val['navigation_id'] : $val] = "{$separator}{$translatedLabel}";
213 if (is_array($val)) {
214 self::_getNavigationLabel($val, $navigations, $separator . '&nbsp;&nbsp;&nbsp;&nbsp;');
215 }
216 }
217 }
218
219 /**
220 * Helper function for getNavigationList().
221 *
222 * @param string $val
223 * Menu name.
224 * @param array $pidGroups
225 * Parent menus.
226 *
227 * @return array
228 */
229 public static function _getNavigationValue($val, &$pidGroups) {
230 if (array_key_exists($val, $pidGroups)) {
231 $list = array('navigation_id' => $val);
232 foreach ($pidGroups[$val] as $label => $id) {
233 $list[$label] = self::_getNavigationValue($id, $pidGroups);
234 }
235 unset($pidGroups[$val]);
236 return $list;
237 }
238 else {
239 return $val;
240 }
241 }
242
243 /**
244 * Build navigation tree.
245 *
246 * @return array
247 * nested array of menus
248 */
249 public static function buildNavigationTree() {
250 $domainID = CRM_Core_Config::domainID();
251 $navigationTree = array();
252
253 $navigationMenu = new self();
254 $navigationMenu->domain_id = $domainID;
255 $navigationMenu->orderBy('parent_id, weight');
256 $navigationMenu->find();
257
258 while ($navigationMenu->fetch()) {
259 $navigationTree[$navigationMenu->id] = array(
260 'attributes' => array(
261 'label' => $navigationMenu->label,
262 'name' => $navigationMenu->name,
263 'url' => $navigationMenu->url,
264 'icon' => $navigationMenu->icon,
265 'weight' => $navigationMenu->weight,
266 'permission' => $navigationMenu->permission,
267 'operator' => $navigationMenu->permission_operator,
268 'separator' => $navigationMenu->has_separator,
269 'parentID' => $navigationMenu->parent_id,
270 'navID' => $navigationMenu->id,
271 'active' => $navigationMenu->is_active,
272 ),
273 );
274 }
275
276 return self::buildTree($navigationTree);
277 }
278
279 /**
280 * Convert flat array to nested.
281 *
282 * @param array $elements
283 * @param int|null $parentId
284 *
285 * @return array
286 */
287 private static function buildTree($elements, $parentId = NULL) {
288 $branch = array();
289
290 foreach ($elements as $id => $element) {
291 if ($element['attributes']['parentID'] == $parentId) {
292 $children = self::buildTree($elements, $id);
293 if ($children) {
294 $element['child'] = $children;
295 }
296 $branch[$id] = $element;
297 }
298 }
299
300 return $branch;
301 }
302
303 /**
304 * Build menu.
305 *
306 * @return string
307 */
308 public static function buildNavigation() {
309 $navigations = self::buildNavigationTree();
310 $navigationString = '';
311
312 // run the Navigation through a hook so users can modify it
313 CRM_Utils_Hook::navigationMenu($navigations);
314 self::fixNavigationMenu($navigations);
315
316 // Hooks have added menu items in an arbitrary order. We need to order by
317 // weight again. I would put this function directly after
318 // CRM_Utils_Hook::navigationMenu but for some reason, fixNavigationMenu is
319 // moving items added by hooks on the end of the menu. Hence I do it
320 // afterwards
321 self::orderByWeight($navigations);
322
323 //skip children menu item if user don't have access to parent menu item
324 $skipMenuItems = array();
325 foreach ($navigations as $key => $value) {
326 // Home is a special case
327 if ($value['attributes']['name'] != 'Home') {
328 $name = self::getMenuName($value, $skipMenuItems);
329 if ($name) {
330 //separator before
331 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) {
332 $navigationString .= '<li class="menu-separator"></li>';
333 }
334 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
335 $navigationString .= '<li class="menumain crm-' . str_replace($removeCharacters, '_', $value['attributes']['label']) . '">' . $name;
336 }
337 }
338 self::recurseNavigation($value, $navigationString, $skipMenuItems);
339 }
340
341 // clean up - Need to remove empty <ul>'s, this happens when user don't have
342 // permission to access parent
343 $navigationString = str_replace('<ul></ul></li>', '', $navigationString);
344
345 return $navigationString;
346 }
347
348 /**
349 * buildNavigationTree retreives items in order. We call this function to
350 * ensure that any items added by the hook are also in the correct order.
351 */
352 private static function orderByWeight(&$navigations) {
353 // sort each item in navigations by weight
354 usort($navigations, function($a, $b) {
355
356 // If no weight have been defined for an item put it at the end of the list
357 if (!isset($a['attributes']['weight'])) {
358 $a['attributes']['weight'] = 1000;
359 }
360 if (!isset($b['attributes']['weight'])) {
361 $b['attributes']['weight'] = 1000;
362 }
363 return $a['attributes']['weight'] - $b['attributes']['weight'];
364 });
365
366 // If any of the $navigations have children, recurse
367 foreach ($navigations as $navigation) {
368 if (isset($navigation['child'])) {
369 self::orderByWeight($navigation['child']);
370 }
371 }
372 }
373
374 /**
375 * Recursively check child menus.
376 *
377 * @param array $value
378 * @param string $navigationString
379 * @param array $skipMenuItems
380 *
381 * @return string
382 */
383 public static function recurseNavigation(&$value, &$navigationString, $skipMenuItems) {
384 if (!empty($value['child'])) {
385 $navigationString .= '<ul>';
386 }
387 else {
388 $navigationString .= '</li>';
389 //locate separator after
390 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
391 $navigationString .= '<li class="menu-separator"></li>';
392 }
393 }
394
395 if (!empty($value['child'])) {
396 foreach ($value['child'] as $val) {
397 $name = self::getMenuName($val, $skipMenuItems);
398 if ($name) {
399 //locate separator before
400 if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) {
401 $navigationString .= '<li class="menu-separator"></li>';
402 }
403 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
404 $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name;
405 self::recurseNavigation($val, $navigationString, $skipMenuItems);
406 }
407 }
408 }
409 if (!empty($value['child'])) {
410 $navigationString .= '</ul></li>';
411 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
412 $navigationString .= '<li class="menu-separator"></li>';
413 }
414 }
415 return $navigationString;
416 }
417
418 /**
419 * Given a navigation menu, generate navIDs for any items which are
420 * missing them.
421 *
422 * @param array $nodes
423 * Each key is a numeral; each value is a node in
424 * the menu tree (with keys "child" and "attributes").
425 */
426 public static function fixNavigationMenu(&$nodes) {
427 $maxNavID = 1;
428 array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) {
429 if ($key === 'navID') {
430 $maxNavID = max($maxNavID, $item);
431 }
432 });
433 self::_fixNavigationMenu($nodes, $maxNavID, NULL);
434 }
435
436 /**
437 * @param array $nodes
438 * Each key is a numeral; each value is a node in
439 * the menu tree (with keys "child" and "attributes").
440 * @param int $maxNavID
441 * @param int $parentID
442 */
443 private static function _fixNavigationMenu(&$nodes, &$maxNavID, $parentID) {
444 $origKeys = array_keys($nodes);
445 foreach ($origKeys as $origKey) {
446 if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) {
447 $nodes[$origKey]['attributes']['parentID'] = $parentID;
448 }
449 // If no navID, then assign navID and fix key.
450 if (!isset($nodes[$origKey]['attributes']['navID'])) {
451 $newKey = ++$maxNavID;
452 $nodes[$origKey]['attributes']['navID'] = $newKey;
453 if ($origKey != $newKey) {
454 // If the keys are different, reset the array index to match.
455 $nodes[$newKey] = $nodes[$origKey];
456 unset($nodes[$origKey]);
457 $origKey = $newKey;
458 }
459 }
460 if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) {
461 self::_fixNavigationMenu($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']);
462 }
463 }
464 }
465
466 /**
467 * Get Menu name.
468 *
469 * @param $value
470 * @param array $skipMenuItems
471 *
472 * @return bool|string
473 */
474 public static function getMenuName(&$value, &$skipMenuItems) {
475 // we need to localise the menu labels (CRM-5456) and don’t
476 // want to use ts() as it would throw the ts-extractor off
477 $i18n = CRM_Core_I18n::singleton();
478
479 $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu'));
480 $url = CRM_Utils_Array::value('url', $value['attributes']);
481 $permission = CRM_Utils_Array::value('permission', $value['attributes']);
482 $operator = CRM_Utils_Array::value('operator', $value['attributes']);
483 $parentID = CRM_Utils_Array::value('parentID', $value['attributes']);
484 $navID = CRM_Utils_Array::value('navID', $value['attributes']);
485 $active = CRM_Utils_Array::value('active', $value['attributes']);
486 $menuName = CRM_Utils_Array::value('name', $value['attributes']);
487 $target = CRM_Utils_Array::value('target', $value['attributes']);
488
489 if (in_array($parentID, $skipMenuItems) || !$active) {
490 $skipMenuItems[] = $navID;
491 return FALSE;
492 }
493
494 $config = CRM_Core_Config::singleton();
495
496 $makeLink = FALSE;
497 if (isset($url) && $url) {
498 if (substr($url, 0, 4) !== 'http') {
499 //CRM-7656 --make sure to separate out url path from url params,
500 //as we'r going to validate url path across cross-site scripting.
501 $parsedUrl = parse_url($url);
502 if (empty($parsedUrl['query'])) {
503 $parsedUrl['query'] = NULL;
504 }
505 if (empty($parsedUrl['fragment'])) {
506 $parsedUrl['fragment'] = NULL;
507 }
508 $url = CRM_Utils_System::url($parsedUrl['path'], $parsedUrl['query'], FALSE, $parsedUrl['fragment'], TRUE);
509 }
510 elseif (strpos($url, '&amp;') === FALSE) {
511 $url = htmlspecialchars($url);
512 }
513 $makeLink = TRUE;
514 }
515
516 static $allComponents;
517 if (!$allComponents) {
518 $allComponents = CRM_Core_Component::getNames();
519 }
520
521 if (isset($permission) && $permission) {
522 $permissions = explode(',', $permission);
523
524 $hasPermission = FALSE;
525 foreach ($permissions as $key) {
526 $key = trim($key);
527 $showItem = TRUE;
528
529 //get the component name from permission.
530 $componentName = CRM_Core_Permission::getComponentName($key);
531
532 if ($componentName) {
533 if (!in_array($componentName, $config->enableComponents) ||
534 !CRM_Core_Permission::check($key)
535 ) {
536 $showItem = FALSE;
537 if ($operator == 'AND') {
538 $skipMenuItems[] = $navID;
539 return $showItem;
540 }
541 }
542 else {
543 $hasPermission = TRUE;
544 }
545 }
546 elseif (!CRM_Core_Permission::check($key)) {
547 $showItem = FALSE;
548 if ($operator == 'AND') {
549 $skipMenuItems[] = $navID;
550 return $showItem;
551 }
552 }
553 else {
554 $hasPermission = TRUE;
555 }
556 }
557
558 if (!$showItem && !$hasPermission) {
559 $skipMenuItems[] = $navID;
560 return FALSE;
561 }
562 }
563
564 if (!empty($value['attributes']['icon'])) {
565 $menuIcon = sprintf('<i class="%s"></i>', $value['attributes']['icon']);
566 $name = $menuIcon . $name;
567 }
568
569 if ($makeLink) {
570 $url = CRM_Utils_System::evalUrl($url);
571 if ($target) {
572 $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>";
573 }
574 else {
575 $name = "<a href=\"{$url}\">{$name}</a>";
576 }
577 }
578
579 return $name;
580 }
581
582 /**
583 * Create navigation for CiviCRM Admin Menu.
584 *
585 * @param int $contactID
586 * Contact id.
587 *
588 * @return string
589 * returns navigation html
590 */
591 public static function createNavigation($contactID) {
592 $config = CRM_Core_Config::singleton();
593
594 $navigation = self::buildNavigation();
595
596 if ($navigation) {
597
598 //add additional navigation items
599 $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1');
600
601 // get home menu from db
602 $homeParams = array('name' => 'Home');
603 $homeNav = array();
604 $homeIcon = '<span class="crm-logo-sm" ></span>';
605 self::retrieve($homeParams, $homeNav);
606 if ($homeNav) {
607 list($path, $q) = explode('?', $homeNav['url']);
608 $homeURL = CRM_Utils_System::url($path, $q);
609 $homeLabel = $homeNav['label'];
610 // CRM-6804 (we need to special-case this as we don’t ts()-tag variables)
611 if ($homeLabel == 'Home') {
612 $homeLabel = ts('CiviCRM Home');
613 }
614 }
615 else {
616 $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
617 $homeLabel = ts('CiviCRM Home');
618 }
619 // Link to hide the menubar
620 $hideLabel = ts('Hide Menu');
621
622 $prepandString = "
623 <li class='menumain crm-link-home'>$homeIcon
624 <ul id='civicrm-home'>
625 <li><a href='$homeURL'>$homeLabel</a></li>
626 <li><a href='#' class='crm-hidemenu'>$hideLabel</a></li>
627 <li><a href='$logoutURL' class='crm-logout-link'>" . ts('Log out') . "</a></li>
628 </ul>";
629 // <li> tag doesn't need to be closed
630 }
631 return $prepandString . $navigation;
632 }
633
634 /**
635 * Reset navigation for all contacts or a specified contact.
636 *
637 * @param int $contactID
638 * Reset only entries belonging to that contact ID.
639 *
640 * @return string
641 */
642 public static function resetNavigation($contactID = NULL) {
643 $newKey = CRM_Utils_String::createRandom(self::CACHE_KEY_STRLEN, CRM_Utils_String::ALPHANUMERIC);
644 if (!$contactID) {
645 $ser = serialize($newKey);
646 $query = "UPDATE civicrm_setting SET value = '$ser' WHERE name='navigation' AND contact_id IS NOT NULL";
647 CRM_Core_DAO::executeQuery($query);
648 CRM_Core_BAO_Cache::deleteGroup('navigation');
649 }
650 else {
651 // before inserting check if contact id exists in db
652 // this is to handle weird case when contact id is in session but not in db
653 $contact = new CRM_Contact_DAO_Contact();
654 $contact->id = $contactID;
655 if ($contact->find(TRUE)) {
656 CRM_Core_BAO_Setting::setItem(
657 $newKey,
658 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
659 'navigation',
660 NULL,
661 $contactID,
662 $contactID
663 );
664 }
665 }
666
667 return $newKey;
668 }
669
670 /**
671 * Process navigation.
672 *
673 * @param array $params
674 * Associated array, $_GET.
675 */
676 public static function processNavigation(&$params) {
677 $nodeID = (int) str_replace("node_", "", $params['id']);
678 $referenceID = (int) str_replace("node_", "", $params['ref_id']);
679 $position = $params['ps'];
680 $type = $params['type'];
681 $label = CRM_Utils_Array::value('data', $params);
682
683 switch ($type) {
684 case "move":
685 self::processMove($nodeID, $referenceID, $position);
686 break;
687
688 case "rename":
689 self::processRename($nodeID, $label);
690 break;
691
692 case "delete":
693 self::processDelete($nodeID);
694 break;
695 }
696
697 //reset navigation menus
698 self::resetNavigation();
699 CRM_Utils_System::civiExit();
700 }
701
702 /**
703 * Process move action.
704 *
705 * @param $nodeID
706 * Node that is being moved.
707 * @param $referenceID
708 * Parent id where node is moved. 0 mean no parent.
709 * @param $position
710 * New position of the nod, it starts with 0 - n.
711 */
712 public static function processMove($nodeID, $referenceID, $position) {
713 // based on the new position we need to get the weight of the node after moved node
714 // 1. update the weight of $position + 1 nodes to weight + 1
715 // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved
716
717 // check if there is parent id, which means node is moved inside existing parent container, so use parent id
718 // to find the correct position else use NULL to get the weights of parent ( $position - 1 )
719 // accordingly set the new parent_id
720 if ($referenceID) {
721 $newParentID = $referenceID;
722 $parentClause = "parent_id = {$referenceID} ";
723 }
724 else {
725 $newParentID = 'NULL';
726 $parentClause = 'parent_id IS NULL';
727 }
728
729 $incrementOtherNodes = TRUE;
730 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
731 $params = array(1 => array($position, 'Positive'));
732 $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params);
733
734 // this means node is moved to last position, so you need to get the weight of last element + 1
735 if (!$newWeight) {
736 // If this is not the first item being added to a parent
737 if ($position) {
738 $lastPosition = $position - 1;
739 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
740 $params = array(1 => array($lastPosition, 'Positive'));
741 $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params);
742
743 // since last node increment + 1
744 $newWeight = $newWeight + 1;
745 }
746 else {
747 $newWeight = '0';
748 }
749
750 // since this is a last node we don't need to increment other nodes
751 $incrementOtherNodes = FALSE;
752 }
753
754 $transaction = new CRM_Core_Transaction();
755
756 // now update the existing nodes to weight + 1, if required.
757 if ($incrementOtherNodes) {
758 $query = "UPDATE civicrm_navigation SET weight = weight + 1
759 WHERE {$parentClause} AND weight >= {$newWeight}";
760
761 CRM_Core_DAO::executeQuery($query);
762 }
763
764 // finally set the weight of current node
765 $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}";
766 CRM_Core_DAO::executeQuery($query);
767
768 $transaction->commit();
769 }
770
771 /**
772 * Function to process rename action for tree.
773 *
774 * @param int $nodeID
775 * @param $label
776 */
777 public static function processRename($nodeID, $label) {
778 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label);
779 }
780
781 /**
782 * Process delete action for tree.
783 *
784 * @param int $nodeID
785 */
786 public static function processDelete($nodeID) {
787 $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}";
788 CRM_Core_DAO::executeQuery($query);
789 }
790
791 /**
792 * Update menu.
793 *
794 * @param array $params
795 * @param array $newParams
796 * New value of params.
797 */
798 public static function processUpdate($params, $newParams) {
799 $dao = new CRM_Core_DAO_Navigation();
800 $dao->copyValues($params);
801 if ($dao->find(TRUE)) {
802 $dao->copyValues($newParams);
803 $dao->save();
804 }
805 }
806
807 /**
808 * Rebuild reports menu.
809 *
810 * All Contact reports will become sub-items of 'Contact Reports' and so on.
811 *
812 * @param int $domain_id
813 */
814 public static function rebuildReportsNavigation($domain_id) {
815 $component_to_nav_name = array(
816 'CiviContact' => 'Contact Reports',
817 'CiviContribute' => 'Contribution Reports',
818 'CiviMember' => 'Membership Reports',
819 'CiviEvent' => 'Event Reports',
820 'CiviPledge' => 'Pledge Reports',
821 'CiviGrant' => 'Grant Reports',
822 'CiviMail' => 'Mailing Reports',
823 'CiviCampaign' => 'Campaign Reports',
824 );
825
826 // Create or update the top level Reports link.
827 $reports_nav = self::createOrUpdateTopLevelReportsNavItem($domain_id);
828
829 // Get all active report instances grouped by component.
830 $components = self::getAllActiveReportsByComponent($domain_id);
831 foreach ($components as $component_id => $component) {
832 // Create or update the per component reports links.
833 $component_nav_name = $component['name'];
834 if (isset($component_to_nav_name[$component_nav_name])) {
835 $component_nav_name = $component_to_nav_name[$component_nav_name];
836 }
837 $permission = "access {$component['name']}";
838 if ($component['name'] === 'CiviContact') {
839 $permission = "administer CiviCRM";
840 }
841 elseif ($component['name'] === 'CiviCampaign') {
842 $permission = "access CiviReport";
843 }
844 $component_nav = self::createOrUpdateReportNavItem($component_nav_name, 'civicrm/report/list',
845 "compid={$component_id}&reset=1", $reports_nav->id, $permission, $domain_id, TRUE);
846 foreach ($component['reports'] as $report_id => $report) {
847 // Create or update the report instance links.
848 $report_nav = self::createOrUpdateReportNavItem($report['title'], $report['url'], 'reset=1', $component_nav->id, $report['permission'], $domain_id, FALSE, TRUE);
849 // Update the report instance to include the navigation id.
850 $query = "UPDATE civicrm_report_instance SET navigation_id = %1 WHERE id = %2";
851 $params = array(
852 1 => array($report_nav->id, 'Integer'),
853 2 => array($report_id, 'Integer'),
854 );
855 CRM_Core_DAO::executeQuery($query, $params);
856 }
857 }
858
859 // Create or update the All Reports link.
860 self::createOrUpdateReportNavItem('All Reports', 'civicrm/report/list', 'reset=1', $reports_nav->id, 'access CiviReport', $domain_id, TRUE);
861 // Create or update the My Reports link.
862 self::createOrUpdateReportNavItem('My Reports', 'civicrm/report/list', 'myreports=1&reset=1', $reports_nav->id, 'access CiviReport', $domain_id, TRUE);
863
864 }
865
866 /**
867 * Create the top level 'Reports' item in the navigation tree.
868 *
869 * @param int $domain_id
870 *
871 * @return bool|\CRM_Core_DAO
872 */
873 static public function createOrUpdateTopLevelReportsNavItem($domain_id) {
874 $id = NULL;
875
876 $dao = new CRM_Core_BAO_Navigation();
877 $dao->name = 'Reports';
878 $dao->domain_id = $domain_id;
879 // The first selectAdd clears it - so that we only retrieve the one field.
880 $dao->selectAdd();
881 $dao->selectAdd('id');
882 if ($dao->find(TRUE)) {
883 $id = $dao->id;
884 }
885
886 $nav = self::createReportNavItem('Reports', NULL, NULL, NULL, 'access CiviReport', $id, $domain_id);
887 return $nav;
888 }
889
890 /**
891 * Retrieve a navigation item using it's url.
892 *
893 * Note that we use LIKE to permit a wildcard as the calling code likely doesn't
894 * care about output params appended.
895 *
896 * @param string $url
897 * @param array $url_params
898 *
899 * @param int|null $parent_id
900 * Optionally restrict to one parent.
901 *
902 * @return bool|\CRM_Core_BAO_Navigation
903 */
904 public static function getNavItemByUrl($url, $url_params, $parent_id = NULL) {
905 $nav = new CRM_Core_BAO_Navigation();
906 $nav->parent_id = $parent_id;
907 $nav->whereAdd("url LIKE '{$url}?{$url_params}'");
908
909 if ($nav->find(TRUE)) {
910 return $nav;
911 }
912 return FALSE;
913 }
914
915 /**
916 * Get all active reports, organised by component.
917 *
918 * @param int $domain_id
919 *
920 * @return array
921 */
922 public static function getAllActiveReportsByComponent($domain_id) {
923 $sql = "
924 SELECT
925 civicrm_report_instance.id, civicrm_report_instance.title, civicrm_report_instance.permission, civicrm_component.name, civicrm_component.id AS component_id
926 FROM
927 civicrm_option_group
928 LEFT JOIN
929 civicrm_option_value ON civicrm_option_value.option_group_id = civicrm_option_group.id AND civicrm_option_group.name = 'report_template'
930 LEFT JOIN
931 civicrm_report_instance ON civicrm_option_value.value = civicrm_report_instance.report_id
932 LEFT JOIN
933 civicrm_component ON civicrm_option_value.component_id = civicrm_component.id
934 WHERE
935 civicrm_option_value.is_active = 1
936 AND
937 civicrm_report_instance.domain_id = %1
938 ORDER BY civicrm_option_value.weight";
939
940 $dao = CRM_Core_DAO::executeQuery($sql, array(
941 1 => array($domain_id, 'Integer'),
942 ));
943 $rows = array();
944 while ($dao->fetch()) {
945 $component_name = is_null($dao->name) ? 'CiviContact' : $dao->name;
946 $component_id = is_null($dao->component_id) ? 99 : $dao->component_id;
947 $rows[$component_id]['name'] = $component_name;
948 $rows[$component_id]['reports'][$dao->id] = array(
949 'title' => $dao->title,
950 'url' => "civicrm/report/instance/{$dao->id}",
951 'permission' => $dao->permission,
952 );
953 }
954 return $rows;
955 }
956
957 /**
958 * Create or update a navigation item for a report instance.
959 *
960 * The function will check whether create or update is required.
961 *
962 * @param string $name
963 * @param string $url
964 * @param string $url_params
965 * @param int $parent_id
966 * @param string $permission
967 * @param int $domain_id
968 *
969 * @param bool $onlyMatchParentID
970 * If True then do not match with a url that has a different parent
971 * (This is because for top level items there is a risk of 'stealing' rows that normally
972 * live under 'Contact' and intentionally duplicate the report examples.)
973 *
974 * @return \CRM_Core_DAO_Navigation
975 */
976 protected static function createOrUpdateReportNavItem($name, $url, $url_params, $parent_id, $permission,
977 $domain_id, $onlyMatchParentID = FALSE, $useWildcard = TRUE) {
978 $id = NULL;
979 $existing_url_params = $useWildcard ? $url_params . '%' : $url_params;
980 $existing_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $existing_url_params, ($onlyMatchParentID ? $parent_id : NULL));
981 if ($existing_nav) {
982 $id = $existing_nav->id;
983 }
984
985 $nav = self::createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id);
986 return $nav;
987 }
988
989 /**
990 * Create a navigation item for a report instance.
991 *
992 * @param string $name
993 * @param string $url
994 * @param string $url_params
995 * @param int $parent_id
996 * @param string $permission
997 * @param int $id
998 * @param int $domain_id
999 * ID of domain to create item in.
1000 *
1001 * @return \CRM_Core_DAO_Navigation
1002 */
1003 public static function createReportNavItem($name, $url, $url_params, $parent_id, $permission, $id, $domain_id) {
1004 if ($url !== NULL) {
1005 $url = "{$url}?{$url_params}";
1006 }
1007 $params = array(
1008 'name' => $name,
1009 'label' => ts($name),
1010 'url' => $url,
1011 'parent_id' => $parent_id,
1012 'is_active' => TRUE,
1013 'permission' => array(
1014 $permission,
1015 ),
1016 'domain_id' => $domain_id,
1017 );
1018 if ($id) {
1019 $params['id'] = $id;
1020 }
1021 return CRM_Core_BAO_Navigation::add($params);
1022 }
1023
1024 /**
1025 * Get cache key.
1026 *
1027 * @param int $cid
1028 *
1029 * @return object|string
1030 */
1031 public static function getCacheKey($cid) {
1032 $key = Civi::service('settings_manager')
1033 ->getBagByContact(NULL, $cid)
1034 ->get('navigation');
1035 if (strlen($key) !== self::CACHE_KEY_STRLEN) {
1036 $key = self::resetNavigation($cid);
1037 }
1038 return $key;
1039 }
1040
1041 }