3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 require_once 'CRM/Core/I18n.php';
45 * the list of menu items
50 static $_items = NULL;
53 * the list of permissioned menu items
58 static $_permissionedItems = NULL;
60 static $_serializedElements = array(
68 static $_menuCache = NULL;
74 static function &xmlItems() {
76 $config = CRM_Core_Config
::singleton();
78 // We needs this until Core becomes a component
79 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
80 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
82 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
84 // Grab component menu files
85 $files = array_merge($files,
86 CRM_Core_Component
::xmlMenu()
89 // lets call a hook and get any additional files if needed
90 CRM_Utils_Hook
::xmlMenu($files);
92 self
::$_items = array();
93 foreach ($files as $file) {
94 self
::read($file, self
::$_items);
107 static function read($name, &$menu) {
109 $config = CRM_Core_Config
::singleton();
111 $xml = simplexml_load_file($name);
112 foreach ($xml->item
as $item) {
113 if (!(string ) $item->path
) {
114 CRM_Core_Error
::debug('i', $item);
115 CRM_Core_Error
::fatal();
117 $path = (string ) $item->path
;
118 $menu[$path] = array();
120 foreach ($item as $key => $value) {
121 $key = (string ) $key;
122 $value = (string ) $value;
123 if (strpos($key, '_callback') &&
126 $value = explode('::', $value);
128 elseif ($key == 'access_arguments') {
129 if (strpos($value, ',') ||
132 if (strpos($value, ',')) {
133 $elements = explode(',', $value);
137 $elements = explode(';', $value);
141 foreach ($elements as $element) {
144 $value = array($items, $op);
147 $value = array(array($value), 'and');
150 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
151 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
153 $menu[$path][$key] = $value;
159 * This function defines information for various menu items
164 static function &items() {
165 return self
::xmlItems();
173 static function isArrayTrue(&$values) {
174 foreach ($values as $name => $value) {
188 static function fillMenuValues(&$menu, $path) {
189 $fieldsToPropagate = array(
196 $fieldsPresent = array();
197 foreach ($fieldsToPropagate as $field) {
198 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
201 $args = explode('/', $path);
202 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
205 $parentPath = implode('/', $args);
207 foreach ($fieldsToPropagate as $field) {
208 if (!$fieldsPresent[$field]) {
209 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
210 $fieldsPresent[$field] = TRUE;
211 $menu[$path][$field] = $menu[$parentPath][$field];
217 if (self
::isArrayTrue($fieldsPresent)) {
222 foreach ($fieldsToPropagate as $field) {
223 if (!$fieldsPresent[$field]) {
224 $messages[] = ts("Could not find %1 in path tree",
229 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
233 * We use this function to
235 * 1. Compute the breadcrumb
236 * 2. Compute local tasks value if any
237 * 3. Propagate access argument, access callback, page callback to the menu item
238 * 4. Build the global navigation block
241 static function build(&$menu) {
242 foreach ($menu as $path => $menuItems) {
243 self
::buildBreadcrumb($menu, $path);
244 self
::fillMenuValues($menu, $path);
245 self
::fillComponentIds($menu, $path);
246 self
::buildReturnUrl($menu, $path);
248 // add add page_type if not present
249 if (!isset($menu[$path]['page_type'])) {
250 $menu[$path]['page_type'] = 0;
254 self
::buildAdminLinks($menu);
258 * @param bool $truncate
260 static function store($truncate = TRUE) {
261 // first clean up the db
263 $query = 'TRUNCATE civicrm_menu';
264 CRM_Core_DAO
::executeQuery($query);
266 $menuArray = self
::items();
268 self
::build($menuArray);
271 $config = CRM_Core_Config
::singleton();
273 foreach ($menuArray as $path => $item) {
274 $menu = new CRM_Core_DAO_Menu();
276 $menu->domain_id
= CRM_Core_Config
::domainID();
280 $menu->copyValues($item);
282 foreach (self
::$_serializedElements as $element) {
283 if (!isset($item[$element]) ||
284 $item[$element] == 'null'
286 $menu->$element = NULL;
289 $menu->$element = serialize($item[$element]);
300 static function buildAdminLinks(&$menu) {
303 foreach ($menu as $path => $item) {
304 if (empty($item['adminGroup'])) {
308 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
311 'title' => $item['title'],
312 'desc' => CRM_Utils_Array
::value('desc', $item),
313 'id' => strtr($item['title'], array(
314 '(' => '_', ')' => '', ' ' => '',
315 ',' => '_', '/' => '_',
318 'url' => CRM_Utils_System
::url($path, $query,
323 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
325 'icon' => CRM_Utils_Array
::value('icon', $item),
326 'extra' => CRM_Utils_Array
::value('extra', $item),
328 if (!array_key_exists($item['adminGroup'], $values)) {
329 $values[$item['adminGroup']] = array();
330 $values[$item['adminGroup']]['fields'] = array();
332 $weight = CRM_Utils_Array
::value('weight', $item, 0);
333 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
334 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
337 foreach ($values as $group => $dontCare) {
338 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
339 ksort($values[$group]);
342 $menu['admin'] = array('breadcrumb' => $values);
351 static function &getNavigation($all = FALSE) {
352 CRM_Core_Error
::fatal();
354 if (!self
::$_menuCache) {
355 self
::get('navigation');
358 if (CRM_Core_Config
::isUpgradeMode()) {
362 if (!array_key_exists('navigation', self
::$_menuCache)) {
363 // problem could be due to menu table empty. Just do a
364 // menu store and try again
368 self
::get('navigation');
369 if (!array_key_exists('navigation', self
::$_menuCache)) {
370 CRM_Core_Error
::fatal();
373 $nav = &self
::$_menuCache['navigation'];
376 !isset($nav['breadcrumb'])
381 $values = &$nav['breadcrumb'];
382 $config = CRM_Core_Config
::singleton();
383 foreach ($values as $index => $item) {
384 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
387 $values[$index]['active'] = 'class="active"';
390 $values[$index]['active'] = '';
393 if ($values[$index]['parent']) {
394 $parent = $values[$index]['parent'];
396 // only reset if still a leaf
397 if ($values[$parent]['class'] == 'leaf') {
398 $values[$parent]['class'] = 'collapsed';
401 // if a child or the parent is active, expand the menu
402 if ($values[$index]['active'] ||
403 $values[$parent]['active']
405 $values[$parent]['class'] = 'expanded';
408 // make the parent inactive if the child is active
409 if ($values[$index]['active'] &&
410 $values[$parent]['active']
412 $values[$parent]['active'] = '';
419 // remove all collapsed menu items from the array
420 foreach ($values as $weight => $v) {
422 $values[$v['parent']]['class'] == 'collapsed'
424 unset($values[$weight]);
429 // check permissions for the rest
430 $activeChildren = array();
432 foreach ($values as $weight => $v) {
433 if (CRM_Core_Permission
::checkMenuItem($v)) {
435 $activeChildren[] = $weight;
439 unset($values[$weight]);
443 // add the start / end tags
444 $len = count($activeChildren) - 1;
446 $values[$activeChildren[0]]['start'] = TRUE;
447 $values[$activeChildren[$len]]['end'] = TRUE;
450 ksort($values, SORT_NUMERIC
);
451 $i18n = CRM_Core_I18n
::singleton();
452 $i18n->localizeTitles($values);
460 static function &getAdminLinks() {
461 $links = self
::get('admin');
464 !isset($links['breadcrumb'])
469 $values = &$links['breadcrumb'];
474 * Get the breadcrumb for a given path.
476 * @param array $menu An array of all the menu items.
477 * @param string $path Path for which breadcrumb is to be build.
479 * @return array The breadcrumb for this path
484 static function buildBreadcrumb(&$menu, $path) {
487 $pathElements = explode('/', $path);
488 array_pop($pathElements);
491 while ($newPath = array_shift($pathElements)) {
492 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
494 // when we come accross breadcrumb which involves ids,
495 // we should skip now and later on append dynamically.
496 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
500 // add to crumb, if current-path exists in params.
501 if (array_key_exists($currentPath, $menu) &&
502 isset($menu[$currentPath]['title'])
504 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
506 'title' => $menu[$currentPath]['title'],
507 'url' => CRM_Utils_System
::url($currentPath,
513 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
518 $menu[$path]['breadcrumb'] = $crumbs;
527 static function buildReturnUrl(&$menu, $path) {
528 if (!isset($menu[$path]['return_url'])) {
529 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
539 static function getReturnUrl(&$menu, $path) {
540 if (!isset($menu[$path]['return_url'])) {
541 $pathElements = explode('/', $path);
542 array_pop($pathElements);
544 if (empty($pathElements)) {
545 return array(NULL, NULL);
547 $newPath = implode('/', $pathElements);
549 return self
::getReturnUrl($menu, $newPath);
553 CRM_Utils_Array
::value('return_url',
556 CRM_Utils_Array
::value('return_url_args',
567 static function fillComponentIds(&$menu, $path) {
568 static $cache = array();
570 if (array_key_exists('component_id', $menu[$path])) {
574 $args = explode('/', $path);
576 if (count($args) > 1) {
577 $compPath = $args[0] . '/' . $args[1];
580 $compPath = $args[0];
585 if (array_key_exists($compPath, $cache)) {
586 $menu[$path]['component_id'] = $cache[$compPath];
589 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
590 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
591 $menu[$compPath]['component'],
595 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
596 $cache[$compPath] = $menu[$path]['component_id'];
605 static function get($path) {
606 // return null if menu rebuild
607 $config = CRM_Core_Config
::singleton();
611 $args = explode('/', $path);
614 while (!empty($args)) {
615 $string = implode('/', $args);
616 $string = CRM_Core_DAO
::escapeString($string);
617 $elements[] = "'{$string}'";
621 $queryString = implode(', ', $elements);
622 $domainID = CRM_Core_Config
::domainID();
623 $domainWhereClause = " AND domain_id = $domainID ";
624 if ($config->isUpgradeMode() &&
625 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
627 //domain_id wouldn't be available for earlier version of
628 //3.0 and therefore can't be used as part of query for
630 $domainWhereClause = "";
637 WHERE path in ( $queryString )
639 ORDER BY length(path) DESC
644 if ($path != 'navigation') {
649 WHERE path IN ( 'navigation' )
655 $menu = new CRM_Core_DAO_Menu();
656 $menu->query($query);
658 self
::$_menuCache = array();
660 while ($menu->fetch()) {
661 self
::$_menuCache[$menu->path
] = array();
662 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
664 foreach (self
::$_serializedElements as $element) {
665 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
667 if (strpos($path, $menu->path
) !== FALSE) {
668 $menuPath = &self
::$_menuCache[$menu->path
];
673 if (strstr($path, 'report/instance')) {
674 $args = explode('/', $path);
675 if (is_numeric(end($args))) {
676 $menuPath['path'] .= '/' . end($args);
680 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
681 if ($path == 'civicrm/upgrade') {
682 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
683 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
684 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
686 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
687 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
688 CRM_Queue_Menu
::alter($path, $menuPath);
691 // Part of upgrade framework but not run inside main upgrade because it deletes data
692 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
693 // which follows upgrade/ and checks for existence of a function in Cleanup class.
694 if ($path == 'civicrm/upgrade/cleanup425') {
695 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup','cleanup425');
696 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
697 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
700 if (!empty($menuPath)) {
701 $i18n = CRM_Core_I18n
::singleton();
702 $i18n->localizeTitles($menuPath);
712 static function getArrayForPathArgs($pathArgs) {
713 if (!is_string($pathArgs)) {
718 $elements = explode(',', $pathArgs);
719 //CRM_Core_Error::debug( 'e', $elements );
720 foreach ($elements as $keyVal) {
721 list($key, $val) = explode('=', $keyVal, 2);
725 if (array_key_exists('urlToSession', $arr)) {
726 $urlToSession = array();
728 $params = explode(';', $arr['urlToSession']);
730 foreach ($params as $keyVal) {
731 list($urlToSession[$count]['urlVar'],
732 $urlToSession[$count]['sessionVar'],
733 $urlToSession[$count]['type'],
734 $urlToSession[$count]['default']
735 ) = explode(':', $keyVal);
738 $arr['urlToSession'] = $urlToSession;