simplemenu.module 6.7 KB

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