3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
75 * Fetch the menu items from xml and not from cache.
79 public static function &xmlItems($fetchFromXML = FALSE) {
80 if (!self
::$_items ||
$fetchFromXML) {
81 $config = CRM_Core_Config
::singleton();
83 // We needs this until Core becomes a component
84 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
85 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
87 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
89 // Grab component menu files
90 $files = array_merge($files,
91 CRM_Core_Component
::xmlMenu()
94 // lets call a hook and get any additional files if needed
95 CRM_Utils_Hook
::xmlMenu($files);
97 self
::$_items = array();
98 foreach ($files as $file) {
99 self
::read($file, self
::$_items);
103 return self
::$_items;
107 * @param string $name
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 $value = explode('::', $value);
133 elseif ($key == 'access_arguments') {
134 if (strpos($value, ',') ||
137 if (strpos($value, ',')) {
138 $elements = explode(',', $value);
142 $elements = explode(';', $value);
146 foreach ($elements as $element) {
149 $value = array($items, $op);
152 $value = array(array($value), 'and');
155 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
156 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
158 $menu[$path][$key] = $value;
164 * This function defines information for various menu items
166 * @param boolen $fetchFromXML
167 * Fetch the menu items from xml and not from cache.
171 public static function &items($fetchFromXML = FALSE) {
172 return self
::xmlItems($fetchFromXML);
180 public static function isArrayTrue(&$values) {
181 foreach ($values as $name => $value) {
195 public static function fillMenuValues(&$menu, $path) {
196 $fieldsToPropagate = array(
203 $fieldsPresent = array();
204 foreach ($fieldsToPropagate as $field) {
205 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
208 $args = explode('/', $path);
209 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
212 $parentPath = implode('/', $args);
214 foreach ($fieldsToPropagate as $field) {
215 if (!$fieldsPresent[$field]) {
216 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
217 $fieldsPresent[$field] = TRUE;
218 $menu[$path][$field] = $menu[$parentPath][$field];
224 if (self
::isArrayTrue($fieldsPresent)) {
229 foreach ($fieldsToPropagate as $field) {
230 if (!$fieldsPresent[$field]) {
231 $messages[] = ts("Could not find %1 in path tree",
236 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
240 * We use this function to
242 * 1. Compute the breadcrumb
243 * 2. Compute local tasks value if any
244 * 3. Propagate access argument, access callback, page callback to the menu item
245 * 4. Build the global navigation block
248 public static function build(&$menu) {
249 foreach ($menu as $path => $menuItems) {
250 self
::buildBreadcrumb($menu, $path);
251 self
::fillMenuValues($menu, $path);
252 self
::fillComponentIds($menu, $path);
253 self
::buildReturnUrl($menu, $path);
255 // add add page_type if not present
256 if (!isset($menu[$path]['page_type'])) {
257 $menu[$path]['page_type'] = 0;
261 self
::buildAdminLinks($menu);
265 * This function recomputes menu from xml and populates civicrm_menu
266 * @param bool $truncate
268 public static function store($truncate = TRUE) {
269 // first clean up the db
271 $query = 'TRUNCATE civicrm_menu';
272 CRM_Core_DAO
::executeQuery($query);
274 $menuArray = self
::items($truncate);
276 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 public 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(
328 'url' => CRM_Utils_System
::url($path, $query,
333 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
335 'icon' => CRM_Utils_Array
::value('icon', $item),
336 'extra' => CRM_Utils_Array
::value('extra', $item),
338 if (!array_key_exists($item['adminGroup'], $values)) {
339 $values[$item['adminGroup']] = array();
340 $values[$item['adminGroup']]['fields'] = array();
342 $weight = CRM_Utils_Array
::value('weight', $item, 0);
343 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
344 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
347 foreach ($values as $group => $dontCare) {
348 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
349 ksort($values[$group]);
352 $menu['admin'] = array('breadcrumb' => $values);
361 public static function &getNavigation($all = FALSE) {
362 CRM_Core_Error
::fatal();
364 if (!self
::$_menuCache) {
365 self
::get('navigation');
368 if (CRM_Core_Config
::isUpgradeMode()) {
372 if (!array_key_exists('navigation', self
::$_menuCache)) {
373 // problem could be due to menu table empty. Just do a
374 // menu store and try again
378 self
::get('navigation');
379 if (!array_key_exists('navigation', self
::$_menuCache)) {
380 CRM_Core_Error
::fatal();
383 $nav = &self
::$_menuCache['navigation'];
386 !isset($nav['breadcrumb'])
391 $values = &$nav['breadcrumb'];
392 $config = CRM_Core_Config
::singleton();
393 foreach ($values as $index => $item) {
394 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
397 $values[$index]['active'] = 'class="active"';
400 $values[$index]['active'] = '';
403 if ($values[$index]['parent']) {
404 $parent = $values[$index]['parent'];
406 // only reset if still a leaf
407 if ($values[$parent]['class'] == 'leaf') {
408 $values[$parent]['class'] = 'collapsed';
411 // if a child or the parent is active, expand the menu
412 if ($values[$index]['active'] ||
413 $values[$parent]['active']
415 $values[$parent]['class'] = 'expanded';
418 // make the parent inactive if the child is active
419 if ($values[$index]['active'] &&
420 $values[$parent]['active']
422 $values[$parent]['active'] = '';
428 // remove all collapsed menu items from the array
429 foreach ($values as $weight => $v) {
431 $values[$v['parent']]['class'] == 'collapsed'
433 unset($values[$weight]);
438 // check permissions for the rest
439 $activeChildren = array();
441 foreach ($values as $weight => $v) {
442 if (CRM_Core_Permission
::checkMenuItem($v)) {
444 $activeChildren[] = $weight;
448 unset($values[$weight]);
452 // add the start / end tags
453 $len = count($activeChildren) - 1;
455 $values[$activeChildren[0]]['start'] = TRUE;
456 $values[$activeChildren[$len]]['end'] = TRUE;
459 ksort($values, SORT_NUMERIC
);
460 $i18n = CRM_Core_I18n
::singleton();
461 $i18n->localizeTitles($values);
469 public static function &getAdminLinks() {
470 $links = self
::get('admin');
473 !isset($links['breadcrumb'])
478 $values = &$links['breadcrumb'];
483 * Get the breadcrumb for a given path.
486 * An array of all the menu items.
487 * @param string $path
488 * Path for which breadcrumb is to be build.
490 * @return array The breadcrumb for this path
494 public static function buildBreadcrumb(&$menu, $path) {
497 $pathElements = explode('/', $path);
498 array_pop($pathElements);
501 while ($newPath = array_shift($pathElements)) {
502 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
504 // when we come accross breadcrumb which involves ids,
505 // we should skip now and later on append dynamically.
506 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
510 // add to crumb, if current-path exists in params.
511 if (array_key_exists($currentPath, $menu) &&
512 isset($menu[$currentPath]['title'])
514 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
516 'title' => $menu[$currentPath]['title'],
517 'url' => CRM_Utils_System
::url($currentPath,
523 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
528 $menu[$path]['breadcrumb'] = $crumbs;
537 public static function buildReturnUrl(&$menu, $path) {
538 if (!isset($menu[$path]['return_url'])) {
539 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
549 public static function getReturnUrl(&$menu, $path) {
550 if (!isset($menu[$path]['return_url'])) {
551 $pathElements = explode('/', $path);
552 array_pop($pathElements);
554 if (empty($pathElements)) {
555 return array(NULL, NULL);
557 $newPath = implode('/', $pathElements);
559 return self
::getReturnUrl($menu, $newPath);
563 CRM_Utils_Array
::value('return_url',
566 CRM_Utils_Array
::value('return_url_args',
577 public static function fillComponentIds(&$menu, $path) {
578 static $cache = array();
580 if (array_key_exists('component_id', $menu[$path])) {
584 $args = explode('/', $path);
586 if (count($args) > 1) {
587 $compPath = $args[0] . '/' . $args[1];
590 $compPath = $args[0];
595 if (array_key_exists($compPath, $cache)) {
596 $menu[$path]['component_id'] = $cache[$compPath];
599 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
600 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
601 $menu[$compPath]['component'],
605 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
606 $cache[$compPath] = $menu[$path]['component_id'];
615 public static function get($path) {
616 // return null if menu rebuild
617 $config = CRM_Core_Config
::singleton();
621 $args = explode('/', $path);
624 while (!empty($args)) {
625 $string = implode('/', $args);
626 $string = CRM_Core_DAO
::escapeString($string);
627 $elements[] = "'{$string}'";
631 $queryString = implode(', ', $elements);
632 $domainID = CRM_Core_Config
::domainID();
633 $domainWhereClause = " AND domain_id = $domainID ";
634 if ($config->isUpgradeMode() &&
635 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
637 //domain_id wouldn't be available for earlier version of
638 //3.0 and therefore can't be used as part of query for
640 $domainWhereClause = "";
647 WHERE path in ( $queryString )
649 ORDER BY length(path) DESC
654 if ($path != 'navigation') {
659 WHERE path IN ( 'navigation' )
665 $menu = new CRM_Core_DAO_Menu();
666 $menu->query($query);
668 self
::$_menuCache = array();
670 while ($menu->fetch()) {
671 self
::$_menuCache[$menu->path
] = array();
672 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
674 foreach (self
::$_serializedElements as $element) {
675 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
677 if (strpos($path, $menu->path
) !== FALSE) {
678 $menuPath = &self
::$_menuCache[$menu->path
];
683 if (strstr($path, 'report/instance')) {
684 $args = explode('/', $path);
685 if (is_numeric(end($args))) {
686 $menuPath['path'] .= '/' . end($args);
690 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
691 if ($path == 'civicrm/upgrade') {
692 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
693 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
694 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
696 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
697 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
698 CRM_Queue_Menu
::alter($path, $menuPath);
701 // Part of upgrade framework but not run inside main upgrade because it deletes data
702 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
703 // which follows upgrade/ and checks for existence of a function in Cleanup class.
704 if ($path == 'civicrm/upgrade/cleanup425') {
705 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
706 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
707 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
710 if (!empty($menuPath)) {
711 $i18n = CRM_Core_I18n
::singleton();
712 $i18n->localizeTitles($menuPath);
722 public static function getArrayForPathArgs($pathArgs) {
723 if (!is_string($pathArgs)) {
728 $elements = explode(',', $pathArgs);
729 foreach ($elements as $keyVal) {
730 list($key, $val) = explode('=', $keyVal, 2);
734 if (array_key_exists('urlToSession', $arr)) {
735 $urlToSession = array();
737 $params = explode(';', $arr['urlToSession']);
739 foreach ($params as $keyVal) {
740 list($urlToSession[$count]['urlVar'],
741 $urlToSession[$count]['sessionVar'],
742 $urlToSession[$count]['type'],
743 $urlToSession[$count]['default']
744 ) = explode(':', $keyVal);
747 $arr['urlToSession'] = $urlToSession;