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;
72 * This function fetches the menu items from xml and xmlMenu hooks
74 * @param boolen $fetchFromXML fetch the menu items from xml and not from cache
78 static function &xmlItems($fetchFromXML = FALSE) {
79 if (!self
::$_items ||
$fetchFromXML) {
80 $config = CRM_Core_Config
::singleton();
82 // We needs this until Core becomes a component
83 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
84 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
86 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
88 // Grab component menu files
89 $files = array_merge($files,
90 CRM_Core_Component
::xmlMenu()
93 // lets call a hook and get any additional files if needed
94 CRM_Utils_Hook
::xmlMenu($files);
96 self
::$_items = array();
97 foreach ($files as $file) {
98 self
::read($file, self
::$_items);
102 return self
::$_items;
106 * @param string $name
111 static function read($name, &$menu) {
113 $config = CRM_Core_Config
::singleton();
115 $xml = simplexml_load_file($name);
116 foreach ($xml->item
as $item) {
117 if (!(string ) $item->path
) {
118 CRM_Core_Error
::debug('i', $item);
119 CRM_Core_Error
::fatal();
121 $path = (string ) $item->path
;
122 $menu[$path] = array();
124 foreach ($item as $key => $value) {
125 $key = (string ) $key;
126 $value = (string ) $value;
127 if (strpos($key, '_callback') &&
130 $value = explode('::', $value);
132 elseif ($key == 'access_arguments') {
133 if (strpos($value, ',') ||
136 if (strpos($value, ',')) {
137 $elements = explode(',', $value);
141 $elements = explode(';', $value);
145 foreach ($elements as $element) {
148 $value = array($items, $op);
151 $value = array(array($value), 'and');
154 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
155 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
157 $menu[$path][$key] = $value;
163 * This function defines information for various menu items
165 * @param boolen $fetchFromXML fetch the menu items from xml and not from cache
170 static function &items($fetchFromXML = FALSE) {
171 return self
::xmlItems($fetchFromXML);
179 static function isArrayTrue(&$values) {
180 foreach ($values as $name => $value) {
194 static function fillMenuValues(&$menu, $path) {
195 $fieldsToPropagate = array(
202 $fieldsPresent = array();
203 foreach ($fieldsToPropagate as $field) {
204 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
207 $args = explode('/', $path);
208 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
211 $parentPath = implode('/', $args);
213 foreach ($fieldsToPropagate as $field) {
214 if (!$fieldsPresent[$field]) {
215 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
216 $fieldsPresent[$field] = TRUE;
217 $menu[$path][$field] = $menu[$parentPath][$field];
223 if (self
::isArrayTrue($fieldsPresent)) {
228 foreach ($fieldsToPropagate as $field) {
229 if (!$fieldsPresent[$field]) {
230 $messages[] = ts("Could not find %1 in path tree",
235 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
239 * We use this function to
241 * 1. Compute the breadcrumb
242 * 2. Compute local tasks value if any
243 * 3. Propagate access argument, access callback, page callback to the menu item
244 * 4. Build the global navigation block
247 static function build(&$menu) {
248 foreach ($menu as $path => $menuItems) {
249 self
::buildBreadcrumb($menu, $path);
250 self
::fillMenuValues($menu, $path);
251 self
::fillComponentIds($menu, $path);
252 self
::buildReturnUrl($menu, $path);
254 // add add page_type if not present
255 if (!isset($menu[$path]['page_type'])) {
256 $menu[$path]['page_type'] = 0;
260 self
::buildAdminLinks($menu);
264 * This function recomputes menu from xml and populates civicrm_menu
265 * @param bool $truncate
267 static function store($truncate = TRUE) {
268 // first clean up the db
270 $query = 'TRUNCATE civicrm_menu';
271 CRM_Core_DAO
::executeQuery($query);
273 $menuArray = self
::items($truncate);
275 self
::build($menuArray);
278 $config = CRM_Core_Config
::singleton();
280 foreach ($menuArray as $path => $item) {
281 $menu = new CRM_Core_DAO_Menu();
283 $menu->domain_id
= CRM_Core_Config
::domainID();
287 $menu->copyValues($item);
289 foreach (self
::$_serializedElements as $element) {
290 if (!isset($item[$element]) ||
291 $item[$element] == 'null'
293 $menu->$element = NULL;
296 $menu->$element = serialize($item[$element]);
307 static function buildAdminLinks(&$menu) {
310 foreach ($menu as $path => $item) {
311 if (empty($item['adminGroup'])) {
315 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
318 'title' => $item['title'],
319 'desc' => CRM_Utils_Array
::value('desc', $item),
320 'id' => strtr($item['title'], array(
321 '(' => '_', ')' => '', ' ' => '',
322 ',' => '_', '/' => '_',
325 'url' => CRM_Utils_System
::url($path, $query,
330 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
332 'icon' => CRM_Utils_Array
::value('icon', $item),
333 'extra' => CRM_Utils_Array
::value('extra', $item),
335 if (!array_key_exists($item['adminGroup'], $values)) {
336 $values[$item['adminGroup']] = array();
337 $values[$item['adminGroup']]['fields'] = array();
339 $weight = CRM_Utils_Array
::value('weight', $item, 0);
340 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
341 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
344 foreach ($values as $group => $dontCare) {
345 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
346 ksort($values[$group]);
349 $menu['admin'] = array('breadcrumb' => $values);
358 static function &getNavigation($all = FALSE) {
359 CRM_Core_Error
::fatal();
361 if (!self
::$_menuCache) {
362 self
::get('navigation');
365 if (CRM_Core_Config
::isUpgradeMode()) {
369 if (!array_key_exists('navigation', self
::$_menuCache)) {
370 // problem could be due to menu table empty. Just do a
371 // menu store and try again
375 self
::get('navigation');
376 if (!array_key_exists('navigation', self
::$_menuCache)) {
377 CRM_Core_Error
::fatal();
380 $nav = &self
::$_menuCache['navigation'];
383 !isset($nav['breadcrumb'])
388 $values = &$nav['breadcrumb'];
389 $config = CRM_Core_Config
::singleton();
390 foreach ($values as $index => $item) {
391 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
394 $values[$index]['active'] = 'class="active"';
397 $values[$index]['active'] = '';
400 if ($values[$index]['parent']) {
401 $parent = $values[$index]['parent'];
403 // only reset if still a leaf
404 if ($values[$parent]['class'] == 'leaf') {
405 $values[$parent]['class'] = 'collapsed';
408 // if a child or the parent is active, expand the menu
409 if ($values[$index]['active'] ||
410 $values[$parent]['active']
412 $values[$parent]['class'] = 'expanded';
415 // make the parent inactive if the child is active
416 if ($values[$index]['active'] &&
417 $values[$parent]['active']
419 $values[$parent]['active'] = '';
426 // remove all collapsed menu items from the array
427 foreach ($values as $weight => $v) {
429 $values[$v['parent']]['class'] == 'collapsed'
431 unset($values[$weight]);
436 // check permissions for the rest
437 $activeChildren = array();
439 foreach ($values as $weight => $v) {
440 if (CRM_Core_Permission
::checkMenuItem($v)) {
442 $activeChildren[] = $weight;
446 unset($values[$weight]);
450 // add the start / end tags
451 $len = count($activeChildren) - 1;
453 $values[$activeChildren[0]]['start'] = TRUE;
454 $values[$activeChildren[$len]]['end'] = TRUE;
457 ksort($values, SORT_NUMERIC
);
458 $i18n = CRM_Core_I18n
::singleton();
459 $i18n->localizeTitles($values);
467 static function &getAdminLinks() {
468 $links = self
::get('admin');
471 !isset($links['breadcrumb'])
476 $values = &$links['breadcrumb'];
481 * Get the breadcrumb for a given path.
483 * @param array $menu An array of all the menu items.
484 * @param string $path Path for which breadcrumb is to be build.
486 * @return array The breadcrumb for this path
491 static function buildBreadcrumb(&$menu, $path) {
494 $pathElements = explode('/', $path);
495 array_pop($pathElements);
498 while ($newPath = array_shift($pathElements)) {
499 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
501 // when we come accross breadcrumb which involves ids,
502 // we should skip now and later on append dynamically.
503 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
507 // add to crumb, if current-path exists in params.
508 if (array_key_exists($currentPath, $menu) &&
509 isset($menu[$currentPath]['title'])
511 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
513 'title' => $menu[$currentPath]['title'],
514 'url' => CRM_Utils_System
::url($currentPath,
520 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
525 $menu[$path]['breadcrumb'] = $crumbs;
534 static function buildReturnUrl(&$menu, $path) {
535 if (!isset($menu[$path]['return_url'])) {
536 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
546 static function getReturnUrl(&$menu, $path) {
547 if (!isset($menu[$path]['return_url'])) {
548 $pathElements = explode('/', $path);
549 array_pop($pathElements);
551 if (empty($pathElements)) {
552 return array(NULL, NULL);
554 $newPath = implode('/', $pathElements);
556 return self
::getReturnUrl($menu, $newPath);
560 CRM_Utils_Array
::value('return_url',
563 CRM_Utils_Array
::value('return_url_args',
574 static function fillComponentIds(&$menu, $path) {
575 static $cache = array();
577 if (array_key_exists('component_id', $menu[$path])) {
581 $args = explode('/', $path);
583 if (count($args) > 1) {
584 $compPath = $args[0] . '/' . $args[1];
587 $compPath = $args[0];
592 if (array_key_exists($compPath, $cache)) {
593 $menu[$path]['component_id'] = $cache[$compPath];
596 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
597 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
598 $menu[$compPath]['component'],
602 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
603 $cache[$compPath] = $menu[$path]['component_id'];
612 static function get($path) {
613 // return null if menu rebuild
614 $config = CRM_Core_Config
::singleton();
618 $args = explode('/', $path);
621 while (!empty($args)) {
622 $string = implode('/', $args);
623 $string = CRM_Core_DAO
::escapeString($string);
624 $elements[] = "'{$string}'";
628 $queryString = implode(', ', $elements);
629 $domainID = CRM_Core_Config
::domainID();
630 $domainWhereClause = " AND domain_id = $domainID ";
631 if ($config->isUpgradeMode() &&
632 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
634 //domain_id wouldn't be available for earlier version of
635 //3.0 and therefore can't be used as part of query for
637 $domainWhereClause = "";
644 WHERE path in ( $queryString )
646 ORDER BY length(path) DESC
651 if ($path != 'navigation') {
656 WHERE path IN ( 'navigation' )
662 $menu = new CRM_Core_DAO_Menu();
663 $menu->query($query);
665 self
::$_menuCache = array();
667 while ($menu->fetch()) {
668 self
::$_menuCache[$menu->path
] = array();
669 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
671 foreach (self
::$_serializedElements as $element) {
672 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
674 if (strpos($path, $menu->path
) !== FALSE) {
675 $menuPath = &self
::$_menuCache[$menu->path
];
680 if (strstr($path, 'report/instance')) {
681 $args = explode('/', $path);
682 if (is_numeric(end($args))) {
683 $menuPath['path'] .= '/' . end($args);
687 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
688 if ($path == 'civicrm/upgrade') {
689 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
690 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
691 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
693 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
694 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
695 CRM_Queue_Menu
::alter($path, $menuPath);
698 // Part of upgrade framework but not run inside main upgrade because it deletes data
699 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
700 // which follows upgrade/ and checks for existence of a function in Cleanup class.
701 if ($path == 'civicrm/upgrade/cleanup425') {
702 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup','cleanup425');
703 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
704 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
707 if (!empty($menuPath)) {
708 $i18n = CRM_Core_I18n
::singleton();
709 $i18n->localizeTitles($menuPath);
719 static function getArrayForPathArgs($pathArgs) {
720 if (!is_string($pathArgs)) {
725 $elements = explode(',', $pathArgs);
726 foreach ($elements as $keyVal) {
727 list($key, $val) = explode('=', $keyVal, 2);
731 if (array_key_exists('urlToSession', $arr)) {
732 $urlToSession = array();
734 $params = explode(';', $arr['urlToSession']);
736 foreach ($params as $keyVal) {
737 list($urlToSession[$count]['urlVar'],
738 $urlToSession[$count]['sessionVar'],
739 $urlToSession[$count]['type'],
740 $urlToSession[$count]['default']
741 ) = explode(':', $keyVal);
744 $arr['urlToSession'] = $urlToSession;