3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
35 require_once 'CRM/Core/I18n.php';
38 * Class CRM_Core_Menu.
43 * The list of menu items.
47 static $_items = NULL;
50 * The list of permissioned menu items.
54 static $_permissionedItems = NULL;
56 static $_serializedElements = array(
64 static $_menuCache = NULL;
68 * This function fetches the menu items from xml and xmlMenu hooks.
70 * @param bool $fetchFromXML
71 * Fetch the menu items from xml and not from cache.
75 public static function &xmlItems($fetchFromXML = FALSE) {
76 if (!self
::$_items ||
$fetchFromXML) {
77 $config = CRM_Core_Config
::singleton();
79 // We needs this until Core becomes a component
80 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
81 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR
, $coreMenuFilesNamespace);
83 $files = CRM_Utils_File
::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR
. $coreMenuFilesPath, 'xml');
85 // Grab component menu files
86 $files = array_merge($files,
87 CRM_Core_Component
::xmlMenu()
90 // lets call a hook and get any additional files if needed
91 CRM_Utils_Hook
::xmlMenu($files);
93 self
::$_items = array();
94 foreach ($files as $file) {
95 self
::read($file, self
::$_items);
98 CRM_Utils_Hook
::alterMenu(self
::$_items);
101 return self
::$_items;
107 * @param string $name
110 * An alterable list of menu items.
114 public static function read($name, &$menu) {
115 $xml = simplexml_load_file($name);
116 self
::readXML($xml, $menu);
120 * @param SimpleXMLElement $xml
121 * An XML document defining a list of menu items.
123 * An alterable list of menu items.
125 public static function readXML($xml, &$menu) {
126 $config = CRM_Core_Config
::singleton();
127 foreach ($xml->item
as $item) {
128 if (!(string ) $item->path
) {
129 CRM_Core_Error
::debug('i', $item);
130 CRM_Core_Error
::fatal();
132 $path = (string ) $item->path
;
133 $menu[$path] = array();
136 if ($item->ids_arguments
) {
138 foreach (array('json' => 'json', 'html' => 'html', 'exception' => 'exceptions') as $tag => $attr) {
139 $ids[$attr] = array();
140 foreach ($item->ids_arguments
->{$tag} as $value) {
141 $ids[$attr][] = (string) $value;
144 $menu[$path]['ids_arguments'] = $ids;
145 unset($item->ids_arguments
);
148 foreach ($item as $key => $value) {
149 $key = (string ) $key;
150 $value = (string ) $value;
151 if (strpos($key, '_callback') &&
154 // FIXME Remove the rewrite at this level. Instead, change downstream call_user_func*($value)
155 // to call_user_func*(Civi\Core\Resolver::singleton()->get($value)).
156 $value = explode('::', $value);
158 elseif ($key == 'access_arguments') {
159 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
160 if (strpos($value, ',') ||
163 if (strpos($value, ',')) {
164 $elements = explode(',', $value);
168 $elements = explode(';', $value);
172 foreach ($elements as $element) {
175 $value = array($items, $op);
178 $value = array(array($value), 'and');
181 elseif ($key == 'is_public' ||
$key == 'is_ssl') {
182 $value = ($value == 'true' ||
$value == 1) ?
1 : 0;
184 $menu[$path][$key] = $value;
190 * This function defines information for various menu items.
192 * @param bool $fetchFromXML
193 * Fetch the menu items from xml and not from cache.
197 public static function &items($fetchFromXML = FALSE) {
198 return self
::xmlItems($fetchFromXML);
202 * Is array true (whatever that means!).
204 * @param array $values
208 public static function isArrayTrue(&$values) {
209 foreach ($values as $name => $value) {
221 * @param string $path
225 public static function fillMenuValues(&$menu, $path) {
226 $fieldsToPropagate = array(
233 $fieldsPresent = array();
234 foreach ($fieldsToPropagate as $field) {
235 $fieldsPresent[$field] = CRM_Utils_Array
::value($field, $menu[$path]) !== NULL ?
TRUE : FALSE;
238 $args = explode('/', $path);
239 while (!self
::isArrayTrue($fieldsPresent) && !empty($args)) {
242 $parentPath = implode('/', $args);
244 foreach ($fieldsToPropagate as $field) {
245 if (!$fieldsPresent[$field]) {
246 if (CRM_Utils_Array
::value($field, CRM_Utils_Array
::value($parentPath, $menu)) !== NULL) {
247 $fieldsPresent[$field] = TRUE;
248 $menu[$path][$field] = $menu[$parentPath][$field];
254 if (self
::isArrayTrue($fieldsPresent)) {
259 foreach ($fieldsToPropagate as $field) {
260 if (!$fieldsPresent[$field]) {
261 $messages[] = ts("Could not find %1 in path tree",
266 CRM_Core_Error
::fatal("'$path': " . implode(', ', $messages));
270 * We use this function to.
272 * 1. Compute the breadcrumb
273 * 2. Compute local tasks value if any
274 * 3. Propagate access argument, access callback, page callback to the menu item
275 * 4. Build the global navigation block
279 public static function build(&$menu) {
280 foreach ($menu as $path => $menuItems) {
281 self
::buildBreadcrumb($menu, $path);
282 self
::fillMenuValues($menu, $path);
283 self
::fillComponentIds($menu, $path);
284 self
::buildReturnUrl($menu, $path);
286 // add add page_type if not present
287 if (!isset($menu[$path]['page_type'])) {
288 $menu[$path]['page_type'] = 0;
292 self
::buildAdminLinks($menu);
296 * This function recomputes menu from xml and populates civicrm_menu.
298 * @param bool $truncate
300 public static function store($truncate = TRUE) {
301 // first clean up the db
303 $query = 'TRUNCATE civicrm_menu';
304 CRM_Core_DAO
::executeQuery($query);
306 $menuArray = self
::items($truncate);
308 self
::build($menuArray);
310 $config = CRM_Core_Config
::singleton();
311 $daoFields = CRM_Core_DAO_Menu
::fields();
313 foreach ($menuArray as $path => $item) {
314 $menu = new CRM_Core_DAO_Menu();
316 $menu->domain_id
= CRM_Core_Config
::domainID();
320 // Move unrecognized fields to $module_data.
321 $module_data = array();
322 foreach (array_keys($item) as $key) {
323 if (!isset($daoFields[$key])) {
324 $module_data[$key] = $item[$key];
329 $menu->copyValues($item);
330 $menu->module_data
= serialize($module_data);
332 foreach (self
::$_serializedElements as $element) {
333 if (!isset($item[$element]) ||
334 $item[$element] == 'null'
336 $menu->$element = NULL;
339 $menu->$element = serialize($item[$element]);
352 public static function buildAdminLinks(&$menu) {
355 foreach ($menu as $path => $item) {
356 if (empty($item['adminGroup'])) {
360 $query = !empty($item['path_arguments']) ?
str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
363 'title' => $item['title'],
364 'desc' => CRM_Utils_Array
::value('desc', $item),
365 'id' => strtr($item['title'], array(
373 'url' => CRM_Utils_System
::url($path, $query,
378 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
381 'icon' => CRM_Utils_Array
::value('icon', $item),
382 'extra' => CRM_Utils_Array
::value('extra', $item),
384 if (!array_key_exists($item['adminGroup'], $values)) {
385 $values[$item['adminGroup']] = array();
386 $values[$item['adminGroup']]['fields'] = array();
388 $weight = CRM_Utils_Array
::value('weight', $item, 0);
389 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
390 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
393 foreach ($values as $group => $dontCare) {
394 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
395 ksort($values[$group]);
398 $menu['admin'] = array('breadcrumb' => $values);
409 public static function &getNavigation($all = FALSE) {
410 CRM_Core_Error
::fatal();
412 if (!self
::$_menuCache) {
413 self
::get('navigation');
416 if (CRM_Core_Config
::isUpgradeMode()) {
420 if (!array_key_exists('navigation', self
::$_menuCache)) {
421 // problem could be due to menu table empty. Just do a
422 // menu store and try again
426 self
::get('navigation');
427 if (!array_key_exists('navigation', self
::$_menuCache)) {
428 CRM_Core_Error
::fatal();
431 $nav = &self
::$_menuCache['navigation'];
434 !isset($nav['breadcrumb'])
439 $values = &$nav['breadcrumb'];
440 $config = CRM_Core_Config
::singleton();
441 foreach ($values as $index => $item) {
442 if (strpos(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_REQUEST),
446 $values[$index]['active'] = 'class="active"';
449 $values[$index]['active'] = '';
452 if ($values[$index]['parent']) {
453 $parent = $values[$index]['parent'];
455 // only reset if still a leaf
456 if ($values[$parent]['class'] == 'leaf') {
457 $values[$parent]['class'] = 'collapsed';
460 // if a child or the parent is active, expand the menu
461 if ($values[$index]['active'] ||
462 $values[$parent]['active']
464 $values[$parent]['class'] = 'expanded';
467 // make the parent inactive if the child is active
468 if ($values[$index]['active'] &&
469 $values[$parent]['active']
471 $values[$parent]['active'] = '';
477 // remove all collapsed menu items from the array
478 foreach ($values as $weight => $v) {
480 $values[$v['parent']]['class'] == 'collapsed'
482 unset($values[$weight]);
487 // check permissions for the rest
488 $activeChildren = array();
490 foreach ($values as $weight => $v) {
491 if (CRM_Core_Permission
::checkMenuItem($v)) {
493 $activeChildren[] = $weight;
497 unset($values[$weight]);
501 // add the start / end tags
502 $len = count($activeChildren) - 1;
504 $values[$activeChildren[0]]['start'] = TRUE;
505 $values[$activeChildren[$len]]['end'] = TRUE;
508 ksort($values, SORT_NUMERIC
);
509 $i18n = CRM_Core_I18n
::singleton();
510 $i18n->localizeTitles($values);
520 public static function &getAdminLinks() {
521 $links = self
::get('admin');
524 !isset($links['breadcrumb'])
529 $values = &$links['breadcrumb'];
534 * Get the breadcrumb for a given path.
537 * An array of all the menu items.
538 * @param string $path
539 * Path for which breadcrumb is to be build.
542 * The breadcrumb for this path
544 public static function buildBreadcrumb(&$menu, $path) {
547 $pathElements = explode('/', $path);
548 array_pop($pathElements);
551 while ($newPath = array_shift($pathElements)) {
552 $currentPath = $currentPath ?
($currentPath . '/' . $newPath) : $newPath;
554 // when we come across breadcrumb which involves ids,
555 // we should skip now and later on append dynamically.
556 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
560 // add to crumb, if current-path exists in params.
561 if (array_key_exists($currentPath, $menu) &&
562 isset($menu[$currentPath]['title'])
564 $urlVar = !empty($menu[$currentPath]['path_arguments']) ?
'&' . $menu[$currentPath]['path_arguments'] : '';
566 'title' => $menu[$currentPath]['title'],
567 'url' => CRM_Utils_System
::url($currentPath,
573 TRUE // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
578 $menu[$path]['breadcrumb'] = $crumbs;
587 public static function buildReturnUrl(&$menu, $path) {
588 if (!isset($menu[$path]['return_url'])) {
589 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self
::getReturnUrl($menu, $path);
599 public static function getReturnUrl(&$menu, $path) {
600 if (!isset($menu[$path]['return_url'])) {
601 $pathElements = explode('/', $path);
602 array_pop($pathElements);
604 if (empty($pathElements)) {
605 return array(NULL, NULL);
607 $newPath = implode('/', $pathElements);
609 return self
::getReturnUrl($menu, $newPath);
613 CRM_Utils_Array
::value('return_url',
616 CRM_Utils_Array
::value('return_url_args',
627 public static function fillComponentIds(&$menu, $path) {
628 static $cache = array();
630 if (array_key_exists('component_id', $menu[$path])) {
634 $args = explode('/', $path);
636 if (count($args) > 1) {
637 $compPath = $args[0] . '/' . $args[1];
640 $compPath = $args[0];
645 if (array_key_exists($compPath, $cache)) {
646 $menu[$path]['component_id'] = $cache[$compPath];
649 if (CRM_Utils_Array
::value('component', CRM_Utils_Array
::value($compPath, $menu))) {
650 $componentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Component',
651 $menu[$compPath]['component'],
655 $menu[$path]['component_id'] = $componentId ?
$componentId : NULL;
656 $cache[$compPath] = $menu[$path]['component_id'];
661 * @param $path string
662 * Path of menu item to retrieve.
667 public static function get($path) {
668 // return null if menu rebuild
669 $config = CRM_Core_Config
::singleton();
673 $args = explode('/', $path);
676 while (!empty($args)) {
677 $string = implode('/', $args);
678 $string = CRM_Core_DAO
::escapeString($string);
679 $elements[] = "'{$string}'";
683 $queryString = implode(', ', $elements);
684 $domainID = CRM_Core_Config
::domainID();
685 $domainWhereClause = " AND domain_id = $domainID ";
686 if ($config->isUpgradeMode() &&
687 !CRM_Core_DAO
::checkFieldExists('civicrm_menu', 'domain_id')
689 //domain_id wouldn't be available for earlier version of
690 //3.0 and therefore can't be used as part of query for
692 $domainWhereClause = "";
699 WHERE path in ( $queryString )
701 ORDER BY length(path) DESC
706 if ($path != 'navigation') {
711 WHERE path IN ( 'navigation' )
717 $menu = new CRM_Core_DAO_Menu();
718 $menu->query($query);
720 self
::$_menuCache = array();
722 while ($menu->fetch()) {
723 self
::$_menuCache[$menu->path
] = array();
724 CRM_Core_DAO
::storeValues($menu, self
::$_menuCache[$menu->path
]);
726 // Move module_data into main item.
727 if (isset(self
::$_menuCache[$menu->path
]['module_data'])) {
728 CRM_Utils_Array
::extend(self
::$_menuCache[$menu->path
],
729 unserialize(self
::$_menuCache[$menu->path
]['module_data']));
730 unset(self
::$_menuCache[$menu->path
]['module_data']);
733 // Unserialize other elements.
734 foreach (self
::$_serializedElements as $element) {
735 self
::$_menuCache[$menu->path
][$element] = unserialize($menu->$element);
737 if (strpos($path, $menu->path
) !== FALSE) {
738 $menuPath = &self
::$_menuCache[$menu->path
];
743 if (strstr($path, 'report/instance')) {
744 $args = explode('/', $path);
745 if (is_numeric(end($args))) {
746 $menuPath['path'] .= '/' . end($args);
750 // *FIXME* : hack for 2.1 -> 2.2 upgrades.
751 if ($path == 'civicrm/upgrade') {
752 $menuPath['page_callback'] = 'CRM_Upgrade_Page_Upgrade';
753 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
754 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
756 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
757 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
758 CRM_Queue_Menu
::alter($path, $menuPath);
761 // Part of upgrade framework but not run inside main upgrade because it deletes data
762 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
763 // which follows upgrade/ and checks for existence of a function in Cleanup class.
764 if ($path == 'civicrm/upgrade/cleanup425') {
765 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
766 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
767 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
770 if (!empty($menuPath)) {
771 $i18n = CRM_Core_I18n
::singleton();
772 $i18n->localizeTitles($menuPath);
782 public static function getArrayForPathArgs($pathArgs) {
783 if (!is_string($pathArgs)) {
788 $elements = explode(',', $pathArgs);
789 foreach ($elements as $keyVal) {
790 list($key, $val) = explode('=', $keyVal, 2);
794 if (array_key_exists('urlToSession', $arr)) {
795 $urlToSession = array();
797 $params = explode(';', $arr['urlToSession']);
799 foreach ($params as $keyVal) {
800 list($urlToSession[$count]['urlVar'],
801 $urlToSession[$count]['sessionVar'],
802 $urlToSession[$count]['type'],
803 $urlToSession[$count]['default']
804 ) = explode(':', $keyVal);
807 $arr['urlToSession'] = $urlToSession;