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