ff45aeebb37b87192385696de755755ce7c4d725
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
252 public static function build(&$menu) {
253 foreach ($menu as $path => $menuItems) {
254 self
::buildBreadcrumb($menu, $path);
255 self
::fillMenuValues($menu, $path);
256 self
::fillComponentIds($menu, $path);
257 self
::buildReturnUrl($menu, $path);
259 // add add page_type if not present
260 if (!isset($menu[$path]['page_type'])) {
261 $menu[$path]['page_type'] = 0;
265 self
::buildAdminLinks($menu);
269 * This function recomputes menu from xml and populates civicrm_menu.
271 * @param bool $truncate
273 public static function store($truncate = TRUE) {
274 // first clean up the db
276 $query = 'TRUNCATE civicrm_menu';
277 CRM_Core_DAO
::executeQuery($query);
279 $menuArray = self
::items($truncate);
281 self
::build($menuArray);
283 $config = CRM_Core_Config
::singleton();
285 foreach ($menuArray as $path => $item) {
286 $menu = new CRM_Core_DAO_Menu();
288 $menu->domain_id
= CRM_Core_Config
::domainID();
292 $menu->copyValues($item);
294 foreach (self
::$_serializedElements as $element) {
295 if (!isset($item[$element]) ||
296 $item[$element] == 'null'
298 $menu->$element = NULL;
301 $menu->$element = serialize($item[$element]);
314 public static function buildAdminLinks(&$menu) {
317 foreach ($menu as $path => $item) {
318 if (empty($item['adminGroup'])) {
322 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
325 'title' => $item['title'],
326 'desc' => CRM_Utils_Array
::value('desc', $item),
327 'id' => strtr($item['title'], array(
335 'url' => CRM_Utils_System
::url($path, $query,
340 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
343 'icon' => CRM_Utils_Array
::value('icon', $item),
344 'extra' => CRM_Utils_Array
::value('extra', $item),
346 if (!array_key_exists($item['adminGroup'], $values)) {
347 $values[$item['adminGroup']] = array();
348 $values[$item['adminGroup']]['fields'] = array();
350 $weight = CRM_Utils_Array
::value('weight', $item, 0);
351 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
352 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
355 foreach ($values as $group => $dontCare) {
356 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
357 ksort($values[$group]);
360 $menu['admin'] = array('breadcrumb' => $values);
371 public static function &getNavigation($all = FALSE) {
372 CRM_Core_Error
::fatal();
374 if (!self
::$_menuCache) {
375 self
::get('navigation');
378 if (CRM_Core_Config
::isUpgradeMode()) {
382 if (!array_key_exists('navigation', self
::$_menuCache)) {
383 // problem could be due to menu table empty. Just do a
384 // menu store and try again
388 self
::get('navigation');
389 if (!array_key_exists('navigation', self
::$_menuCache)) {
390 CRM_Core_Error
::fatal();
393 $nav = &self
::$_menuCache['navigation'];
396 !isset($nav['breadcrumb'])
401 $values = &$nav['breadcrumb'];
402 $config = CRM_Core_Config
::singleton();
403 foreach ($values as $index => $item) {
404 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
408 $values[$index]['active'] = 'class="active"';
411 $values[$index]['active'] = '';
414 if ($values[$index]['parent']) {
415 $parent = $values[$index]['parent'];
417 // only reset if still a leaf
418 if ($values[$parent]['class'] == 'leaf') {
419 $values[$parent]['class'] = 'collapsed';
422 // if a child or the parent is active, expand the menu
423 if ($values[$index]['active'] ||
424 $values[$parent]['active']
426 $values[$parent]['class'] = 'expanded';
429 // make the parent inactive if the child is active
430 if ($values[$index]['active'] &&
431 $values[$parent]['active']
433 $values[$parent]['active'] = '';
439 // remove all collapsed menu items from the array
440 foreach ($values as $weight => $v) {
442 $values[$v['parent']]['class'] == 'collapsed'
444 unset($values[$weight]);
449 // check permissions for the rest
450 $activeChildren = array();
452 foreach ($values as $weight => $v) {
453 if (CRM_Core_Permission
::checkMenuItem($v)) {
455 $activeChildren[] = $weight;
459 unset($values[$weight]);
463 // add the start / end tags
464 $len = count($activeChildren) - 1;
466 $values[$activeChildren[0]]['start'] = TRUE;
467 $values[$activeChildren[$len]]['end'] = TRUE;
470 ksort($values, SORT_NUMERIC
);
471 $i18n = CRM_Core_I18n
::singleton();
472 $i18n->localizeTitles($values);
482 public static function &getAdminLinks() {
483 $links = self
::get('admin');
486 !isset($links['breadcrumb'])
491 $values = &$links['breadcrumb'];
496 * Get the breadcrumb for a given path.
499 * An array of all the menu items.
500 * @param string $path
501 * Path for which breadcrumb is to be build.
504 * The breadcrumb for this path
506 public static function buildBreadcrumb(&$menu, $path) {
509 $pathElements = explode('/', $path);
510 array_pop($pathElements);
513 while ($newPath = array_shift($pathElements)) {
514 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
516 // when we come across breadcrumb which involves ids,
517 // we should skip now and later on append dynamically.
518 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
522 // add to crumb, if current-path exists in params.
523 if (array_key_exists($currentPath, $menu) &&
524 isset($menu[$currentPath]['title'])
526 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
528 'title' => $menu[$currentPath]['title'],
529 'url' => CRM_Utils_System
::url($currentPath,
535 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
540 $menu[$path]['breadcrumb'] = $crumbs;
549 public static function buildReturnUrl(&$menu, $path) {
550 if (!isset($menu[$path]['return_url'])) {
551 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
561 public static function getReturnUrl(&$menu, $path) {
562 if (!isset($menu[$path]['return_url'])) {
563 $pathElements = explode('/', $path);
564 array_pop($pathElements);
566 if (empty($pathElements)) {
567 return array(NULL, NULL);
569 $newPath = implode('/', $pathElements);
571 return self
::getReturnUrl($menu, $newPath);
575 CRM_Utils_Array
::value('return_url',
578 CRM_Utils_Array
::value('return_url_args',
589 public static function fillComponentIds(&$menu, $path) {
590 static $cache = array();
592 if (array_key_exists('component_id', $menu[$path])) {
596 $args = explode('/', $path);
598 if (count($args) > 1) {
599 $compPath = $args[0] . '/' . $args[1];
602 $compPath = $args[0];
607 if (array_key_exists($compPath, $cache)) {
608 $menu[$path]['component_id'] = $cache[$compPath];
611 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
612 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
613 $menu[$compPath]['component'],
617 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
618 $cache[$compPath] = $menu[$path]['component_id'];
627 public static function get($path) {
628 // return null if menu rebuild
629 $config = CRM_Core_Config
::singleton();
633 $args = explode('/', $path);
636 while (!empty($args)) {
637 $string = implode('/', $args);
638 $string = CRM_Core_DAO
::escapeString($string);
639 $elements[] = "'{$string}'";
643 $queryString = implode(', ', $elements);
644 $domainID = CRM_Core_Config
::domainID();
645 $domainWhereClause = " AND domain_id = $domainID ";
646 if ($config->isUpgradeMode() &&
647 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
649 //domain_id wouldn't be available for earlier version of
650 //3.0 and therefore can't be used as part of query for
652 $domainWhereClause = "";
659 WHERE path in ( $queryString )
661 ORDER BY length(path) DESC
666 if ($path != 'navigation') {
671 WHERE path IN ( 'navigation' )
677 $menu = new CRM_Core_DAO_Menu();
678 $menu->query($query);
680 self
::$_menuCache = array();
682 while ($menu->fetch()) {
683 self
::$_menuCache[$menu->path
] = array();
684 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
686 foreach (self
::$_serializedElements as $element) {
687 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
689 if (strpos($path, $menu->path
) !== FALSE) {
690 $menuPath = &self
::$_menuCache[$menu->path
];
695 if (strstr($path, 'report/instance')) {
696 $args = explode('/', $path);
697 if (is_numeric(end($args))) {
698 $menuPath['path'] .= '/' . end($args);
702 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
703 if ($path == 'civicrm/upgrade') {
704 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
705 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
706 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
708 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
709 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
710 CRM_Queue_Menu
::alter($path, $menuPath);
713 // Part of upgrade framework but not run inside main upgrade because it deletes data
714 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
715 // which follows upgrade/ and checks for existence of a function in Cleanup class.
716 if ($path == 'civicrm/upgrade/cleanup425') {
717 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
718 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
719 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
722 if (!empty($menuPath)) {
723 $i18n = CRM_Core_I18n
::singleton();
724 $i18n->localizeTitles($menuPath);
734 public static function getArrayForPathArgs($pathArgs) {
735 if (!is_string($pathArgs)) {
740 $elements = explode(',', $pathArgs);
741 foreach ($elements as $keyVal) {
742 list($key, $val) = explode('=', $keyVal, 2);
746 if (array_key_exists('urlToSession', $arr)) {
747 $urlToSession = array();
749 $params = explode(';', $arr['urlToSession']);
751 foreach ($params as $keyVal) {
752 list($urlToSession[$count]['urlVar'],
753 $urlToSession[$count]['sessionVar'],
754 $urlToSession[$count]['type'],
755 $urlToSession[$count]['default']
756 ) = explode(':', $keyVal);
759 $arr['urlToSession'] = $urlToSession;