3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * This file contains the various menus of the CiviCRM module
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 require_once 'CRM/Core/I18n.php';
22 * Class CRM_Core_Menu.
27 * The list of menu items.
31 public static $_items = NULL;
34 * The list of permissioned menu items.
38 public static $_permissionedItems = NULL;
40 public static $_serializedElements = array(
48 public static $_menuCache = NULL;
52 * This function fetches the menu items from xml and xmlMenu hooks.
54 * @param bool $fetchFromXML
55 * Fetch the menu items from xml and not from cache.
59 public static function &xmlItems($fetchFromXML = FALSE) {
60 if (!self
::$_items ||
$fetchFromXML) {
61 $config = CRM_Core_Config
::singleton();
63 // We needs this until Core becomes a component
64 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
65 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
67 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
69 // Grab component menu files
70 $files = array_merge($files,
71 CRM_Core_Component
::xmlMenu()
74 // lets call a hook and get any additional files if needed
75 CRM_Utils_Hook
::xmlMenu($files);
77 self
::$_items = array();
78 foreach ($files as $file) {
79 self
::read($file, self
::$_items);
82 CRM_Utils_Hook
::alterMenu(self
::$_items);
94 * An alterable list of menu items.
98 public static function read($name, &$menu) {
99 $xml = simplexml_load_string(file_get_contents($name));
100 self
::readXML($xml, $menu);
104 * @param SimpleXMLElement $xml
105 * An XML document defining a list of menu items.
107 * An alterable list of menu items.
109 public static function readXML($xml, &$menu) {
110 $config = CRM_Core_Config
::singleton();
111 foreach ($xml->item
as $item) {
112 if (!(string ) $item->path
) {
113 CRM_Core_Error
::debug('i', $item);
114 CRM_Core_Error
::fatal();
116 $path = (string ) $item->path
;
117 $menu[$path] = array();
120 if ($item->ids_arguments
) {
122 foreach (array('json' => 'json', 'html' => 'html', 'exception' => 'exceptions') as $tag => $attr) {
123 $ids[$attr] = array();
124 foreach ($item->ids_arguments
->{$tag} as $value) {
125 $ids[$attr][] = (string) $value;
128 $menu[$path]['ids_arguments'] = $ids;
129 unset($item->ids_arguments
);
132 foreach ($item as $key => $value) {
133 $key = (string ) $key;
134 $value = (string ) $value;
135 if (strpos($key, '_callback') &&
138 // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value)
139 // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)).
140 $value = explode('::', $value);
142 elseif ($key == 'access_arguments') {
143 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
144 if (strpos($value, ',') ||
147 if (strpos($value, ',')) {
148 $elements = explode(',', $value);
152 $elements = explode(';', $value);
156 foreach ($elements as $element) {
159 $value = array($items, $op);
162 $value = array(array($value), 'and');
165 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
166 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
168 $menu[$path][$key] = $value;
174 * This function defines information for various menu items.
176 * @param bool $fetchFromXML
177 * Fetch the menu items from xml and not from cache.
181 public static function &items($fetchFromXML = FALSE) {
182 return self
::xmlItems($fetchFromXML);
186 * Is array true (whatever that means!).
188 * @param array $values
192 public static function isArrayTrue(&$values) {
193 foreach ($values as $name => $value) {
205 * @param string $path
209 public static function fillMenuValues(&$menu, $path) {
210 $fieldsToPropagate = array(
217 $fieldsPresent = array();
218 foreach ($fieldsToPropagate as $field) {
219 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
222 $args = explode('/', $path);
223 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
226 $parentPath = implode('/', $args);
228 foreach ($fieldsToPropagate as $field) {
229 if (!$fieldsPresent[$field]) {
230 $fieldInParentMenu = $menu[$parentPath][$field] ??
NULL;
231 if ($fieldInParentMenu !== NULL) {
232 $fieldsPresent[$field] = TRUE;
233 $menu[$path][$field] = $fieldInParentMenu;
239 if (self
::isArrayTrue($fieldsPresent)) {
244 foreach ($fieldsToPropagate as $field) {
245 if (!$fieldsPresent[$field]) {
246 $messages[] = ts("Could not find %1 in path tree",
251 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
255 * We use this function to.
257 * 1. Compute the breadcrumb
258 * 2. Compute local tasks value if any
259 * 3. Propagate access argument, access callback, page callback to the menu item
260 * 4. Build the global navigation block
264 public static function build(&$menu) {
265 foreach ($menu as $path => $menuItems) {
266 self
::buildBreadcrumb($menu, $path);
267 self
::fillMenuValues($menu, $path);
268 self
::fillComponentIds($menu, $path);
269 self
::buildReturnUrl($menu, $path);
271 // add add page_type if not present
272 if (!isset($menu[$path]['page_type'])) {
273 $menu[$path]['page_type'] = 0;
277 self
::buildAdminLinks($menu);
281 * This function recomputes menu from xml and populates civicrm_menu.
283 * @param bool $truncate
285 public static function store($truncate = TRUE) {
286 // first clean up the db
288 $query = 'TRUNCATE civicrm_menu';
289 CRM_Core_DAO
::executeQuery($query);
291 $menuArray = self
::items($truncate);
293 self
::build($menuArray);
295 $daoFields = CRM_Core_DAO_Menu
::fields();
297 foreach ($menuArray as $path => $item) {
298 $menu = new CRM_Core_DAO_Menu();
300 $menu->domain_id
= CRM_Core_Config
::domainID();
304 if (!CRM_Core_Config
::isUpgradeMode() ||
305 CRM_Core_BAO_SchemaHandler
::checkIfFieldExists('civicrm_menu', 'module_data', FALSE)
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];
316 $menu->module_data
= serialize($module_data);
319 $menu->copyValues($item);
321 foreach (self
::$_serializedElements as $element) {
322 if (!isset($item[$element]) ||
323 $item[$element] == 'null'
325 $menu->$element = NULL;
328 $menu->$element = serialize($item[$element]);
341 public static function buildAdminLinks(&$menu) {
344 foreach ($menu as $path => $item) {
345 if (empty($item['adminGroup'])) {
349 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
352 'title' => $item['title'],
353 'desc' => $item['desc'] ??
NULL,
354 'id' => strtr($item['title'], array(
361 'url' => CRM_Utils_System
::url($path, $query,
366 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
369 'icon' => $item['icon'] ??
NULL,
370 'extra' => $item['extra'] ??
NULL,
372 if (!array_key_exists($item['adminGroup'], $values)) {
373 $values[$item['adminGroup']] = array();
374 $values[$item['adminGroup']]['fields'] = array();
376 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
377 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
380 foreach ($values as $group => $dontCare) {
381 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
382 ksort($values[$group]);
385 $menu['admin'] = array('breadcrumb' => $values);
393 public static function &getAdminLinks() {
394 $links = self
::get('admin');
397 !isset($links['breadcrumb'])
402 $values = &$links['breadcrumb'];
407 * Get the breadcrumb for a given path.
410 * An array of all the menu items.
411 * @param string $path
412 * Path for which breadcrumb is to be build.
415 * The breadcrumb for this path
417 public static function buildBreadcrumb(&$menu, $path) {
420 $pathElements = explode('/', $path);
421 array_pop($pathElements);
424 while ($newPath = array_shift($pathElements)) {
425 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
427 // when we come across breadcrumb which involves ids,
428 // we should skip now and later on append dynamically.
429 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
433 // add to crumb, if current-path exists in params.
434 if (array_key_exists($currentPath, $menu) &&
435 isset($menu[$currentPath]['title'])
437 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
439 'title' => $menu[$currentPath]['title'],
440 'url' => CRM_Utils_System
::url($currentPath,
450 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
456 $menu[$path]['breadcrumb'] = $crumbs;
465 public static function buildReturnUrl(&$menu, $path) {
466 if (!isset($menu[$path]['return_url'])) {
467 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
477 public static function getReturnUrl(&$menu, $path) {
478 if (!isset($menu[$path]['return_url'])) {
479 $pathElements = explode('/', $path);
480 array_pop($pathElements);
482 if (empty($pathElements)) {
483 return array(NULL, NULL);
485 $newPath = implode('/', $pathElements);
487 return self
::getReturnUrl($menu, $newPath);
491 CRM_Utils_Array
::value('return_url',
494 CRM_Utils_Array
::value('return_url_args',
505 * @throws \CRM_Core_Exception
507 public static function fillComponentIds(&$menu, $path) {
508 static $cache = array();
510 if (array_key_exists('component_id', $menu[$path])) {
514 $args = explode('/', $path);
516 if (count($args) > 1) {
517 $compPath = $args[0] . '/' . $args[1];
520 $compPath = $args[0];
525 if (array_key_exists($compPath, $cache)) {
526 $menu[$path]['component_id'] = $cache[$compPath];
529 if (!empty($menu[$compPath]['component'])) {
530 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
531 $menu[$compPath]['component'],
535 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
536 $cache[$compPath] = $menu[$path]['component_id'];
541 * @param $path string
542 * Path of menu item to retrieve.
547 public static function get($path) {
548 // return null if menu rebuild
549 $config = CRM_Core_Config
::singleton();
551 $args = explode('/', $path);
554 while (!empty($args)) {
555 $string = implode('/', $args);
556 $string = CRM_Core_DAO
::escapeString($string);
557 $elements[] = "'{$string}'";
561 $queryString = implode(', ', $elements);
562 $domainID = CRM_Core_Config
::domainID();
568 WHERE path in ( $queryString )
569 AND domain_id = $domainID
570 ORDER BY length(path) DESC
575 if ($path != 'navigation') {
580 WHERE path IN ( 'navigation' )
581 AND domain_id = $domainID
586 $menu = new CRM_Core_DAO_Menu();
587 $menu->query($query);
589 self
::$_menuCache = array();
591 while ($menu->fetch()) {
592 self
::$_menuCache[$menu->path
] = array();
593 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
595 // Move module_data into main item.
596 if (isset(self
::$_menuCache[$menu->path
]['module_data'])) {
597 CRM_Utils_Array
::extend(self
::$_menuCache[$menu->path
],
598 CRM_Utils_String
::unserialize(self
::$_menuCache[$menu->path
]['module_data']));
599 unset(self
::$_menuCache[$menu->path
]['module_data']);
602 // Unserialize other elements.
603 foreach (self
::$_serializedElements as $element) {
604 self
::$_menuCache[$menu->path
][$element] = CRM_Utils_String
::unserialize($menu->$element);
606 if (strpos($path, $menu->path
) !== FALSE) {
607 $menuPath = &self
::$_menuCache[$menu->path
];
612 if (strstr($path, 'report/instance')) {
613 $args = explode('/', $path);
614 if (is_numeric(end($args))) {
615 $menuPath['path'] .= '/' . end($args);
619 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
620 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
621 CRM_Queue_Menu
::alter($path, $menuPath);
624 // Part of upgrade framework but not run inside main upgrade because it deletes data
625 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
626 // which follows upgrade/ and checks for existence of a function in Cleanup class.
627 if ($path == 'civicrm/upgrade/cleanup425') {
628 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
629 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
630 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
633 if (!empty($menuPath)) {
634 $i18n = CRM_Core_I18n
::singleton();
635 $i18n->localizeTitles($menuPath);
645 public static function getArrayForPathArgs($pathArgs) {
646 if (!is_string($pathArgs)) {
651 $elements = explode(',', $pathArgs);
652 foreach ($elements as $keyVal) {
653 list($key, $val) = explode('=', $keyVal, 2);
657 if (array_key_exists('urlToSession', $arr)) {
658 $urlToSession = array();
660 $params = explode(';', $arr['urlToSession']);
662 foreach ($params as $keyVal) {
663 list($urlToSession[$count]['urlVar'],
664 $urlToSession[$count]['sessionVar'],
665 $urlToSession[$count]['type'],
666 $urlToSession[$count]['default']
667 ) = explode(':', $keyVal);
670 $arr['urlToSession'] = $urlToSession;