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, FALSE),
319 'icon' => CRM_Utils_Array
::value('icon', $item),
320 'extra' => CRM_Utils_Array
::value('extra', $item),
322 if (!array_key_exists($item['adminGroup'], $values)) {
323 $values[$item['adminGroup']] = array();
324 $values[$item['adminGroup']]['fields'] = array();
326 $weight = CRM_Utils_Array
::value('weight', $item, 0);
327 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
328 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
331 foreach ($values as $group => $dontCare) {
332 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
333 ksort($values[$group]);
336 $menu['admin'] = array('breadcrumb' => $values);
345 static function &getNavigation($all = FALSE) {
346 CRM_Core_Error
::fatal();
348 if (!self
::$_menuCache) {
349 self
::get('navigation');
352 if (CRM_Core_Config
::isUpgradeMode()) {
356 if (!array_key_exists('navigation', self
::$_menuCache)) {
357 // problem could be due to menu table empty. Just do a
358 // menu store and try again
362 self
::get('navigation');
363 if (!array_key_exists('navigation', self
::$_menuCache)) {
364 CRM_Core_Error
::fatal();
367 $nav = &self
::$_menuCache['navigation'];
370 !isset($nav['breadcrumb'])
375 $values = &$nav['breadcrumb'];
376 $config = CRM_Core_Config
::singleton();
377 foreach ($values as $index => $item) {
378 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
381 $values[$index]['active'] = 'class="active"';
384 $values[$index]['active'] = '';
387 if ($values[$index]['parent']) {
388 $parent = $values[$index]['parent'];
390 // only reset if still a leaf
391 if ($values[$parent]['class'] == 'leaf') {
392 $values[$parent]['class'] = 'collapsed';
395 // if a child or the parent is active, expand the menu
396 if ($values[$index]['active'] ||
397 $values[$parent]['active']
399 $values[$parent]['class'] = 'expanded';
402 // make the parent inactive if the child is active
403 if ($values[$index]['active'] &&
404 $values[$parent]['active']
406 $values[$parent]['active'] = '';
413 // remove all collapsed menu items from the array
414 foreach ($values as $weight => $v) {
416 $values[$v['parent']]['class'] == 'collapsed'
418 unset($values[$weight]);
423 // check permissions for the rest
424 $activeChildren = array();
426 foreach ($values as $weight => $v) {
427 if (CRM_Core_Permission
::checkMenuItem($v)) {
429 $activeChildren[] = $weight;
433 unset($values[$weight]);
437 // add the start / end tags
438 $len = count($activeChildren) - 1;
440 $values[$activeChildren[0]]['start'] = TRUE;
441 $values[$activeChildren[$len]]['end'] = TRUE;
444 ksort($values, SORT_NUMERIC
);
445 $i18n = CRM_Core_I18n
::singleton();
446 $i18n->localizeTitles($values);
454 static function &getAdminLinks() {
455 $links = self
::get('admin');
458 !isset($links['breadcrumb'])
463 $values = &$links['breadcrumb'];
468 * Get the breadcrumb for a given path.
470 * @param array $menu An array of all the menu items.
471 * @param string $path Path for which breadcrumb is to be build.
473 * @return array The breadcrumb for this path
478 static function buildBreadcrumb(&$menu, $path) {
481 $pathElements = explode('/', $path);
482 array_pop($pathElements);
485 while ($newPath = array_shift($pathElements)) {
486 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
488 // when we come accross breadcrumb which involves ids,
489 // we should skip now and later on append dynamically.
490 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
494 // add to crumb, if current-path exists in params.
495 if (array_key_exists($currentPath, $menu) &&
496 isset($menu[$currentPath]['title'])
498 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
500 'title' => $menu[$currentPath]['title'],
501 'url' => CRM_Utils_System
::url($currentPath,
507 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
512 $menu[$path]['breadcrumb'] = $crumbs;
521 static function buildReturnUrl(&$menu, $path) {
522 if (!isset($menu[$path]['return_url'])) {
523 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
533 static function getReturnUrl(&$menu, $path) {
534 if (!isset($menu[$path]['return_url'])) {
535 $pathElements = explode('/', $path);
536 array_pop($pathElements);
538 if (empty($pathElements)) {
539 return array(NULL, NULL);
541 $newPath = implode('/', $pathElements);
543 return self
::getReturnUrl($menu, $newPath);
547 CRM_Utils_Array
::value('return_url',
550 CRM_Utils_Array
::value('return_url_args',
561 static function fillComponentIds(&$menu, $path) {
562 static $cache = array();
564 if (array_key_exists('component_id', $menu[$path])) {
568 $args = explode('/', $path);
570 if (count($args) > 1) {
571 $compPath = $args[0] . '/' . $args[1];
574 $compPath = $args[0];
579 if (array_key_exists($compPath, $cache)) {
580 $menu[$path]['component_id'] = $cache[$compPath];
583 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
584 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
585 $menu[$compPath]['component'],
589 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
590 $cache[$compPath] = $menu[$path]['component_id'];
599 static function get($path) {
600 // return null if menu rebuild
601 $config = CRM_Core_Config
::singleton();
605 $args = explode('/', $path);
608 while (!empty($args)) {
609 $string = implode('/', $args);
610 $string = CRM_Core_DAO
::escapeString($string);
611 $elements[] = "'{$string}'";
615 $queryString = implode(', ', $elements);
616 $domainID = CRM_Core_Config
::domainID();
617 $domainWhereClause = " AND domain_id = $domainID ";
618 if ($config->isUpgradeMode() &&
619 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
621 //domain_id wouldn't be available for earlier version of
622 //3.0 and therefore can't be used as part of query for
624 $domainWhereClause = "";
631 WHERE path in ( $queryString )
633 ORDER BY length(path) DESC
638 if ($path != 'navigation') {
643 WHERE path IN ( 'navigation' )
649 $menu = new CRM_Core_DAO_Menu();
650 $menu->query($query);
652 self
::$_menuCache = array();
654 while ($menu->fetch()) {
655 self
::$_menuCache[$menu->path
] = array();
656 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
658 foreach (self
::$_serializedElements as $element) {
659 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
661 if (strpos($path, $menu->path
) !== FALSE) {
662 $menuPath = &self
::$_menuCache[$menu->path
];
667 if (strstr($path, 'report/instance')) {
668 $args = explode('/', $path);
669 if (is_numeric(end($args))) {
670 $menuPath['path'] .= '/' . end($args);
674 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
675 if ($path == 'civicrm/upgrade') {
676 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
677 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
678 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
680 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
681 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
682 CRM_Queue_Menu
::alter($path, $menuPath);
685 // Part of upgrade framework but not run inside main upgrade because it deletes data
686 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
687 // which follows upgrade/ and checks for existence of a function in Cleanup class.
688 if ($path == 'civicrm/upgrade/cleanup425') {
689 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup','cleanup425');
690 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
691 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
694 if (!empty($menuPath)) {
695 $i18n = CRM_Core_I18n
::singleton();
696 $i18n->localizeTitles($menuPath);
706 static function getArrayForPathArgs($pathArgs) {
707 if (!is_string($pathArgs)) {
712 $elements = explode(',', $pathArgs);
713 //CRM_Core_Error::debug( 'e', $elements );
714 foreach ($elements as $keyVal) {
715 list($key, $val) = explode('=', $keyVal, 2);
719 if (array_key_exists('urlToSession', $arr)) {
720 $urlToSession = array();
722 $params = explode(';', $arr['urlToSession']);
724 foreach ($params as $keyVal) {
725 list($urlToSession[$count]['urlVar'],
726 $urlToSession[$count]['sessionVar'],
727 $urlToSession[$count]['type'],
728 $urlToSession[$count]['default']
729 ) = explode(':', $keyVal);
732 $arr['urlToSession'] = $urlToSession;