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