3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 require_once 'CRM/Core/I18n.php';
41 * the list of menu items
46 static $_items = NULL;
49 * the list of permissioned menu items
54 static $_permissionedItems = NULL;
56 static $_serializedElements = array(
64 static $_menuCache = NULL;
67 static function &xmlItems() {
69 $config = CRM_Core_Config
::singleton();
71 // We needs this until Core becomes a component
72 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
73 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
75 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
77 // Grab component menu files
78 $files = array_merge($files,
79 CRM_Core_Component
::xmlMenu()
82 // lets call a hook and get any additional files if needed
83 CRM_Utils_Hook
::xmlMenu($files);
85 self
::$_items = array();
86 foreach ($files as $file) {
87 self
::read($file, self
::$_items);
94 static function read($name, &$menu) {
96 $config = CRM_Core_Config
::singleton();
98 $xml = simplexml_load_file($name);
99 foreach ($xml->item
as $item) {
100 if (!(string ) $item->path
) {
101 CRM_Core_Error
::debug('i', $item);
102 CRM_Core_Error
::fatal();
104 $path = (string ) $item->path
;
105 $menu[$path] = array();
107 foreach ($item as $key => $value) {
108 $key = (string ) $key;
109 $value = (string ) $value;
110 if (strpos($key, '_callback') &&
113 $value = explode('::', $value);
115 elseif ($key == 'access_arguments') {
116 if (strpos($value, ',') ||
119 if (strpos($value, ',')) {
120 $elements = explode(',', $value);
124 $elements = explode(';', $value);
128 foreach ($elements as $element) {
131 $value = array($items, $op);
134 $value = array(array($value), 'and');
137 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
138 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
140 $menu[$path][$key] = $value;
146 * This function defines information for various menu items
151 static function &items() {
152 return self
::xmlItems();
155 static function isArrayTrue(&$values) {
156 foreach ($values as $name => $value) {
164 static function fillMenuValues(&$menu, $path) {
165 $fieldsToPropagate = array(
172 $fieldsPresent = array();
173 foreach ($fieldsToPropagate as $field) {
174 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
177 $args = explode('/', $path);
178 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
181 $parentPath = implode('/', $args);
183 foreach ($fieldsToPropagate as $field) {
184 if (!$fieldsPresent[$field]) {
185 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
186 $fieldsPresent[$field] = TRUE;
187 $menu[$path][$field] = $menu[$parentPath][$field];
193 if (self
::isArrayTrue($fieldsPresent)) {
198 foreach ($fieldsToPropagate as $field) {
199 if (!$fieldsPresent[$field]) {
200 $messages[] = ts("Could not find %1 in path tree",
205 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
209 * We use this function to
211 * 1. Compute the breadcrumb
212 * 2. Compute local tasks value if any
213 * 3. Propagate access argument, access callback, page callback to the menu item
214 * 4. Build the global navigation block
217 static function build(&$menu) {
218 foreach ($menu as $path => $menuItems) {
219 self
::buildBreadcrumb($menu, $path);
220 self
::fillMenuValues($menu, $path);
221 self
::fillComponentIds($menu, $path);
222 self
::buildReturnUrl($menu, $path);
224 // add add page_type if not present
225 if (!isset($menu[$path]['page_type'])) {
226 $menu[$path]['page_type'] = 0;
230 self
::buildAdminLinks($menu);
233 static function store($truncate = TRUE) {
234 // first clean up the db
236 $query = 'TRUNCATE civicrm_menu';
237 CRM_Core_DAO
::executeQuery($query);
239 $menuArray = self
::items();
241 self
::build($menuArray);
244 $config = CRM_Core_Config
::singleton();
246 foreach ($menuArray as $path => $item) {
247 $menu = new CRM_Core_DAO_Menu();
249 $menu->domain_id
= CRM_Core_Config
::domainID();
253 $menu->copyValues($item);
255 foreach (self
::$_serializedElements as $element) {
256 if (!isset($item[$element]) ||
257 $item[$element] == 'null'
259 $menu->$element = NULL;
262 $menu->$element = serialize($item[$element]);
270 static function buildAdminLinks(&$menu) {
273 foreach ($menu as $path => $item) {
274 if (!CRM_Utils_Array
::value('adminGroup', $item)) {
278 $query = CRM_Utils_Array
::value('path_arguments', $item) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
281 'title' => $item['title'],
282 'desc' => CRM_Utils_Array
::value('desc', $item),
283 'id' => strtr($item['title'], array(
284 '(' => '_', ')' => '', ' ' => '',
285 ',' => '_', '/' => '_',
288 'url' => CRM_Utils_System
::url($path, $query, FALSE),
289 'icon' => CRM_Utils_Array
::value('icon', $item),
290 'extra' => CRM_Utils_Array
::value('extra', $item),
292 if (!array_key_exists($item['adminGroup'], $values)) {
293 $values[$item['adminGroup']] = array();
294 $values[$item['adminGroup']]['fields'] = array();
296 $weight = CRM_Utils_Array
::value('weight', $item, 0);
297 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
298 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
301 foreach ($values as $group => $dontCare) {
302 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
303 ksort($values[$group]);
306 $menu['admin'] = array('breadcrumb' => $values);
309 static function &getNavigation($all = FALSE) {
310 CRM_Core_Error
::fatal();
312 if (!self
::$_menuCache) {
313 self
::get('navigation');
316 if (CRM_Core_Config
::isUpgradeMode()) {
320 if (!array_key_exists('navigation', self
::$_menuCache)) {
321 // problem could be due to menu table empty. Just do a
322 // menu store and try again
326 self
::get('navigation');
327 if (!array_key_exists('navigation', self
::$_menuCache)) {
328 CRM_Core_Error
::fatal();
331 $nav = &self
::$_menuCache['navigation'];
334 !isset($nav['breadcrumb'])
339 $values = &$nav['breadcrumb'];
340 $config = CRM_Core_Config
::singleton();
341 foreach ($values as $index => $item) {
342 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
345 $values[$index]['active'] = 'class="active"';
348 $values[$index]['active'] = '';
351 if ($values[$index]['parent']) {
352 $parent = $values[$index]['parent'];
354 // only reset if still a leaf
355 if ($values[$parent]['class'] == 'leaf') {
356 $values[$parent]['class'] = 'collapsed';
359 // if a child or the parent is active, expand the menu
360 if ($values[$index]['active'] ||
361 $values[$parent]['active']
363 $values[$parent]['class'] = 'expanded';
366 // make the parent inactive if the child is active
367 if ($values[$index]['active'] &&
368 $values[$parent]['active']
370 $values[$parent]['active'] = '';
377 // remove all collapsed menu items from the array
378 foreach ($values as $weight => $v) {
380 $values[$v['parent']]['class'] == 'collapsed'
382 unset($values[$weight]);
387 // check permissions for the rest
388 $activeChildren = array();
390 foreach ($values as $weight => $v) {
391 if (CRM_Core_Permission
::checkMenuItem($v)) {
393 $activeChildren[] = $weight;
397 unset($values[$weight]);
401 // add the start / end tags
402 $len = count($activeChildren) - 1;
404 $values[$activeChildren[0]]['start'] = TRUE;
405 $values[$activeChildren[$len]]['end'] = TRUE;
408 ksort($values, SORT_NUMERIC
);
409 $i18n = CRM_Core_I18n
::singleton();
410 $i18n->localizeTitles($values);
415 static function &getAdminLinks() {
416 $links = self
::get('admin');
419 !isset($links['breadcrumb'])
424 $values = &$links['breadcrumb'];
429 * Get the breadcrumb for a given path.
431 * @param array $menu An array of all the menu items.
432 * @param string $path Path for which breadcrumb is to be build.
434 * @return array The breadcrumb for this path
439 static function buildBreadcrumb(&$menu, $path) {
442 $pathElements = explode('/', $path);
443 array_pop($pathElements);
446 while ($newPath = array_shift($pathElements)) {
447 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
449 // when we come accross breadcrumb which involves ids,
450 // we should skip now and later on append dynamically.
451 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
455 // add to crumb, if current-path exists in params.
456 if (array_key_exists($currentPath, $menu) &&
457 isset($menu[$currentPath]['title'])
459 $urlVar = CRM_Utils_Array
::value('path_arguments', $menu[$currentPath]) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
461 'title' => $menu[$currentPath]['title'],
462 'url' => CRM_Utils_System
::url($currentPath,
468 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
473 $menu[$path]['breadcrumb'] = $crumbs;
478 static function buildReturnUrl(&$menu, $path) {
479 if (!isset($menu[$path]['return_url'])) {
480 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
484 static function getReturnUrl(&$menu, $path) {
485 if (!isset($menu[$path]['return_url'])) {
486 $pathElements = explode('/', $path);
487 array_pop($pathElements);
489 if (empty($pathElements)) {
490 return array(NULL, NULL);
492 $newPath = implode('/', $pathElements);
494 return self
::getReturnUrl($menu, $newPath);
498 CRM_Utils_Array
::value('return_url',
501 CRM_Utils_Array
::value('return_url_args',
508 static function fillComponentIds(&$menu, $path) {
509 static $cache = array();
511 if (array_key_exists('component_id', $menu[$path])) {
515 $args = explode('/', $path);
517 if (count($args) > 1) {
518 $compPath = $args[0] . '/' . $args[1];
521 $compPath = $args[0];
526 if (array_key_exists($compPath, $cache)) {
527 $menu[$path]['component_id'] = $cache[$compPath];
530 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
531 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
532 $menu[$compPath]['component'],
536 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
537 $cache[$compPath] = $menu[$path]['component_id'];
541 static function get($path) {
542 // return null if menu rebuild
543 $config = CRM_Core_Config
::singleton();
547 $args = explode('/', $path);
550 while (!empty($args)) {
551 $string = implode('/', $args);
552 $string = CRM_Core_DAO
::escapeString($string);
553 $elements[] = "'{$string}'";
557 $queryString = implode(', ', $elements);
558 $domainID = CRM_Core_Config
::domainID();
559 $domainWhereClause = " AND domain_id = $domainID ";
560 if ($config->isUpgradeMode() &&
561 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
563 //domain_id wouldn't be available for earlier version of
564 //3.0 and therefore can't be used as part of query for
566 $domainWhereClause = "";
573 WHERE path in ( $queryString )
575 ORDER BY length(path) DESC
580 if ($path != 'navigation') {
585 WHERE path IN ( 'navigation' )
591 $menu = new CRM_Core_DAO_Menu();
592 $menu->query($query);
594 self
::$_menuCache = array();
596 while ($menu->fetch()) {
597 self
::$_menuCache[$menu->path
] = array();
598 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
600 foreach (self
::$_serializedElements as $element) {
601 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
603 if (strpos($path, $menu->path
) !== FALSE) {
604 $menuPath = &self
::$_menuCache[$menu->path
];
609 if (strstr($path, 'report/instance')) {
610 $args = explode('/', $path);
611 if (is_numeric(end($args))) {
612 $menuPath['path'] .= '/' . end($args);
616 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
617 if ($path == 'civicrm/upgrade') {
618 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
619 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
620 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
622 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
623 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
624 CRM_Queue_Menu
::alter($path, $menuPath);
627 // Part of upgrade framework but not run inside main upgrade because it deletes data
628 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
629 // which follows upgrade/ and checks for existence of a function in Cleanup class.
630 if ($path == 'civicrm/upgrade/cleanup425') {
631 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup','cleanup425');
632 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
633 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
636 if (!empty($menuPath)) {
637 $i18n = CRM_Core_I18n
::singleton();
638 $i18n->localizeTitles($menuPath);
643 static function getArrayForPathArgs($pathArgs) {
644 if (!is_string($pathArgs)) {
649 $elements = explode(',', $pathArgs);
650 //CRM_Core_Error::debug( 'e', $elements );
651 foreach ($elements as $keyVal) {
652 list($key, $val) = explode('=', $keyVal);
656 if (array_key_exists('urlToSession', $arr)) {
657 $urlToSession = array();
659 $params = explode(';', $arr['urlToSession']);
661 foreach ($params as $keyVal) {
662 list($urlToSession[$count]['urlVar'],
663 $urlToSession[$count]['sessionVar'],
664 $urlToSession[$count]['type'],
665 $urlToSession[$count]['default']
666 ) = explode(':', $keyVal);
669 $arr['urlToSession'] = $urlToSession;