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 | ||
d6f1a16c NM |
320 | if (!CRM_Core_Config::isUpgradeMode() || |
321 | CRM_Core_DAO::checkFieldExists('civicrm_menu', 'module_data', FALSE) | |
322 | ) { | |
323 | // Move unrecognized fields to $module_data. | |
324 | $module_data = array(); | |
325 | foreach (array_keys($item) as $key) { | |
326 | if (!isset($daoFields[$key])) { | |
327 | $module_data[$key] = $item[$key]; | |
328 | unset($item[$key]); | |
329 | } | |
b44dc91e | 330 | } |
b44dc91e | 331 | |
d6f1a16c NM |
332 | $menu->module_data = serialize($module_data); |
333 | } | |
6a488035 | 334 | |
c09129a5 NM |
335 | $menu->copyValues($item); |
336 | ||
6a488035 TO |
337 | foreach (self::$_serializedElements as $element) { |
338 | if (!isset($item[$element]) || | |
339 | $item[$element] == 'null' | |
340 | ) { | |
341 | $menu->$element = NULL; | |
342 | } | |
343 | else { | |
344 | $menu->$element = serialize($item[$element]); | |
345 | } | |
346 | } | |
347 | ||
348 | $menu->save(); | |
349 | } | |
350 | } | |
351 | ||
a0ee3941 | 352 | /** |
ad37ac8e | 353 | * Build admin links. |
354 | * | |
355 | * @param array $menu | |
a0ee3941 | 356 | */ |
00be9182 | 357 | public static function buildAdminLinks(&$menu) { |
6a488035 TO |
358 | $values = array(); |
359 | ||
360 | foreach ($menu as $path => $item) { | |
a7488080 | 361 | if (empty($item['adminGroup'])) { |
6a488035 TO |
362 | continue; |
363 | } | |
364 | ||
0d8afee2 | 365 | $query = !empty($item['path_arguments']) ? str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1'; |
6a488035 TO |
366 | |
367 | $value = array( | |
368 | 'title' => $item['title'], | |
369 | 'desc' => CRM_Utils_Array::value('desc', $item), | |
370 | 'id' => strtr($item['title'], array( | |
353ffa53 TO |
371 | '(' => '_', |
372 | ')' => '', | |
373 | ' ' => '', | |
2aa397bc | 374 | ',' => '_', |
353ffa53 | 375 | '/' => '_', |
6a488035 TO |
376 | ) |
377 | ), | |
d75f2f47 | 378 | 'url' => CRM_Utils_System::url($path, $query, |
ad37ac8e | 379 | FALSE, |
380 | NULL, | |
381 | TRUE, | |
382 | FALSE, | |
383 | // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend | |
384 | TRUE | |
f4bdec6a | 385 | ), |
6a488035 TO |
386 | 'icon' => CRM_Utils_Array::value('icon', $item), |
387 | 'extra' => CRM_Utils_Array::value('extra', $item), | |
388 | ); | |
389 | if (!array_key_exists($item['adminGroup'], $values)) { | |
390 | $values[$item['adminGroup']] = array(); | |
391 | $values[$item['adminGroup']]['fields'] = array(); | |
392 | } | |
393 | $weight = CRM_Utils_Array::value('weight', $item, 0); | |
394 | $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value; | |
395 | $values[$item['adminGroup']]['component_id'] = $item['component_id']; | |
396 | } | |
397 | ||
398 | foreach ($values as $group => $dontCare) { | |
399 | $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2); | |
400 | ksort($values[$group]); | |
401 | } | |
402 | ||
403 | $menu['admin'] = array('breadcrumb' => $values); | |
404 | } | |
405 | ||
a0ee3941 | 406 | /** |
ad37ac8e | 407 | * Get navigation. |
408 | * | |
a0ee3941 EM |
409 | * @param bool $all |
410 | * | |
411 | * @return mixed | |
412 | * @throws Exception | |
413 | */ | |
00be9182 | 414 | public static function &getNavigation($all = FALSE) { |
6a488035 TO |
415 | CRM_Core_Error::fatal(); |
416 | ||
417 | if (!self::$_menuCache) { | |
418 | self::get('navigation'); | |
419 | } | |
420 | ||
421 | if (CRM_Core_Config::isUpgradeMode()) { | |
422 | return array(); | |
423 | } | |
424 | ||
425 | if (!array_key_exists('navigation', self::$_menuCache)) { | |
426 | // problem could be due to menu table empty. Just do a | |
427 | // menu store and try again | |
428 | self::store(); | |
429 | ||
430 | // here we goo | |
431 | self::get('navigation'); | |
432 | if (!array_key_exists('navigation', self::$_menuCache)) { | |
433 | CRM_Core_Error::fatal(); | |
434 | } | |
435 | } | |
436 | $nav = &self::$_menuCache['navigation']; | |
437 | ||
438 | if (!$nav || | |
439 | !isset($nav['breadcrumb']) | |
440 | ) { | |
441 | return NULL; | |
442 | } | |
443 | ||
444 | $values = &$nav['breadcrumb']; | |
445 | $config = CRM_Core_Config::singleton(); | |
446 | foreach ($values as $index => $item) { | |
447 | if (strpos(CRM_Utils_Array::value($config->userFrameworkURLVar, $_REQUEST), | |
448 | $item['path'] | |
353ffa53 TO |
449 | ) === 0 |
450 | ) { | |
6a488035 TO |
451 | $values[$index]['active'] = 'class="active"'; |
452 | } | |
453 | else { | |
454 | $values[$index]['active'] = ''; | |
455 | } | |
456 | ||
457 | if ($values[$index]['parent']) { | |
458 | $parent = $values[$index]['parent']; | |
459 | ||
460 | // only reset if still a leaf | |
461 | if ($values[$parent]['class'] == 'leaf') { | |
462 | $values[$parent]['class'] = 'collapsed'; | |
463 | } | |
464 | ||
465 | // if a child or the parent is active, expand the menu | |
466 | if ($values[$index]['active'] || | |
467 | $values[$parent]['active'] | |
468 | ) { | |
469 | $values[$parent]['class'] = 'expanded'; | |
470 | } | |
471 | ||
472 | // make the parent inactive if the child is active | |
473 | if ($values[$index]['active'] && | |
474 | $values[$parent]['active'] | |
475 | ) { | |
476 | $values[$parent]['active'] = ''; | |
477 | } | |
478 | } | |
479 | } | |
480 | ||
6a488035 TO |
481 | if (!$all) { |
482 | // remove all collapsed menu items from the array | |
483 | foreach ($values as $weight => $v) { | |
484 | if ($v['parent'] && | |
485 | $values[$v['parent']]['class'] == 'collapsed' | |
486 | ) { | |
487 | unset($values[$weight]); | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | // check permissions for the rest | |
493 | $activeChildren = array(); | |
494 | ||
495 | foreach ($values as $weight => $v) { | |
496 | if (CRM_Core_Permission::checkMenuItem($v)) { | |
497 | if ($v['parent']) { | |
498 | $activeChildren[] = $weight; | |
499 | } | |
500 | } | |
501 | else { | |
502 | unset($values[$weight]); | |
503 | } | |
504 | } | |
505 | ||
506 | // add the start / end tags | |
507 | $len = count($activeChildren) - 1; | |
508 | if ($len >= 0) { | |
509 | $values[$activeChildren[0]]['start'] = TRUE; | |
510 | $values[$activeChildren[$len]]['end'] = TRUE; | |
511 | } | |
512 | ||
513 | ksort($values, SORT_NUMERIC); | |
514 | $i18n = CRM_Core_I18n::singleton(); | |
515 | $i18n->localizeTitles($values); | |
516 | ||
517 | return $values; | |
518 | } | |
519 | ||
a0ee3941 | 520 | /** |
ad37ac8e | 521 | * Get admin links. |
522 | * | |
a0ee3941 EM |
523 | * @return null |
524 | */ | |
00be9182 | 525 | public static function &getAdminLinks() { |
6a488035 TO |
526 | $links = self::get('admin'); |
527 | ||
528 | if (!$links || | |
529 | !isset($links['breadcrumb']) | |
530 | ) { | |
531 | return NULL; | |
532 | } | |
533 | ||
534 | $values = &$links['breadcrumb']; | |
535 | return $values; | |
536 | } | |
537 | ||
538 | /** | |
539 | * Get the breadcrumb for a given path. | |
540 | * | |
6a0b768e TO |
541 | * @param array $menu |
542 | * An array of all the menu items. | |
543 | * @param string $path | |
544 | * Path for which breadcrumb is to be build. | |
6a488035 | 545 | * |
a6c01b45 CW |
546 | * @return array |
547 | * The breadcrumb for this path | |
6a488035 | 548 | */ |
00be9182 | 549 | public static function buildBreadcrumb(&$menu, $path) { |
6a488035 TO |
550 | $crumbs = array(); |
551 | ||
552 | $pathElements = explode('/', $path); | |
553 | array_pop($pathElements); | |
554 | ||
555 | $currentPath = NULL; | |
556 | while ($newPath = array_shift($pathElements)) { | |
557 | $currentPath = $currentPath ? ($currentPath . '/' . $newPath) : $newPath; | |
558 | ||
b44e3f84 | 559 | // when we come across breadcrumb which involves ids, |
6a488035 TO |
560 | // we should skip now and later on append dynamically. |
561 | if (isset($menu[$currentPath]['skipBreadcrumb'])) { | |
562 | continue; | |
563 | } | |
564 | ||
565 | // add to crumb, if current-path exists in params. | |
566 | if (array_key_exists($currentPath, $menu) && | |
567 | isset($menu[$currentPath]['title']) | |
568 | ) { | |
0d8afee2 | 569 | $urlVar = !empty($menu[$currentPath]['path_arguments']) ? '&' . $menu[$currentPath]['path_arguments'] : ''; |
6a488035 TO |
570 | $crumbs[] = array( |
571 | 'title' => $menu[$currentPath]['title'], | |
572 | 'url' => CRM_Utils_System::url($currentPath, | |
38a0e912 TO |
573 | 'reset=1' . $urlVar, |
574 | FALSE, // absolute | |
575 | NULL, // fragment | |
576 | TRUE, // htmlize | |
577 | FALSE, // frontend | |
578 | TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend | |
6a488035 TO |
579 | ), |
580 | ); | |
581 | } | |
582 | } | |
583 | $menu[$path]['breadcrumb'] = $crumbs; | |
584 | ||
585 | return $crumbs; | |
586 | } | |
587 | ||
a0ee3941 EM |
588 | /** |
589 | * @param $menu | |
590 | * @param $path | |
591 | */ | |
00be9182 | 592 | public static function buildReturnUrl(&$menu, $path) { |
6a488035 TO |
593 | if (!isset($menu[$path]['return_url'])) { |
594 | list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self::getReturnUrl($menu, $path); | |
595 | } | |
596 | } | |
597 | ||
a0ee3941 EM |
598 | /** |
599 | * @param $menu | |
600 | * @param $path | |
601 | * | |
602 | * @return array | |
603 | */ | |
00be9182 | 604 | public static function getReturnUrl(&$menu, $path) { |
6a488035 TO |
605 | if (!isset($menu[$path]['return_url'])) { |
606 | $pathElements = explode('/', $path); | |
607 | array_pop($pathElements); | |
608 | ||
609 | if (empty($pathElements)) { | |
610 | return array(NULL, NULL); | |
611 | } | |
612 | $newPath = implode('/', $pathElements); | |
613 | ||
614 | return self::getReturnUrl($menu, $newPath); | |
615 | } | |
616 | else { | |
617 | return array( | |
618 | CRM_Utils_Array::value('return_url', | |
619 | $menu[$path] | |
620 | ), | |
621 | CRM_Utils_Array::value('return_url_args', | |
622 | $menu[$path] | |
623 | ), | |
624 | ); | |
625 | } | |
626 | } | |
627 | ||
a0ee3941 EM |
628 | /** |
629 | * @param $menu | |
630 | * @param $path | |
631 | */ | |
00be9182 | 632 | public static function fillComponentIds(&$menu, $path) { |
6a488035 TO |
633 | static $cache = array(); |
634 | ||
635 | if (array_key_exists('component_id', $menu[$path])) { | |
636 | return; | |
637 | } | |
638 | ||
639 | $args = explode('/', $path); | |
640 | ||
641 | if (count($args) > 1) { | |
642 | $compPath = $args[0] . '/' . $args[1]; | |
643 | } | |
644 | else { | |
645 | $compPath = $args[0]; | |
646 | } | |
647 | ||
648 | $componentId = NULL; | |
649 | ||
650 | if (array_key_exists($compPath, $cache)) { | |
651 | $menu[$path]['component_id'] = $cache[$compPath]; | |
652 | } | |
653 | else { | |
654 | if (CRM_Utils_Array::value('component', CRM_Utils_Array::value($compPath, $menu))) { | |
655 | $componentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', | |
656 | $menu[$compPath]['component'], | |
657 | 'id', 'name' | |
658 | ); | |
659 | } | |
660 | $menu[$path]['component_id'] = $componentId ? $componentId : NULL; | |
661 | $cache[$compPath] = $menu[$path]['component_id']; | |
662 | } | |
663 | } | |
664 | ||
a0ee3941 | 665 | /** |
35146d69 | 666 | * @param $path string |
2bc65854 | 667 | * Path of menu item to retrieve. |
a0ee3941 | 668 | * |
35146d69 | 669 | * @return array |
2bc65854 | 670 | * Menu entry array. |
a0ee3941 | 671 | */ |
00be9182 | 672 | public static function get($path) { |
6a488035 TO |
673 | // return null if menu rebuild |
674 | $config = CRM_Core_Config::singleton(); | |
675 | ||
676 | $params = array(); | |
677 | ||
678 | $args = explode('/', $path); | |
679 | ||
680 | $elements = array(); | |
681 | while (!empty($args)) { | |
353ffa53 TO |
682 | $string = implode('/', $args); |
683 | $string = CRM_Core_DAO::escapeString($string); | |
6a488035 TO |
684 | $elements[] = "'{$string}'"; |
685 | array_pop($args); | |
686 | } | |
687 | ||
353ffa53 TO |
688 | $queryString = implode(', ', $elements); |
689 | $domainID = CRM_Core_Config::domainID(); | |
6a488035 TO |
690 | $domainWhereClause = " AND domain_id = $domainID "; |
691 | if ($config->isUpgradeMode() && | |
692 | !CRM_Core_DAO::checkFieldExists('civicrm_menu', 'domain_id') | |
693 | ) { | |
694 | //domain_id wouldn't be available for earlier version of | |
695 | //3.0 and therefore can't be used as part of query for | |
696 | //upgrade case | |
697 | $domainWhereClause = ""; | |
698 | } | |
699 | ||
700 | $query = " | |
701 | ( | |
702 | SELECT * | |
703 | FROM civicrm_menu | |
704 | WHERE path in ( $queryString ) | |
705 | $domainWhereClause | |
706 | ORDER BY length(path) DESC | |
707 | LIMIT 1 | |
708 | ) | |
709 | "; | |
710 | ||
711 | if ($path != 'navigation') { | |
712 | $query .= " | |
713 | UNION ( | |
714 | SELECT * | |
715 | FROM civicrm_menu | |
716 | WHERE path IN ( 'navigation' ) | |
717 | $domainWhereClause | |
718 | ) | |
719 | "; | |
720 | } | |
721 | ||
722 | $menu = new CRM_Core_DAO_Menu(); | |
723 | $menu->query($query); | |
724 | ||
725 | self::$_menuCache = array(); | |
726 | $menuPath = NULL; | |
727 | while ($menu->fetch()) { | |
728 | self::$_menuCache[$menu->path] = array(); | |
729 | CRM_Core_DAO::storeValues($menu, self::$_menuCache[$menu->path]); | |
730 | ||
b44dc91e TO |
731 | // Move module_data into main item. |
732 | if (isset(self::$_menuCache[$menu->path]['module_data'])) { | |
733 | CRM_Utils_Array::extend(self::$_menuCache[$menu->path], | |
734 | unserialize(self::$_menuCache[$menu->path]['module_data'])); | |
735 | unset(self::$_menuCache[$menu->path]['module_data']); | |
736 | } | |
737 | ||
738 | // Unserialize other elements. | |
6a488035 TO |
739 | foreach (self::$_serializedElements as $element) { |
740 | self::$_menuCache[$menu->path][$element] = unserialize($menu->$element); | |
741 | ||
742 | if (strpos($path, $menu->path) !== FALSE) { | |
743 | $menuPath = &self::$_menuCache[$menu->path]; | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | if (strstr($path, 'report/instance')) { | |
749 | $args = explode('/', $path); | |
750 | if (is_numeric(end($args))) { | |
751 | $menuPath['path'] .= '/' . end($args); | |
752 | } | |
753 | } | |
754 | ||
755 | // *FIXME* : hack for 2.1 -> 2.2 upgrades. | |
756 | if ($path == 'civicrm/upgrade') { | |
757 | $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade'; | |
758 | $menuPath['access_arguments'][0][] = 'administer CiviCRM'; | |
759 | $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu'); | |
760 | } | |
761 | // *FIXME* : hack for 4.1 -> 4.2 upgrades. | |
762 | if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) { | |
763 | CRM_Queue_Menu::alter($path, $menuPath); | |
764 | } | |
765 | ||
766 | // Part of upgrade framework but not run inside main upgrade because it deletes data | |
767 | // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string | |
768 | // which follows upgrade/ and checks for existence of a function in Cleanup class. | |
769 | if ($path == 'civicrm/upgrade/cleanup425') { | |
2aa397bc | 770 | $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425'); |
6a488035 TO |
771 | $menuPath['access_arguments'][0][] = 'administer CiviCRM'; |
772 | $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu'); | |
773 | } | |
774 | ||
775 | if (!empty($menuPath)) { | |
776 | $i18n = CRM_Core_I18n::singleton(); | |
777 | $i18n->localizeTitles($menuPath); | |
778 | } | |
779 | return $menuPath; | |
780 | } | |
781 | ||
a0ee3941 EM |
782 | /** |
783 | * @param $pathArgs | |
784 | * | |
785 | * @return mixed | |
786 | */ | |
00be9182 | 787 | public static function getArrayForPathArgs($pathArgs) { |
6a488035 TO |
788 | if (!is_string($pathArgs)) { |
789 | return; | |
790 | } | |
791 | $args = array(); | |
792 | ||
793 | $elements = explode(',', $pathArgs); | |
6a488035 | 794 | foreach ($elements as $keyVal) { |
3b4339fd | 795 | list($key, $val) = explode('=', $keyVal, 2); |
6a488035 TO |
796 | $arr[$key] = $val; |
797 | } | |
798 | ||
799 | if (array_key_exists('urlToSession', $arr)) { | |
800 | $urlToSession = array(); | |
801 | ||
802 | $params = explode(';', $arr['urlToSession']); | |
803 | $count = 0; | |
804 | foreach ($params as $keyVal) { | |
805 | list($urlToSession[$count]['urlVar'], | |
806 | $urlToSession[$count]['sessionVar'], | |
807 | $urlToSession[$count]['type'], | |
808 | $urlToSession[$count]['default'] | |
353ffa53 | 809 | ) = explode(':', $keyVal); |
6a488035 TO |
810 | $count++; |
811 | } | |
812 | $arr['urlToSession'] = $urlToSession; | |
813 | } | |
814 | return $arr; | |
815 | } | |
96025800 | 816 | |
6a488035 | 817 | } |