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