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