3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This file contains the various menus of the CiviCRM module
32 * @copyright CiviCRM LLC (c) 2004-2015
35 require_once 'CRM/Core/I18n.php';
38 * Class CRM_Core_Menu.
43 * The list of menu items.
47 static $_items = NULL;
50 * The list of permissioned menu items.
54 static $_permissionedItems = NULL;
56 static $_serializedElements = array(
64 static $_menuCache = NULL;
68 * This function fetches the menu items from xml and xmlMenu hooks.
70 * @param bool $fetchFromXML
71 * Fetch the menu items from xml and not from cache.
75 public static function &xmlItems($fetchFromXML = FALSE) {
76 if (!self
::$_items ||
$fetchFromXML) {
77 $config = CRM_Core_Config
::singleton();
79 // We needs this until Core becomes a component
80 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
81 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
83 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
85 // Grab component menu files
86 $files = array_merge($files,
87 CRM_Core_Component
::xmlMenu()
90 // lets call a hook and get any additional files if needed
91 CRM_Utils_Hook
::xmlMenu($files);
93 self
::$_items = array();
94 foreach ($files as $file) {
95 self
::read($file, self
::$_items);
105 * @param string $name
106 * @param string $menu
110 public static function read($name, &$menu) {
112 $config = CRM_Core_Config
::singleton();
114 $xml = simplexml_load_file($name);
115 foreach ($xml->item
as $item) {
116 if (!(string ) $item->path
) {
117 CRM_Core_Error
::debug('i', $item);
118 CRM_Core_Error
::fatal();
120 $path = (string ) $item->path
;
121 $menu[$path] = array();
123 foreach ($item as $key => $value) {
124 $key = (string ) $key;
125 $value = (string ) $value;
126 if (strpos($key, '_callback') &&
129 // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value)
130 // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)).
131 $value = explode('::', $value);
133 elseif ($key == 'access_arguments') {
134 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
135 if (strpos($value, ',') ||
138 if (strpos($value, ',')) {
139 $elements = explode(',', $value);
143 $elements = explode(';', $value);
147 foreach ($elements as $element) {
150 $value = array($items, $op);
153 $value = array(array($value), 'and');
156 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
157 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
159 $menu[$path][$key] = $value;
165 * This function defines information for various menu items.
167 * @param bool $fetchFromXML
168 * Fetch the menu items from xml and not from cache.
172 public static function &items($fetchFromXML = FALSE) {
173 return self
::xmlItems($fetchFromXML);
177 * Is array true (whatever that means!).
179 * @param array $values
183 public static function isArrayTrue(&$values) {
184 foreach ($values as $name => $value) {
196 * @param string $path
200 public static function fillMenuValues(&$menu, $path) {
201 $fieldsToPropagate = array(
208 $fieldsPresent = array();
209 foreach ($fieldsToPropagate as $field) {
210 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
213 $args = explode('/', $path);
214 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
217 $parentPath = implode('/', $args);
219 foreach ($fieldsToPropagate as $field) {
220 if (!$fieldsPresent[$field]) {
221 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
222 $fieldsPresent[$field] = TRUE;
223 $menu[$path][$field] = $menu[$parentPath][$field];
229 if (self
::isArrayTrue($fieldsPresent)) {
234 foreach ($fieldsToPropagate as $field) {
235 if (!$fieldsPresent[$field]) {
236 $messages[] = ts("Could not find %1 in path tree",
241 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
245 * We use this function to.
247 * 1. Compute the breadcrumb
248 * 2. Compute local tasks value if any
249 * 3. Propagate access argument, access callback, page callback to the menu item
250 * 4. Build the global navigation block
254 public static function build(&$menu) {
255 foreach ($menu as $path => $menuItems) {
256 self
::buildBreadcrumb($menu, $path);
257 self
::fillMenuValues($menu, $path);
258 self
::fillComponentIds($menu, $path);
259 self
::buildReturnUrl($menu, $path);
261 // add add page_type if not present
262 if (!isset($menu[$path]['page_type'])) {
263 $menu[$path]['page_type'] = 0;
267 self
::buildAdminLinks($menu);
271 * This function recomputes menu from xml and populates civicrm_menu.
273 * @param bool $truncate
275 public static function store($truncate = TRUE) {
276 // first clean up the db
278 $query = 'TRUNCATE civicrm_menu';
279 CRM_Core_DAO
::executeQuery($query);
281 $menuArray = self
::items($truncate);
283 self
::build($menuArray);
285 $config = CRM_Core_Config
::singleton();
287 foreach ($menuArray as $path => $item) {
288 $menu = new CRM_Core_DAO_Menu();
290 $menu->domain_id
= CRM_Core_Config
::domainID();
294 $menu->copyValues($item);
296 foreach (self
::$_serializedElements as $element) {
297 if (!isset($item[$element]) ||
298 $item[$element] == 'null'
300 $menu->$element = NULL;
303 $menu->$element = serialize($item[$element]);
316 public static function buildAdminLinks(&$menu) {
319 foreach ($menu as $path => $item) {
320 if (empty($item['adminGroup'])) {
324 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
327 'title' => $item['title'],
328 'desc' => CRM_Utils_Array
::value('desc', $item),
329 'id' => strtr($item['title'], array(
337 'url' => CRM_Utils_System
::url($path, $query,
342 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
345 'icon' => CRM_Utils_Array
::value('icon', $item),
346 'extra' => CRM_Utils_Array
::value('extra', $item),
348 if (!array_key_exists($item['adminGroup'], $values)) {
349 $values[$item['adminGroup']] = array();
350 $values[$item['adminGroup']]['fields'] = array();
352 $weight = CRM_Utils_Array
::value('weight', $item, 0);
353 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
354 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
357 foreach ($values as $group => $dontCare) {
358 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
359 ksort($values[$group]);
362 $menu['admin'] = array('breadcrumb' => $values);
373 public static function &getNavigation($all = FALSE) {
374 CRM_Core_Error
::fatal();
376 if (!self
::$_menuCache) {
377 self
::get('navigation');
380 if (CRM_Core_Config
::isUpgradeMode()) {
384 if (!array_key_exists('navigation', self
::$_menuCache)) {
385 // problem could be due to menu table empty. Just do a
386 // menu store and try again
390 self
::get('navigation');
391 if (!array_key_exists('navigation', self
::$_menuCache)) {
392 CRM_Core_Error
::fatal();
395 $nav = &self
::$_menuCache['navigation'];
398 !isset($nav['breadcrumb'])
403 $values = &$nav['breadcrumb'];
404 $config = CRM_Core_Config
::singleton();
405 foreach ($values as $index => $item) {
406 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
410 $values[$index]['active'] = 'class="active"';
413 $values[$index]['active'] = '';
416 if ($values[$index]['parent']) {
417 $parent = $values[$index]['parent'];
419 // only reset if still a leaf
420 if ($values[$parent]['class'] == 'leaf') {
421 $values[$parent]['class'] = 'collapsed';
424 // if a child or the parent is active, expand the menu
425 if ($values[$index]['active'] ||
426 $values[$parent]['active']
428 $values[$parent]['class'] = 'expanded';
431 // make the parent inactive if the child is active
432 if ($values[$index]['active'] &&
433 $values[$parent]['active']
435 $values[$parent]['active'] = '';
441 // remove all collapsed menu items from the array
442 foreach ($values as $weight => $v) {
444 $values[$v['parent']]['class'] == 'collapsed'
446 unset($values[$weight]);
451 // check permissions for the rest
452 $activeChildren = array();
454 foreach ($values as $weight => $v) {
455 if (CRM_Core_Permission
::checkMenuItem($v)) {
457 $activeChildren[] = $weight;
461 unset($values[$weight]);
465 // add the start / end tags
466 $len = count($activeChildren) - 1;
468 $values[$activeChildren[0]]['start'] = TRUE;
469 $values[$activeChildren[$len]]['end'] = TRUE;
472 ksort($values, SORT_NUMERIC
);
473 $i18n = CRM_Core_I18n
::singleton();
474 $i18n->localizeTitles($values);
484 public static function &getAdminLinks() {
485 $links = self
::get('admin');
488 !isset($links['breadcrumb'])
493 $values = &$links['breadcrumb'];
498 * Get the breadcrumb for a given path.
501 * An array of all the menu items.
502 * @param string $path
503 * Path for which breadcrumb is to be build.
506 * The breadcrumb for this path
508 public static function buildBreadcrumb(&$menu, $path) {
511 $pathElements = explode('/', $path);
512 array_pop($pathElements);
515 while ($newPath = array_shift($pathElements)) {
516 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
518 // when we come across breadcrumb which involves ids,
519 // we should skip now and later on append dynamically.
520 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
524 // add to crumb, if current-path exists in params.
525 if (array_key_exists($currentPath, $menu) &&
526 isset($menu[$currentPath]['title'])
528 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
530 'title' => $menu[$currentPath]['title'],
531 'url' => CRM_Utils_System
::url($currentPath,
537 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
542 $menu[$path]['breadcrumb'] = $crumbs;
551 public static function buildReturnUrl(&$menu, $path) {
552 if (!isset($menu[$path]['return_url'])) {
553 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
563 public static function getReturnUrl(&$menu, $path) {
564 if (!isset($menu[$path]['return_url'])) {
565 $pathElements = explode('/', $path);
566 array_pop($pathElements);
568 if (empty($pathElements)) {
569 return array(NULL, NULL);
571 $newPath = implode('/', $pathElements);
573 return self
::getReturnUrl($menu, $newPath);
577 CRM_Utils_Array
::value('return_url',
580 CRM_Utils_Array
::value('return_url_args',
591 public static function fillComponentIds(&$menu, $path) {
592 static $cache = array();
594 if (array_key_exists('component_id', $menu[$path])) {
598 $args = explode('/', $path);
600 if (count($args) > 1) {
601 $compPath = $args[0] . '/' . $args[1];
604 $compPath = $args[0];
609 if (array_key_exists($compPath, $cache)) {
610 $menu[$path]['component_id'] = $cache[$compPath];
613 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
614 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
615 $menu[$compPath]['component'],
619 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
620 $cache[$compPath] = $menu[$path]['component_id'];
629 public static function get($path) {
630 // return null if menu rebuild
631 $config = CRM_Core_Config
::singleton();
635 $args = explode('/', $path);
638 while (!empty($args)) {
639 $string = implode('/', $args);
640 $string = CRM_Core_DAO
::escapeString($string);
641 $elements[] = "'{$string}'";
645 $queryString = implode(', ', $elements);
646 $domainID = CRM_Core_Config
::domainID();
647 $domainWhereClause = " AND domain_id = $domainID ";
648 if ($config->isUpgradeMode() &&
649 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
651 //domain_id wouldn't be available for earlier version of
652 //3.0 and therefore can't be used as part of query for
654 $domainWhereClause = "";
661 WHERE path in ( $queryString )
663 ORDER BY length(path) DESC
668 if ($path != 'navigation') {
673 WHERE path IN ( 'navigation' )
679 $menu = new CRM_Core_DAO_Menu();
680 $menu->query($query);
682 self
::$_menuCache = array();
684 while ($menu->fetch()) {
685 self
::$_menuCache[$menu->path
] = array();
686 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
688 foreach (self
::$_serializedElements as $element) {
689 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
691 if (strpos($path, $menu->path
) !== FALSE) {
692 $menuPath = &self
::$_menuCache[$menu->path
];
697 if (strstr($path, 'report/instance')) {
698 $args = explode('/', $path);
699 if (is_numeric(end($args))) {
700 $menuPath['path'] .= '/' . end($args);
704 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
705 if ($path == 'civicrm/upgrade') {
706 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
707 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
708 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
710 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
711 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
712 CRM_Queue_Menu
::alter($path, $menuPath);
715 // Part of upgrade framework but not run inside main upgrade because it deletes data
716 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
717 // which follows upgrade/ and checks for existence of a function in Cleanup class.
718 if ($path == 'civicrm/upgrade/cleanup425') {
719 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
720 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
721 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
724 if (!empty($menuPath)) {
725 $i18n = CRM_Core_I18n
::singleton();
726 $i18n->localizeTitles($menuPath);
736 public static function getArrayForPathArgs($pathArgs) {
737 if (!is_string($pathArgs)) {
742 $elements = explode(',', $pathArgs);
743 foreach ($elements as $keyVal) {
744 list($key, $val) = explode('=', $keyVal, 2);
748 if (array_key_exists('urlToSession', $arr)) {
749 $urlToSession = array();
751 $params = explode(';', $arr['urlToSession']);
753 foreach ($params as $keyVal) {
754 list($urlToSession[$count]['urlVar'],
755 $urlToSession[$count]['sessionVar'],
756 $urlToSession[$count]['type'],
757 $urlToSession[$count]['default']
758 ) = explode(':', $keyVal);
761 $arr['urlToSession'] = $urlToSession;