Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation { | |
36 | ||
73538697 CW |
37 | // Number of characters in the menu js cache key |
38 | const CACHE_KEY_STRLEN = 8; | |
39 | ||
6a488035 | 40 | /** |
fe482240 | 41 | * Class constructor. |
6a488035 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | parent::__construct(); |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 48 | * Update the is_active flag in the db. |
6a488035 | 49 | * |
6a0b768e TO |
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. | |
6a488035 | 54 | * |
ffbff132 EM |
55 | * @return CRM_Core_DAO_Navigation|NULL |
56 | * DAO object on success, NULL otherwise | |
6a488035 | 57 | */ |
00be9182 | 58 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
59 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active); |
60 | } | |
61 | ||
62 | /** | |
ffbff132 | 63 | * Get existing / build navigation for CiviCRM Admin Menu. |
6a488035 | 64 | * |
a6c01b45 CW |
65 | * @return array |
66 | * associated array | |
6a488035 | 67 | */ |
00be9182 | 68 | public static function getMenus() { |
6a488035 TO |
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 | /** | |
ffbff132 | 84 | * Add/update navigation record. |
6a488035 | 85 | * |
ffbff132 | 86 | * @param array $params Submitted values |
6a488035 | 87 | * |
ffbff132 | 88 | * @return CRM_Core_DAO_Navigation |
a6c01b45 | 89 | * navigation object |
6a488035 | 90 | */ |
00be9182 | 91 | public static function add(&$params) { |
6a488035 TO |
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']) || | |
481a74f4 | 98 | (CRM_Utils_Array::value('parent_id', $params) != CRM_Utils_Array::value('current_parent_id', $params)) |
6a488035 TO |
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 | } | |
c5e077b2 PJ |
105 | elseif ($navLabel = CRM_Utils_Array::value('label', $params)) { |
106 | $params['name'] = $navLabel; | |
6a488035 TO |
107 | } |
108 | ||
109 | $params['weight'] = self::calculateWeight(CRM_Utils_Array::value('parent_id', $params)); | |
110 | } | |
111 | ||
c5e077b2 | 112 | if (array_key_exists('permission', $params) && is_array($params['permission'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 125 | * Fetch object based on array of properties. |
6a488035 | 126 | * |
6a0b768e TO |
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. | |
6a488035 | 131 | * |
16b10e64 CW |
132 | * @return CRM_Core_BAO_Navigation|null |
133 | * object on success, NULL otherwise | |
6a488035 | 134 | */ |
00be9182 | 135 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
fe482240 | 149 | * Calculate navigation weight. |
6a488035 | 150 | * |
5a4f6742 | 151 | * @param int $parentID |
6a0b768e | 152 | * Parent_id of a menu. |
5a4f6742 | 153 | * @param int $menuID |
6a0b768e | 154 | * Menu id. |
6a488035 | 155 | * |
a6c01b45 CW |
156 | * @return int |
157 | * $weight string | |
6a488035 | 158 | */ |
00be9182 | 159 | public static function calculateWeight($parentID = NULL, $menuID = NULL) { |
6a488035 TO |
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 | /** | |
fe482240 | 179 | * Get formatted menu list. |
6a488035 | 180 | * |
a6c01b45 CW |
181 | * @return array |
182 | * returns associated array | |
6a488035 | 183 | */ |
00be9182 | 184 | public static function getNavigationList() { |
6a488035 TO |
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 | /** | |
ffbff132 | 218 | * Helper function for getNavigationList(). |
6a488035 | 219 | * |
6a0b768e TO |
220 | * @param array $list |
221 | * Menu info. | |
222 | * @param array $navigations | |
223 | * Navigation menus. | |
224 | * @param string $separator | |
225 | * Menu separator. | |
6a488035 | 226 | */ |
00be9182 | 227 | public static function _getNavigationLabel($list, &$navigations, $separator = '') { |
6a488035 TO |
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 | /** | |
ffbff132 | 242 | * Helper function for getNavigationList(). |
6a488035 | 243 | * |
6a0b768e TO |
244 | * @param string $val |
245 | * Menu name. | |
246 | * @param array $pidGroups | |
247 | * Parent menus. | |
ffbff132 | 248 | * |
6a488035 TO |
249 | * @return array |
250 | */ | |
00be9182 | 251 | public static function _getNavigationValue($val, &$pidGroups) { |
6a488035 TO |
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 | /** | |
fe482240 | 266 | * Build navigation tree. |
6a488035 | 267 | * |
6a0b768e TO |
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. | |
6a488035 | 274 | * |
a6c01b45 CW |
275 | * @return array |
276 | * nested array of menus | |
6a488035 | 277 | */ |
00be9182 | 278 | public static function buildNavigationTree(&$navigationTree, $parentID, $navigationMenu = TRUE) { |
6a488035 TO |
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( | |
6ea503d4 TO |
305 | 'attributes' => array( |
306 | 'label' => $label, | |
6a488035 TO |
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, | |
acb1052e | 315 | ), |
353ffa53 | 316 | ); |
6a488035 TO |
317 | self::buildNavigationTree($navigationTree[$navigation->id]['child'], $navigation->id, $navigationMenu); |
318 | } | |
319 | ||
320 | return $navigationTree; | |
321 | } | |
322 | ||
323 | /** | |
fe482240 | 324 | * Build menu. |
6a488035 | 325 | * |
6a0b768e TO |
326 | * @param bool $json |
327 | * By default output is html. | |
328 | * @param bool $navigationMenu | |
329 | * True when called for building top navigation menu. | |
6a488035 | 330 | * |
72b3a70c CW |
331 | * @return string |
332 | * html or json string | |
6a488035 | 333 | */ |
00be9182 | 334 | public static function buildNavigation($json = FALSE, $navigationMenu = TRUE) { |
6a488035 TO |
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 | /** | |
fe482240 | 390 | * Recursively check child menus. |
6a488035 TO |
391 | * |
392 | * @param array $value | |
393 | * @param string $navigationString | |
6a0b768e TO |
394 | * @param bool $json |
395 | * @param bool $skipMenuItems | |
ffbff132 | 396 | * |
6a488035 TO |
397 | * @return string |
398 | */ | |
00be9182 | 399 | public static function recurseNavigation(&$value, &$navigationString, $json, $skipMenuItems) { |
6a488035 TO |
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 | /** | |
fe482240 | 468 | * Get Menu name. |
6a488035 TO |
469 | * |
470 | * @param $value | |
100fef9d | 471 | * @param array $skipMenuItems |
ffbff132 | 472 | * |
6a488035 TO |
473 | * @return bool|string |
474 | */ | |
00be9182 | 475 | public static function getMenuName(&$value, &$skipMenuItems) { |
6a488035 TO |
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 | ||
353ffa53 | 480 | $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu')); |
29c4b4bf WA |
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']); | |
353ffa53 | 488 | $target = CRM_Utils_Array::value('target', $value['attributes']); |
6a488035 TO |
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( | |
353ffa53 | 497 | 'Search...', |
acb1052e | 498 | 'Contacts', |
353ffa53 | 499 | ))) { |
6a488035 TO |
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) { | |
8ce923d1 | 510 | if (substr($url, 0, 4) !== 'http') { |
6a488035 TO |
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. | |
710199c8 | 513 | $urlParam = explode('?', $url); |
514 | if (empty($urlParam[1])) { | |
515 | $urlParam[1] = NULL; | |
516 | } | |
1351950a | 517 | $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, TRUE); |
6a488035 TO |
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 | /** | |
fe482240 | 583 | * Create navigation for CiviCRM Admin Menu. |
6a488035 | 584 | * |
6a0b768e TO |
585 | * @param int $contactID |
586 | * Contact id. | |
6a488035 | 587 | * |
a6c01b45 CW |
588 | * @return string |
589 | * returns navigation html | |
6a488035 | 590 | */ |
00be9182 | 591 | public static function createNavigation($contactID) { |
6a488035 TO |
592 | $config = CRM_Core_Config::singleton(); |
593 | ||
73538697 | 594 | $navigation = self::buildNavigation(); |
6a488035 | 595 | |
73538697 | 596 | if ($navigation) { |
6a488035 TO |
597 | |
598 | //add additional navigation items | |
599 | $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1'); | |
6a488035 TO |
600 | |
601 | // get home menu from db | |
602 | $homeParams = array('name' => 'Home'); | |
603 | $homeNav = array(); | |
a2e1885c | 604 | $homeIcon = '<span class="crm-logo-sm" ></span>'; |
6a488035 TO |
605 | self::retrieve($homeParams, $homeNav); |
606 | if ($homeNav) { | |
710199c8 | 607 | list($path, $q) = explode('?', $homeNav['url']); |
6a488035 TO |
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') { | |
a65693e5 | 612 | $homeLabel = ts('CiviCRM Home'); |
6a488035 TO |
613 | } |
614 | } | |
615 | else { | |
616 | $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
a65693e5 | 617 | $homeLabel = ts('CiviCRM Home'); |
6a488035 | 618 | } |
8960d9b9 | 619 | // Link to hide the menubar |
8b571e4c | 620 | $hideLabel = ts('Hide Menu'); |
6a488035 | 621 | |
8960d9b9 CW |
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> | |
86bfa4f6 | 627 | <li><a href='$logoutURL' class='crm-logout-link'>" . ts('Log out') . "</a></li> |
8960d9b9 CW |
628 | </ul>"; |
629 | // <li> tag doesn't need to be closed | |
73538697 | 630 | } |
8960d9b9 | 631 | return $prepandString . $navigation; |
73538697 | 632 | } |
6a488035 | 633 | |
73538697 | 634 | /** |
fe482240 | 635 | * Reset navigation for all contacts or a specified contact. |
73538697 | 636 | * |
6a0b768e TO |
637 | * @param int $contactID |
638 | * Reset only entries belonging to that contact ID. | |
ffbff132 | 639 | * |
73538697 CW |
640 | * @return string |
641 | */ | |
00be9182 | 642 | public static function resetNavigation($contactID = NULL) { |
73538697 CW |
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 { | |
6a488035 | 650 | // before inserting check if contact id exists in db |
73538697 | 651 | // this is to handle weird case when contact id is in session but not in db |
6a488035 TO |
652 | $contact = new CRM_Contact_DAO_Contact(); |
653 | $contact->id = $contactID; | |
654 | if ($contact->find(TRUE)) { | |
655 | CRM_Core_BAO_Setting::setItem( | |
73538697 | 656 | $newKey, |
6a488035 TO |
657 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, |
658 | 'navigation', | |
659 | NULL, | |
660 | $contactID, | |
661 | $contactID | |
662 | ); | |
663 | } | |
664 | } | |
6a488035 | 665 | // also reset the dashlet cache in case permissions have changed etc |
73538697 | 666 | // FIXME: decouple this |
6a488035 | 667 | CRM_Core_BAO_Dashboard::resetDashletCache($contactID); |
73538697 CW |
668 | |
669 | return $newKey; | |
6a488035 TO |
670 | } |
671 | ||
672 | /** | |
fe482240 | 673 | * Process navigation. |
6a488035 | 674 | * |
6a0b768e TO |
675 | * @param array $params |
676 | * Associated array, $_GET. | |
6a488035 | 677 | */ |
00be9182 | 678 | public static function processNavigation(&$params) { |
353ffa53 TO |
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); | |
6a488035 TO |
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 | /** | |
fe482240 | 705 | * Process move action. |
6a488035 | 706 | * |
6a0b768e TO |
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. | |
6a488035 | 713 | */ |
00be9182 | 714 | public static function processMove($nodeID, $referenceID, $position) { |
6a488035 TO |
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 | ||
4eeb9a5b | 731 | $incrementOtherNodes = TRUE; |
353ffa53 | 732 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; |
481a74f4 | 733 | $params = array(1 => array($position, 'Positive')); |
6a488035 TO |
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) { | |
7cc5305f CW |
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 | } | |
6a488035 TO |
751 | |
752 | // since this is a last node we don't need to increment other nodes | |
ab8a593e | 753 | $incrementOtherNodes = FALSE; |
6a488035 TO |
754 | } |
755 | ||
756 | $transaction = new CRM_Core_Transaction(); | |
757 | ||
758 | // now update the existing nodes to weight + 1, if required. | |
481a74f4 | 759 | if ($incrementOtherNodes) { |
6a488035 TO |
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 | /** | |
ffbff132 | 774 | * Function to process rename action for tree. |
6a488035 | 775 | * |
100fef9d | 776 | * @param int $nodeID |
6a488035 TO |
777 | * @param $label |
778 | */ | |
00be9182 | 779 | public static function processRename($nodeID, $label) { |
6a488035 TO |
780 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label); |
781 | } | |
782 | ||
783 | /** | |
fe482240 | 784 | * Process delete action for tree. |
6a488035 | 785 | * |
100fef9d | 786 | * @param int $nodeID |
6a488035 | 787 | */ |
00be9182 | 788 | public static function processDelete($nodeID) { |
6a488035 TO |
789 | $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}"; |
790 | CRM_Core_DAO::executeQuery($query); | |
791 | } | |
792 | ||
793 | /** | |
fe482240 | 794 | * Get the info on navigation item. |
6a488035 | 795 | * |
6a0b768e TO |
796 | * @param int $navigationID |
797 | * Navigation id. | |
6a488035 | 798 | * |
a6c01b45 CW |
799 | * @return array |
800 | * associated array | |
6a488035 | 801 | */ |
00be9182 | 802 | public static function getNavigationInfo($navigationID) { |
353ffa53 | 803 | $query = "SELECT parent_id, weight FROM civicrm_navigation WHERE id = %1"; |
6a488035 | 804 | $params = array($navigationID, 'Integer'); |
353ffa53 | 805 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => $params)); |
6a488035 TO |
806 | $dao->fetch(); |
807 | return array( | |
808 | 'parent_id' => $dao->parent_id, | |
809 | 'weight' => $dao->weight, | |
810 | ); | |
811 | } | |
812 | ||
813 | /** | |
fe482240 | 814 | * Update menu. |
6a488035 | 815 | * |
6a0b768e TO |
816 | * @param array $params |
817 | * @param array $newParams | |
818 | * New value of params. | |
6a488035 | 819 | */ |
00be9182 | 820 | public static function processUpdate($params, $newParams) { |
6a488035 TO |
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 | } | |
73538697 | 828 | |
b5c2afd0 | 829 | /** |
ffbff132 EM |
830 | * Get cache key. |
831 | * | |
100fef9d | 832 | * @param int $cid |
b5c2afd0 EM |
833 | * |
834 | * @return object|string | |
835 | */ | |
00be9182 | 836 | public static function getCacheKey($cid) { |
73538697 CW |
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 | } | |
96025800 | 849 | |
6a488035 | 850 | } |