Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * update the is_active flag in the db | |
46 | * | |
47 | * @param int $id id of the database record | |
48 | * @param boolean $is_active value we want to set the is_active field | |
49 | * | |
50 | * @return Object DAO object on sucess, null otherwise | |
51 | * | |
52 | * @access public | |
53 | * @static | |
54 | */ | |
55 | static function setIsActive($id, $is_active) { | |
56 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $id, 'is_active', $is_active); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Function to get existing / build navigation for CiviCRM Admin Menu | |
61 | * | |
62 | * @static | |
63 | * @return array associated array | |
64 | */ | |
65 | static function getMenus() { | |
66 | $menus = array(); | |
67 | ||
68 | $menu = new CRM_Core_DAO_Menu(); | |
69 | $menu->domain_id = CRM_Core_Config::domainID(); | |
70 | $menu->find(); | |
71 | ||
72 | while ($menu->fetch()) { | |
73 | if ($menu->title) { | |
74 | $menus[$menu->path] = $menu->title; | |
75 | } | |
76 | } | |
77 | return $menus; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Function to add/update navigation record | |
82 | * | |
83 | * @param array associated array of submitted values | |
84 | * | |
85 | * @return object navigation object | |
86 | * @static | |
87 | */ | |
88 | static function add(&$params) { | |
89 | $navigation = new CRM_Core_DAO_Navigation(); | |
90 | ||
91 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
92 | $params['has_separator'] = CRM_Utils_Array::value('has_separator', $params, FALSE); | |
93 | ||
94 | if (!isset($params['id']) || | |
95 | (CRM_Utils_Array::value( 'parent_id', $params ) != CRM_Utils_Array::value('current_parent_id', $params)) | |
96 | ) { | |
97 | /* re/calculate the weight, if the Parent ID changed OR create new menu */ | |
98 | ||
99 | if ($navName = CRM_Utils_Array::value('name', $params)) { | |
100 | $params['name'] = $navName; | |
101 | } | |
c5e077b2 PJ |
102 | elseif ($navLabel = CRM_Utils_Array::value('label', $params)) { |
103 | $params['name'] = $navLabel; | |
6a488035 TO |
104 | } |
105 | ||
106 | $params['weight'] = self::calculateWeight(CRM_Utils_Array::value('parent_id', $params)); | |
107 | } | |
108 | ||
c5e077b2 | 109 | if (array_key_exists('permission', $params) && is_array($params['permission'])) { |
6a488035 TO |
110 | $params['permission'] = implode(',', $params['permission']); |
111 | } | |
112 | ||
113 | $navigation->copyValues($params); | |
114 | ||
115 | $navigation->domain_id = CRM_Core_Config::domainID(); | |
116 | ||
117 | $navigation->save(); | |
118 | return $navigation; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Takes a bunch of params that are needed to match certain criteria and | |
123 | * retrieves the relevant objects. Typically the valid params are only | |
124 | * contact_id. We'll tweak this function to be more full featured over a period | |
125 | * of time. This is the inverse function of create. It also stores all the retrieved | |
126 | * values in the default array | |
127 | * | |
128 | * @param array $params (reference ) an assoc array of name/value pairs | |
129 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
130 | * | |
131 | * @return object CRM_Core_BAO_Navigation object on success, null otherwise | |
132 | * @access public | |
133 | * @static | |
134 | */ | |
135 | static function retrieve(&$params, &$defaults) { | |
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 | /** | |
149 | * Calculate navigation weight | |
150 | * | |
151 | * @param $parentID parent_id of a menu | |
152 | * @param $menuID menu id | |
153 | * | |
154 | * @return $weight string | |
155 | * @static | |
156 | */ | |
157 | static function calculateWeight($parentID = NULL, $menuID = NULL) { | |
158 | $domainID = CRM_Core_Config::domainID(); | |
159 | ||
160 | $weight = 1; | |
161 | // we reset weight for each parent, i.e we start from 1 to n | |
162 | // calculate max weight for top level menus, if parent id is absent | |
163 | if (!$parentID) { | |
164 | $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id IS NULL AND domain_id = $domainID"; | |
165 | } | |
166 | else { | |
167 | // if parent is passed, we need to get max weight for that particular parent | |
168 | $query = "SELECT max(weight) as weight FROM civicrm_navigation WHERE parent_id = {$parentID} AND domain_id = $domainID"; | |
169 | } | |
170 | ||
171 | $dao = CRM_Core_DAO::executeQuery($query); | |
172 | $dao->fetch(); | |
173 | return $weight = $weight + $dao->weight; | |
174 | } | |
175 | ||
176 | /** | |
177 | * Get formatted menu list | |
178 | * | |
179 | * @return array $navigations returns associated array | |
180 | * @static | |
181 | */ | |
182 | static function getNavigationList() { | |
183 | $cacheKeyString = "navigationList"; | |
184 | $whereClause = ''; | |
185 | ||
186 | $config = CRM_Core_Config::singleton(); | |
187 | ||
188 | // check if we can retrieve from database cache | |
189 | $navigations = CRM_Core_BAO_Cache::getItem('navigation', $cacheKeyString); | |
190 | ||
191 | if (!$navigations) { | |
192 | $domainID = CRM_Core_Config::domainID(); | |
193 | $query = " | |
194 | SELECT id, label, parent_id, weight, is_active, name | |
195 | FROM civicrm_navigation WHERE domain_id = $domainID {$whereClause} ORDER BY parent_id, weight ASC"; | |
196 | $result = CRM_Core_DAO::executeQuery($query); | |
197 | ||
198 | $pidGroups = array(); | |
199 | while ($result->fetch()) { | |
200 | $pidGroups[$result->parent_id][$result->label] = $result->id; | |
201 | } | |
202 | ||
203 | foreach ($pidGroups[''] as $label => $val) { | |
204 | $pidGroups[''][$label] = self::_getNavigationValue($val, $pidGroups); | |
205 | } | |
206 | ||
207 | $navigations = array(); | |
208 | self::_getNavigationLabel($pidGroups[''], $navigations); | |
209 | ||
210 | CRM_Core_BAO_Cache::setItem($navigations, 'navigation', $cacheKeyString); | |
211 | } | |
212 | return $navigations; | |
213 | } | |
214 | ||
215 | /** | |
216 | * helper function for getNavigationList( ) | |
217 | * | |
218 | * @param array $list menu info | |
219 | * @param array $navigations navigation menus | |
220 | * @param string $separator menu separator | |
221 | */ | |
222 | static function _getNavigationLabel($list, &$navigations, $separator = '') { | |
223 | $i18n = CRM_Core_I18n::singleton(); | |
224 | foreach ($list as $label => $val) { | |
225 | if ($label == 'navigation_id') { | |
226 | continue; | |
227 | } | |
228 | $translatedLabel = $i18n->crm_translate($label, array('context' => 'menu')); | |
229 | $navigations[is_array($val) ? $val['navigation_id'] : $val] = "{$separator}{$translatedLabel}"; | |
230 | if (is_array($val)) { | |
231 | self::_getNavigationLabel($val, $navigations, $separator . ' '); | |
232 | } | |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
237 | * helper function for getNavigationList( ) | |
238 | * | |
239 | * @param string $val menu name | |
240 | * @param array $pidGroups parent menus | |
241 | * @return array | |
242 | */ | |
243 | static function _getNavigationValue($val, &$pidGroups) { | |
244 | if (array_key_exists($val, $pidGroups)) { | |
245 | $list = array('navigation_id' => $val); | |
246 | foreach ($pidGroups[$val] as $label => $id) { | |
247 | $list[$label] = self::_getNavigationValue($id, $pidGroups); | |
248 | } | |
249 | unset($pidGroups[$val]); | |
250 | return $list; | |
251 | } | |
252 | else { | |
253 | return $val; | |
254 | } | |
255 | } | |
256 | ||
257 | /** | |
258 | * Function to build navigation tree | |
259 | * | |
260 | * @param array $navigationTree nested array of menus | |
261 | * @param int $parentID parent id | |
262 | * @param boolean $navigationMenu true when called for building top navigation menu | |
263 | * | |
264 | * @return array $navigationTree nested array of menus | |
265 | * @static | |
266 | */ | |
267 | static function buildNavigationTree(&$navigationTree, $parentID, $navigationMenu = TRUE) { | |
268 | $whereClause = " parent_id IS NULL"; | |
269 | ||
270 | if ($parentID) { | |
271 | $whereClause = " parent_id = {$parentID}"; | |
272 | } | |
273 | ||
274 | $domainID = CRM_Core_Config::domainID(); | |
275 | ||
276 | // get the list of menus | |
277 | $query = " | |
278 | SELECT id, label, url, permission, permission_operator, has_separator, parent_id, is_active, name | |
279 | FROM civicrm_navigation | |
280 | WHERE {$whereClause} | |
281 | AND domain_id = $domainID | |
282 | ORDER BY parent_id, weight"; | |
283 | ||
284 | $navigation = CRM_Core_DAO::executeQuery($query); | |
285 | $config = CRM_Core_Config::singleton(); | |
286 | while ($navigation->fetch()) { | |
287 | $label = $navigation->label; | |
288 | if (!$navigationMenu) { | |
289 | $label = addcslashes($label, '"'); | |
290 | } | |
291 | ||
292 | // for each menu get their children | |
293 | $navigationTree[$navigation->id] = array( | |
294 | 'attributes' => array('label' => $label, | |
295 | 'name' => $navigation->name, | |
296 | 'url' => $navigation->url, | |
297 | 'permission' => $navigation->permission, | |
298 | 'operator' => $navigation->permission_operator, | |
299 | 'separator' => $navigation->has_separator, | |
300 | 'parentID' => $navigation->parent_id, | |
301 | 'navID' => $navigation->id, | |
302 | 'active' => $navigation->is_active, | |
303 | )); | |
304 | self::buildNavigationTree($navigationTree[$navigation->id]['child'], $navigation->id, $navigationMenu); | |
305 | } | |
306 | ||
307 | return $navigationTree; | |
308 | } | |
309 | ||
310 | /** | |
311 | * Function to build menu | |
312 | * | |
313 | * @param boolean $json by default output is html | |
314 | * @param boolean $navigationMenu true when called for building top navigation menu | |
315 | * | |
316 | * @return returns html or json object | |
317 | * @static | |
318 | */ | |
319 | static function buildNavigation($json = FALSE, $navigationMenu = TRUE) { | |
320 | $navigations = array(); | |
321 | self::buildNavigationTree($navigations, $parent = NULL, $navigationMenu); | |
322 | $navigationString = NULL; | |
323 | ||
324 | // run the Navigation through a hook so users can modify it | |
325 | CRM_Utils_Hook::navigationMenu($navigations); | |
326 | ||
327 | $i18n = CRM_Core_I18n::singleton(); | |
328 | ||
329 | //skip children menu item if user don't have access to parent menu item | |
330 | $skipMenuItems = array(); | |
331 | foreach ($navigations as $key => $value) { | |
332 | if ($json) { | |
333 | if ($navigationString) { | |
334 | $navigationString .= '},'; | |
335 | } | |
336 | $data = $value['attributes']['label']; | |
337 | $class = ''; | |
338 | if (!$value['attributes']['active']) { | |
339 | $class = ', "attr": { "class" : "disabled"} '; | |
340 | } | |
341 | $l10nName = $i18n->crm_translate($data, array('context' => 'menu')); | |
342 | $navigationString .= ' { "attr": { "id" : "node_' . $key . '"}, "data": { "title":"' . $l10nName . '"' . $class . '}'; | |
343 | } | |
344 | else { | |
345 | // Home is a special case | |
346 | if ($value['attributes']['name'] != 'Home') { | |
347 | $name = self::getMenuName($value, $skipMenuItems); | |
348 | if ($name) { | |
349 | //separator before | |
350 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 2) { | |
351 | $navigationString .= '<li class="menu-separator"></li>'; | |
352 | } | |
353 | $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); | |
354 | $navigationString .= '<li class="menumain crm-' . str_replace($removeCharacters, '_', $value['attributes']['label']) . '">' . $name; | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
359 | self::recurseNavigation($value, $navigationString, $json, $skipMenuItems); | |
360 | } | |
361 | ||
362 | if ($json) { | |
363 | $navigationString = '[' . $navigationString . '}]'; | |
364 | } | |
365 | else { | |
366 | // clean up - Need to remove empty <ul>'s, this happens when user don't have | |
367 | // permission to access parent | |
368 | $navigationString = str_replace('<ul></ul></li>', '', $navigationString); | |
369 | } | |
370 | ||
371 | return $navigationString; | |
372 | } | |
373 | ||
374 | /** | |
375 | * Recursively check child menus | |
376 | * | |
377 | * @param array $value | |
378 | * @param string $navigationString | |
379 | * @param boolean $json | |
380 | * @param boolean $skipMenuItems | |
381 | * @return string | |
382 | */ | |
383 | static function recurseNavigation(&$value, &$navigationString, $json, $skipMenuItems) { | |
384 | if ($json) { | |
385 | if (!empty($value['child'])) { | |
386 | $navigationString .= ', "children": [ '; | |
387 | } | |
388 | else { | |
389 | return $navigationString; | |
390 | } | |
391 | ||
392 | if (!empty($value['child'])) { | |
393 | $appendComma = TRUE; | |
394 | $count = 1; | |
395 | foreach ($value['child'] as $k => $val) { | |
396 | if ($count == count($value['child'])) { | |
397 | $appendComma = FALSE; | |
398 | } | |
399 | $data = $val['attributes']['label']; | |
400 | $class = ''; | |
401 | if (!$val['attributes']['active']) { | |
402 | $class = ', "attr": { "class" : "disabled"} '; | |
403 | } | |
404 | $navigationString .= ' { "attr": { "id" : "node_' . $k . '"}, "data": { "title":"' . $data . '"' . $class . '}'; | |
405 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
406 | $navigationString .= $appendComma ? ' },' : ' }'; | |
407 | $count++; | |
408 | } | |
409 | } | |
410 | ||
411 | if (!empty($value['child'])) { | |
412 | $navigationString .= ' ]'; | |
413 | } | |
414 | } | |
415 | else { | |
416 | if (!empty($value['child'])) { | |
417 | $navigationString .= '<ul>'; | |
418 | } | |
419 | else { | |
420 | $navigationString .= '</li>'; | |
421 | //locate separator after | |
422 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
423 | $navigationString .= '<li class="menu-separator"></li>'; | |
424 | } | |
425 | } | |
426 | ||
427 | if (!empty($value['child'])) { | |
428 | foreach ($value['child'] as $val) { | |
429 | $name = self::getMenuName($val, $skipMenuItems); | |
430 | if ($name) { | |
431 | //locate separator before | |
432 | if (isset($val['attributes']['separator']) && $val['attributes']['separator'] == 2) { | |
433 | $navigationString .= '<li class="menu-separator"></li>'; | |
434 | } | |
435 | $removeCharacters = array('/', '!', '&', '*', ' ', '(', ')', '.'); | |
436 | $navigationString .= '<li class="crm-' . str_replace($removeCharacters, '_', $val['attributes']['label']) . '">' . $name; | |
437 | self::recurseNavigation($val, $navigationString, $json, $skipMenuItems); | |
438 | } | |
439 | } | |
440 | } | |
441 | if (!empty($value['child'])) { | |
442 | $navigationString .= '</ul></li>'; | |
443 | if (isset($value['attributes']['separator']) && $value['attributes']['separator'] == 1) { | |
444 | $navigationString .= '<li class="menu-separator"></li>'; | |
445 | } | |
446 | } | |
447 | } | |
448 | return $navigationString; | |
449 | } | |
450 | ||
451 | /** | |
452 | * Get Menu name | |
453 | * | |
454 | * @param $value | |
455 | * @param $skipMenuItems | |
456 | * @return bool|string | |
457 | */ | |
458 | static function getMenuName(&$value, &$skipMenuItems) { | |
459 | // we need to localise the menu labels (CRM-5456) and don’t | |
460 | // want to use ts() as it would throw the ts-extractor off | |
461 | $i18n = CRM_Core_I18n::singleton(); | |
462 | ||
463 | $name = $i18n->crm_translate($value['attributes']['label'], array('context' => 'menu')); | |
464 | $url = $value['attributes']['url']; | |
465 | $permission = $value['attributes']['permission']; | |
466 | $operator = $value['attributes']['operator']; | |
467 | $parentID = $value['attributes']['parentID']; | |
468 | $navID = $value['attributes']['navID']; | |
469 | $active = $value['attributes']['active']; | |
470 | $menuName = $value['attributes']['name']; | |
471 | $target = CRM_Utils_Array::value('target', $value['attributes']); | |
472 | ||
473 | if (in_array($parentID, $skipMenuItems) || !$active) { | |
474 | $skipMenuItems[] = $navID; | |
475 | return FALSE; | |
476 | } | |
477 | ||
478 | //we need to check core view/edit or supported acls. | |
479 | if (in_array($menuName, array( | |
480 | 'Search...', 'Contacts'))) { | |
481 | if (!CRM_Core_Permission::giveMeAllACLs()) { | |
482 | $skipMenuItems[] = $navID; | |
483 | return FALSE; | |
484 | } | |
485 | } | |
486 | ||
487 | $config = CRM_Core_Config::singleton(); | |
488 | ||
489 | $makeLink = FALSE; | |
490 | if (isset($url) && $url) { | |
491 | if (substr($url, 0, 4) === 'http') { | |
492 | $url = $url; | |
493 | } | |
494 | else { | |
495 | //CRM-7656 --make sure to separate out url path from url params, | |
496 | //as we'r going to validate url path across cross-site scripting. | |
497 | $urlParam = CRM_Utils_System::explode('&', str_replace('?', '&', $url), 2); | |
498 | $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, FALSE); | |
499 | } | |
500 | $makeLink = TRUE; | |
501 | } | |
502 | ||
503 | static $allComponents; | |
504 | if (!$allComponents) { | |
505 | $allComponents = CRM_Core_Component::getNames(); | |
506 | } | |
507 | ||
508 | if (isset($permission) && $permission) { | |
509 | $permissions = explode(',', $permission); | |
510 | ||
511 | $hasPermission = FALSE; | |
512 | foreach ($permissions as $key) { | |
513 | $key = trim($key); | |
514 | $showItem = TRUE; | |
515 | ||
516 | //get the component name from permission. | |
517 | $componentName = CRM_Core_Permission::getComponentName($key); | |
518 | ||
519 | if ($componentName) { | |
520 | if (!in_array($componentName, $config->enableComponents) || | |
521 | !CRM_Core_Permission::check($key) | |
522 | ) { | |
523 | $showItem = FALSE; | |
524 | if ($operator == 'AND') { | |
525 | $skipMenuItems[] = $navID; | |
526 | return $showItem; | |
527 | } | |
528 | } | |
529 | else { | |
530 | $hasPermission = TRUE; | |
531 | } | |
532 | } | |
533 | elseif (!CRM_Core_Permission::check($key)) { | |
534 | $showItem = FALSE; | |
535 | if ($operator == 'AND') { | |
536 | $skipMenuItems[] = $navID; | |
537 | return $showItem; | |
538 | } | |
539 | } | |
540 | else { | |
541 | $hasPermission = TRUE; | |
542 | } | |
543 | } | |
544 | ||
545 | if (!$showItem && !$hasPermission) { | |
546 | $skipMenuItems[] = $navID; | |
547 | return FALSE; | |
548 | } | |
549 | } | |
550 | ||
551 | if ($makeLink) { | |
552 | if ($target) { | |
553 | $name = "<a href=\"{$url}\" target=\"{$target}\">{$name}</a>"; | |
554 | } | |
555 | else { | |
556 | $name = "<a href=\"{$url}\">{$name}</a>"; | |
557 | } | |
558 | } | |
559 | ||
560 | return $name; | |
561 | } | |
562 | ||
563 | /** | |
564 | * Function to create navigation for CiviCRM Admin Menu | |
565 | * | |
566 | * @param int $contactID contact id | |
567 | * | |
568 | * @return string $navigation returns navigation html | |
569 | * @static | |
570 | */ | |
571 | static function createNavigation($contactID) { | |
572 | $config = CRM_Core_Config::singleton(); | |
573 | ||
25482510 DL |
574 | // if on frontend, do not create navigation menu items, CRM-5349 |
575 | if ($config->userFrameworkFrontend) { | |
6a488035 TO |
576 | return "<!-- $config->lcMessages -->"; |
577 | } | |
578 | ||
579 | $navParams = array('contact_id' => $contactID); | |
580 | ||
581 | $navigation = CRM_Core_BAO_Setting::getItem( | |
582 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, | |
583 | 'navigation', | |
584 | NULL, | |
585 | NULL, | |
586 | $contactID | |
587 | ); | |
588 | ||
589 | // FIXME: hack for CRM-5027: we need to prepend the navigation string with | |
590 | // (HTML-commented-out) locale info so that we rebuild menu on locale changes | |
591 | if ( | |
592 | !$navigation || | |
593 | substr($navigation, 0, 14) != "<!-- $config->lcMessages -->" | |
594 | ) { | |
595 | //retrieve navigation if it's not cached. | |
596 | $navigation = self::buildNavigation(); | |
597 | ||
598 | //add additional navigation items | |
599 | $logoutURL = CRM_Utils_System::url('civicrm/logout', 'reset=1'); | |
600 | $appendSring = "<li id=\"menu-logout\" class=\"menumain\"><a href=\"{$logoutURL}\">" . ts('Logout') . "</a></li>"; | |
601 | ||
602 | // get home menu from db | |
603 | $homeParams = array('name' => 'Home'); | |
604 | $homeNav = array(); | |
605 | self::retrieve($homeParams, $homeNav); | |
606 | if ($homeNav) { | |
607 | list($path, $q) = explode('&', $homeNav['url']); | |
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') { | |
612 | $homeLabel = ts('Home'); | |
613 | } | |
614 | } | |
615 | else { | |
616 | $homeURL = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
617 | $homeLabel = ts('Home'); | |
618 | } | |
619 | ||
620 | if ( | |
621 | ($config->userSystem->is_drupal) && | |
622 | ((module_exists('toolbar') && user_access('access toolbar')) || | |
623 | module_exists('admin_menu') && user_access('access administration menu') | |
624 | ) | |
625 | ) { | |
626 | $prepandString = "<li class=\"menumain crm-link-home\">" . $homeLabel . "<ul id=\"civicrm-home\"><li><a href=\"{$homeURL}\">" . $homeLabel . "</a></li><li><a href=\"#\" onclick=\"cj.Menu.closeAll( );cj('#civicrm-menu').toggle( );\">" . ts('Drupal Menu') . "</a></li></ul></li>"; | |
627 | } | |
628 | elseif ($config->userSystem->is_wordpress) { | |
629 | $prepandString = "<li class=\"menumain crm-link-home\">" . $homeLabel . "<ul id=\"civicrm-home\"><li><a href=\"{$homeURL}\">" . $homeLabel . "</a></li><li><a href=\"#\" onclick=\"cj.Menu.closeAll( );cj('#civicrm-menu').toggle( );\">" . ts('WordPress Menu') . "</a></li></ul></li>"; | |
630 | } | |
631 | else { | |
632 | $prepandString = "<li class=\"menumain crm-link-home\"><a href=\"{$homeURL}\" title=\"" . $homeLabel . "\">" . $homeLabel . "</a></li>"; | |
633 | } | |
634 | ||
635 | // prepend the navigation with locale info for CRM-5027 | |
636 | $navigation = "<!-- $config->lcMessages -->" . $prepandString . $navigation . $appendSring; | |
637 | ||
638 | // before inserting check if contact id exists in db | |
639 | // this is to handle wierd case when contact id is in session but not in db | |
640 | $contact = new CRM_Contact_DAO_Contact(); | |
641 | $contact->id = $contactID; | |
642 | if ($contact->find(TRUE)) { | |
643 | CRM_Core_BAO_Setting::setItem( | |
644 | $navigation, | |
645 | CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME, | |
646 | 'navigation', | |
647 | NULL, | |
648 | $contactID, | |
649 | $contactID | |
650 | ); | |
651 | } | |
652 | } | |
653 | return $navigation; | |
654 | } | |
655 | ||
656 | /** | |
657 | * Reset navigation for all contacts | |
658 | * | |
659 | * @param integer $contactID - reset only entries belonging to that contact ID | |
660 | */ | |
661 | static function resetNavigation($contactID = NULL) { | |
662 | $params = array(); | |
663 | $query = "UPDATE civicrm_setting SET value = NULL WHERE name='navigation'"; | |
664 | if ($contactID) { | |
665 | $query .= " AND contact_id = %1"; | |
666 | $params[1] = array($contactID, 'Integer'); | |
667 | } | |
668 | else { | |
669 | $query .= " AND contact_id IS NOT NULL"; | |
670 | } | |
671 | ||
672 | CRM_Core_DAO::executeQuery($query, $params); | |
673 | CRM_Core_BAO_Cache::deleteGroup('navigation'); | |
674 | ||
675 | // also reset the dashlet cache in case permissions have changed etc | |
676 | CRM_Core_BAO_Dashboard::resetDashletCache($contactID); | |
677 | } | |
678 | ||
679 | /** | |
680 | * Function to process navigation | |
681 | * | |
682 | * @param array $params associated array, $_GET | |
683 | * | |
684 | * @return void | |
685 | * @static | |
686 | */ | |
687 | static function processNavigation(&$params) { | |
688 | $nodeID = (int)str_replace("node_", "", $params['id']); | |
689 | $referenceID = (int)str_replace("node_", "", $params['ref_id']); | |
690 | $position = $params['ps']; | |
691 | $type = $params['type']; | |
692 | $label = CRM_Utils_Array::value('data', $params); | |
693 | ||
694 | switch ($type) { | |
695 | case "move": | |
696 | self::processMove($nodeID, $referenceID, $position); | |
697 | break; | |
698 | ||
699 | case "rename": | |
700 | self::processRename($nodeID, $label); | |
701 | break; | |
702 | ||
703 | case "delete": | |
704 | self::processDelete($nodeID); | |
705 | break; | |
706 | } | |
707 | ||
708 | //reset navigation menus | |
709 | self::resetNavigation(); | |
710 | CRM_Utils_System::civiExit(); | |
711 | } | |
712 | ||
713 | /** | |
714 | * Function to process move action | |
715 | * | |
716 | * @param $nodeID node that is being moved | |
717 | * @param $referenceID parent id where node is moved. 0 mean no parent | |
718 | * @param $position new position of the nod, it starts with 0 - n | |
719 | * | |
720 | * @return void | |
721 | * @static | |
722 | */ | |
723 | static function processMove($nodeID, $referenceID, $position) { | |
724 | // based on the new position we need to get the weight of the node after moved node | |
725 | // 1. update the weight of $position + 1 nodes to weight + 1 | |
726 | // 2. weight of the ( $position -1 ) node - 1 is the new weight of the node being moved | |
727 | ||
728 | // check if there is parent id, which means node is moved inside existing parent container, so use parent id | |
729 | // to find the correct position else use NULL to get the weights of parent ( $position - 1 ) | |
730 | // accordingly set the new parent_id | |
731 | if ($referenceID) { | |
732 | $newParentID = $referenceID; | |
733 | $parentClause = "parent_id = {$referenceID} "; | |
734 | } | |
735 | else { | |
736 | $newParentID = 'NULL'; | |
737 | $parentClause = 'parent_id IS NULL'; | |
738 | } | |
739 | ||
740 | $incrementOtherNodes = true; | |
741 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; | |
742 | $params = array(1 => array( $position, 'Positive')); | |
743 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); | |
744 | ||
745 | // this means node is moved to last position, so you need to get the weight of last element + 1 | |
746 | if (!$newWeight) { | |
747 | $lastPosition = $position - 1; | |
748 | $sql = "SELECT weight from civicrm_navigation WHERE {$parentClause} ORDER BY weight LIMIT %1, 1"; | |
749 | $params = array(1 => array($lastPosition, 'Positive')); | |
750 | $newWeight = CRM_Core_DAO::singleValueQuery($sql, $params); | |
751 | ||
752 | // since last node increment + 1 | |
753 | $newWeight = $newWeight + 1; | |
754 | ||
755 | // since this is a last node we don't need to increment other nodes | |
756 | $incrementOtherNodes = false; | |
757 | } | |
758 | ||
759 | $transaction = new CRM_Core_Transaction(); | |
760 | ||
761 | // now update the existing nodes to weight + 1, if required. | |
762 | if ( $incrementOtherNodes ) { | |
763 | $query = "UPDATE civicrm_navigation SET weight = weight + 1 | |
764 | WHERE {$parentClause} AND weight >= {$newWeight}"; | |
765 | ||
766 | CRM_Core_DAO::executeQuery($query); | |
767 | } | |
768 | ||
769 | // finally set the weight of current node | |
770 | $query = "UPDATE civicrm_navigation SET weight = {$newWeight}, parent_id = {$newParentID} WHERE id = {$nodeID}"; | |
771 | CRM_Core_DAO::executeQuery($query); | |
772 | ||
773 | $transaction->commit(); | |
774 | } | |
775 | ||
776 | /** | |
777 | * Function to process rename action for tree | |
778 | * | |
779 | * @param $nodeID | |
780 | * @param $label | |
781 | */ | |
782 | static function processRename($nodeID, $label) { | |
783 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Navigation', $nodeID, 'label', $label); | |
784 | } | |
785 | ||
786 | /** | |
787 | * Function to process delete action for tree | |
788 | * | |
789 | * @param $nodeID | |
790 | */ | |
791 | static function processDelete($nodeID) { | |
792 | $query = "DELETE FROM civicrm_navigation WHERE id = {$nodeID}"; | |
793 | CRM_Core_DAO::executeQuery($query); | |
794 | } | |
795 | ||
796 | /** | |
797 | * Function to get the info on navigation item | |
798 | * | |
799 | * @param int $navigationID navigation id | |
800 | * | |
801 | * @return array associated array | |
802 | * @static | |
803 | */ | |
804 | static function getNavigationInfo($navigationID) { | |
805 | $query = "SELECT parent_id, weight FROM civicrm_navigation WHERE id = %1"; | |
806 | $params = array($navigationID, 'Integer'); | |
807 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => $params)); | |
808 | $dao->fetch(); | |
809 | return array( | |
810 | 'parent_id' => $dao->parent_id, | |
811 | 'weight' => $dao->weight, | |
812 | ); | |
813 | } | |
814 | ||
815 | /** | |
816 | * Function to update menu | |
817 | * | |
818 | * @param array $params | |
819 | * @param array $newParams new value of params | |
820 | * @static | |
821 | */ | |
822 | static function processUpdate($params, $newParams) { | |
823 | $dao = new CRM_Core_DAO_Navigation(); | |
824 | $dao->copyValues($params); | |
825 | if ($dao->find(TRUE)) { | |
826 | $dao->copyValues($newParams); | |
827 | $dao->save(); | |
828 | } | |
829 | } | |
830 | } | |
831 |