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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | /** |
100fef9d | 41 | * Class constructor |
6a488035 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | parent::__construct(); |
45 | } | |
46 | ||
47 | /** | |
100fef9d | 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 TO |
54 | * |
55 | * @return Object DAO object on sucess, null otherwise | |
56 | * | |
6a488035 TO |
57 | * @static |
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 TO |
65 | * |
66 | * @static | |
67 | * @return array associated array | |
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 TO |
86 | * |
87 | * @param array associated array of submitted values | |
88 | * | |
89 | * @return object navigation object | |
90 | * @static | |
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 | /** | |
c490a46a | 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 | * |
c490a46a | 133 | * @return CRM_Core_BAO_Navigation object on success, null otherwise |
6a488035 TO |
134 | * @static |
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 | /** | |
150 | * Calculate navigation weight | |
151 | * | |
6a0b768e TO |
152 | * @param $parentID |
153 | * Parent_id of a menu. | |
154 | * @param $menuID | |
155 | * Menu id. | |
6a488035 | 156 | * |
da6b46f4 | 157 | * @return int $weight string@static |
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 | /** | |
179 | * Get formatted menu list | |
180 | * | |
181 | * @return array $navigations returns associated array | |
182 | * @static | |
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 | /** | |
100fef9d | 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 | /** | |
100fef9d | 242 | * Helper function for getNavigationList( ) |
6a488035 | 243 | * |
6a0b768e TO |
244 | * @param string $val |
245 | * Menu name. | |
246 | * @param array $pidGroups | |
247 | * Parent menus. | |
6a488035 TO |
248 | * @return array |
249 | */ | |
00be9182 | 250 | public static function _getNavigationValue($val, &$pidGroups) { |
6a488035 TO |
251 | if (array_key_exists($val, $pidGroups)) { |
252 | $list = array('navigation_id' => $val); | |
253 | foreach ($pidGroups[$val] as $label => $id) { | |
254 | $list[$label] = self::_getNavigationValue($id, $pidGroups); | |
255 | } | |
256 | unset($pidGroups[$val]); | |
257 | return $list; | |
258 | } | |
259 | else { | |
260 | return $val; | |
261 | } | |
262 | } | |
263 | ||
264 | /** | |
100fef9d | 265 | * Build navigation tree |
6a488035 | 266 | * |
6a0b768e TO |
267 | * @param array $navigationTree |
268 | * Nested array of menus. | |
269 | * @param int $parentID | |
270 | * Parent id. | |
271 | * @param bool $navigationMenu | |
272 | * True when called for building top navigation menu. | |
6a488035 TO |
273 | * |
274 | * @return array $navigationTree nested array of menus | |
275 | * @static | |
276 | */ | |
00be9182 | 277 | public static function buildNavigationTree(&$navigationTree, $parentID, $navigationMenu = TRUE) { |
6a488035 TO |
278 | $whereClause = " parent_id IS NULL"; |
279 | ||
280 | if ($parentID) { | |
281 | $whereClause = " parent_id = {$parentID}"; | |
282 | } | |
283 | ||
284 | $domainID = CRM_Core_Config::domainID(); | |
285 | ||
286 | // get the list of menus | |
287 | $query = " | |
288 | SELECT id, label, url, permission, permission_operator, has_separator, parent_id, is_active, name | |
289 | FROM civicrm_navigation | |
290 | WHERE {$whereClause} | |
291 | AND domain_id = $domainID | |
292 | ORDER BY parent_id, weight"; | |
293 | ||
294 | $navigation = CRM_Core_DAO::executeQuery($query); | |
295 | $config = CRM_Core_Config::singleton(); | |
296 | while ($navigation->fetch()) { | |
297 | $label = $navigation->label; | |
298 | if (!$navigationMenu) { | |
299 | $label = addcslashes($label, '"'); | |
300 | } | |
301 | ||
302 | // for each menu get their children | |
303 | $navigationTree[$navigation->id] = array( | |
304 | 'attributes' => array('label' => $label, | |
305 | 'name' => $navigation->name, | |
306 | 'url' => $navigation->url, | |
307 | 'permission' => $navigation->permission, | |
308 | 'operator' => $navigation->permission_operator, | |
309 | 'separator' => $navigation->has_separator, | |
310 | 'parentID' => $navigation->parent_id, | |
311 | 'navID' => $navigation->id, | |
312 | 'active' => $navigation->is_active, | |
313 | )); | |
314 | self::buildNavigationTree($navigationTree[$navigation->id]['child'], $navigation->id, $navigationMenu); | |
315 | } | |
316 | ||
317 | return $navigationTree; | |
318 | } | |
319 | ||
320 | /** | |
100fef9d | 321 | * Build menu |
6a488035 | 322 | * |
6a0b768e TO |
323 | * @param bool $json |
324 | * By default output is html. | |
325 | * @param bool $navigationMenu | |
326 | * True when called for building top navigation menu. | |
6a488035 TO |
327 | * |
328 | * @return returns html or json object | |
329 | * @static | |
330 | */ | |
00be9182 | 331 | public static function buildNavigation($json = FALSE, $navigationMenu = TRUE) { |
6a488035 TO |
332 | $navigations = array(); |
333 | self::buildNavigationTree($navigations, $parent = NULL, $navigationMenu); | |
334 | $navigationString = NULL; | |
335 | ||
336 | // run the Navigation through a hook so users can modify it | |
337 | CRM_Utils_Hook::navigationMenu($navigations); | |
338 | ||
339 | $i18n = CRM_Core_I18n::singleton(); | |
340 | ||
341 | //skip children menu item if user don't have access to parent menu item | |
342 | $skipMenuItems = array(); | |
343 | foreach ($navigations as $key => $value) { | |
344 | if ($json) { | |
345 | if ($navigationString) { | |
346 | $navigationString .= '},'; | |
347 | } | |
348 | $data = $value['attributes']['label']; | |
349 | $class = ''; | |
350 | if (!$value['attributes']['active']) { | |
351 | $class = ', "attr": { "class" : "disabled"} '; | |
352 | } | |
353 | $l10nName = $i18n->crm_translate($data, array('context' => 'menu')); | |
354 | $navigationString .= ' { "attr": { "id" : "node_' . $key . '"}, "data": { "title":"' . $l10nName . '"' . $class . '}'; | |
355 | } | |
356 | else { | |
357 | // Home is a special case | |
358 | if ($value['attributes']['name'] != 'Home') { | |
359 | $name = self::getMenuName($value, $skipMenuItems); | |
360 | if ($name) { | |
361 | //separator before | |
362 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) { | |
363 | $navigationString .= '<li class="menu-separator"></li>'; | |
364 | } | |
365 | $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); | |
366 | $navigationString .= '<li class="menumain crm-' . str_replace($removeCharacters, '_', $value['attributes']['label']) . '">' . $name; | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
371 | self::recurseNavigation($value, $navigationString, $json, $skipMenuItems); | |
372 | } | |
373 | ||
374 | if ($json) { | |
375 | $navigationString = '[' . $navigationString . '}]'; | |
376 | } | |
377 | else { | |
378 | // clean up - Need to remove empty <ul>'s, this happens when user don't have | |
379 | // permission to access parent | |
380 | $navigationString = str_replace('<ul></ul></li>', '', $navigationString); | |
381 | } | |
382 | ||
383 | return $navigationString; | |
384 | } | |
385 | ||
386 | /** | |
387 | * Recursively check child menus | |
388 | * | |
389 | * @param array $value | |
390 | * @param string $navigationString | |
6a0b768e TO |
391 | * @param bool $json |
392 | * @param bool $skipMenuItems | |
6a488035 TO |
393 | * @return string |
394 | */ | |
00be9182 | 395 | public static function recurseNavigation(&$value, &$navigationString, $json, $skipMenuItems) { |
6a488035 TO |
396 | if ($json) { |
397 | if (!empty($value['child'])) { | |
398 | $navigationString .= ', "children": [ '; | |
399 | } | |
400 | else { | |
401 | return $navigationString; | |
402 | } | |
403 | ||
404 | if (!empty($value['child'])) { | |
405 | $appendComma = TRUE; | |
406 | $count = 1; | |
407 | foreach ($value['child'] as $k => $val) { | |
408 | if ($count == count($value['child'])) { | |
409 | $appendComma = FALSE; | |
410 | } | |
411 | $data = $val['attributes']['label']; | |
412 | $class = ''; | |
413 | if (!$val['attributes']['active']) { | |
414 | $class = ', "attr": { "class" : "disabled"} '; | |
415 | } | |
416 | $navigationString .= ' { "attr": { "id" : "node_' . $k . '"}, "data": { "title":"' . $data . '"' . $class . '}'; | |
417 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
418 | $navigationString .= $appendComma ? ' },' : ' }'; | |
419 | $count++; | |
420 | } | |
421 | } | |
422 | ||
423 | if (!empty($value['child'])) { | |
424 | $navigationString .= ' ]'; | |
425 | } | |
426 | } | |
427 | else { | |
428 | if (!empty($value['child'])) { | |
429 | $navigationString .= '<ul>'; | |
430 | } | |
431 | else { | |
432 | $navigationString .= '</li>'; | |
433 | //locate separator after | |
434 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
435 | $navigationString .= '<li class="menu-separator"></li>'; | |
436 | } | |
437 | } | |
438 | ||
439 | if (!empty($value['child'])) { | |
440 | foreach ($value['child'] as $val) { | |
441 | $name = self::getMenuName($val, $skipMenuItems); | |
442 | if ($name) { | |
443 | //locate separator before | |
444 | if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) { | |
445 | $navigationString .= '<li class="menu-separator"></li>'; | |
446 | } | |
447 | $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); | |
448 | $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name; | |
449 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
450 | } | |
451 | } | |
452 | } | |
453 | if (!empty($value['child'])) { | |
454 | $navigationString .= '</ul></li>'; | |
455 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
456 | $navigationString .= '<li class="menu-separator"></li>'; | |
457 | } | |
458 | } | |
459 | } | |
460 | return $navigationString; | |
461 | } | |
462 | ||
463 | /** | |
464 | * Get Menu name | |
465 | * | |
466 | * @param $value | |
100fef9d | 467 | * @param array $skipMenuItems |
6a488035 TO |
468 | * @return bool|string |
469 | */ | |
00be9182 | 470 | public static function getMenuName(&$value, &$skipMenuItems) { |
6a488035 TO |
471 | // we need to localise the menu labels (CRM-5456) and don’t |
472 | // want to use ts() as it would throw the ts-extractor off | |
473 | $i18n = CRM_Core_I18n::singleton(); | |
474 | ||
475 | $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu')); | |
476 | $url = $value['attributes']['url']; | |
477 | $permission = $value['attributes']['permission']; | |
478 | $operator = $value['attributes']['operator']; | |
479 | $parentID = $value['attributes']['parentID']; | |
480 | $navID = $value['attributes']['navID']; | |
481 | $active = $value['attributes']['active']; | |
482 | $menuName = $value['attributes']['name']; | |
483 | $target = CRM_Utils_Array::value('target', $value['attributes']); | |
484 | ||
485 | if (in_array($parentID, $skipMenuItems) || !$active) { | |
486 | $skipMenuItems[] = $navID; | |
487 | return FALSE; | |
488 | } | |
489 | ||
490 | //we need to check core view/edit or supported acls. | |
491 | if (in_array($menuName, array( | |
492 | 'Search...', 'Contacts'))) { | |
493 | if (!CRM_Core_Permission::giveMeAllACLs()) { | |
494 | $skipMenuItems[] = $navID; | |
495 | return FALSE; | |
496 | } | |
497 | } | |
498 | ||
499 | $config = CRM_Core_Config::singleton(); | |
500 | ||
501 | $makeLink = FALSE; | |
502 | if (isset($url) && $url) { | |
8ce923d1 | 503 | if (substr($url, 0, 4) !== 'http') { |
6a488035 TO |
504 | //CRM-7656 --make sure to separate out url path from url params, |
505 | //as we'r going to validate url path across cross-site scripting. | |
710199c8 | 506 | $urlParam = explode('?', $url); |
507 | if (empty($urlParam[1])) { | |
508 | $urlParam[1] = NULL; | |
509 | } | |
1351950a | 510 | $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, TRUE); |
6a488035 TO |
511 | } |
512 | $makeLink = TRUE; | |
513 | } | |
514 | ||
515 | static $allComponents; | |
516 | if (!$allComponents) { | |
517 | $allComponents = CRM_Core_Component::getNames(); | |
518 | } | |
519 | ||
520 | if (isset($permission) && $permission) { | |
521 | $permissions = explode(',', $permission); | |
522 | ||
523 | $hasPermission = FALSE; | |
524 | foreach ($permissions as $key) { | |
525 | $key = trim($key); | |
526 | $showItem = TRUE; | |
527 | ||
528 | //get the component name from permission. | |
529 | $componentName = CRM_Core_Permission::getComponentName($key); | |
530 | ||
531 | if ($componentName) { | |
532 | if (!in_array($componentName, $config->enableComponents) || | |
533 | !CRM_Core_Permission::check($key) | |
534 | ) { | |
535 | $showItem = FALSE; | |
536 | if ($operator == 'AND') { | |
537 | $skipMenuItems[] = $navID; | |
538 | return $showItem; | |
539 | } | |
540 | } | |
541 | else { | |
542 | $hasPermission = TRUE; | |
543 | } | |
544 | } | |
545 | elseif (!CRM_Core_Permission::check($key)) { | |
546 | $showItem = FALSE; | |
547 | if ($operator == 'AND') { | |
548 | $skipMenuItems[] = $navID; | |
549 | return $showItem; | |
550 | } | |
551 | } | |
552 | else { | |
553 | $hasPermission = TRUE; | |
554 | } | |
555 | } | |
556 | ||
557 | if (!$showItem && !$hasPermission) { | |
558 | $skipMenuItems[] = $navID; | |
559 | return FALSE; | |
560 | } | |
561 | } | |
562 | ||
563 | if ($makeLink) { | |
564 | if ($target) { | |
565 | $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>"; | |
566 | } | |
567 | else { | |
568 | $name = "<a href=\"{$url}\">{$name}</a>"; | |
569 | } | |
570 | } | |
571 | ||
572 | return $name; | |
573 | } | |
574 | ||
575 | /** | |
100fef9d | 576 | * Create navigation for CiviCRM Admin Menu |
6a488035 | 577 | * |
6a0b768e TO |
578 | * @param int $contactID |
579 | * Contact id. | |
6a488035 TO |
580 | * |
581 | * @return string $navigation returns navigation html | |
582 | * @static | |
583 | */ | |
00be9182 | 584 | public static function createNavigation($contactID) { |
6a488035 TO |
585 | $config = CRM_Core_Config::singleton(); |
586 | ||
73538697 | 587 | $navigation = self::buildNavigation(); |
6a488035 | 588 | |
73538697 | 589 | if ($navigation) { |
6a488035 TO |
590 | |
591 | //add additional navigation items | |
592 | $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1'); | |
6a488035 TO |
593 | |
594 | // get home menu from db | |
595 | $homeParams = array('name' => 'Home'); | |
596 | $homeNav = array(); | |
8960d9b9 | 597 | $homeIcon = '<img src="' . $config->userFrameworkResourceURL . 'i/logo16px.png" style="vertical-align:middle;" />'; |
6a488035 TO |
598 | self::retrieve($homeParams, $homeNav); |
599 | if ($homeNav) { | |
710199c8 | 600 | list($path, $q) = explode('?', $homeNav['url']); |
6a488035 TO |
601 | $homeURL = CRM_Utils_System::url($path, $q); |
602 | $homeLabel = $homeNav['label']; | |
603 | // CRM-6804 (we need to special-case this as we don’t ts()-tag variables) | |
604 | if ($homeLabel == 'Home') { | |
a65693e5 | 605 | $homeLabel = ts('CiviCRM Home'); |
6a488035 TO |
606 | } |
607 | } | |
608 | else { | |
609 | $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
a65693e5 | 610 | $homeLabel = ts('CiviCRM Home'); |
6a488035 | 611 | } |
8960d9b9 | 612 | // Link to hide the menubar |
6a488035 TO |
613 | if ( |
614 | ($config->userSystem->is_drupal) && | |
615 | ((module_exists('toolbar') && user_access('access toolbar')) || | |
616 | module_exists('admin_menu') && user_access('access administration menu') | |
617 | ) | |
618 | ) { | |
8960d9b9 | 619 | $hideLabel = ts('Drupal Menu'); |
6a488035 TO |
620 | } |
621 | elseif ($config->userSystem->is_wordpress) { | |
8960d9b9 | 622 | $hideLabel = ts('WordPress Menu'); |
6a488035 TO |
623 | } |
624 | else { | |
8960d9b9 | 625 | $hideLabel = ts('Hide Menu'); |
6a488035 TO |
626 | } |
627 | ||
8960d9b9 CW |
628 | $prepandString = " |
629 | <li class='menumain crm-link-home'>$homeIcon | |
630 | <ul id='civicrm-home'> | |
631 | <li><a href='$homeURL'>$homeLabel</a></li> | |
632 | <li><a href='#' class='crm-hidemenu'>$hideLabel</a></li> | |
92fcb95f | 633 | <li><a href='$logoutURL' class='crm-logout-link'>" . ts('Log out') . "</a></li> |
8960d9b9 CW |
634 | </ul>"; |
635 | // <li> tag doesn't need to be closed | |
73538697 | 636 | } |
8960d9b9 | 637 | return $prepandString . $navigation; |
73538697 | 638 | } |
6a488035 | 639 | |
73538697 CW |
640 | /** |
641 | * Reset navigation for all contacts or a specified contact | |
642 | * | |
6a0b768e TO |
643 | * @param int $contactID |
644 | * Reset only entries belonging to that contact ID. | |
73538697 CW |
645 | * @return string |
646 | */ | |
00be9182 | 647 | public static function resetNavigation($contactID = NULL) { |
73538697 CW |
648 | $newKey = CRM_Utils_String::createRandom(self::CACHE_KEY_STRLEN, CRM_Utils_String::ALPHANUMERIC); |
649 | if (!$contactID) { | |
650 | $query = "UPDATE civicrm_setting SET value = '$newKey' WHERE name='navigation' AND contact_id IS NOT NULL"; | |
651 | CRM_Core_DAO::executeQuery($query); | |
652 | CRM_Core_BAO_Cache::deleteGroup('navigation'); | |
653 | } | |
654 | else { | |
6a488035 | 655 | // before inserting check if contact id exists in db |
73538697 | 656 | // this is to handle weird case when contact id is in session but not in db |
6a488035 TO |
657 | $contact = new CRM_Contact_DAO_Contact(); |
658 | $contact->id = $contactID; | |
659 | if ($contact->find(TRUE)) { | |
660 | CRM_Core_BAO_Setting::setItem( | |
73538697 | 661 | $newKey, |
6a488035 TO |
662 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, |
663 | 'navigation', | |
664 | NULL, | |
665 | $contactID, | |
666 | $contactID | |
667 | ); | |
668 | } | |
669 | } | |
6a488035 | 670 | // also reset the dashlet cache in case permissions have changed etc |
73538697 | 671 | // FIXME: decouple this |
6a488035 | 672 | CRM_Core_BAO_Dashboard::resetDashletCache($contactID); |
73538697 CW |
673 | |
674 | return $newKey; | |
6a488035 TO |
675 | } |
676 | ||
677 | /** | |
100fef9d | 678 | * Process navigation |
6a488035 | 679 | * |
6a0b768e TO |
680 | * @param array $params |
681 | * Associated array, $_GET. | |
6a488035 TO |
682 | * |
683 | * @return void | |
684 | * @static | |
685 | */ | |
00be9182 | 686 | public static function processNavigation(&$params) { |
6a488035 TO |
687 | $nodeID = (int)str_replace("node_", "", $params['id']); |
688 | $referenceID = (int)str_replace("node_", "", $params['ref_id']); | |
689 | $position = $params['ps']; | |
690 | $type = $params['type']; | |
691 | $label = CRM_Utils_Array::value('data', $params); | |
692 | ||
693 | switch ($type) { | |
694 | case "move": | |
695 | self::processMove($nodeID, $referenceID, $position); | |
696 | break; | |
697 | ||
698 | case "rename": | |
699 | self::processRename($nodeID, $label); | |
700 | break; | |
701 | ||
702 | case "delete": | |
703 | self::processDelete($nodeID); | |
704 | break; | |
705 | } | |
706 | ||
707 | //reset navigation menus | |
708 | self::resetNavigation(); | |
709 | CRM_Utils_System::civiExit(); | |
710 | } | |
711 | ||
712 | /** | |
100fef9d | 713 | * Process move action |
6a488035 | 714 | * |
6a0b768e TO |
715 | * @param $nodeID |
716 | * Node that is being moved. | |
717 | * @param $referenceID | |
718 | * Parent id where node is moved. 0 mean no parent. | |
719 | * @param $position | |
720 | * New position of the nod, it starts with 0 - n. | |
6a488035 TO |
721 | * |
722 | * @return void | |
723 | * @static | |
724 | */ | |
00be9182 | 725 | public static function processMove($nodeID, $referenceID, $position) { |
6a488035 TO |
726 | // based on the new position we need to get the weight of the node after moved node |
727 | // 1. update the weight of $position + 1 nodes to weight + 1 | |
728 | // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved | |
729 | ||
730 | // check if there is parent id, which means node is moved inside existing parent container, so use parent id | |
731 | // to find the correct position else use NULL to get the weights of parent ( $position - 1 ) | |
732 | // accordingly set the new parent_id | |
733 | if ($referenceID) { | |
734 | $newParentID = $referenceID; | |
735 | $parentClause = "parent_id = {$referenceID} "; | |
736 | } | |
737 | else { | |
738 | $newParentID = 'NULL'; | |
739 | $parentClause = 'parent_id IS NULL'; | |
740 | } | |
741 | ||
742 | $incrementOtherNodes = true; | |
743 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; | |
481a74f4 | 744 | $params = array(1 => array($position, 'Positive')); |
6a488035 TO |
745 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); |
746 | ||
747 | // this means node is moved to last position, so you need to get the weight of last element + 1 | |
748 | if (!$newWeight) { | |
749 | $lastPosition = $position - 1; | |
750 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; | |
751 | $params = array(1 => array($lastPosition, 'Positive')); | |
752 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); | |
753 | ||
754 | // since last node increment + 1 | |
755 | $newWeight = $newWeight + 1; | |
756 | ||
757 | // since this is a last node we don't need to increment other nodes | |
758 | $incrementOtherNodes = false; | |
759 | } | |
760 | ||
761 | $transaction = new CRM_Core_Transaction(); | |
762 | ||
763 | // now update the existing nodes to weight + 1, if required. | |
481a74f4 | 764 | if ($incrementOtherNodes) { |
6a488035 TO |
765 | $query = "UPDATE civicrm_navigation SET weight = weight + 1 |
766 | WHERE {$parentClause} AND weight >= {$newWeight}"; | |
767 | ||
768 | CRM_Core_DAO::executeQuery($query); | |
769 | } | |
770 | ||
771 | // finally set the weight of current node | |
772 | $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}"; | |
773 | CRM_Core_DAO::executeQuery($query); | |
774 | ||
775 | $transaction->commit(); | |
776 | } | |
777 | ||
778 | /** | |
779 | * Function to process rename action for tree | |
780 | * | |
100fef9d | 781 | * @param int $nodeID |
6a488035 TO |
782 | * @param $label |
783 | */ | |
00be9182 | 784 | public static function processRename($nodeID, $label) { |
6a488035 TO |
785 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label); |
786 | } | |
787 | ||
788 | /** | |
100fef9d | 789 | * Process delete action for tree |
6a488035 | 790 | * |
100fef9d | 791 | * @param int $nodeID |
6a488035 | 792 | */ |
00be9182 | 793 | public static function processDelete($nodeID) { |
6a488035 TO |
794 | $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}"; |
795 | CRM_Core_DAO::executeQuery($query); | |
796 | } | |
797 | ||
798 | /** | |
100fef9d | 799 | * Get the info on navigation item |
6a488035 | 800 | * |
6a0b768e TO |
801 | * @param int $navigationID |
802 | * Navigation id. | |
6a488035 TO |
803 | * |
804 | * @return array associated array | |
805 | * @static | |
806 | */ | |
00be9182 | 807 | public static function getNavigationInfo($navigationID) { |
6a488035 TO |
808 | $query = "SELECT parent_id, weight FROM civicrm_navigation WHERE id = %1"; |
809 | $params = array($navigationID, 'Integer'); | |
810 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => $params)); | |
811 | $dao->fetch(); | |
812 | return array( | |
813 | 'parent_id' => $dao->parent_id, | |
814 | 'weight' => $dao->weight, | |
815 | ); | |
816 | } | |
817 | ||
818 | /** | |
100fef9d | 819 | * Update menu |
6a488035 | 820 | * |
6a0b768e TO |
821 | * @param array $params |
822 | * @param array $newParams | |
823 | * New value of params. | |
6a488035 TO |
824 | * @static |
825 | */ | |
00be9182 | 826 | public static function processUpdate($params, $newParams) { |
6a488035 TO |
827 | $dao = new CRM_Core_DAO_Navigation(); |
828 | $dao->copyValues($params); | |
829 | if ($dao->find(TRUE)) { | |
830 | $dao->copyValues($newParams); | |
831 | $dao->save(); | |
832 | } | |
833 | } | |
73538697 | 834 | |
b5c2afd0 | 835 | /** |
100fef9d | 836 | * @param int $cid |
b5c2afd0 EM |
837 | * |
838 | * @return object|string | |
839 | */ | |
00be9182 | 840 | public static function getCacheKey($cid) { |
73538697 CW |
841 | $key = CRM_Core_BAO_Setting::getItem( |
842 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, | |
843 | 'navigation', | |
844 | NULL, | |
845 | '', | |
846 | $cid | |
847 | ); | |
848 | if (strlen($key) !== self::CACHE_KEY_STRLEN) { | |
849 | $key = self::resetNavigation($cid); | |
850 | } | |
851 | return $key; | |
852 | } | |
6a488035 | 853 | } |