Our changes
[civicrm-core.git] / CRM / Core / Menu.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This file contains the various menus of the CiviCRM module
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 */
34
35 require_once 'CRM/Core/I18n.php';
36
37 /**
38 * Class CRM_Core_Menu.
39 */
40 class CRM_Core_Menu {
41
42 /**
43 * The list of menu items.
44 *
45 * @var array
46 */
47 public static $_items = NULL;
48
49 /**
50 * The list of permissioned menu items.
51 *
52 * @var array
53 */
54 public static $_permissionedItems = NULL;
55
56 public static $_serializedElements = array(
57 'access_arguments',
58 'access_callback',
59 'page_arguments',
60 'page_callback',
61 'breadcrumb',
62 );
63
64 public static $_menuCache = NULL;
65 const MENU_ITEM = 1;
66
67 /**
68 * This function fetches the menu items from xml and xmlMenu hooks.
69 *
70 * @param bool $fetchFromXML
71 * Fetch the menu items from xml and not from cache.
72 *
73 * @return array
74 */
75 public static function &xmlItems($fetchFromXML = FALSE) {
76 if (!self::$_items || $fetchFromXML) {
77 $config = CRM_Core_Config::singleton();
78
79 // We needs this until Core becomes a component
80 $coreMenuFilesNamespace = 'CRM_Core_xml_Menu';
81 $coreMenuFilesPath = str_replace('_', DIRECTORY_SEPARATOR, $coreMenuFilesNamespace);
82 global $civicrm_root;
83 $files = CRM_Utils_File::getFilesByExtension($civicrm_root . DIRECTORY_SEPARATOR . $coreMenuFilesPath, 'xml');
84
85 // Grab component menu files
86 $files = array_merge($files,
87 CRM_Core_Component::xmlMenu()
88 );
89
90 // lets call a hook and get any additional files if needed
91 CRM_Utils_Hook::xmlMenu($files);
92
93 self::$_items = array();
94 foreach ($files as $file) {
95 self::read($file, self::$_items);
96 }
97
98 CRM_Utils_Hook::alterMenu(self::$_items);
99 }
100
101 return self::$_items;
102 }
103
104 /**
105 * Read menu.
106 *
107 * @param string $name
108 * File name
109 * @param array $menu
110 * An alterable list of menu items.
111 *
112 * @throws Exception
113 */
114 public static function read($name, &$menu) {
115 $xml = simplexml_load_file($name);
116 self::readXML($xml, $menu);
117 }
118
119 /**
120 * @param SimpleXMLElement $xml
121 * An XML document defining a list of menu items.
122 * @param array $menu
123 * An alterable list of menu items.
124 */
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();
131 }
132 $path = (string ) $item->path;
133 $menu[$path] = array();
134 unset($item->path);
135
136 if ($item->ids_arguments) {
137 $ids = array();
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;
142 }
143 }
144 $menu[$path]['ids_arguments'] = $ids;
145 unset($item->ids_arguments);
146 }
147
148 foreach ($item as $key => $value) {
149 $key = (string ) $key;
150 $value = (string ) $value;
151 if (strpos($key, '_callback') &&
152 strpos($value, '::')
153 ) {
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);
157 }
158 elseif ($key == 'access_arguments') {
159 // FIXME Move the permission parser to its own class (or *maybe* CRM_Core_Permission).
160 if (strpos($value, ',') ||
161 strpos($value, ';')
162 ) {
163 if (strpos($value, ',')) {
164 $elements = explode(',', $value);
165 $op = 'and';
166 }
167 else {
168 $elements = explode(';', $value);
169 $op = 'or';
170 }
171 $items = array();
172 foreach ($elements as $element) {
173 $items[] = $element;
174 }
175 $value = array($items, $op);
176 }
177 else {
178 $value = array(array($value), 'and');
179 }
180 }
181 elseif ($key == 'is_public' || $key == 'is_ssl') {
182 $value = ($value == 'true' || $value == 1) ? 1 : 0;
183 }
184 $menu[$path][$key] = $value;
185 }
186 }
187 }
188
189 /**
190 * This function defines information for various menu items.
191 *
192 * @param bool $fetchFromXML
193 * Fetch the menu items from xml and not from cache.
194 *
195 * @return array
196 */
197 public static function &items($fetchFromXML = FALSE) {
198 return self::xmlItems($fetchFromXML);
199 }
200
201 /**
202 * Is array true (whatever that means!).
203 *
204 * @param array $values
205 *
206 * @return bool
207 */
208 public static function isArrayTrue(&$values) {
209 foreach ($values as $name => $value) {
210 if (!$value) {
211 return FALSE;
212 }
213 }
214 return TRUE;
215 }
216
217 /**
218 * Fill menu values.
219 *
220 * @param array $menu
221 * @param string $path
222 *
223 * @throws Exception
224 */
225 public static function fillMenuValues(&$menu, $path) {
226 $fieldsToPropagate = array(
227 'access_callback',
228 'access_arguments',
229 'page_callback',
230 'page_arguments',
231 'is_ssl',
232 );
233 $fieldsPresent = array();
234 foreach ($fieldsToPropagate as $field) {
235 $fieldsPresent[$field] = CRM_Utils_Array::value($field, $menu[$path]) !== NULL ? TRUE : FALSE;
236 }
237
238 $args = explode('/', $path);
239 while (!self::isArrayTrue($fieldsPresent) && !empty($args)) {
240
241 array_pop($args);
242 $parentPath = implode('/', $args);
243
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];
249 }
250 }
251 }
252 }
253
254 if (self::isArrayTrue($fieldsPresent)) {
255 return;
256 }
257
258 $messages = array();
259 foreach ($fieldsToPropagate as $field) {
260 if (!$fieldsPresent[$field]) {
261 $messages[] = ts("Could not find %1 in path tree",
262 array(1 => $field)
263 );
264 }
265 }
266 CRM_Core_Error::fatal("'$path': " . implode(', ', $messages));
267 }
268
269 /**
270 * We use this function to.
271 *
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
276 *
277 * @param array $menu
278 */
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);
285
286 // add add page_type if not present
287 if (!isset($menu[$path]['page_type'])) {
288 $menu[$path]['page_type'] = 0;
289 }
290 }
291
292 self::buildAdminLinks($menu);
293 }
294
295 /**
296 * This function recomputes menu from xml and populates civicrm_menu.
297 *
298 * @param bool $truncate
299 */
300 public static function store($truncate = TRUE) {
301 // first clean up the db
302 if ($truncate) {
303 $query = 'TRUNCATE civicrm_menu';
304 CRM_Core_DAO::executeQuery($query);
305 }
306 $menuArray = self::items($truncate);
307
308 self::build($menuArray);
309
310 $daoFields = CRM_Core_DAO_Menu::fields();
311
312 foreach ($menuArray as $path => $item) {
313 $menu = new CRM_Core_DAO_Menu();
314 $menu->path = $path;
315 $menu->domain_id = CRM_Core_Config::domainID();
316
317 $menu->find(TRUE);
318
319 if (!CRM_Core_Config::isUpgradeMode() ||
320 CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_menu', 'module_data', FALSE)
321 ) {
322 // Move unrecognized fields to $module_data.
323 $module_data = array();
324 foreach (array_keys($item) as $key) {
325 if (!isset($daoFields[$key])) {
326 $module_data[$key] = $item[$key];
327 unset($item[$key]);
328 }
329 }
330
331 $menu->module_data = serialize($module_data);
332 }
333
334 $menu->copyValues($item);
335
336 foreach (self::$_serializedElements as $element) {
337 if (!isset($item[$element]) ||
338 $item[$element] == 'null'
339 ) {
340 $menu->$element = NULL;
341 }
342 else {
343 $menu->$element = serialize($item[$element]);
344 }
345 }
346
347 $menu->save();
348 }
349 }
350
351 /**
352 * Build admin links.
353 *
354 * @param array $menu
355 */
356 public static function buildAdminLinks(&$menu) {
357 $values = array();
358
359 foreach ($menu as $path => $item) {
360 if (empty($item['adminGroup'])) {
361 continue;
362 }
363
364 $query = !empty($item['path_arguments']) ? str_replace(',', '&', $item['path_arguments']) . '&reset=1' : 'reset=1';
365
366 $value = array(
367 'title' => $item['title'],
368 'desc' => CRM_Utils_Array::value('desc', $item),
369 'id' => strtr($item['title'], array(
370 '(' => '_',
371 ')' => '',
372 ' ' => '',
373 ',' => '_',
374 '/' => '_',
375 )),
376 'url' => CRM_Utils_System::url($path, $query,
377 FALSE,
378 NULL,
379 TRUE,
380 FALSE,
381 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
382 TRUE
383 ),
384 'icon' => CRM_Utils_Array::value('icon', $item),
385 'extra' => CRM_Utils_Array::value('extra', $item),
386 );
387 if (!array_key_exists($item['adminGroup'], $values)) {
388 $values[$item['adminGroup']] = array();
389 $values[$item['adminGroup']]['fields'] = array();
390 }
391 $weight = CRM_Utils_Array::value('weight', $item, 0);
392 $values[$item['adminGroup']]['fields']["{weight}.{$item['title']}"] = $value;
393 $values[$item['adminGroup']]['component_id'] = $item['component_id'];
394 }
395
396 foreach ($values as $group => $dontCare) {
397 $values[$group]['perColumn'] = round(count($values[$group]['fields']) / 2);
398 ksort($values[$group]);
399 }
400
401 $menu['admin'] = array('breadcrumb' => $values);
402 }
403
404 /**
405 * Get navigation.
406 *
407 * @param bool $all
408 *
409 * @return mixed
410 * @throws Exception
411 */
412 public static function &getNavigation($all = FALSE) {
413 CRM_Core_Error::fatal();
414
415 if (!self::$_menuCache) {
416 self::get('navigation');
417 }
418
419 if (CRM_Core_Config::isUpgradeMode()) {
420 return array();
421 }
422
423 if (!array_key_exists('navigation', self::$_menuCache)) {
424 // problem could be due to menu table empty. Just do a
425 // menu store and try again
426 self::store();
427
428 // here we goo
429 self::get('navigation');
430 if (!array_key_exists('navigation', self::$_menuCache)) {
431 CRM_Core_Error::fatal();
432 }
433 }
434 $nav = &self::$_menuCache['navigation'];
435
436 if (!$nav ||
437 !isset($nav['breadcrumb'])
438 ) {
439 return NULL;
440 }
441
442 $values = &$nav['breadcrumb'];
443 $config = CRM_Core_Config::singleton();
444 foreach ($values as $index => $item) {
445 if (strpos(CRM_Utils_Array::value($config->userFrameworkURLVar, $_REQUEST),
446 $item['path']
447 ) === 0
448 ) {
449 $values[$index]['active'] = 'class="active"';
450 }
451 else {
452 $values[$index]['active'] = '';
453 }
454
455 if ($values[$index]['parent']) {
456 $parent = $values[$index]['parent'];
457
458 // only reset if still a leaf
459 if ($values[$parent]['class'] == 'leaf') {
460 $values[$parent]['class'] = 'collapsed';
461 }
462
463 // if a child or the parent is active, expand the menu
464 if ($values[$index]['active'] ||
465 $values[$parent]['active']
466 ) {
467 $values[$parent]['class'] = 'expanded';
468 }
469
470 // make the parent inactive if the child is active
471 if ($values[$index]['active'] &&
472 $values[$parent]['active']
473 ) {
474 $values[$parent]['active'] = '';
475 }
476 }
477 }
478
479 if (!$all) {
480 // remove all collapsed menu items from the array
481 foreach ($values as $weight => $v) {
482 if ($v['parent'] &&
483 $values[$v['parent']]['class'] == 'collapsed'
484 ) {
485 unset($values[$weight]);
486 }
487 }
488 }
489
490 // check permissions for the rest
491 $activeChildren = array();
492
493 foreach ($values as $weight => $v) {
494 if (CRM_Core_Permission::checkMenuItem($v)) {
495 if ($v['parent']) {
496 $activeChildren[] = $weight;
497 }
498 }
499 else {
500 unset($values[$weight]);
501 }
502 }
503
504 // add the start / end tags
505 $len = count($activeChildren) - 1;
506 if ($len >= 0) {
507 $values[$activeChildren[0]]['start'] = TRUE;
508 $values[$activeChildren[$len]]['end'] = TRUE;
509 }
510
511 ksort($values, SORT_NUMERIC);
512 $i18n = CRM_Core_I18n::singleton();
513 $i18n->localizeTitles($values);
514
515 return $values;
516 }
517
518 /**
519 * Get admin links.
520 *
521 * @return null
522 */
523 public static function &getAdminLinks() {
524 $links = self::get('admin');
525
526 if (!$links ||
527 !isset($links['breadcrumb'])
528 ) {
529 return NULL;
530 }
531
532 $values = &$links['breadcrumb'];
533 return $values;
534 }
535
536 /**
537 * Get the breadcrumb for a given path.
538 *
539 * @param array $menu
540 * An array of all the menu items.
541 * @param string $path
542 * Path for which breadcrumb is to be build.
543 *
544 * @return array
545 * The breadcrumb for this path
546 */
547 public static function buildBreadcrumb(&$menu, $path) {
548 $crumbs = array();
549
550 $pathElements = explode('/', $path);
551 array_pop($pathElements);
552
553 $currentPath = NULL;
554 while ($newPath = array_shift($pathElements)) {
555 $currentPath = $currentPath ? ($currentPath . '/' . $newPath) : $newPath;
556
557 // when we come across breadcrumb which involves ids,
558 // we should skip now and later on append dynamically.
559 if (isset($menu[$currentPath]['skipBreadcrumb'])) {
560 continue;
561 }
562
563 // add to crumb, if current-path exists in params.
564 if (array_key_exists($currentPath, $menu) &&
565 isset($menu[$currentPath]['title'])
566 ) {
567 $urlVar = !empty($menu[$currentPath]['path_arguments']) ? '&' . $menu[$currentPath]['path_arguments'] : '';
568 $crumbs[] = array(
569 'title' => $menu[$currentPath]['title'],
570 'url' => CRM_Utils_System::url($currentPath,
571 'reset=1' . $urlVar,
572 // absolute
573 FALSE,
574 // fragment
575 NULL,
576 // htmlize
577 TRUE,
578 // frontend
579 FALSE,
580 // forceBackend; CRM-14439 work-around; acceptable for now because we don't display breadcrumbs on frontend
581 TRUE
582 ),
583 );
584 }
585 }
586 $menu[$path]['breadcrumb'] = $crumbs;
587
588 return $crumbs;
589 }
590
591 /**
592 * @param $menu
593 * @param $path
594 */
595 public static function buildReturnUrl(&$menu, $path) {
596 if (!isset($menu[$path]['return_url'])) {
597 list($menu[$path]['return_url'], $menu[$path]['return_url_args']) = self::getReturnUrl($menu, $path);
598 }
599 }
600
601 /**
602 * @param $menu
603 * @param $path
604 *
605 * @return array
606 */
607 public static function getReturnUrl(&$menu, $path) {
608 if (!isset($menu[$path]['return_url'])) {
609 $pathElements = explode('/', $path);
610 array_pop($pathElements);
611
612 if (empty($pathElements)) {
613 return array(NULL, NULL);
614 }
615 $newPath = implode('/', $pathElements);
616
617 return self::getReturnUrl($menu, $newPath);
618 }
619 else {
620 return array(
621 CRM_Utils_Array::value('return_url',
622 $menu[$path]
623 ),
624 CRM_Utils_Array::value('return_url_args',
625 $menu[$path]
626 ),
627 );
628 }
629 }
630
631 /**
632 * @param $menu
633 * @param $path
634 */
635 public static function fillComponentIds(&$menu, $path) {
636 static $cache = array();
637
638 if (array_key_exists('component_id', $menu[$path])) {
639 return;
640 }
641
642 $args = explode('/', $path);
643
644 if (count($args) > 1) {
645 $compPath = $args[0] . '/' . $args[1];
646 }
647 else {
648 $compPath = $args[0];
649 }
650
651 $componentId = NULL;
652
653 if (array_key_exists($compPath, $cache)) {
654 $menu[$path]['component_id'] = $cache[$compPath];
655 }
656 else {
657 if (CRM_Utils_Array::value('component', CRM_Utils_Array::value($compPath, $menu))) {
658 $componentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component',
659 $menu[$compPath]['component'],
660 'id', 'name'
661 );
662 }
663 $menu[$path]['component_id'] = $componentId ? $componentId : NULL;
664 $cache[$compPath] = $menu[$path]['component_id'];
665 }
666 }
667
668 /**
669 * @param $path string
670 * Path of menu item to retrieve.
671 *
672 * @return array
673 * Menu entry array.
674 */
675 public static function get($path) {
676 // return null if menu rebuild
677 $config = CRM_Core_Config::singleton();
678
679 $args = explode('/', $path);
680
681 $elements = array();
682 while (!empty($args)) {
683 $string = implode('/', $args);
684 $string = CRM_Core_DAO::escapeString($string);
685 $elements[] = "'{$string}'";
686 array_pop($args);
687 }
688
689 $queryString = implode(', ', $elements);
690 $domainID = CRM_Core_Config::domainID();
691
692 $query = "
693 (
694 SELECT *
695 FROM civicrm_menu
696 WHERE path in ( $queryString )
697 AND domain_id = $domainID
698 ORDER BY length(path) DESC
699 LIMIT 1
700 )
701 ";
702
703 if ($path != 'navigation') {
704 $query .= "
705 UNION (
706 SELECT *
707 FROM civicrm_menu
708 WHERE path IN ( 'navigation' )
709 AND domain_id = $domainID
710 )
711 ";
712 }
713
714 $menu = new CRM_Core_DAO_Menu();
715 $menu->query($query);
716
717 self::$_menuCache = array();
718 $menuPath = NULL;
719 while ($menu->fetch()) {
720 self::$_menuCache[$menu->path] = array();
721 CRM_Core_DAO::storeValues($menu, self::$_menuCache[$menu->path]);
722
723 // Move module_data into main item.
724 if (isset(self::$_menuCache[$menu->path]['module_data'])) {
725 CRM_Utils_Array::extend(self::$_menuCache[$menu->path],
726 unserialize(self::$_menuCache[$menu->path]['module_data']));
727 unset(self::$_menuCache[$menu->path]['module_data']);
728 }
729
730 // Unserialize other elements.
731 foreach (self::$_serializedElements as $element) {
732 self::$_menuCache[$menu->path][$element] = unserialize($menu->$element);
733
734 if (strpos($path, $menu->path) !== FALSE) {
735 $menuPath = &self::$_menuCache[$menu->path];
736 }
737 }
738 }
739
740 if (strstr($path, 'report/instance')) {
741 $args = explode('/', $path);
742 if (is_numeric(end($args))) {
743 $menuPath['path'] .= '/' . end($args);
744 }
745 }
746
747 // *FIXME* : hack for 4.1 -> 4.2 upgrades.
748 if (preg_match('/^civicrm\/(upgrade\/)?queue\//', $path)) {
749 CRM_Queue_Menu::alter($path, $menuPath);
750 }
751
752 // Part of upgrade framework but not run inside main upgrade because it deletes data
753 // Once we have another example of a 'cleanup' we should generalize the clause below so it grabs string
754 // which follows upgrade/ and checks for existence of a function in Cleanup class.
755 if ($path == 'civicrm/upgrade/cleanup425') {
756 $menuPath['page_callback'] = array('CRM_Upgrade_Page_Cleanup', 'cleanup425');
757 $menuPath['access_arguments'][0][] = 'administer CiviCRM';
758 $menuPath['access_callback'] = array('CRM_Core_Permission', 'checkMenu');
759 }
760
761 if (!empty($menuPath)) {
762 $i18n = CRM_Core_I18n::singleton();
763 $i18n->localizeTitles($menuPath);
764 }
765 return $menuPath;
766 }
767
768 /**
769 * @param $pathArgs
770 *
771 * @return mixed
772 */
773 public static function getArrayForPathArgs($pathArgs) {
774 if (!is_string($pathArgs)) {
775 return;
776 }
777 $args = array();
778
779 $elements = explode(',', $pathArgs);
780 foreach ($elements as $keyVal) {
781 list($key, $val) = explode('=', $keyVal, 2);
782 $arr[$key] = $val;
783 }
784
785 if (array_key_exists('urlToSession', $arr)) {
786 $urlToSession = array();
787
788 $params = explode(';', $arr['urlToSession']);
789 $count = 0;
790 foreach ($params as $keyVal) {
791 list($urlToSession[$count]['urlVar'],
792 $urlToSession[$count]['sessionVar'],
793 $urlToSession[$count]['type'],
794 $urlToSession[$count]['default']
795 ) = explode(':', $keyVal);
796 $count++;
797 }
798 $arr['urlToSession'] = $urlToSession;
799 }
800 return $arr;
801 }
802
803 }