simplemenu.module 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $settings = array(
  33. // pass in base path to the JS file
  34. // url() handles appending ?q= but in this case, we need to pass in the variable so the menus work when mod_rewrite is off
  35. 'basePath' => base_path() . (variable_get('clean_url', 0) ? '' : '?q='),
  36. 'placement' => variable_get('simplemenu_element_method', 'prepend'),
  37. 'element' => variable_get('simplemenu_element', 'body')
  38. );
  39. drupal_add_js(array('simplemenu' => $settings), 'setting');
  40. drupal_add_js($path .'/simplemenu.js');
  41. }
  42. return $items;
  43. }
  44. /**
  45. * Implementation of hook_perm().
  46. */
  47. function simplemenu_perm() {
  48. return array('view simplemenu', 'administer simplemenu');
  49. }
  50. /**
  51. * SimpleMenu settings page.
  52. */
  53. function simplemenu_admin_settings() {
  54. $form['default_menu']['simplemenu_menu'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Menu'),
  57. '#options' => menu_parent_options(0),
  58. '#default_value' => variable_get('simplemenu_menu', 1),
  59. '#description' => t('Select the menu to display.')
  60. );
  61. $form['default_menu']['simplemenu_devel'] = array(
  62. '#type' => 'checkbox',
  63. '#title' => t('Add devel module links'),
  64. '#default_value' => variable_get('simplemenu_devel', 0),
  65. '#description' => t('Add devel module links for those users that can access the devel module.')
  66. );
  67. $form['default_menu']['advanced'] = array(
  68. '#type' => 'fieldset',
  69. '#title' => t('Advanced settings'),
  70. '#collapsible' => TRUE,
  71. '#collapsed' => TRUE
  72. );
  73. $form['default_menu']['advanced']['simplemenu_element'] = array(
  74. '#type' => 'textfield',
  75. '#title' => t('CSS selector to attach menu to'),
  76. '#default_value' => variable_get('simplemenu_element', 'body'),
  77. '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
  78. '#required' => TRUE
  79. );
  80. $form['default_menu']['advanced']['simplemenu_element_method'] = array(
  81. '#type' => 'radios',
  82. '#title' => 'Attach method',
  83. '#options' => drupal_map_assoc(array('prepend', 'append', 'replace')),
  84. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  85. '#description' => t('Choose how the menu should be attached to the above selector.'),
  86. '#required' => TRUE
  87. );
  88. return system_settings_form($form);
  89. }
  90. /**
  91. * Return a list of devel module links if the module is enabled
  92. * and the user has access to this module.
  93. */
  94. function simplemenu_get_devel() {
  95. $output = '';
  96. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  97. if (user_access('access devel information')) {
  98. $links[] = l('module settings', 'admin/settings/devel');
  99. $links[] = l('empty cache', 'devel/cache/clear');
  100. $links[] = l('phpinfo()', 'devel/phpinfo');
  101. $links[] = l('reinstall modules', 'devel/reinstall');
  102. $links[] = l('reset menus', 'devel/menu/reset');
  103. $links[] = l('variable viewer', 'devel/variable');
  104. $links[] = l('session viewer', 'devel/session');
  105. if (function_exists('devel_node_access_perm') && user_access(DNA_ACCESS_VIEW)) {
  106. // True only if devel_node_access enabled.
  107. $links[] = l('node_access summary', 'devel/node_access/summary');
  108. }
  109. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a><ul>';
  110. $output .= '<li class="leaf">'. implode($links, '</li><li class="leaf">') .'</li>';
  111. $output .= '</ul></li>';
  112. }
  113. }
  114. return $output;
  115. }
  116. /**
  117. * Custom implementation of menu_tree().
  118. * We want to retrieve the entire menu structure for a given menu,
  119. * regardless of whether or not the menu item is expanded or not.
  120. */
  121. function simplemenu_menu_tree($pid = 1) {
  122. $menu = menu_get_menu();
  123. $output = '';
  124. if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
  125. foreach ($menu['visible'][$pid]['children'] as $mid) {
  126. $type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : NULL;
  127. $children = isset($menu['visible'][$mid]['children']) ? $menu['visible'][$mid]['children'] : NULL;
  128. $output .= theme('menu_item', $mid, simplemenu_theme_menu_tree($mid), count($children) == 0);
  129. }
  130. }
  131. return $output;
  132. }
  133. /**
  134. * Custom implementation of theme_menu_tree() to call our custom menu above.
  135. */
  136. function simplemenu_theme_menu_tree($pid = 1) {
  137. if ($tree = simplemenu_menu_tree($pid)) {
  138. return '<ul>'. $tree .'</ul>';
  139. }
  140. }
  141. /**
  142. * AJAX menu callback to return an HTML list of links for a given menu.
  143. */
  144. function simplemenu_get_menu() {
  145. $menu = simplemenu_menu_tree(variable_get('simplemenu_menu', 1));
  146. if (!$menu) {
  147. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  148. }
  149. print simplemenu_get_devel();
  150. print $menu;
  151. exit;
  152. }