CRM-15495 - Improve entityRef contact_type filter
[civicrm-core.git] / CRM / Core / BAO / Navigation.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {
36
73538697
CW
37 // Number of characters in the menu js cache key
38 const CACHE_KEY_STRLEN = 8;
39
6a488035 40 /**
100fef9d 41 * Class constructor
6a488035 42 */
00be9182 43 public function __construct() {
6a488035
TO
44 parent::__construct();
45 }
46
47 /**
100fef9d 48 * Update the is_active flag in the db
6a488035
TO
49 *
50 * @param int $id id of the database record
51 * @param boolean $is_active value we want to set the is_active field
52 *
53 * @return Object DAO object on sucess, null otherwise
54 *
6a488035
TO
55 * @static
56 */
00be9182 57 public static function setIsActive($id, $is_active) {
6a488035
TO
58 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active);
59 }
60
61 /**
100fef9d 62 * Get existing / build navigation for CiviCRM Admin Menu
6a488035
TO
63 *
64 * @static
65 * @return array associated array
66 */
00be9182 67 public static function getMenus() {
6a488035
TO
68 $menus = array();
69
70 $menu = new CRM_Core_DAO_Menu();
71 $menu->domain_id = CRM_Core_Config::domainID();
72 $menu->find();
73
74 while ($menu->fetch()) {
75 if ($menu->title) {
76 $menus[$menu->path] = $menu->title;
77 }
78 }
79 return $menus;
80 }
81
82 /**
100fef9d 83 * Add/update navigation record
6a488035
TO
84 *
85 * @param array associated array of submitted values
86 *
87 * @return object navigation object
88 * @static
89 */
00be9182 90 public static function add(&$params) {
6a488035
TO
91 $navigation = new CRM_Core_DAO_Navigation();
92
93 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
94 $params['has_separator'] = CRM_Utils_Array::value('has_separator', $params, FALSE);
95
96 if (!isset($params['id']) ||
97 (CRM_Utils_Array::value( 'parent_id', $params ) != CRM_Utils_Array::value('current_parent_id', $params))
98 ) {
99 /* re/calculate the weight, if the Parent ID changed OR create new menu */
100
101 if ($navName = CRM_Utils_Array::value('name', $params)) {
102 $params['name'] = $navName;
103 }
c5e077b2
PJ
104 elseif ($navLabel = CRM_Utils_Array::value('label', $params)) {
105 $params['name'] = $navLabel;
6a488035
TO
106 }
107
108 $params['weight'] = self::calculateWeight(CRM_Utils_Array::value('parent_id', $params));
109 }
110
c5e077b2 111 if (array_key_exists('permission', $params) && is_array($params['permission'])) {
6a488035
TO
112 $params['permission'] = implode(',', $params['permission']);
113 }
114
115 $navigation->copyValues($params);
116
117 $navigation->domain_id = CRM_Core_Config::domainID();
118
119 $navigation->save();
120 return $navigation;
121 }
122
123 /**
c490a46a 124 * Fetch object based on array of properties
6a488035
TO
125 *
126 * @param array $params (reference ) an assoc array of name/value pairs
127 * @param array $defaults (reference ) an assoc array to hold the flattened values
128 *
c490a46a 129 * @return CRM_Core_BAO_Navigation object on success, null otherwise
6a488035
TO
130 * @static
131 */
00be9182 132 public static function retrieve(&$params, &$defaults) {
6a488035
TO
133 $navigation = new CRM_Core_DAO_Navigation();
134 $navigation->copyValues($params);
135
136 $navigation->domain_id = CRM_Core_Config::domainID();
137
138 if ($navigation->find(TRUE)) {
139 CRM_Core_DAO::storeValues($navigation, $defaults);
140 return $navigation;
141 }
142 return NULL;
143 }
144
145 /**
146 * Calculate navigation weight
147 *
148 * @param $parentID parent_id of a menu
149 * @param $menuID menu id
150 *
da6b46f4 151 * @return int $weight string@static
6a488035 152 */
00be9182 153 public static function calculateWeight($parentID = NULL, $menuID = NULL) {
6a488035
TO
154 $domainID = CRM_Core_Config::domainID();
155
156 $weight = 1;
157 // we reset weight for each parent, i.e we start from 1 to n
158 // calculate max weight for top level menus, if parent id is absent
159 if (!$parentID) {
160 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID";
161 }
162 else {
163 // if parent is passed, we need to get max weight for that particular parent
164 $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID";
165 }
166
167 $dao = CRM_Core_DAO::executeQuery($query);
168 $dao->fetch();
169 return $weight = $weight + $dao->weight;
170 }
171
172 /**
173 * Get formatted menu list
174 *
175 * @return array $navigations returns associated array
176 * @static
177 */
00be9182 178 public static function getNavigationList() {
6a488035
TO
179 $cacheKeyString = "navigationList";
180 $whereClause = '';
181
182 $config = CRM_Core_Config::singleton();
183
184 // check if we can retrieve from database cache
185 $navigations = CRM_Core_BAO_Cache::getItem('navigation', $cacheKeyString);
186
187 if (!$navigations) {
188 $domainID = CRM_Core_Config::domainID();
189 $query = "
190SELECT id, label, parent_id, weight, is_active, name
191FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY parent_id, weight ASC";
192 $result = CRM_Core_DAO::executeQuery($query);
193
194 $pidGroups = array();
195 while ($result->fetch()) {
196 $pidGroups[$result->parent_id][$result->label] = $result->id;
197 }
198
199 foreach ($pidGroups[''] as $label => $val) {
200 $pidGroups[''][$label] = self::_getNavigationValue($val, $pidGroups);
201 }
202
203 $navigations = array();
204 self::_getNavigationLabel($pidGroups[''], $navigations);
205
206 CRM_Core_BAO_Cache::setItem($navigations, 'navigation', $cacheKeyString);
207 }
208 return $navigations;
209 }
210
211 /**
100fef9d 212 * Helper function for getNavigationList( )
6a488035
TO
213 *
214 * @param array $list menu info
215 * @param array $navigations navigation menus
216 * @param string $separator menu separator
217 */
00be9182 218 public static function _getNavigationLabel($list, &$navigations, $separator = '') {
6a488035
TO
219 $i18n = CRM_Core_I18n::singleton();
220 foreach ($list as $label => $val) {
221 if ($label == 'navigation_id') {
222 continue;
223 }
224 $translatedLabel = $i18n->crm_translate($label, array('context' => 'menu'));
225 $navigations[is_array($val) ? $val['navigation_id'] : $val] = "{$separator}{$translatedLabel}";
226 if (is_array($val)) {
227 self::_getNavigationLabel($val, $navigations, $separator . '&nbsp;&nbsp;&nbsp;&nbsp;');
228 }
229 }
230 }
231
232 /**
100fef9d 233 * Helper function for getNavigationList( )
6a488035
TO
234 *
235 * @param string $val menu name
236 * @param array $pidGroups parent menus
237 * @return array
238 */
00be9182 239 public static function _getNavigationValue($val, &$pidGroups) {
6a488035
TO
240 if (array_key_exists($val, $pidGroups)) {
241 $list = array('navigation_id' => $val);
242 foreach ($pidGroups[$val] as $label => $id) {
243 $list[$label] = self::_getNavigationValue($id, $pidGroups);
244 }
245 unset($pidGroups[$val]);
246 return $list;
247 }
248 else {
249 return $val;
250 }
251 }
252
253 /**
100fef9d 254 * Build navigation tree
6a488035
TO
255 *
256 * @param array $navigationTree nested array of menus
257 * @param int $parentID parent id
258 * @param boolean $navigationMenu true when called for building top navigation menu
259 *
260 * @return array $navigationTree nested array of menus
261 * @static
262 */
00be9182 263 public static function buildNavigationTree(&$navigationTree, $parentID, $navigationMenu = TRUE) {
6a488035
TO
264 $whereClause = " parent_id IS NULL";
265
266 if ($parentID) {
267 $whereClause = " parent_id = {$parentID}";
268 }
269
270 $domainID = CRM_Core_Config::domainID();
271
272 // get the list of menus
273 $query = "
274SELECT id, label, url, permission, permission_operator, has_separator, parent_id, is_active, name
275FROM civicrm_navigation
276WHERE {$whereClause}
277AND domain_id = $domainID
278ORDER BY parent_id, weight";
279
280 $navigation = CRM_Core_DAO::executeQuery($query);
281 $config = CRM_Core_Config::singleton();
282 while ($navigation->fetch()) {
283 $label = $navigation->label;
284 if (!$navigationMenu) {
285 $label = addcslashes($label, '"');
286 }
287
288 // for each menu get their children
289 $navigationTree[$navigation->id] = array(
290 'attributes' => array('label' => $label,
291 'name' => $navigation->name,
292 'url' => $navigation->url,
293 'permission' => $navigation->permission,
294 'operator' => $navigation->permission_operator,
295 'separator' => $navigation->has_separator,
296 'parentID' => $navigation->parent_id,
297 'navID' => $navigation->id,
298 'active' => $navigation->is_active,
299 ));
300 self::buildNavigationTree($navigationTree[$navigation->id]['child'], $navigation->id, $navigationMenu);
301 }
302
303 return $navigationTree;
304 }
305
306 /**
100fef9d 307 * Build menu
6a488035
TO
308 *
309 * @param boolean $json by default output is html
310 * @param boolean $navigationMenu true when called for building top navigation menu
311 *
312 * @return returns html or json object
313 * @static
314 */
00be9182 315 public static function buildNavigation($json = FALSE, $navigationMenu = TRUE) {
6a488035
TO
316 $navigations = array();
317 self::buildNavigationTree($navigations, $parent = NULL, $navigationMenu);
318 $navigationString = NULL;
319
320 // run the Navigation through a hook so users can modify it
321 CRM_Utils_Hook::navigationMenu($navigations);
322
323 $i18n = CRM_Core_I18n::singleton();
324
325 //skip children menu item if user don't have access to parent menu item
326 $skipMenuItems = array();
327 foreach ($navigations as $key => $value) {
328 if ($json) {
329 if ($navigationString) {
330 $navigationString .= '},';
331 }
332 $data = $value['attributes']['label'];
333 $class = '';
334 if (!$value['attributes']['active']) {
335 $class = ', "attr": { "class" : "disabled"} ';
336 }
337 $l10nName = $i18n->crm_translate($data, array('context' => 'menu'));
338 $navigationString .= ' { "attr": { "id" : "node_' . $key . '"}, "data": { "title":"' . $l10nName . '"' . $class . '}';
339 }
340 else {
341 // Home is a special case
342 if ($value['attributes']['name'] != 'Home') {
343 $name = self::getMenuName($value, $skipMenuItems);
344 if ($name) {
345 //separator before
346 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) {
347 $navigationString .= '<li class="menu-separator"></li>';
348 }
349 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
350 $navigationString .= '<li class="menumain crm-' . str_replace($removeCharacters, '_', $value['attributes']['label']) . '">' . $name;
351 }
352 }
353 }
354
355 self::recurseNavigation($value, $navigationString, $json, $skipMenuItems);
356 }
357
358 if ($json) {
359 $navigationString = '[' . $navigationString . '}]';
360 }
361 else {
362 // clean up - Need to remove empty <ul>'s, this happens when user don't have
363 // permission to access parent
364 $navigationString = str_replace('<ul></ul></li>', '', $navigationString);
365 }
366
367 return $navigationString;
368 }
369
370 /**
371 * Recursively check child menus
372 *
373 * @param array $value
374 * @param string $navigationString
375 * @param boolean $json
376 * @param boolean $skipMenuItems
377 * @return string
378 */
00be9182 379 public static function recurseNavigation(&$value, &$navigationString, $json, $skipMenuItems) {
6a488035
TO
380 if ($json) {
381 if (!empty($value['child'])) {
382 $navigationString .= ', "children": [ ';
383 }
384 else {
385 return $navigationString;
386 }
387
388 if (!empty($value['child'])) {
389 $appendComma = TRUE;
390 $count = 1;
391 foreach ($value['child'] as $k => $val) {
392 if ($count == count($value['child'])) {
393 $appendComma = FALSE;
394 }
395 $data = $val['attributes']['label'];
396 $class = '';
397 if (!$val['attributes']['active']) {
398 $class = ', "attr": { "class" : "disabled"} ';
399 }
400 $navigationString .= ' { "attr": { "id" : "node_' . $k . '"}, "data": { "title":"' . $data . '"' . $class . '}';
401 self::recurseNavigation($val, $navigationString, $json, $skipMenuItems);
402 $navigationString .= $appendComma ? ' },' : ' }';
403 $count++;
404 }
405 }
406
407 if (!empty($value['child'])) {
408 $navigationString .= ' ]';
409 }
410 }
411 else {
412 if (!empty($value['child'])) {
413 $navigationString .= '<ul>';
414 }
415 else {
416 $navigationString .= '</li>';
417 //locate separator after
418 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
419 $navigationString .= '<li class="menu-separator"></li>';
420 }
421 }
422
423 if (!empty($value['child'])) {
424 foreach ($value['child'] as $val) {
425 $name = self::getMenuName($val, $skipMenuItems);
426 if ($name) {
427 //locate separator before
428 if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) {
429 $navigationString .= '<li class="menu-separator"></li>';
430 }
431 $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.');
432 $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name;
433 self::recurseNavigation($val, $navigationString, $json, $skipMenuItems);
434 }
435 }
436 }
437 if (!empty($value['child'])) {
438 $navigationString .= '</ul></li>';
439 if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) {
440 $navigationString .= '<li class="menu-separator"></li>';
441 }
442 }
443 }
444 return $navigationString;
445 }
446
447 /**
448 * Get Menu name
449 *
450 * @param $value
100fef9d 451 * @param array $skipMenuItems
6a488035
TO
452 * @return bool|string
453 */
00be9182 454 public static function getMenuName(&$value, &$skipMenuItems) {
6a488035
TO
455 // we need to localise the menu labels (CRM-5456) and don’t
456 // want to use ts() as it would throw the ts-extractor off
457 $i18n = CRM_Core_I18n::singleton();
458
459 $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu'));
460 $url = $value['attributes']['url'];
461 $permission = $value['attributes']['permission'];
462 $operator = $value['attributes']['operator'];
463 $parentID = $value['attributes']['parentID'];
464 $navID = $value['attributes']['navID'];
465 $active = $value['attributes']['active'];
466 $menuName = $value['attributes']['name'];
467 $target = CRM_Utils_Array::value('target', $value['attributes']);
468
469 if (in_array($parentID, $skipMenuItems) || !$active) {
470 $skipMenuItems[] = $navID;
471 return FALSE;
472 }
473
474 //we need to check core view/edit or supported acls.
475 if (in_array($menuName, array(
476 'Search...', 'Contacts'))) {
477 if (!CRM_Core_Permission::giveMeAllACLs()) {
478 $skipMenuItems[] = $navID;
479 return FALSE;
480 }
481 }
482
483 $config = CRM_Core_Config::singleton();
484
485 $makeLink = FALSE;
486 if (isset($url) && $url) {
8ce923d1 487 if (substr($url, 0, 4) !== 'http') {
6a488035
TO
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.
710199c8 490 $urlParam = explode('?', $url);
491 if (empty($urlParam[1])) {
492 $urlParam[1] = NULL;
493 }
1351950a 494 $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, TRUE);
6a488035
TO
495 }
496 $makeLink = TRUE;
497 }
498
499 static $allComponents;
500 if (!$allComponents) {
501 $allComponents = CRM_Core_Component::getNames();
502 }
503
504 if (isset($permission) && $permission) {
505 $permissions = explode(',', $permission);
506
507 $hasPermission = FALSE;
508 foreach ($permissions as $key) {
509 $key = trim($key);
510 $showItem = TRUE;
511
512 //get the component name from permission.
513 $componentName = CRM_Core_Permission::getComponentName($key);
514
515 if ($componentName) {
516 if (!in_array($componentName, $config->enableComponents) ||
517 !CRM_Core_Permission::check($key)
518 ) {
519 $showItem = FALSE;
520 if ($operator == 'AND') {
521 $skipMenuItems[] = $navID;
522 return $showItem;
523 }
524 }
525 else {
526 $hasPermission = TRUE;
527 }
528 }
529 elseif (!CRM_Core_Permission::check($key)) {
530 $showItem = FALSE;
531 if ($operator == 'AND') {
532 $skipMenuItems[] = $navID;
533 return $showItem;
534 }
535 }
536 else {
537 $hasPermission = TRUE;
538 }
539 }
540
541 if (!$showItem && !$hasPermission) {
542 $skipMenuItems[] = $navID;
543 return FALSE;
544 }
545 }
546
547 if ($makeLink) {
548 if ($target) {
549 $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>";
550 }
551 else {
552 $name = "<a href=\"{$url}\">{$name}</a>";
553 }
554 }
555
556 return $name;
557 }
558
559 /**
100fef9d 560 * Create navigation for CiviCRM Admin Menu
6a488035
TO
561 *
562 * @param int $contactID contact id
563 *
564 * @return string $navigation returns navigation html
565 * @static
566 */
00be9182 567 public static function createNavigation($contactID) {
6a488035
TO
568 $config = CRM_Core_Config::singleton();
569
73538697 570 $navigation = self::buildNavigation();
6a488035 571
73538697 572 if ($navigation) {
6a488035
TO
573
574 //add additional navigation items
575 $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1');
6a488035
TO
576
577 // get home menu from db
578 $homeParams = array('name' => 'Home');
579 $homeNav = array();
8960d9b9 580 $homeIcon = '<img src="' . $config->userFrameworkResourceURL . 'i/logo16px.png" style="vertical-align:middle;" />';
6a488035
TO
581 self::retrieve($homeParams, $homeNav);
582 if ($homeNav) {
710199c8 583 list($path, $q) = explode('?', $homeNav['url']);
6a488035
TO
584 $homeURL = CRM_Utils_System::url($path, $q);
585 $homeLabel = $homeNav['label'];
586 // CRM-6804 (we need to special-case this as we don’t ts()-tag variables)
587 if ($homeLabel == 'Home') {
a65693e5 588 $homeLabel = ts('CiviCRM Home');
6a488035
TO
589 }
590 }
591 else {
592 $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
a65693e5 593 $homeLabel = ts('CiviCRM Home');
6a488035 594 }
8960d9b9 595 // Link to hide the menubar
6a488035
TO
596 if (
597 ($config->userSystem->is_drupal) &&
598 ((module_exists('toolbar') && user_access('access toolbar')) ||
599 module_exists('admin_menu') && user_access('access administration menu')
600 )
601 ) {
8960d9b9 602 $hideLabel = ts('Drupal Menu');
6a488035
TO
603 }
604 elseif ($config->userSystem->is_wordpress) {
8960d9b9 605 $hideLabel = ts('WordPress Menu');
6a488035
TO
606 }
607 else {
8960d9b9 608 $hideLabel = ts('Hide Menu');
6a488035
TO
609 }
610
8960d9b9
CW
611 $prepandString = "
612 <li class='menumain crm-link-home'>$homeIcon
613 <ul id='civicrm-home'>
614 <li><a href='$homeURL'>$homeLabel</a></li>
615 <li><a href='#' class='crm-hidemenu'>$hideLabel</a></li>
a1079307 616 <li><a href='$logoutURL' class='crm-logout-link'>". ts('Log out') ."</a></li>
8960d9b9
CW
617 </ul>";
618 // <li> tag doesn't need to be closed
73538697 619 }
8960d9b9 620 return $prepandString . $navigation;
73538697 621 }
6a488035 622
73538697
CW
623 /**
624 * Reset navigation for all contacts or a specified contact
625 *
626 * @param integer $contactID - reset only entries belonging to that contact ID
627 * @return string
628 */
00be9182 629 public static function resetNavigation($contactID = NULL) {
73538697
CW
630 $newKey = CRM_Utils_String::createRandom(self::CACHE_KEY_STRLEN, CRM_Utils_String::ALPHANUMERIC);
631 if (!$contactID) {
632 $query = "UPDATE civicrm_setting SET value = '$newKey' WHERE name='navigation' AND contact_id IS NOT NULL";
633 CRM_Core_DAO::executeQuery($query);
634 CRM_Core_BAO_Cache::deleteGroup('navigation');
635 }
636 else {
6a488035 637 // before inserting check if contact id exists in db
73538697 638 // this is to handle weird case when contact id is in session but not in db
6a488035
TO
639 $contact = new CRM_Contact_DAO_Contact();
640 $contact->id = $contactID;
641 if ($contact->find(TRUE)) {
642 CRM_Core_BAO_Setting::setItem(
73538697 643 $newKey,
6a488035
TO
644 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
645 'navigation',
646 NULL,
647 $contactID,
648 $contactID
649 );
650 }
651 }
6a488035 652 // also reset the dashlet cache in case permissions have changed etc
73538697 653 // FIXME: decouple this
6a488035 654 CRM_Core_BAO_Dashboard::resetDashletCache($contactID);
73538697
CW
655
656 return $newKey;
6a488035
TO
657 }
658
659 /**
100fef9d 660 * Process navigation
6a488035
TO
661 *
662 * @param array $params associated array, $_GET
663 *
664 * @return void
665 * @static
666 */
00be9182 667 public static function processNavigation(&$params) {
6a488035
TO
668 $nodeID = (int)str_replace("node_", "", $params['id']);
669 $referenceID = (int)str_replace("node_", "", $params['ref_id']);
670 $position = $params['ps'];
671 $type = $params['type'];
672 $label = CRM_Utils_Array::value('data', $params);
673
674 switch ($type) {
675 case "move":
676 self::processMove($nodeID, $referenceID, $position);
677 break;
678
679 case "rename":
680 self::processRename($nodeID, $label);
681 break;
682
683 case "delete":
684 self::processDelete($nodeID);
685 break;
686 }
687
688 //reset navigation menus
689 self::resetNavigation();
690 CRM_Utils_System::civiExit();
691 }
692
693 /**
100fef9d 694 * Process move action
6a488035
TO
695 *
696 * @param $nodeID node that is being moved
697 * @param $referenceID parent id where node is moved. 0 mean no parent
698 * @param $position new position of the nod, it starts with 0 - n
699 *
700 * @return void
701 * @static
702 */
00be9182 703 public static function processMove($nodeID, $referenceID, $position) {
6a488035
TO
704 // based on the new position we need to get the weight of the node after moved node
705 // 1. update the weight of $position + 1 nodes to weight + 1
706 // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved
707
708 // check if there is parent id, which means node is moved inside existing parent container, so use parent id
709 // to find the correct position else use NULL to get the weights of parent ( $position - 1 )
710 // accordingly set the new parent_id
711 if ($referenceID) {
712 $newParentID = $referenceID;
713 $parentClause = "parent_id = {$referenceID} ";
714 }
715 else {
716 $newParentID = 'NULL';
717 $parentClause = 'parent_id IS NULL';
718 }
719
720 $incrementOtherNodes = true;
721 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
722 $params = array(1 => array( $position, 'Positive'));
723 $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params);
724
725 // this means node is moved to last position, so you need to get the weight of last element + 1
726 if (!$newWeight) {
727 $lastPosition = $position - 1;
728 $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1";
729 $params = array(1 => array($lastPosition, 'Positive'));
730 $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params);
731
732 // since last node increment + 1
733 $newWeight = $newWeight + 1;
734
735 // since this is a last node we don't need to increment other nodes
736 $incrementOtherNodes = false;
737 }
738
739 $transaction = new CRM_Core_Transaction();
740
741 // now update the existing nodes to weight + 1, if required.
742 if ( $incrementOtherNodes ) {
743 $query = "UPDATE civicrm_navigation SET weight = weight + 1
744 WHERE {$parentClause} AND weight >= {$newWeight}";
745
746 CRM_Core_DAO::executeQuery($query);
747 }
748
749 // finally set the weight of current node
750 $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}";
751 CRM_Core_DAO::executeQuery($query);
752
753 $transaction->commit();
754 }
755
756 /**
757 * Function to process rename action for tree
758 *
100fef9d 759 * @param int $nodeID
6a488035
TO
760 * @param $label
761 */
00be9182 762 public static function processRename($nodeID, $label) {
6a488035
TO
763 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label);
764 }
765
766 /**
100fef9d 767 * Process delete action for tree
6a488035 768 *
100fef9d 769 * @param int $nodeID
6a488035 770 */
00be9182 771 public static function processDelete($nodeID) {
6a488035
TO
772 $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}";
773 CRM_Core_DAO::executeQuery($query);
774 }
775
776 /**
100fef9d 777 * Get the info on navigation item
6a488035
TO
778 *
779 * @param int $navigationID navigation id
780 *
781 * @return array associated array
782 * @static
783 */
00be9182 784 public static function getNavigationInfo($navigationID) {
6a488035
TO
785 $query = "SELECT parent_id, weight FROM civicrm_navigation WHERE id = %1";
786 $params = array($navigationID, 'Integer');
787 $dao = CRM_Core_DAO::executeQuery($query, array(1 => $params));
788 $dao->fetch();
789 return array(
790 'parent_id' => $dao->parent_id,
791 'weight' => $dao->weight,
792 );
793 }
794
795 /**
100fef9d 796 * Update menu
6a488035
TO
797 *
798 * @param array $params
799 * @param array $newParams new value of params
800 * @static
801 */
00be9182 802 public static function processUpdate($params, $newParams) {
6a488035
TO
803 $dao = new CRM_Core_DAO_Navigation();
804 $dao->copyValues($params);
805 if ($dao->find(TRUE)) {
806 $dao->copyValues($newParams);
807 $dao->save();
808 }
809 }
73538697 810
b5c2afd0 811 /**
100fef9d 812 * @param int $cid
b5c2afd0
EM
813 *
814 * @return object|string
815 */
00be9182 816 public static function getCacheKey($cid) {
73538697
CW
817 $key = CRM_Core_BAO_Setting::getItem(
818 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
819 'navigation',
820 NULL,
821 '',
822 $cid
823 );
824 if (strlen($key) !== self::CACHE_KEY_STRLEN) {
825 $key = self::resetNavigation($cid);
826 }
827 return $key;
828 }
6a488035 829}