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