ensure recur options are present on backend cc contribution form.
[civicrm-core.git] / CRM / Core / Menu.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This file contains the various menus of the CiviCRM module
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 */
18
19require_once 'CRM/Core/I18n.php';
28518c90
EM
20
21/**
ad37ac8e 22 * Class CRM_Core_Menu.
28518c90 23 */
6a488035
TO
24class CRM_Core_Menu {
25
26 /**
fe482240 27 * The list of menu items.
6a488035
TO
28 *
29 * @var array
6a488035 30 */
518fa0ee 31 public static $_items = NULL;
6a488035
TO
32
33 /**
fe482240 34 * The list of permissioned menu items.
6a488035
TO
35 *
36 * @var array
6a488035 37 */
518fa0ee 38 public static $_permissionedItems = NULL;
6a488035 39
518fa0ee 40 public static $_serializedElements = array(
6a488035
TO
41 'access_arguments',
42 'access_callback',
43 'page_arguments',
44 'page_callback',
45 'breadcrumb',
46 );
47
518fa0ee 48 public static $_menuCache = NULL;
7da04cde 49 const MENU_ITEM = 1;
6a488035 50
a0ee3941 51 /**
fe482240 52 * This function fetches the menu items from xml and xmlMenu hooks.
49d97c48 53 *
ad37ac8e 54 * @param bool $fetchFromXML
6a0b768e 55 * Fetch the menu items from xml and not from cache.
49d97c48 56 *
a0ee3941
EM
57 * @return array
58 */
00be9182 59 public static function &xmlItems($fetchFromXML = FALSE) {
49d97c48 60 if (!self::$_items || $fetchFromXML) {
6a488035
TO
61 $config = CRM_Core_Config::singleton();
62
63 // We needs this until Core becomes a component
64 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
65 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR, $coreMenuFilesNamespace);
66 global $civicrm_root;
67 $files = CRM_Utils_File::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR . $coreMenuFilesPath, 'xml');
68
69 // Grab component menu files
70 $files = array_merge($files,
71 CRM_Core_Component::xmlMenu()
72 );
73
74 // lets call a hook and get any additional files if needed
75 CRM_Utils_Hook::xmlMenu($files);
76
affcc9d2 77 self::$_items = [];
6a488035
TO
78 foreach ($files as $file) {
79 self::read($file, self::$_items);
80 }
7dc34fb8
TO
81
82 CRM_Utils_Hook::alterMenu(self::$_items);
6a488035
TO
83 }
84
85 return self::$_items;
86 }
87
a0ee3941 88 /**
ad37ac8e 89 * Read menu.
90 *
100fef9d 91 * @param string $name
5bfe764c
TO
92 * File name
93 * @param array $menu
94 * An alterable list of menu items.
a0ee3941
EM
95 *
96 * @throws Exception
97 */
00be9182 98 public static function read($name, &$menu) {
292fbfa3 99 $xml = simplexml_load_string(file_get_contents($name));
5bfe764c
TO
100 self::readXML($xml, $menu);
101 }
6a488035 102
5bfe764c
TO
103 /**
104 * @param SimpleXMLElement $xml
105 * An XML document defining a list of menu items.
106 * @param array $menu
107 * An alterable list of menu items.
ac15829d
SL
108 *
109 * @throws CRM_Core_Exception
5bfe764c
TO
110 */
111 public static function readXML($xml, &$menu) {
6a488035 112 $config = CRM_Core_Config::singleton();
6a488035
TO
113 foreach ($xml->item as $item) {
114 if (!(string ) $item->path) {
115 CRM_Core_Error::debug('i', $item);
ac15829d 116 throw new CRM_Core_Exception('Unable to read XML file');
6a488035
TO
117 }
118 $path = (string ) $item->path;
affcc9d2 119 $menu[$path] = [];
6a488035 120 unset($item->path);
4535c1f5
TO
121
122 if ($item->ids_arguments) {
affcc9d2 123 $ids = [];
36d4fa1b 124 foreach (array('json' => 'json', 'html' => 'html', 'exception' => 'exceptions') as $tag => $attr) {
affcc9d2 125 $ids[$attr] = [];
36d4fa1b
TO
126 foreach ($item->ids_arguments->{$tag} as $value) {
127 $ids[$attr][] = (string) $value;
4535c1f5
TO
128 }
129 }
130 $menu[$path]['ids_arguments'] = $ids;
131 unset($item->ids_arguments);
132 }
133
6a488035
TO
134 foreach ($item as $key => $value) {
135 $key = (string ) $key;
136 $value = (string ) $value;
137 if (strpos($key, '_callback') &&
138 strpos($value, '::')
139 ) {
c8074a93
TO
140 // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value)
141 // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)).
6a488035
TO
142 $value = explode('::', $value);
143 }
144 elseif ($key == 'access_arguments') {
c8074a93 145 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
6a488035
TO
146 if (strpos($value, ',') ||
147 strpos($value, ';')
148 ) {
149 if (strpos($value, ',')) {
150 $elements = explode(',', $value);
151 $op = 'and';
152 }
153 else {
154 $elements = explode(';', $value);
155 $op = 'or';
156 }
affcc9d2 157 $items = [];
6a488035
TO
158 foreach ($elements as $element) {
159 $items[] = $element;
160 }
161 $value = array($items, $op);
162 }
163 else {
164 $value = array(array($value), 'and');
165 }
166 }
167 elseif ($key == 'is_public' || $key == 'is_ssl') {
168 $value = ($value == 'true' || $value == 1) ? 1 : 0;
169 }
170 $menu[$path][$key] = $value;
171 }
172 }
173 }
174
175 /**
fe482240 176 * This function defines information for various menu items.
6a488035 177 *
ad37ac8e 178 * @param bool $fetchFromXML
6a0b768e 179 * Fetch the menu items from xml and not from cache.
49d97c48 180 *
7a9ab499 181 * @return array
6a488035 182 */
00be9182 183 public static function &items($fetchFromXML = FALSE) {
49d97c48 184 return self::xmlItems($fetchFromXML);
6a488035
TO
185 }
186
a0ee3941 187 /**
ad37ac8e 188 * Is array true (whatever that means!).
189 *
190 * @param array $values
a0ee3941
EM
191 *
192 * @return bool
193 */
00be9182 194 public static function isArrayTrue(&$values) {
6a488035
TO
195 foreach ($values as $name => $value) {
196 if (!$value) {
197 return FALSE;
198 }
199 }
200 return TRUE;
201 }
202
a0ee3941 203 /**
ad37ac8e 204 * Fill menu values.
205 *
206 * @param array $menu
207 * @param string $path
a0ee3941 208 *
ac15829d 209 * @throws CRM_Core_Exception
a0ee3941 210 */
00be9182 211 public static function fillMenuValues(&$menu, $path) {
6a488035
TO
212 $fieldsToPropagate = array(
213 'access_callback',
214 'access_arguments',
215 'page_callback',
216 'page_arguments',
217 'is_ssl',
218 );
affcc9d2 219 $fieldsPresent = [];
6a488035 220 foreach ($fieldsToPropagate as $field) {
63d76404 221 $fieldsPresent[$field] = isset($menu[$path][$field]);
6a488035
TO
222 }
223
224 $args = explode('/', $path);
225 while (!self::isArrayTrue($fieldsPresent) && !empty($args)) {
226
227 array_pop($args);
228 $parentPath = implode('/', $args);
229
230 foreach ($fieldsToPropagate as $field) {
231 if (!$fieldsPresent[$field]) {
8d9ce16a 232 $fieldInParentMenu = $menu[$parentPath][$field] ?? NULL;
233 if ($fieldInParentMenu !== NULL) {
6a488035 234 $fieldsPresent[$field] = TRUE;
8d9ce16a 235 $menu[$path][$field] = $fieldInParentMenu;
6a488035
TO
236 }
237 }
238 }
239 }
240
241 if (self::isArrayTrue($fieldsPresent)) {
242 return;
243 }
244
ac15829d 245 $messages = [];
6a488035
TO
246 foreach ($fieldsToPropagate as $field) {
247 if (!$fieldsPresent[$field]) {
248 $messages[] = ts("Could not find %1 in path tree",
ac15829d 249 [1 => $field]
6a488035
TO
250 );
251 }
252 }
ac15829d 253 throw new CRM_Core_Exception("'$path': " . implode(', ', $messages));
6a488035
TO
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 267 foreach ($menu as $path => $menuItems) {
bc537a0c 268 try {
269 self::buildBreadcrumb($menu, $path);
270 self::fillMenuValues($menu, $path);
271 self::fillComponentIds($menu, $path);
272 self::buildReturnUrl($menu, $path);
273
274 // add add page_type if not present
275 if (!isset($menu[$path]['page_type'])) {
276 $menu[$path]['page_type'] = 0;
277 }
278 }
279 catch (CRM_Core_Exception $e) {
280 Civi::log()->error('Menu path skipped:' . $e->getMessage());
6a488035
TO
281 }
282 }
283
284 self::buildAdminLinks($menu);
285 }
286
a0ee3941 287 /**
fe482240 288 * This function recomputes menu from xml and populates civicrm_menu.
ad37ac8e 289 *
a0ee3941
EM
290 * @param bool $truncate
291 */
00be9182 292 public static function store($truncate = TRUE) {
6a488035
TO
293 // first clean up the db
294 if ($truncate) {
295 $query = 'TRUNCATE civicrm_menu';
296 CRM_Core_DAO::executeQuery($query);
297 }
49d97c48 298 $menuArray = self::items($truncate);
6a488035
TO
299
300 self::build($menuArray);
301
b44dc91e 302 $daoFields = CRM_Core_DAO_Menu::fields();
6a488035
TO
303
304 foreach ($menuArray as $path => $item) {
353ffa53
TO
305 $menu = new CRM_Core_DAO_Menu();
306 $menu->path = $path;
6a488035
TO
307 $menu->domain_id = CRM_Core_Config::domainID();
308
309 $menu->find(TRUE);
310
d6f1a16c 311 if (!CRM_Core_Config::isUpgradeMode() ||
eed7e803 312 CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_menu', 'module_data', FALSE)
d6f1a16c
NM
313 ) {
314 // Move unrecognized fields to $module_data.
affcc9d2 315 $module_data = [];
d6f1a16c
NM
316 foreach (array_keys($item) as $key) {
317 if (!isset($daoFields[$key])) {
318 $module_data[$key] = $item[$key];
319 unset($item[$key]);
320 }
b44dc91e 321 }
b44dc91e 322
d6f1a16c
NM
323 $menu->module_data = serialize($module_data);
324 }
6a488035 325
c09129a5
NM
326 $menu->copyValues($item);
327
6a488035
TO
328 foreach (self::$_serializedElements as $element) {
329 if (!isset($item[$element]) ||
330 $item[$element] == 'null'
331 ) {
332 $menu->$element = NULL;
333 }
334 else {
335 $menu->$element = serialize($item[$element]);
336 }
337 }
338
339 $menu->save();
340 }
341 }
342
a0ee3941 343 /**
ad37ac8e 344 * Build admin links.
345 *
346 * @param array $menu
a0ee3941 347 */
00be9182 348 public static function buildAdminLinks(&$menu) {
affcc9d2 349 $values = [];
6a488035
TO
350
351 foreach ($menu as $path => $item) {
a7488080 352 if (empty($item['adminGroup'])) {
6a488035
TO
353 continue;
354 }
355
0d8afee2 356 $query = !empty($item['path_arguments']) ? str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
6a488035
TO
357
358 $value = array(
359 'title' => $item['title'],
6b409353 360 'desc' => $item['desc'] ?? NULL,
6a488035 361 'id' => strtr($item['title'], array(
518fa0ee
SL
362 '(' => '_',
363 ')' => '',
364 ' ' => '',
365 ',' => '_',
366 '/' => '_',
367 )),
d75f2f47 368 'url' => CRM_Utils_System::url($path, $query,
ad37ac8e 369 FALSE,
370 NULL,
371 TRUE,
372 FALSE,
373 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
374 TRUE
f4bdec6a 375 ),
6b409353
CW
376 'icon' => $item['icon'] ?? NULL,
377 'extra' => $item['extra'] ?? NULL,
6a488035
TO
378 );
379 if (!array_key_exists($item['adminGroup'], $values)) {
affcc9d2
CW
380 $values[$item['adminGroup']] = [];
381 $values[$item['adminGroup']]['fields'] = [];
6a488035 382 }
6a488035
TO
383 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
384 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
385 }
386
387 foreach ($values as $group => $dontCare) {
6a488035
TO
388 ksort($values[$group]);
389 }
390
391 $menu['admin'] = array('breadcrumb' => $values);
392 }
393
a0ee3941 394 /**
ad37ac8e 395 * Get admin links.
396 *
a0ee3941
EM
397 * @return null
398 */
00be9182 399 public static function &getAdminLinks() {
6a488035
TO
400 $links = self::get('admin');
401
402 if (!$links ||
403 !isset($links['breadcrumb'])
404 ) {
405 return NULL;
406 }
407
408 $values = &$links['breadcrumb'];
409 return $values;
410 }
411
412 /**
413 * Get the breadcrumb for a given path.
414 *
6a0b768e
TO
415 * @param array $menu
416 * An array of all the menu items.
417 * @param string $path
418 * Path for which breadcrumb is to be build.
6a488035 419 *
a6c01b45
CW
420 * @return array
421 * The breadcrumb for this path
6a488035 422 */
00be9182 423 public static function buildBreadcrumb(&$menu, $path) {
affcc9d2 424 $crumbs = [];
6a488035
TO
425
426 $pathElements = explode('/', $path);
427 array_pop($pathElements);
428
429 $currentPath = NULL;
430 while ($newPath = array_shift($pathElements)) {
431 $currentPath = $currentPath ? ($currentPath . '/' . $newPath) : $newPath;
432
b44e3f84 433 // when we come across breadcrumb which involves ids,
6a488035
TO
434 // we should skip now and later on append dynamically.
435 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
436 continue;
437 }
438
439 // add to crumb, if current-path exists in params.
440 if (array_key_exists($currentPath, $menu) &&
441 isset($menu[$currentPath]['title'])
442 ) {
0d8afee2 443 $urlVar = !empty($menu[$currentPath]['path_arguments']) ? '&' . $menu[$currentPath]['path_arguments'] : '';
6a488035
TO
444 $crumbs[] = array(
445 'title' => $menu[$currentPath]['title'],
446 'url' => CRM_Utils_System::url($currentPath,
38a0e912 447 'reset=1' . $urlVar,
518fa0ee
SL
448 // absolute
449 FALSE,
450 // fragment
451 NULL,
452 // htmlize
453 TRUE,
454 // frontend
455 FALSE,
456 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
457 TRUE
6a488035
TO
458 ),
459 );
460 }
461 }
462 $menu[$path]['breadcrumb'] = $crumbs;
463
464 return $crumbs;
465 }
466
a0ee3941
EM
467 /**
468 * @param $menu
469 * @param $path
470 */
00be9182 471 public static function buildReturnUrl(&$menu, $path) {
6a488035 472 if (!isset($menu[$path]['return_url'])) {
bc537a0c 473 [$menu[$path]['return_url'], $menu[$path]['return_url_args']] = self::getReturnUrl($menu, $path);
6a488035
TO
474 }
475 }
476
a0ee3941
EM
477 /**
478 * @param $menu
479 * @param $path
480 *
481 * @return array
482 */
00be9182 483 public static function getReturnUrl(&$menu, $path) {
6a488035
TO
484 if (!isset($menu[$path]['return_url'])) {
485 $pathElements = explode('/', $path);
486 array_pop($pathElements);
487
488 if (empty($pathElements)) {
489 return array(NULL, NULL);
490 }
491 $newPath = implode('/', $pathElements);
492
493 return self::getReturnUrl($menu, $newPath);
494 }
495 else {
496 return array(
497 CRM_Utils_Array::value('return_url',
498 $menu[$path]
499 ),
500 CRM_Utils_Array::value('return_url_args',
501 $menu[$path]
502 ),
503 );
504 }
505 }
506
a0ee3941
EM
507 /**
508 * @param $menu
509 * @param $path
508db412 510 *
511 * @throws \CRM_Core_Exception
a0ee3941 512 */
00be9182 513 public static function fillComponentIds(&$menu, $path) {
affcc9d2 514 static $cache = [];
6a488035
TO
515
516 if (array_key_exists('component_id', $menu[$path])) {
517 return;
518 }
519
520 $args = explode('/', $path);
521
522 if (count($args) > 1) {
523 $compPath = $args[0] . '/' . $args[1];
524 }
525 else {
526 $compPath = $args[0];
527 }
528
529 $componentId = NULL;
530
531 if (array_key_exists($compPath, $cache)) {
532 $menu[$path]['component_id'] = $cache[$compPath];
533 }
534 else {
508db412 535 if (!empty($menu[$compPath]['component'])) {
6a488035
TO
536 $componentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component',
537 $menu[$compPath]['component'],
538 'id', 'name'
539 );
540 }
541 $menu[$path]['component_id'] = $componentId ? $componentId : NULL;
542 $cache[$compPath] = $menu[$path]['component_id'];
543 }
544 }
545
a0ee3941 546 /**
35146d69 547 * @param $path string
2bc65854 548 * Path of menu item to retrieve.
a0ee3941 549 *
35146d69 550 * @return array
2bc65854 551 * Menu entry array.
a0ee3941 552 */
00be9182 553 public static function get($path) {
6a488035
TO
554 // return null if menu rebuild
555 $config = CRM_Core_Config::singleton();
556
6a488035
TO
557 $args = explode('/', $path);
558
affcc9d2 559 $elements = [];
6a488035 560 while (!empty($args)) {
353ffa53
TO
561 $string = implode('/', $args);
562 $string = CRM_Core_DAO::escapeString($string);
6a488035
TO
563 $elements[] = "'{$string}'";
564 array_pop($args);
565 }
566
353ffa53
TO
567 $queryString = implode(', ', $elements);
568 $domainID = CRM_Core_Config::domainID();
6a488035
TO
569
570 $query = "
571(
572 SELECT *
573 FROM civicrm_menu
574 WHERE path in ( $queryString )
99a2c00a 575 AND domain_id = $domainID
6a488035
TO
576 ORDER BY length(path) DESC
577 LIMIT 1
578)
579";
580
581 if ($path != 'navigation') {
582 $query .= "
583UNION (
584 SELECT *
585 FROM civicrm_menu
586 WHERE path IN ( 'navigation' )
99a2c00a 587 AND domain_id = $domainID
6a488035
TO
588)
589";
590 }
591
592 $menu = new CRM_Core_DAO_Menu();
593 $menu->query($query);
594
affcc9d2 595 self::$_menuCache = [];
6a488035
TO
596 $menuPath = NULL;
597 while ($menu->fetch()) {
affcc9d2 598 self::$_menuCache[$menu->path] = [];
6a488035
TO
599 CRM_Core_DAO::storeValues($menu, self::$_menuCache[$menu->path]);
600
b44dc91e
TO
601 // Move module_data into main item.
602 if (isset(self::$_menuCache[$menu->path]['module_data'])) {
603 CRM_Utils_Array::extend(self::$_menuCache[$menu->path],
f24846d5 604 CRM_Utils_String::unserialize(self::$_menuCache[$menu->path]['module_data']));
b44dc91e
TO
605 unset(self::$_menuCache[$menu->path]['module_data']);
606 }
607
608 // Unserialize other elements.
6a488035 609 foreach (self::$_serializedElements as $element) {
f24846d5 610 self::$_menuCache[$menu->path][$element] = CRM_Utils_String::unserialize($menu->$element);
6a488035
TO
611
612 if (strpos($path, $menu->path) !== FALSE) {
613 $menuPath = &self::$_menuCache[$menu->path];
614 }
615 }
616 }
617
618 if (strstr($path, 'report/instance')) {
619 $args = explode('/', $path);
620 if (is_numeric(end($args))) {
621 $menuPath['path'] .= '/' . end($args);
622 }
623 }
624
6a488035
TO
625 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
626 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
627 CRM_Queue_Menu::alter($path, $menuPath);
628 }
629
630 // Part of upgrade framework but not run inside main upgrade because it deletes data
631 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
632 // which follows upgrade/ and checks for existence of a function in Cleanup class.
633 if ($path == 'civicrm/upgrade/cleanup425') {
2aa397bc 634 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
6a488035
TO
635 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
636 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
637 }
638
639 if (!empty($menuPath)) {
640 $i18n = CRM_Core_I18n::singleton();
641 $i18n->localizeTitles($menuPath);
642 }
643 return $menuPath;
644 }
645
a0ee3941
EM
646 /**
647 * @param $pathArgs
648 *
649 * @return mixed
650 */
00be9182 651 public static function getArrayForPathArgs($pathArgs) {
6a488035
TO
652 if (!is_string($pathArgs)) {
653 return;
654 }
affcc9d2 655 $args = [];
6a488035
TO
656
657 $elements = explode(',', $pathArgs);
6a488035 658 foreach ($elements as $keyVal) {
3b4339fd 659 list($key, $val) = explode('=', $keyVal, 2);
6a488035
TO
660 $arr[$key] = $val;
661 }
662
663 if (array_key_exists('urlToSession', $arr)) {
affcc9d2 664 $urlToSession = [];
6a488035
TO
665
666 $params = explode(';', $arr['urlToSession']);
667 $count = 0;
668 foreach ($params as $keyVal) {
669 list($urlToSession[$count]['urlVar'],
670 $urlToSession[$count]['sessionVar'],
671 $urlToSession[$count]['type'],
672 $urlToSession[$count]['default']
353ffa53 673 ) = explode(':', $keyVal);
6a488035
TO
674 $count++;
675 }
676 $arr['urlToSession'] = $urlToSession;
677 }
678 return $arr;
679 }
96025800 680
6a488035 681}