3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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);
98 CRM_Utils_Hook
::alterMenu(self
::$_items);
101 return self
::$_items;
107 * @param string $name
108 * @param string $menu
112 public static function read($name, &$menu) {
114 $config = CRM_Core_Config
::singleton();
116 $xml = simplexml_load_file($name);
117 foreach ($xml->item
as $item) {
118 if (!(string ) $item->path
) {
119 CRM_Core_Error
::debug('i', $item);
120 CRM_Core_Error
::fatal();
122 $path = (string ) $item->path
;
123 $menu[$path] = array();
125 foreach ($item as $key => $value) {
126 $key = (string ) $key;
127 $value = (string ) $value;
128 if (strpos($key, '_callback') &&
131 // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value)
132 // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)).
133 $value = explode('::', $value);
135 elseif ($key == 'access_arguments') {
136 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
137 if (strpos($value, ',') ||
140 if (strpos($value, ',')) {
141 $elements = explode(',', $value);
145 $elements = explode(';', $value);
149 foreach ($elements as $element) {
152 $value = array($items, $op);
155 $value = array(array($value), 'and');
158 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
159 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
161 $menu[$path][$key] = $value;
167 * This function defines information for various menu items.
169 * @param bool $fetchFromXML
170 * Fetch the menu items from xml and not from cache.
174 public static function &items($fetchFromXML = FALSE) {
175 return self
::xmlItems($fetchFromXML);
179 * Is array true (whatever that means!).
181 * @param array $values
185 public static function isArrayTrue(&$values) {
186 foreach ($values as $name => $value) {
198 * @param string $path
202 public static function fillMenuValues(&$menu, $path) {
203 $fieldsToPropagate = array(
210 $fieldsPresent = array();
211 foreach ($fieldsToPropagate as $field) {
212 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
215 $args = explode('/', $path);
216 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
219 $parentPath = implode('/', $args);
221 foreach ($fieldsToPropagate as $field) {
222 if (!$fieldsPresent[$field]) {
223 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
224 $fieldsPresent[$field] = TRUE;
225 $menu[$path][$field] = $menu[$parentPath][$field];
231 if (self
::isArrayTrue($fieldsPresent)) {
236 foreach ($fieldsToPropagate as $field) {
237 if (!$fieldsPresent[$field]) {
238 $messages[] = ts("Could not find %1 in path tree",
243 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
247 * We use this function to.
249 * 1. Compute the breadcrumb
250 * 2. Compute local tasks value if any
251 * 3. Propagate access argument, access callback, page callback to the menu item
252 * 4. Build the global navigation block
256 public static function build(&$menu) {
257 foreach ($menu as $path => $menuItems) {
258 self
::buildBreadcrumb($menu, $path);
259 self
::fillMenuValues($menu, $path);
260 self
::fillComponentIds($menu, $path);
261 self
::buildReturnUrl($menu, $path);
263 // add add page_type if not present
264 if (!isset($menu[$path]['page_type'])) {
265 $menu[$path]['page_type'] = 0;
269 self
::buildAdminLinks($menu);
273 * This function recomputes menu from xml and populates civicrm_menu.
275 * @param bool $truncate
277 public static function store($truncate = TRUE) {
278 // first clean up the db
280 $query = 'TRUNCATE civicrm_menu';
281 CRM_Core_DAO
::executeQuery($query);
283 $menuArray = self
::items($truncate);
285 self
::build($menuArray);
287 $config = CRM_Core_Config
::singleton();
289 foreach ($menuArray as $path => $item) {
290 $menu = new CRM_Core_DAO_Menu();
292 $menu->domain_id
= CRM_Core_Config
::domainID();
296 $menu->copyValues($item);
298 foreach (self
::$_serializedElements as $element) {
299 if (!isset($item[$element]) ||
300 $item[$element] == 'null'
302 $menu->$element = NULL;
305 $menu->$element = serialize($item[$element]);
318 public static function buildAdminLinks(&$menu) {
321 foreach ($menu as $path => $item) {
322 if (empty($item['adminGroup'])) {
326 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
329 'title' => $item['title'],
330 'desc' => CRM_Utils_Array
::value('desc', $item),
331 'id' => strtr($item['title'], array(
339 'url' => CRM_Utils_System
::url($path, $query,
344 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
347 'icon' => CRM_Utils_Array
::value('icon', $item),
348 'extra' => CRM_Utils_Array
::value('extra', $item),
350 if (!array_key_exists($item['adminGroup'], $values)) {
351 $values[$item['adminGroup']] = array();
352 $values[$item['adminGroup']]['fields'] = array();
354 $weight = CRM_Utils_Array
::value('weight', $item, 0);
355 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
356 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
359 foreach ($values as $group => $dontCare) {
360 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
361 ksort($values[$group]);
364 $menu['admin'] = array('breadcrumb' => $values);
375 public static function &getNavigation($all = FALSE) {
376 CRM_Core_Error
::fatal();
378 if (!self
::$_menuCache) {
379 self
::get('navigation');
382 if (CRM_Core_Config
::isUpgradeMode()) {
386 if (!array_key_exists('navigation', self
::$_menuCache)) {
387 // problem could be due to menu table empty. Just do a
388 // menu store and try again
392 self
::get('navigation');
393 if (!array_key_exists('navigation', self
::$_menuCache)) {
394 CRM_Core_Error
::fatal();
397 $nav = &self
::$_menuCache['navigation'];
400 !isset($nav['breadcrumb'])
405 $values = &$nav['breadcrumb'];
406 $config = CRM_Core_Config
::singleton();
407 foreach ($values as $index => $item) {
408 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
412 $values[$index]['active'] = 'class="active"';
415 $values[$index]['active'] = '';
418 if ($values[$index]['parent']) {
419 $parent = $values[$index]['parent'];
421 // only reset if still a leaf
422 if ($values[$parent]['class'] == 'leaf') {
423 $values[$parent]['class'] = 'collapsed';
426 // if a child or the parent is active, expand the menu
427 if ($values[$index]['active'] ||
428 $values[$parent]['active']
430 $values[$parent]['class'] = 'expanded';
433 // make the parent inactive if the child is active
434 if ($values[$index]['active'] &&
435 $values[$parent]['active']
437 $values[$parent]['active'] = '';
443 // remove all collapsed menu items from the array
444 foreach ($values as $weight => $v) {
446 $values[$v['parent']]['class'] == 'collapsed'
448 unset($values[$weight]);
453 // check permissions for the rest
454 $activeChildren = array();
456 foreach ($values as $weight => $v) {
457 if (CRM_Core_Permission
::checkMenuItem($v)) {
459 $activeChildren[] = $weight;
463 unset($values[$weight]);
467 // add the start / end tags
468 $len = count($activeChildren) - 1;
470 $values[$activeChildren[0]]['start'] = TRUE;
471 $values[$activeChildren[$len]]['end'] = TRUE;
474 ksort($values, SORT_NUMERIC
);
475 $i18n = CRM_Core_I18n
::singleton();
476 $i18n->localizeTitles($values);
486 public static function &getAdminLinks() {
487 $links = self
::get('admin');
490 !isset($links['breadcrumb'])
495 $values = &$links['breadcrumb'];
500 * Get the breadcrumb for a given path.
503 * An array of all the menu items.
504 * @param string $path
505 * Path for which breadcrumb is to be build.
508 * The breadcrumb for this path
510 public static function buildBreadcrumb(&$menu, $path) {
513 $pathElements = explode('/', $path);
514 array_pop($pathElements);
517 while ($newPath = array_shift($pathElements)) {
518 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
520 // when we come across breadcrumb which involves ids,
521 // we should skip now and later on append dynamically.
522 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
526 // add to crumb, if current-path exists in params.
527 if (array_key_exists($currentPath, $menu) &&
528 isset($menu[$currentPath]['title'])
530 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
532 'title' => $menu[$currentPath]['title'],
533 'url' => CRM_Utils_System
::url($currentPath,
539 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
544 $menu[$path]['breadcrumb'] = $crumbs;
553 public static function buildReturnUrl(&$menu, $path) {
554 if (!isset($menu[$path]['return_url'])) {
555 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
565 public static function getReturnUrl(&$menu, $path) {
566 if (!isset($menu[$path]['return_url'])) {
567 $pathElements = explode('/', $path);
568 array_pop($pathElements);
570 if (empty($pathElements)) {
571 return array(NULL, NULL);
573 $newPath = implode('/', $pathElements);
575 return self
::getReturnUrl($menu, $newPath);
579 CRM_Utils_Array
::value('return_url',
582 CRM_Utils_Array
::value('return_url_args',
593 public static function fillComponentIds(&$menu, $path) {
594 static $cache = array();
596 if (array_key_exists('component_id', $menu[$path])) {
600 $args = explode('/', $path);
602 if (count($args) > 1) {
603 $compPath = $args[0] . '/' . $args[1];
606 $compPath = $args[0];
611 if (array_key_exists($compPath, $cache)) {
612 $menu[$path]['component_id'] = $cache[$compPath];
615 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
616 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
617 $menu[$compPath]['component'],
621 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
622 $cache[$compPath] = $menu[$path]['component_id'];
627 * @param $path string
628 * Path of menu item to retrieve.
633 public static function get($path) {
634 // return null if menu rebuild
635 $config = CRM_Core_Config
::singleton();
639 $args = explode('/', $path);
642 while (!empty($args)) {
643 $string = implode('/', $args);
644 $string = CRM_Core_DAO
::escapeString($string);
645 $elements[] = "'{$string}'";
649 $queryString = implode(', ', $elements);
650 $domainID = CRM_Core_Config
::domainID();
651 $domainWhereClause = " AND domain_id = $domainID ";
652 if ($config->isUpgradeMode() &&
653 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
655 //domain_id wouldn't be available for earlier version of
656 //3.0 and therefore can't be used as part of query for
658 $domainWhereClause = "";
665 WHERE path in ( $queryString )
667 ORDER BY length(path) DESC
672 if ($path != 'navigation') {
677 WHERE path IN ( 'navigation' )
683 $menu = new CRM_Core_DAO_Menu();
684 $menu->query($query);
686 self
::$_menuCache = array();
688 while ($menu->fetch()) {
689 self
::$_menuCache[$menu->path
] = array();
690 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
692 foreach (self
::$_serializedElements as $element) {
693 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
695 if (strpos($path, $menu->path
) !== FALSE) {
696 $menuPath = &self
::$_menuCache[$menu->path
];
701 if (strstr($path, 'report/instance')) {
702 $args = explode('/', $path);
703 if (is_numeric(end($args))) {
704 $menuPath['path'] .= '/' . end($args);
708 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
709 if ($path == 'civicrm/upgrade') {
710 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
711 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
712 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
714 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
715 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
716 CRM_Queue_Menu
::alter($path, $menuPath);
719 // Part of upgrade framework but not run inside main upgrade because it deletes data
720 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
721 // which follows upgrade/ and checks for existence of a function in Cleanup class.
722 if ($path == 'civicrm/upgrade/cleanup425') {
723 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
724 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
725 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
728 if (!empty($menuPath)) {
729 $i18n = CRM_Core_I18n
::singleton();
730 $i18n->localizeTitles($menuPath);
740 public static function getArrayForPathArgs($pathArgs) {
741 if (!is_string($pathArgs)) {
746 $elements = explode(',', $pathArgs);
747 foreach ($elements as $keyVal) {
748 list($key, $val) = explode('=', $keyVal, 2);
752 if (array_key_exists('urlToSession', $arr)) {
753 $urlToSession = array();
755 $params = explode(';', $arr['urlToSession']);
757 foreach ($params as $keyVal) {
758 list($urlToSession[$count]['urlVar'],
759 $urlToSession[$count]['sessionVar'],
760 $urlToSession[$count]['type'],
761 $urlToSession[$count]['default']
762 ) = explode(':', $keyVal);
765 $arr['urlToSession'] = $urlToSession;