Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | * This file contains the various menus of the CiviCRM module | |
30 | * | |
31 | * @package CRM | |
0f03f337 | 32 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
33 | */ |
34 | ||
35 | require_once 'CRM/Core/I18n.php'; | |
28518c90 EM |
36 | |
37 | /** | |
ad37ac8e | 38 | * Class CRM_Core_Menu. |
28518c90 | 39 | */ |
6a488035 TO |
40 | class CRM_Core_Menu { |
41 | ||
42 | /** | |
fe482240 | 43 | * The list of menu items. |
6a488035 TO |
44 | * |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | static $_items = NULL; | |
48 | ||
49 | /** | |
fe482240 | 50 | * The list of permissioned menu items. |
6a488035 TO |
51 | * |
52 | * @var array | |
6a488035 TO |
53 | */ |
54 | static $_permissionedItems = NULL; | |
55 | ||
56 | static $_serializedElements = array( | |
57 | 'access_arguments', | |
58 | 'access_callback', | |
59 | 'page_arguments', | |
60 | 'page_callback', | |
61 | 'breadcrumb', | |
62 | ); | |
63 | ||
64 | static $_menuCache = NULL; | |
7da04cde | 65 | const MENU_ITEM = 1; |
6a488035 | 66 | |
a0ee3941 | 67 | /** |
fe482240 | 68 | * This function fetches the menu items from xml and xmlMenu hooks. |
49d97c48 | 69 | * |
ad37ac8e | 70 | * @param bool $fetchFromXML |
6a0b768e | 71 | * Fetch the menu items from xml and not from cache. |
49d97c48 | 72 | * |
a0ee3941 EM |
73 | * @return array |
74 | */ | |
00be9182 | 75 | public static function &xmlItems($fetchFromXML = FALSE) { |
49d97c48 | 76 | if (!self::$_items || $fetchFromXML) { |
6a488035 TO |
77 | $config = CRM_Core_Config::singleton(); |
78 | ||
79 | // We needs this until Core becomes a component | |
80 | $coreMenuFilesNamespace = 'CRM_Core_xml_Menu'; | |
81 | $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR, $coreMenuFilesNamespace); | |
82 | global $civicrm_root; | |
83 | $files = CRM_Utils_File::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR . $coreMenuFilesPath, 'xml'); | |
84 | ||
85 | // Grab component menu files | |
86 | $files = array_merge($files, | |
87 | CRM_Core_Component::xmlMenu() | |
88 | ); | |
89 | ||
90 | // lets call a hook and get any additional files if needed | |
91 | CRM_Utils_Hook::xmlMenu($files); | |
92 | ||
93 | self::$_items = array(); | |
94 | foreach ($files as $file) { | |
95 | self::read($file, self::$_items); | |
96 | } | |
7dc34fb8 TO |
97 | |
98 | CRM_Utils_Hook::alterMenu(self::$_items); | |
6a488035 TO |
99 | } |
100 | ||
101 | return self::$_items; | |
102 | } | |
103 | ||
a0ee3941 | 104 | /** |
ad37ac8e | 105 | * Read menu. |
106 | * | |
100fef9d | 107 | * @param string $name |
5bfe764c TO |
108 | * File name |
109 | * @param array $menu | |
110 | * An alterable list of menu items. | |
a0ee3941 EM |
111 | * |
112 | * @throws Exception | |
113 | */ | |
00be9182 | 114 | public static function read($name, &$menu) { |
5bfe764c TO |
115 | $xml = simplexml_load_file($name); |
116 | self::readXML($xml, $menu); | |
117 | } | |
6a488035 | 118 | |
5bfe764c TO |
119 | /** |
120 | * @param SimpleXMLElement $xml | |
121 | * An XML document defining a list of menu items. | |
122 | * @param array $menu | |
123 | * An alterable list of menu items. | |
124 | */ | |
125 | public static function readXML($xml, &$menu) { | |
6a488035 | 126 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
127 | foreach ($xml->item as $item) { |
128 | if (!(string ) $item->path) { | |
129 | CRM_Core_Error::debug('i', $item); | |
130 | CRM_Core_Error::fatal(); | |
131 | } | |
132 | $path = (string ) $item->path; | |
133 | $menu[$path] = array(); | |
134 | unset($item->path); | |
135 | foreach ($item as $key => $value) { | |
136 | $key = (string ) $key; | |
137 | $value = (string ) $value; | |
138 | if (strpos($key, '_callback') && | |
139 | strpos($value, '::') | |
140 | ) { | |
c8074a93 TO |
141 | // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value) |
142 | // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)). | |
6a488035 TO |
143 | $value = explode('::', $value); |
144 | } | |
145 | elseif ($key == 'access_arguments') { | |
c8074a93 | 146 | // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission). |
6a488035 TO |
147 | if (strpos($value, ',') || |
148 | strpos($value, ';') | |
149 | ) { | |
150 | if (strpos($value, ',')) { | |
151 | $elements = explode(',', $value); | |
152 | $op = 'and'; | |
153 | } | |
154 | else { | |
155 | $elements = explode(';', $value); | |
156 | $op = 'or'; | |
157 | } | |
158 | $items = array(); | |
159 | foreach ($elements as $element) { | |
160 | $items[] = $element; | |
161 | } | |
162 | $value = array($items, $op); | |
163 | } | |
164 | else { | |
165 | $value = array(array($value), 'and'); | |
166 | } | |
167 | } | |
168 | elseif ($key == 'is_public' || $key == 'is_ssl') { | |
169 | $value = ($value == 'true' || $value == 1) ? 1 : 0; | |
170 | } | |
171 | $menu[$path][$key] = $value; | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | /** | |
fe482240 | 177 | * This function defines information for various menu items. |
6a488035 | 178 | * |
ad37ac8e | 179 | * @param bool $fetchFromXML |
6a0b768e | 180 | * Fetch the menu items from xml and not from cache. |
49d97c48 | 181 | * |
7a9ab499 | 182 | * @return array |
6a488035 | 183 | */ |
00be9182 | 184 | public static function &items($fetchFromXML = FALSE) { |
49d97c48 | 185 | return self::xmlItems($fetchFromXML); |
6a488035 TO |
186 | } |
187 | ||
a0ee3941 | 188 | /** |
ad37ac8e | 189 | * Is array true (whatever that means!). |
190 | * | |
191 | * @param array $values | |
a0ee3941 EM |
192 | * |
193 | * @return bool | |
194 | */ | |
00be9182 | 195 | public static function isArrayTrue(&$values) { |
6a488035 TO |
196 | foreach ($values as $name => $value) { |
197 | if (!$value) { | |
198 | return FALSE; | |
199 | } | |
200 | } | |
201 | return TRUE; | |
202 | } | |
203 | ||
a0ee3941 | 204 | /** |
ad37ac8e | 205 | * Fill menu values. |
206 | * | |
207 | * @param array $menu | |
208 | * @param string $path | |
a0ee3941 EM |
209 | * |
210 | * @throws Exception | |
211 | */ | |
00be9182 | 212 | public static function fillMenuValues(&$menu, $path) { |
6a488035 TO |
213 | $fieldsToPropagate = array( |
214 | 'access_callback', | |
215 | 'access_arguments', | |
216 | 'page_callback', | |
217 | 'page_arguments', | |
218 | 'is_ssl', | |
219 | ); | |
220 | $fieldsPresent = array(); | |
221 | foreach ($fieldsToPropagate as $field) { | |
222 | $fieldsPresent[$field] = CRM_Utils_Array::value($field, $menu[$path]) !== NULL ? TRUE : FALSE; | |
223 | } | |
224 | ||
225 | $args = explode('/', $path); | |
226 | while (!self::isArrayTrue($fieldsPresent) && !empty($args)) { | |
227 | ||
228 | array_pop($args); | |
229 | $parentPath = implode('/', $args); | |
230 | ||
231 | foreach ($fieldsToPropagate as $field) { | |
232 | if (!$fieldsPresent[$field]) { | |
233 | if (CRM_Utils_Array::value($field, CRM_Utils_Array::value($parentPath, $menu)) !== NULL) { | |
234 | $fieldsPresent[$field] = TRUE; | |
235 | $menu[$path][$field] = $menu[$parentPath][$field]; | |
236 | } | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | if (self::isArrayTrue($fieldsPresent)) { | |
242 | return; | |
243 | } | |
244 | ||
245 | $messages = array(); | |
246 | foreach ($fieldsToPropagate as $field) { | |
247 | if (!$fieldsPresent[$field]) { | |
248 | $messages[] = ts("Could not find %1 in path tree", | |
249 | array(1 => $field) | |
250 | ); | |
251 | } | |
252 | } | |
253 | CRM_Core_Error::fatal("'$path': " . implode(', ', $messages)); | |
254 | } | |
255 | ||
256 | /** | |
fe482240 | 257 | * We use this function to. |
6a488035 TO |
258 | * |
259 | * 1. Compute the breadcrumb | |
260 | * 2. Compute local tasks value if any | |
261 | * 3. Propagate access argument, access callback, page callback to the menu item | |
262 | * 4. Build the global navigation block | |
ea3ddccf | 263 | * |
264 | * @param array $menu | |
6a488035 | 265 | */ |
00be9182 | 266 | public static function build(&$menu) { |
6a488035 TO |
267 | foreach ($menu as $path => $menuItems) { |
268 | self::buildBreadcrumb($menu, $path); | |
269 | self::fillMenuValues($menu, $path); | |
270 | self::fillComponentIds($menu, $path); | |
271 | self::buildReturnUrl($menu, $path); | |
272 | ||
273 | // add add page_type if not present | |
274 | if (!isset($menu[$path]['page_type'])) { | |
275 | $menu[$path]['page_type'] = 0; | |
276 | } | |
277 | } | |
278 | ||
279 | self::buildAdminLinks($menu); | |
280 | } | |
281 | ||
a0ee3941 | 282 | /** |
fe482240 | 283 | * This function recomputes menu from xml and populates civicrm_menu. |
ad37ac8e | 284 | * |
a0ee3941 EM |
285 | * @param bool $truncate |
286 | */ | |
00be9182 | 287 | public static function store($truncate = TRUE) { |
6a488035 TO |
288 | // first clean up the db |
289 | if ($truncate) { | |
290 | $query = 'TRUNCATE civicrm_menu'; | |
291 | CRM_Core_DAO::executeQuery($query); | |
292 | } | |
49d97c48 | 293 | $menuArray = self::items($truncate); |
6a488035 TO |
294 | |
295 | self::build($menuArray); | |
296 | ||
6a488035 | 297 | $config = CRM_Core_Config::singleton(); |
b44dc91e | 298 | $daoFields = CRM_Core_DAO_Menu::fields(); |
6a488035 TO |
299 | |
300 | foreach ($menuArray as $path => $item) { | |
353ffa53 TO |
301 | $menu = new CRM_Core_DAO_Menu(); |
302 | $menu->path = $path; | |
6a488035 TO |
303 | $menu->domain_id = CRM_Core_Config::domainID(); |
304 | ||
305 | $menu->find(TRUE); | |
306 | ||
b44dc91e TO |
307 | // Move unrecognized fields to $module_data. |
308 | $module_data = array(); | |
309 | foreach (array_keys($item) as $key) { | |
310 | if (!isset($daoFields[$key])) { | |
311 | $module_data[$key] = $item[$key]; | |
312 | unset($item[$key]); | |
313 | } | |
314 | } | |
315 | ||
6a488035 | 316 | $menu->copyValues($item); |
b44dc91e | 317 | $menu->module_data = serialize($module_data); |
6a488035 TO |
318 | |
319 | foreach (self::$_serializedElements as $element) { | |
320 | if (!isset($item[$element]) || | |
321 | $item[$element] == 'null' | |
322 | ) { | |
323 | $menu->$element = NULL; | |
324 | } | |
325 | else { | |
326 | $menu->$element = serialize($item[$element]); | |
327 | } | |
328 | } | |
329 | ||
330 | $menu->save(); | |
331 | } | |
332 | } | |
333 | ||
a0ee3941 | 334 | /** |
ad37ac8e | 335 | * Build admin links. |
336 | * | |
337 | * @param array $menu | |
a0ee3941 | 338 | */ |
00be9182 | 339 | public static function buildAdminLinks(&$menu) { |
6a488035 TO |
340 | $values = array(); |
341 | ||
342 | foreach ($menu as $path => $item) { | |
a7488080 | 343 | if (empty($item['adminGroup'])) { |
6a488035 TO |
344 | continue; |
345 | } | |
346 | ||
0d8afee2 | 347 | $query = !empty($item['path_arguments']) ? str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1'; |
6a488035 TO |
348 | |
349 | $value = array( | |
350 | 'title' => $item['title'], | |
351 | 'desc' => CRM_Utils_Array::value('desc', $item), | |
352 | 'id' => strtr($item['title'], array( | |
353ffa53 TO |
353 | '(' => '_', |
354 | ')' => '', | |
355 | ' ' => '', | |
2aa397bc | 356 | ',' => '_', |
353ffa53 | 357 | '/' => '_', |
6a488035 TO |
358 | ) |
359 | ), | |
d75f2f47 | 360 | 'url' => CRM_Utils_System::url($path, $query, |
ad37ac8e | 361 | FALSE, |
362 | NULL, | |
363 | TRUE, | |
364 | FALSE, | |
365 | // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend | |
366 | TRUE | |
f4bdec6a | 367 | ), |
6a488035 TO |
368 | 'icon' => CRM_Utils_Array::value('icon', $item), |
369 | 'extra' => CRM_Utils_Array::value('extra', $item), | |
370 | ); | |
371 | if (!array_key_exists($item['adminGroup'], $values)) { | |
372 | $values[$item['adminGroup']] = array(); | |
373 | $values[$item['adminGroup']]['fields'] = array(); | |
374 | } | |
375 | $weight = CRM_Utils_Array::value('weight', $item, 0); | |
376 | $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value; | |
377 | $values[$item['adminGroup']]['component_id'] = $item['component_id']; | |
378 | } | |
379 | ||
380 | foreach ($values as $group => $dontCare) { | |
381 | $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2); | |
382 | ksort($values[$group]); | |
383 | } | |
384 | ||
385 | $menu['admin'] = array('breadcrumb' => $values); | |
386 | } | |
387 | ||
a0ee3941 | 388 | /** |
ad37ac8e | 389 | * Get navigation. |
390 | * | |
a0ee3941 EM |
391 | * @param bool $all |
392 | * | |
393 | * @return mixed | |
394 | * @throws Exception | |
395 | */ | |
00be9182 | 396 | public static function &getNavigation($all = FALSE) { |
6a488035 TO |
397 | CRM_Core_Error::fatal(); |
398 | ||
399 | if (!self::$_menuCache) { | |
400 | self::get('navigation'); | |
401 | } | |
402 | ||
403 | if (CRM_Core_Config::isUpgradeMode()) { | |
404 | return array(); | |
405 | } | |
406 | ||
407 | if (!array_key_exists('navigation', self::$_menuCache)) { | |
408 | // problem could be due to menu table empty. Just do a | |
409 | // menu store and try again | |
410 | self::store(); | |
411 | ||
412 | // here we goo | |
413 | self::get('navigation'); | |
414 | if (!array_key_exists('navigation', self::$_menuCache)) { | |
415 | CRM_Core_Error::fatal(); | |
416 | } | |
417 | } | |
418 | $nav = &self::$_menuCache['navigation']; | |
419 | ||
420 | if (!$nav || | |
421 | !isset($nav['breadcrumb']) | |
422 | ) { | |
423 | return NULL; | |
424 | } | |
425 | ||
426 | $values = &$nav['breadcrumb']; | |
427 | $config = CRM_Core_Config::singleton(); | |
428 | foreach ($values as $index => $item) { | |
429 | if (strpos(CRM_Utils_Array::value($config->userFrameworkURLVar, $_REQUEST), | |
430 | $item['path'] | |
353ffa53 TO |
431 | ) === 0 |
432 | ) { | |
6a488035 TO |
433 | $values[$index]['active'] = 'class="active"'; |
434 | } | |
435 | else { | |
436 | $values[$index]['active'] = ''; | |
437 | } | |
438 | ||
439 | if ($values[$index]['parent']) { | |
440 | $parent = $values[$index]['parent']; | |
441 | ||
442 | // only reset if still a leaf | |
443 | if ($values[$parent]['class'] == 'leaf') { | |
444 | $values[$parent]['class'] = 'collapsed'; | |
445 | } | |
446 | ||
447 | // if a child or the parent is active, expand the menu | |
448 | if ($values[$index]['active'] || | |
449 | $values[$parent]['active'] | |
450 | ) { | |
451 | $values[$parent]['class'] = 'expanded'; | |
452 | } | |
453 | ||
454 | // make the parent inactive if the child is active | |
455 | if ($values[$index]['active'] && | |
456 | $values[$parent]['active'] | |
457 | ) { | |
458 | $values[$parent]['active'] = ''; | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
6a488035 TO |
463 | if (!$all) { |
464 | // remove all collapsed menu items from the array | |
465 | foreach ($values as $weight => $v) { | |
466 | if ($v['parent'] && | |
467 | $values[$v['parent']]['class'] == 'collapsed' | |
468 | ) { | |
469 | unset($values[$weight]); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
474 | // check permissions for the rest | |
475 | $activeChildren = array(); | |
476 | ||
477 | foreach ($values as $weight => $v) { | |
478 | if (CRM_Core_Permission::checkMenuItem($v)) { | |
479 | if ($v['parent']) { | |
480 | $activeChildren[] = $weight; | |
481 | } | |
482 | } | |
483 | else { | |
484 | unset($values[$weight]); | |
485 | } | |
486 | } | |
487 | ||
488 | // add the start / end tags | |
489 | $len = count($activeChildren) - 1; | |
490 | if ($len >= 0) { | |
491 | $values[$activeChildren[0]]['start'] = TRUE; | |
492 | $values[$activeChildren[$len]]['end'] = TRUE; | |
493 | } | |
494 | ||
495 | ksort($values, SORT_NUMERIC); | |
496 | $i18n = CRM_Core_I18n::singleton(); | |
497 | $i18n->localizeTitles($values); | |
498 | ||
499 | return $values; | |
500 | } | |
501 | ||
a0ee3941 | 502 | /** |
ad37ac8e | 503 | * Get admin links. |
504 | * | |
a0ee3941 EM |
505 | * @return null |
506 | */ | |
00be9182 | 507 | public static function &getAdminLinks() { |
6a488035 TO |
508 | $links = self::get('admin'); |
509 | ||
510 | if (!$links || | |
511 | !isset($links['breadcrumb']) | |
512 | ) { | |
513 | return NULL; | |
514 | } | |
515 | ||
516 | $values = &$links['breadcrumb']; | |
517 | return $values; | |
518 | } | |
519 | ||
520 | /** | |
521 | * Get the breadcrumb for a given path. | |
522 | * | |
6a0b768e TO |
523 | * @param array $menu |
524 | * An array of all the menu items. | |
525 | * @param string $path | |
526 | * Path for which breadcrumb is to be build. | |
6a488035 | 527 | * |
a6c01b45 CW |
528 | * @return array |
529 | * The breadcrumb for this path | |
6a488035 | 530 | */ |
00be9182 | 531 | public static function buildBreadcrumb(&$menu, $path) { |
6a488035 TO |
532 | $crumbs = array(); |
533 | ||
534 | $pathElements = explode('/', $path); | |
535 | array_pop($pathElements); | |
536 | ||
537 | $currentPath = NULL; | |
538 | while ($newPath = array_shift($pathElements)) { | |
539 | $currentPath = $currentPath ? ($currentPath . '/' . $newPath) : $newPath; | |
540 | ||
b44e3f84 | 541 | // when we come across breadcrumb which involves ids, |
6a488035 TO |
542 | // we should skip now and later on append dynamically. |
543 | if (isset($menu[$currentPath]['skipBreadcrumb'])) { | |
544 | continue; | |
545 | } | |
546 | ||
547 | // add to crumb, if current-path exists in params. | |
548 | if (array_key_exists($currentPath, $menu) && | |
549 | isset($menu[$currentPath]['title']) | |
550 | ) { | |
0d8afee2 | 551 | $urlVar = !empty($menu[$currentPath]['path_arguments']) ? '&' . $menu[$currentPath]['path_arguments'] : ''; |
6a488035 TO |
552 | $crumbs[] = array( |
553 | 'title' => $menu[$currentPath]['title'], | |
554 | 'url' => CRM_Utils_System::url($currentPath, | |
38a0e912 TO |
555 | 'reset=1' . $urlVar, |
556 | FALSE, // absolute | |
557 | NULL, // fragment | |
558 | TRUE, // htmlize | |
559 | FALSE, // frontend | |
560 | TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend | |
6a488035 TO |
561 | ), |
562 | ); | |
563 | } | |
564 | } | |
565 | $menu[$path]['breadcrumb'] = $crumbs; | |
566 | ||
567 | return $crumbs; | |
568 | } | |
569 | ||
a0ee3941 EM |
570 | /** |
571 | * @param $menu | |
572 | * @param $path | |
573 | */ | |
00be9182 | 574 | public static function buildReturnUrl(&$menu, $path) { |
6a488035 TO |
575 | if (!isset($menu[$path]['return_url'])) { |
576 | list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self::getReturnUrl($menu, $path); | |
577 | } | |
578 | } | |
579 | ||
a0ee3941 EM |
580 | /** |
581 | * @param $menu | |
582 | * @param $path | |
583 | * | |
584 | * @return array | |
585 | */ | |
00be9182 | 586 | public static function getReturnUrl(&$menu, $path) { |
6a488035 TO |
587 | if (!isset($menu[$path]['return_url'])) { |
588 | $pathElements = explode('/', $path); | |
589 | array_pop($pathElements); | |
590 | ||
591 | if (empty($pathElements)) { | |
592 | return array(NULL, NULL); | |
593 | } | |
594 | $newPath = implode('/', $pathElements); | |
595 | ||
596 | return self::getReturnUrl($menu, $newPath); | |
597 | } | |
598 | else { | |
599 | return array( | |
600 | CRM_Utils_Array::value('return_url', | |
601 | $menu[$path] | |
602 | ), | |
603 | CRM_Utils_Array::value('return_url_args', | |
604 | $menu[$path] | |
605 | ), | |
606 | ); | |
607 | } | |
608 | } | |
609 | ||
a0ee3941 EM |
610 | /** |
611 | * @param $menu | |
612 | * @param $path | |
613 | */ | |
00be9182 | 614 | public static function fillComponentIds(&$menu, $path) { |
6a488035 TO |
615 | static $cache = array(); |
616 | ||
617 | if (array_key_exists('component_id', $menu[$path])) { | |
618 | return; | |
619 | } | |
620 | ||
621 | $args = explode('/', $path); | |
622 | ||
623 | if (count($args) > 1) { | |
624 | $compPath = $args[0] . '/' . $args[1]; | |
625 | } | |
626 | else { | |
627 | $compPath = $args[0]; | |
628 | } | |
629 | ||
630 | $componentId = NULL; | |
631 | ||
632 | if (array_key_exists($compPath, $cache)) { | |
633 | $menu[$path]['component_id'] = $cache[$compPath]; | |
634 | } | |
635 | else { | |
636 | if (CRM_Utils_Array::value('component', CRM_Utils_Array::value($compPath, $menu))) { | |
637 | $componentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', | |
638 | $menu[$compPath]['component'], | |
639 | 'id', 'name' | |
640 | ); | |
641 | } | |
642 | $menu[$path]['component_id'] = $componentId ? $componentId : NULL; | |
643 | $cache[$compPath] = $menu[$path]['component_id']; | |
644 | } | |
645 | } | |
646 | ||
a0ee3941 | 647 | /** |
35146d69 | 648 | * @param $path string |
2bc65854 | 649 | * Path of menu item to retrieve. |
a0ee3941 | 650 | * |
35146d69 | 651 | * @return array |
2bc65854 | 652 | * Menu entry array. |
a0ee3941 | 653 | */ |
00be9182 | 654 | public static function get($path) { |
6a488035 TO |
655 | // return null if menu rebuild |
656 | $config = CRM_Core_Config::singleton(); | |
657 | ||
658 | $params = array(); | |
659 | ||
660 | $args = explode('/', $path); | |
661 | ||
662 | $elements = array(); | |
663 | while (!empty($args)) { | |
353ffa53 TO |
664 | $string = implode('/', $args); |
665 | $string = CRM_Core_DAO::escapeString($string); | |
6a488035 TO |
666 | $elements[] = "'{$string}'"; |
667 | array_pop($args); | |
668 | } | |
669 | ||
353ffa53 TO |
670 | $queryString = implode(', ', $elements); |
671 | $domainID = CRM_Core_Config::domainID(); | |
6a488035 TO |
672 | $domainWhereClause = " AND domain_id = $domainID "; |
673 | if ($config->isUpgradeMode() && | |
674 | !CRM_Core_DAO::checkFieldExists('civicrm_menu', 'domain_id') | |
675 | ) { | |
676 | //domain_id wouldn't be available for earlier version of | |
677 | //3.0 and therefore can't be used as part of query for | |
678 | //upgrade case | |
679 | $domainWhereClause = ""; | |
680 | } | |
681 | ||
682 | $query = " | |
683 | ( | |
684 | SELECT * | |
685 | FROM civicrm_menu | |
686 | WHERE path in ( $queryString ) | |
687 | $domainWhereClause | |
688 | ORDER BY length(path) DESC | |
689 | LIMIT 1 | |
690 | ) | |
691 | "; | |
692 | ||
693 | if ($path != 'navigation') { | |
694 | $query .= " | |
695 | UNION ( | |
696 | SELECT * | |
697 | FROM civicrm_menu | |
698 | WHERE path IN ( 'navigation' ) | |
699 | $domainWhereClause | |
700 | ) | |
701 | "; | |
702 | } | |
703 | ||
704 | $menu = new CRM_Core_DAO_Menu(); | |
705 | $menu->query($query); | |
706 | ||
707 | self::$_menuCache = array(); | |
708 | $menuPath = NULL; | |
709 | while ($menu->fetch()) { | |
710 | self::$_menuCache[$menu->path] = array(); | |
711 | CRM_Core_DAO::storeValues($menu, self::$_menuCache[$menu->path]); | |
712 | ||
b44dc91e TO |
713 | // Move module_data into main item. |
714 | if (isset(self::$_menuCache[$menu->path]['module_data'])) { | |
715 | CRM_Utils_Array::extend(self::$_menuCache[$menu->path], | |
716 | unserialize(self::$_menuCache[$menu->path]['module_data'])); | |
717 | unset(self::$_menuCache[$menu->path]['module_data']); | |
718 | } | |
719 | ||
720 | // Unserialize other elements. | |
6a488035 TO |
721 | foreach (self::$_serializedElements as $element) { |
722 | self::$_menuCache[$menu->path][$element] = unserialize($menu->$element); | |
723 | ||
724 | if (strpos($path, $menu->path) !== FALSE) { | |
725 | $menuPath = &self::$_menuCache[$menu->path]; | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | if (strstr($path, 'report/instance')) { | |
731 | $args = explode('/', $path); | |
732 | if (is_numeric(end($args))) { | |
733 | $menuPath['path'] .= '/' . end($args); | |
734 | } | |
735 | } | |
736 | ||
737 | // *FIXME* : hack for 2.1 -> 2.2 upgrades. | |
738 | if ($path == 'civicrm/upgrade') { | |
739 | $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade'; | |
740 | $menuPath['access_arguments'][0][] = 'administer CiviCRM'; | |
741 | $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu'); | |
742 | } | |
743 | // *FIXME* : hack for 4.1 -> 4.2 upgrades. | |
744 | if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) { | |
745 | CRM_Queue_Menu::alter($path, $menuPath); | |
746 | } | |
747 | ||
748 | // Part of upgrade framework but not run inside main upgrade because it deletes data | |
749 | // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string | |
750 | // which follows upgrade/ and checks for existence of a function in Cleanup class. | |
751 | if ($path == 'civicrm/upgrade/cleanup425') { | |
2aa397bc | 752 | $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425'); |
6a488035 TO |
753 | $menuPath['access_arguments'][0][] = 'administer CiviCRM'; |
754 | $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu'); | |
755 | } | |
756 | ||
757 | if (!empty($menuPath)) { | |
758 | $i18n = CRM_Core_I18n::singleton(); | |
759 | $i18n->localizeTitles($menuPath); | |
760 | } | |
761 | return $menuPath; | |
762 | } | |
763 | ||
a0ee3941 EM |
764 | /** |
765 | * @param $pathArgs | |
766 | * | |
767 | * @return mixed | |
768 | */ | |
00be9182 | 769 | public static function getArrayForPathArgs($pathArgs) { |
6a488035 TO |
770 | if (!is_string($pathArgs)) { |
771 | return; | |
772 | } | |
773 | $args = array(); | |
774 | ||
775 | $elements = explode(',', $pathArgs); | |
6a488035 | 776 | foreach ($elements as $keyVal) { |
3b4339fd | 777 | list($key, $val) = explode('=', $keyVal, 2); |
6a488035 TO |
778 | $arr[$key] = $val; |
779 | } | |
780 | ||
781 | if (array_key_exists('urlToSession', $arr)) { | |
782 | $urlToSession = array(); | |
783 | ||
784 | $params = explode(';', $arr['urlToSession']); | |
785 | $count = 0; | |
786 | foreach ($params as $keyVal) { | |
787 | list($urlToSession[$count]['urlVar'], | |
788 | $urlToSession[$count]['sessionVar'], | |
789 | $urlToSession[$count]['type'], | |
790 | $urlToSession[$count]['default'] | |
353ffa53 | 791 | ) = explode(':', $keyVal); |
6a488035 TO |
792 | $count++; |
793 | } | |
794 | $arr['urlToSession'] = $urlToSession; | |
795 | } | |
796 | return $arr; | |
797 | } | |
96025800 | 798 | |
6a488035 | 799 | } |