simplemenu.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Creates a simplemenu.
  6. */
  7. /**
  8. * Implementation of hook_menu().
  9. */
  10. function simplemenu_menu($may_cache) {
  11. $items = array();
  12. if ($may_cache) {
  13. $items[] = array(
  14. 'path' => 'simplemenu/menu',
  15. 'access' => user_access('view simplemenu'),
  16. 'callback' => 'simplemenu_get_menu',
  17. 'type' => MENU_CALLBACK
  18. );
  19. $items[] = array(
  20. 'path' => 'admin/settings/simplemenu',
  21. 'title' => t('SimpleMenu'),
  22. 'description' => t('Select the menu to display.'),
  23. 'callback' => 'drupal_get_form',
  24. 'callback arguments' => array('simplemenu_admin_settings'),
  25. 'access' => user_access('administer simplemenu')
  26. );
  27. }
  28. // We put this in !$may_cache so it's only added once per request
  29. elseif (user_access('view simplemenu')) {
  30. $path = drupal_get_path('module', 'simplemenu');
  31. drupal_add_css($path .'/simplemenu.css');
  32. // pass in base path to the JS file
  33. // url() handles appending ?q= but in this case, we need to pass in the variable so the menus work when mod_rewrite is off
  34. drupal_add_js(array('simplemenu' => array('basePath' => base_path() . (variable_get('clean_url', 0) ? '' : '?q='))), 'setting');
  35. drupal_add_js($path .'/simplemenu.js');
  36. }
  37. return $items;
  38. }
  39. /**
  40. * Implementation of hook_perm().
  41. */
  42. function simplemenu_perm() {
  43. return array('view simplemenu', 'administer simplemenu');
  44. }
  45. /**
  46. * SimpleMenu settings page.
  47. */
  48. function simplemenu_admin_settings() {
  49. $form['default_menu']['simplemenu_menu'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Menu'),
  52. '#options' => menu_get_root_menus(),
  53. '#default_value' => variable_get('simplemenu_menu', 1),
  54. '#description' => t('Select the menu to display.')
  55. );
  56. $form['default_menu']['simplemenu_devel'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => t('Add devel module links'),
  59. '#default_value' => variable_get('simplemenu_devel', 0),
  60. '#description' => t('Add devel module links for those users that can access the devel module.')
  61. );
  62. return system_settings_form($form);
  63. }
  64. /**
  65. * Return a list of devel module links if the module is enabled
  66. * and the user has access to this module.
  67. */
  68. function simplemenu_get_devel() {
  69. $output = '';
  70. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  71. if (user_access('access devel information')) {
  72. $links[] = l('module settings', 'admin/settings/devel');
  73. $links[] = l('empty cache', 'devel/cache/clear');
  74. $links[] = l('phpinfo()', 'devel/phpinfo');
  75. $links[] = l('reinstall modules', 'devel/reinstall');
  76. $links[] = l('reset menus', 'devel/menu/reset');
  77. $links[] = l('variable viewer', 'devel/variable');
  78. $links[] = l('session viewer', 'devel/session');
  79. if (function_exists('devel_node_access_perm') && user_access(DNA_ACCESS_VIEW)) {
  80. // True only if devel_node_access enabled.
  81. $links[] = l('node_access summary', 'devel/node_access/summary');
  82. }
  83. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('devel module') .'</a><ul>';
  84. $output .= '<li class="leaf">'. implode($links, '</li><li class="leaf">') .'</li>';
  85. $output .= '</ul></li>';
  86. }
  87. }
  88. return $output;
  89. }
  90. /**
  91. * Custom implementation of menu_tree().
  92. * We want to retrieve the entire menu structure for a given menu,
  93. * regardless of whether or not the menu item is expanded or not.
  94. */
  95. function simplemenu_menu_tree($pid = 1) {
  96. $menu = menu_get_menu();
  97. $output = '';
  98. if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
  99. foreach ($menu['visible'][$pid]['children'] as $mid) {
  100. $type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : NULL;
  101. $children = isset($menu['visible'][$mid]['children']) ? $menu['visible'][$mid]['children'] : NULL;
  102. $output .= theme('menu_item', $mid, simplemenu_theme_menu_tree($mid), count($children) == 0);
  103. }
  104. }
  105. return $output;
  106. }
  107. /**
  108. * Custom implementation of theme_menu_tree() to call our custom menu above.
  109. */
  110. function simplemenu_theme_menu_tree($pid = 1) {
  111. if ($tree = simplemenu_menu_tree($pid)) {
  112. return '<ul>'. $tree .'</ul>';
  113. }
  114. }
  115. /**
  116. * AJAX menu callback to return an HTML list of links for a given menu.
  117. */
  118. function simplemenu_get_menu() {
  119. $menu = simplemenu_menu_tree(variable_get('simplemenu_menu', 1));
  120. if (!$menu) {
  121. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  122. }
  123. print simplemenu_get_devel();
  124. print $menu;
  125. exit;
  126. }