Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | * |
a6c01b45 CW |
55 | * @return Object |
56 | * DAO object on sucess, NULL otherwise | |
6a488035 | 57 | * |
6a488035 | 58 | */ |
00be9182 | 59 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
60 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active); |
61 | } | |
62 | ||
63 | /** | |
100fef9d | 64 | * Get existing / build navigation for CiviCRM Admin Menu |
6a488035 | 65 | * |
a6c01b45 CW |
66 | * @return array |
67 | * associated array | |
6a488035 | 68 | */ |
00be9182 | 69 | public static function getMenus() { |
6a488035 TO |
70 | $menus = array(); |
71 | ||
72 | $menu = new CRM_Core_DAO_Menu(); | |
73 | $menu->domain_id = CRM_Core_Config::domainID(); | |
74 | $menu->find(); | |
75 | ||
76 | while ($menu->fetch()) { | |
77 | if ($menu->title) { | |
78 | $menus[$menu->path] = $menu->title; | |
79 | } | |
80 | } | |
81 | return $menus; | |
82 | } | |
83 | ||
84 | /** | |
100fef9d | 85 | * Add/update navigation record |
6a488035 | 86 | * |
acb1052e | 87 | * @param $params associated array of submitted values |
6a488035 | 88 | * |
a6c01b45 CW |
89 | * @return object |
90 | * navigation object | |
6a488035 | 91 | */ |
00be9182 | 92 | public static function add(&$params) { |
6a488035 TO |
93 | $navigation = new CRM_Core_DAO_Navigation(); |
94 | ||
95 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
96 | $params['has_separator'] = CRM_Utils_Array::value('has_separator', $params, FALSE); | |
97 | ||
98 | if (!isset($params['id']) || | |
481a74f4 | 99 | (CRM_Utils_Array::value('parent_id', $params) != CRM_Utils_Array::value('current_parent_id', $params)) |
6a488035 TO |
100 | ) { |
101 | /* re/calculate the weight, if the Parent ID changed OR create new menu */ | |
102 | ||
103 | if ($navName = CRM_Utils_Array::value('name', $params)) { | |
104 | $params['name'] = $navName; | |
105 | } | |
c5e077b2 PJ |
106 | elseif ($navLabel = CRM_Utils_Array::value('label', $params)) { |
107 | $params['name'] = $navLabel; | |
6a488035 TO |
108 | } |
109 | ||
110 | $params['weight'] = self::calculateWeight(CRM_Utils_Array::value('parent_id', $params)); | |
111 | } | |
112 | ||
c5e077b2 | 113 | if (array_key_exists('permission', $params) && is_array($params['permission'])) { |
6a488035 TO |
114 | $params['permission'] = implode(',', $params['permission']); |
115 | } | |
116 | ||
117 | $navigation->copyValues($params); | |
118 | ||
119 | $navigation->domain_id = CRM_Core_Config::domainID(); | |
120 | ||
121 | $navigation->save(); | |
122 | return $navigation; | |
123 | } | |
124 | ||
125 | /** | |
fe482240 | 126 | * Fetch object based on array of properties. |
6a488035 | 127 | * |
6a0b768e TO |
128 | * @param array $params |
129 | * (reference ) an assoc array of name/value pairs. | |
130 | * @param array $defaults | |
131 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 132 | * |
16b10e64 CW |
133 | * @return CRM_Core_BAO_Navigation|null |
134 | * object on success, NULL otherwise | |
6a488035 | 135 | */ |
00be9182 | 136 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
137 | $navigation = new CRM_Core_DAO_Navigation(); |
138 | $navigation->copyValues($params); | |
139 | ||
140 | $navigation->domain_id = CRM_Core_Config::domainID(); | |
141 | ||
142 | if ($navigation->find(TRUE)) { | |
143 | CRM_Core_DAO::storeValues($navigation, $defaults); | |
144 | return $navigation; | |
145 | } | |
146 | return NULL; | |
147 | } | |
148 | ||
149 | /** | |
fe482240 | 150 | * Calculate navigation weight. |
6a488035 | 151 | * |
5a4f6742 | 152 | * @param int $parentID |
6a0b768e | 153 | * Parent_id of a menu. |
5a4f6742 | 154 | * @param int $menuID |
6a0b768e | 155 | * Menu id. |
6a488035 | 156 | * |
a6c01b45 CW |
157 | * @return int |
158 | * $weight string | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function calculateWeight($parentID = NULL, $menuID = NULL) { |
6a488035 TO |
161 | $domainID = CRM_Core_Config::domainID(); |
162 | ||
163 | $weight = 1; | |
164 | // we reset weight for each parent, i.e we start from 1 to n | |
165 | // calculate max weight for top level menus, if parent id is absent | |
166 | if (!$parentID) { | |
167 | $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID"; | |
168 | } | |
169 | else { | |
170 | // if parent is passed, we need to get max weight for that particular parent | |
171 | $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID"; | |
172 | } | |
173 | ||
174 | $dao = CRM_Core_DAO::executeQuery($query); | |
175 | $dao->fetch(); | |
176 | return $weight = $weight + $dao->weight; | |
177 | } | |
178 | ||
179 | /** | |
fe482240 | 180 | * Get formatted menu list. |
6a488035 | 181 | * |
a6c01b45 CW |
182 | * @return array |
183 | * returns associated array | |
6a488035 | 184 | */ |
00be9182 | 185 | public static function getNavigationList() { |
6a488035 TO |
186 | $cacheKeyString = "navigationList"; |
187 | $whereClause = ''; | |
188 | ||
189 | $config = CRM_Core_Config::singleton(); | |
190 | ||
191 | // check if we can retrieve from database cache | |
192 | $navigations = CRM_Core_BAO_Cache::getItem('navigation', $cacheKeyString); | |
193 | ||
194 | if (!$navigations) { | |
195 | $domainID = CRM_Core_Config::domainID(); | |
196 | $query = " | |
197 | SELECT id, label, parent_id, weight, is_active, name | |
198 | FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY parent_id, weight ASC"; | |
199 | $result = CRM_Core_DAO::executeQuery($query); | |
200 | ||
201 | $pidGroups = array(); | |
202 | while ($result->fetch()) { | |
203 | $pidGroups[$result->parent_id][$result->label] = $result->id; | |
204 | } | |
205 | ||
206 | foreach ($pidGroups[''] as $label => $val) { | |
207 | $pidGroups[''][$label] = self::_getNavigationValue($val, $pidGroups); | |
208 | } | |
209 | ||
210 | $navigations = array(); | |
211 | self::_getNavigationLabel($pidGroups[''], $navigations); | |
212 | ||
213 | CRM_Core_BAO_Cache::setItem($navigations, 'navigation', $cacheKeyString); | |
214 | } | |
215 | return $navigations; | |
216 | } | |
217 | ||
218 | /** | |
100fef9d | 219 | * Helper function for getNavigationList( ) |
6a488035 | 220 | * |
6a0b768e TO |
221 | * @param array $list |
222 | * Menu info. | |
223 | * @param array $navigations | |
224 | * Navigation menus. | |
225 | * @param string $separator | |
226 | * Menu separator. | |
6a488035 | 227 | */ |
00be9182 | 228 | public static function _getNavigationLabel($list, &$navigations, $separator = '') { |
6a488035 TO |
229 | $i18n = CRM_Core_I18n::singleton(); |
230 | foreach ($list as $label => $val) { | |
231 | if ($label == 'navigation_id') { | |
232 | continue; | |
233 | } | |
234 | $translatedLabel = $i18n->crm_translate($label, array('context' => 'menu')); | |
235 | $navigations[is_array($val) ? $val['navigation_id'] : $val] = "{$separator}{$translatedLabel}"; | |
236 | if (is_array($val)) { | |
237 | self::_getNavigationLabel($val, $navigations, $separator . ' '); | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | /** | |
100fef9d | 243 | * Helper function for getNavigationList( ) |
6a488035 | 244 | * |
6a0b768e TO |
245 | * @param string $val |
246 | * Menu name. | |
247 | * @param array $pidGroups | |
248 | * Parent menus. | |
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 | |
6a488035 TO |
396 | * @return string |
397 | */ | |
00be9182 | 398 | public static function recurseNavigation(&$value, &$navigationString, $json, $skipMenuItems) { |
6a488035 TO |
399 | if ($json) { |
400 | if (!empty($value['child'])) { | |
401 | $navigationString .= ', "children": [ '; | |
402 | } | |
403 | else { | |
404 | return $navigationString; | |
405 | } | |
406 | ||
407 | if (!empty($value['child'])) { | |
408 | $appendComma = TRUE; | |
409 | $count = 1; | |
410 | foreach ($value['child'] as $k => $val) { | |
411 | if ($count == count($value['child'])) { | |
412 | $appendComma = FALSE; | |
413 | } | |
414 | $data = $val['attributes']['label']; | |
415 | $class = ''; | |
416 | if (!$val['attributes']['active']) { | |
417 | $class = ', "attr": { "class" : "disabled"} '; | |
418 | } | |
419 | $navigationString .= ' { "attr": { "id" : "node_' . $k . '"}, "data": { "title":"' . $data . '"' . $class . '}'; | |
420 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
421 | $navigationString .= $appendComma ? ' },' : ' }'; | |
422 | $count++; | |
423 | } | |
424 | } | |
425 | ||
426 | if (!empty($value['child'])) { | |
427 | $navigationString .= ' ]'; | |
428 | } | |
429 | } | |
430 | else { | |
431 | if (!empty($value['child'])) { | |
432 | $navigationString .= '<ul>'; | |
433 | } | |
434 | else { | |
435 | $navigationString .= '</li>'; | |
436 | //locate separator after | |
437 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
438 | $navigationString .= '<li class="menu-separator"></li>'; | |
439 | } | |
440 | } | |
441 | ||
442 | if (!empty($value['child'])) { | |
443 | foreach ($value['child'] as $val) { | |
444 | $name = self::getMenuName($val, $skipMenuItems); | |
445 | if ($name) { | |
446 | //locate separator before | |
447 | if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) { | |
448 | $navigationString .= '<li class="menu-separator"></li>'; | |
449 | } | |
450 | $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); | |
451 | $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name; | |
452 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
453 | } | |
454 | } | |
455 | } | |
456 | if (!empty($value['child'])) { | |
457 | $navigationString .= '</ul></li>'; | |
458 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
459 | $navigationString .= '<li class="menu-separator"></li>'; | |
460 | } | |
461 | } | |
462 | } | |
463 | return $navigationString; | |
464 | } | |
465 | ||
466 | /** | |
fe482240 | 467 | * Get Menu name. |
6a488035 TO |
468 | * |
469 | * @param $value | |
100fef9d | 470 | * @param array $skipMenuItems |
6a488035 TO |
471 | * @return bool|string |
472 | */ | |
00be9182 | 473 | public static function getMenuName(&$value, &$skipMenuItems) { |
6a488035 TO |
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 | ||
353ffa53 TO |
478 | $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu')); |
479 | $url = $value['attributes']['url']; | |
6a488035 | 480 | $permission = $value['attributes']['permission']; |
353ffa53 TO |
481 | $operator = $value['attributes']['operator']; |
482 | $parentID = $value['attributes']['parentID']; | |
483 | $navID = $value['attributes']['navID']; | |
484 | $active = $value['attributes']['active']; | |
485 | $menuName = $value['attributes']['name']; | |
486 | $target = CRM_Utils_Array::value('target', $value['attributes']); | |
6a488035 TO |
487 | |
488 | if (in_array($parentID, $skipMenuItems) || !$active) { | |
489 | $skipMenuItems[] = $navID; | |
490 | return FALSE; | |
491 | } | |
492 | ||
493 | //we need to check core view/edit or supported acls. | |
494 | if (in_array($menuName, array( | |
353ffa53 | 495 | 'Search...', |
acb1052e | 496 | 'Contacts', |
353ffa53 | 497 | ))) { |
6a488035 TO |
498 | if (!CRM_Core_Permission::giveMeAllACLs()) { |
499 | $skipMenuItems[] = $navID; | |
500 | return FALSE; | |
501 | } | |
502 | } | |
503 | ||
504 | $config = CRM_Core_Config::singleton(); | |
505 | ||
506 | $makeLink = FALSE; | |
507 | if (isset($url) && $url) { | |
8ce923d1 | 508 | if (substr($url, 0, 4) !== 'http') { |
6a488035 TO |
509 | //CRM-7656 --make sure to separate out url path from url params, |
510 | //as we'r going to validate url path across cross-site scripting. | |
710199c8 | 511 | $urlParam = explode('?', $url); |
512 | if (empty($urlParam[1])) { | |
513 | $urlParam[1] = NULL; | |
514 | } | |
1351950a | 515 | $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, TRUE); |
6a488035 TO |
516 | } |
517 | $makeLink = TRUE; | |
518 | } | |
519 | ||
520 | static $allComponents; | |
521 | if (!$allComponents) { | |
522 | $allComponents = CRM_Core_Component::getNames(); | |
523 | } | |
524 | ||
525 | if (isset($permission) && $permission) { | |
526 | $permissions = explode(',', $permission); | |
527 | ||
528 | $hasPermission = FALSE; | |
529 | foreach ($permissions as $key) { | |
530 | $key = trim($key); | |
531 | $showItem = TRUE; | |
532 | ||
533 | //get the component name from permission. | |
534 | $componentName = CRM_Core_Permission::getComponentName($key); | |
535 | ||
536 | if ($componentName) { | |
537 | if (!in_array($componentName, $config->enableComponents) || | |
538 | !CRM_Core_Permission::check($key) | |
539 | ) { | |
540 | $showItem = FALSE; | |
541 | if ($operator == 'AND') { | |
542 | $skipMenuItems[] = $navID; | |
543 | return $showItem; | |
544 | } | |
545 | } | |
546 | else { | |
547 | $hasPermission = TRUE; | |
548 | } | |
549 | } | |
550 | elseif (!CRM_Core_Permission::check($key)) { | |
551 | $showItem = FALSE; | |
552 | if ($operator == 'AND') { | |
553 | $skipMenuItems[] = $navID; | |
554 | return $showItem; | |
555 | } | |
556 | } | |
557 | else { | |
558 | $hasPermission = TRUE; | |
559 | } | |
560 | } | |
561 | ||
562 | if (!$showItem && !$hasPermission) { | |
563 | $skipMenuItems[] = $navID; | |
564 | return FALSE; | |
565 | } | |
566 | } | |
567 | ||
568 | if ($makeLink) { | |
569 | if ($target) { | |
570 | $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>"; | |
571 | } | |
572 | else { | |
573 | $name = "<a href=\"{$url}\">{$name}</a>"; | |
574 | } | |
575 | } | |
576 | ||
577 | return $name; | |
578 | } | |
579 | ||
580 | /** | |
fe482240 | 581 | * Create navigation for CiviCRM Admin Menu. |
6a488035 | 582 | * |
6a0b768e TO |
583 | * @param int $contactID |
584 | * Contact id. | |
6a488035 | 585 | * |
a6c01b45 CW |
586 | * @return string |
587 | * returns navigation html | |
6a488035 | 588 | */ |
00be9182 | 589 | public static function createNavigation($contactID) { |
6a488035 TO |
590 | $config = CRM_Core_Config::singleton(); |
591 | ||
73538697 | 592 | $navigation = self::buildNavigation(); |
6a488035 | 593 | |
73538697 | 594 | if ($navigation) { |
6a488035 TO |
595 | |
596 | //add additional navigation items | |
597 | $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1'); | |
6a488035 TO |
598 | |
599 | // get home menu from db | |
600 | $homeParams = array('name' => 'Home'); | |
601 | $homeNav = array(); | |
a2e1885c | 602 | $homeIcon = '<span class="crm-logo-sm" ></span>'; |
6a488035 TO |
603 | self::retrieve($homeParams, $homeNav); |
604 | if ($homeNav) { | |
710199c8 | 605 | list($path, $q) = explode('?', $homeNav['url']); |
6a488035 TO |
606 | $homeURL = CRM_Utils_System::url($path, $q); |
607 | $homeLabel = $homeNav['label']; | |
608 | // CRM-6804 (we need to special-case this as we don’t ts()-tag variables) | |
609 | if ($homeLabel == 'Home') { | |
a65693e5 | 610 | $homeLabel = ts('CiviCRM Home'); |
6a488035 TO |
611 | } |
612 | } | |
613 | else { | |
614 | $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
a65693e5 | 615 | $homeLabel = ts('CiviCRM Home'); |
6a488035 | 616 | } |
8960d9b9 | 617 | // Link to hide the menubar |
6a488035 TO |
618 | if ( |
619 | ($config->userSystem->is_drupal) && | |
620 | ((module_exists('toolbar') && user_access('access toolbar')) || | |
621 | module_exists('admin_menu') && user_access('access administration menu') | |
622 | ) | |
623 | ) { | |
8960d9b9 | 624 | $hideLabel = ts('Drupal Menu'); |
6a488035 TO |
625 | } |
626 | elseif ($config->userSystem->is_wordpress) { | |
8960d9b9 | 627 | $hideLabel = ts('WordPress Menu'); |
6a488035 TO |
628 | } |
629 | else { | |
8960d9b9 | 630 | $hideLabel = ts('Hide Menu'); |
6a488035 TO |
631 | } |
632 | ||
8960d9b9 CW |
633 | $prepandString = " |
634 | <li class='menumain crm-link-home'>$homeIcon | |
635 | <ul id='civicrm-home'> | |
636 | <li><a href='$homeURL'>$homeLabel</a></li> | |
637 | <li><a href='#' class='crm-hidemenu'>$hideLabel</a></li> | |
86bfa4f6 | 638 | <li><a href='$logoutURL' class='crm-logout-link'>" . ts('Log out') . "</a></li> |
8960d9b9 CW |
639 | </ul>"; |
640 | // <li> tag doesn't need to be closed | |
73538697 | 641 | } |
8960d9b9 | 642 | return $prepandString . $navigation; |
73538697 | 643 | } |
6a488035 | 644 | |
73538697 | 645 | /** |
fe482240 | 646 | * Reset navigation for all contacts or a specified contact. |
73538697 | 647 | * |
6a0b768e TO |
648 | * @param int $contactID |
649 | * Reset only entries belonging to that contact ID. | |
73538697 CW |
650 | * @return string |
651 | */ | |
00be9182 | 652 | public static function resetNavigation($contactID = NULL) { |
73538697 CW |
653 | $newKey = CRM_Utils_String::createRandom(self::CACHE_KEY_STRLEN, CRM_Utils_String::ALPHANUMERIC); |
654 | if (!$contactID) { | |
655 | $query = "UPDATE civicrm_setting SET value = '$newKey' WHERE name='navigation' AND contact_id IS NOT NULL"; | |
656 | CRM_Core_DAO::executeQuery($query); | |
657 | CRM_Core_BAO_Cache::deleteGroup('navigation'); | |
658 | } | |
659 | else { | |
6a488035 | 660 | // before inserting check if contact id exists in db |
73538697 | 661 | // this is to handle weird case when contact id is in session but not in db |
6a488035 TO |
662 | $contact = new CRM_Contact_DAO_Contact(); |
663 | $contact->id = $contactID; | |
664 | if ($contact->find(TRUE)) { | |
665 | CRM_Core_BAO_Setting::setItem( | |
73538697 | 666 | $newKey, |
6a488035 TO |
667 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, |
668 | 'navigation', | |
669 | NULL, | |
670 | $contactID, | |
671 | $contactID | |
672 | ); | |
673 | } | |
674 | } | |
6a488035 | 675 | // also reset the dashlet cache in case permissions have changed etc |
73538697 | 676 | // FIXME: decouple this |
6a488035 | 677 | CRM_Core_BAO_Dashboard::resetDashletCache($contactID); |
73538697 CW |
678 | |
679 | return $newKey; | |
6a488035 TO |
680 | } |
681 | ||
682 | /** | |
fe482240 | 683 | * Process navigation. |
6a488035 | 684 | * |
6a0b768e TO |
685 | * @param array $params |
686 | * Associated array, $_GET. | |
6a488035 TO |
687 | * |
688 | * @return void | |
6a488035 | 689 | */ |
00be9182 | 690 | public static function processNavigation(&$params) { |
353ffa53 TO |
691 | $nodeID = (int) str_replace("node_", "", $params['id']); |
692 | $referenceID = (int) str_replace("node_", "", $params['ref_id']); | |
693 | $position = $params['ps']; | |
694 | $type = $params['type']; | |
695 | $label = CRM_Utils_Array::value('data', $params); | |
6a488035 TO |
696 | |
697 | switch ($type) { | |
698 | case "move": | |
699 | self::processMove($nodeID, $referenceID, $position); | |
700 | break; | |
701 | ||
702 | case "rename": | |
703 | self::processRename($nodeID, $label); | |
704 | break; | |
705 | ||
706 | case "delete": | |
707 | self::processDelete($nodeID); | |
708 | break; | |
709 | } | |
710 | ||
711 | //reset navigation menus | |
712 | self::resetNavigation(); | |
713 | CRM_Utils_System::civiExit(); | |
714 | } | |
715 | ||
716 | /** | |
fe482240 | 717 | * Process move action. |
6a488035 | 718 | * |
6a0b768e TO |
719 | * @param $nodeID |
720 | * Node that is being moved. | |
721 | * @param $referenceID | |
722 | * Parent id where node is moved. 0 mean no parent. | |
723 | * @param $position | |
724 | * New position of the nod, it starts with 0 - n. | |
6a488035 TO |
725 | * |
726 | * @return void | |
6a488035 | 727 | */ |
00be9182 | 728 | public static function processMove($nodeID, $referenceID, $position) { |
6a488035 TO |
729 | // based on the new position we need to get the weight of the node after moved node |
730 | // 1. update the weight of $position + 1 nodes to weight + 1 | |
731 | // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved | |
732 | ||
733 | // check if there is parent id, which means node is moved inside existing parent container, so use parent id | |
734 | // to find the correct position else use NULL to get the weights of parent ( $position - 1 ) | |
735 | // accordingly set the new parent_id | |
736 | if ($referenceID) { | |
737 | $newParentID = $referenceID; | |
738 | $parentClause = "parent_id = {$referenceID} "; | |
739 | } | |
740 | else { | |
741 | $newParentID = 'NULL'; | |
742 | $parentClause = 'parent_id IS NULL'; | |
743 | } | |
744 | ||
4eeb9a5b | 745 | $incrementOtherNodes = TRUE; |
353ffa53 | 746 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; |
481a74f4 | 747 | $params = array(1 => array($position, 'Positive')); |
6a488035 TO |
748 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); |
749 | ||
750 | // this means node is moved to last position, so you need to get the weight of last element + 1 | |
751 | if (!$newWeight) { | |
752 | $lastPosition = $position - 1; | |
353ffa53 | 753 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; |
6a488035 TO |
754 | $params = array(1 => array($lastPosition, 'Positive')); |
755 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); | |
756 | ||
757 | // since last node increment + 1 | |
758 | $newWeight = $newWeight + 1; | |
759 | ||
760 | // since this is a last node we don't need to increment other nodes | |
ab8a593e | 761 | $incrementOtherNodes = FALSE; |
6a488035 TO |
762 | } |
763 | ||
764 | $transaction = new CRM_Core_Transaction(); | |
765 | ||
766 | // now update the existing nodes to weight + 1, if required. | |
481a74f4 | 767 | if ($incrementOtherNodes) { |
6a488035 TO |
768 | $query = "UPDATE civicrm_navigation SET weight = weight + 1 |
769 | WHERE {$parentClause} AND weight >= {$newWeight}"; | |
770 | ||
771 | CRM_Core_DAO::executeQuery($query); | |
772 | } | |
773 | ||
774 | // finally set the weight of current node | |
775 | $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}"; | |
776 | CRM_Core_DAO::executeQuery($query); | |
777 | ||
778 | $transaction->commit(); | |
779 | } | |
780 | ||
781 | /** | |
fe482240 | 782 | * Function to process rename action for tree. |
6a488035 | 783 | * |
100fef9d | 784 | * @param int $nodeID |
6a488035 TO |
785 | * @param $label |
786 | */ | |
00be9182 | 787 | public static function processRename($nodeID, $label) { |
6a488035 TO |
788 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label); |
789 | } | |
790 | ||
791 | /** | |
fe482240 | 792 | * Process delete action for tree. |
6a488035 | 793 | * |
100fef9d | 794 | * @param int $nodeID |
6a488035 | 795 | */ |
00be9182 | 796 | public static function processDelete($nodeID) { |
6a488035 TO |
797 | $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}"; |
798 | CRM_Core_DAO::executeQuery($query); | |
799 | } | |
800 | ||
801 | /** | |
fe482240 | 802 | * Get the info on navigation item. |
6a488035 | 803 | * |
6a0b768e TO |
804 | * @param int $navigationID |
805 | * Navigation id. | |
6a488035 | 806 | * |
a6c01b45 CW |
807 | * @return array |
808 | * associated array | |
6a488035 | 809 | */ |
00be9182 | 810 | public static function getNavigationInfo($navigationID) { |
353ffa53 | 811 | $query = "SELECT parent_id, weight FROM civicrm_navigation WHERE id = %1"; |
6a488035 | 812 | $params = array($navigationID, 'Integer'); |
353ffa53 | 813 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => $params)); |
6a488035 TO |
814 | $dao->fetch(); |
815 | return array( | |
816 | 'parent_id' => $dao->parent_id, | |
817 | 'weight' => $dao->weight, | |
818 | ); | |
819 | } | |
820 | ||
821 | /** | |
fe482240 | 822 | * Update menu. |
6a488035 | 823 | * |
6a0b768e TO |
824 | * @param array $params |
825 | * @param array $newParams | |
826 | * New value of params. | |
6a488035 | 827 | */ |
00be9182 | 828 | public static function processUpdate($params, $newParams) { |
6a488035 TO |
829 | $dao = new CRM_Core_DAO_Navigation(); |
830 | $dao->copyValues($params); | |
831 | if ($dao->find(TRUE)) { | |
832 | $dao->copyValues($newParams); | |
833 | $dao->save(); | |
834 | } | |
835 | } | |
73538697 | 836 | |
b5c2afd0 | 837 | /** |
100fef9d | 838 | * @param int $cid |
b5c2afd0 EM |
839 | * |
840 | * @return object|string | |
841 | */ | |
00be9182 | 842 | public static function getCacheKey($cid) { |
73538697 CW |
843 | $key = CRM_Core_BAO_Setting::getItem( |
844 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, | |
845 | 'navigation', | |
846 | NULL, | |
847 | '', | |
848 | $cid | |
849 | ); | |
850 | if (strlen($key) !== self::CACHE_KEY_STRLEN) { | |
851 | $key = self::resetNavigation($cid); | |
852 | } | |
853 | return $key; | |
854 | } | |
96025800 | 855 | |
6a488035 | 856 | } |