simplemenu.module 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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' => 'admin/settings/simplemenu',
  15. 'title' => t('SimpleMenu'),
  16. 'description' => t('Select the menu to display.'),
  17. 'callback' => 'drupal_get_form',
  18. 'callback arguments' => array('simplemenu_admin_settings'),
  19. 'access' => user_access('administer simplemenu')
  20. );
  21. }
  22. return $items;
  23. }
  24. /**
  25. * Implementation of hook_footer()
  26. */
  27. function simplemenu_footer() {
  28. global $theme;
  29. $exclusions = variable_get('simplemenu_exclusions', array());
  30. if (user_access('view simplemenu') && !$exclusions[$theme]) {
  31. global $theme, $custom_theme;
  32. $path = drupal_get_path('module', 'simplemenu');
  33. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  34. drupal_add_css($path .'/simplemenu.css');
  35. if ($simplemenu_theme != 'custom') {
  36. drupal_add_css($path .'/themes/'. $simplemenu_theme .'/'. $simplemenu_theme .'.css');
  37. }
  38. $settings = array(
  39. // pass in base path to the JS file
  40. // url() handles appending ?q= but in this case, we need to pass in the variable so the menus work when mod_rewrite is off
  41. 'basePath' => base_path() . (variable_get('clean_url', 0) ? '' : '?q='),
  42. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  43. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  44. 'element' => variable_get('simplemenu_element', 'body'),
  45. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  46. 'placement' => variable_get('simplemenu_element_method', 'prepend'),
  47. );
  48. drupal_add_js(array('simplemenu' => $settings), 'setting');
  49. drupal_add_js('var simplemenu = '. drupal_to_js(simplemenu_get_menu()) .';', 'inline');
  50. drupal_add_js($path .'/simplemenu.js');
  51. }
  52. }
  53. /**
  54. * Implementation of hook_perm().
  55. */
  56. function simplemenu_perm() {
  57. return array('view simplemenu', 'administer simplemenu');
  58. }
  59. /**
  60. * SimpleMenu settings page.
  61. */
  62. function simplemenu_admin_settings() {
  63. if (module_exists('menu')) {
  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. }
  72. if (module_exists('devel')) {
  73. $form['default_menu']['simplemenu_devel'] = array(
  74. '#type' => 'checkbox',
  75. '#title' => t('Add devel module links'),
  76. '#default_value' => variable_get('simplemenu_devel', 0),
  77. '#description' => t('Add devel module links for those users that can access the devel module.')
  78. );
  79. }
  80. $form['default_menu']['simplemenu_theme'] = array(
  81. '#type' => 'select',
  82. '#title' => t('Theme'),
  83. '#options' => array('original' => 'original', 'blackblue' => 'black & blue', 'custom' => 'custom'),
  84. '#default_value' => variable_get('simplemenu_theme', 'original'),
  85. '#description' => t('Select which theme to use. If you specify custom, you need to define CSS in your theme.')
  86. );
  87. $form['default_menu']['advanced'] = array(
  88. '#type' => 'fieldset',
  89. '#title' => t('Advanced settings'),
  90. '#collapsible' => TRUE,
  91. '#collapsed' => TRUE
  92. );
  93. $form['default_menu']['advanced']['simplemenu_element'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('CSS selector to attach menu to'),
  96. '#default_value' => variable_get('simplemenu_element', 'body'),
  97. '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
  98. '#required' => TRUE
  99. );
  100. $form['default_menu']['advanced']['simplemenu_element_method'] = array(
  101. '#type' => 'radios',
  102. '#title' => t('Attach method'),
  103. '#options' => drupal_map_assoc(array('prepend', 'append')),
  104. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  105. '#description' => t('Choose how the menu should be attached to the above selector.'),
  106. '#required' => TRUE
  107. );
  108. $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
  109. '#type' => 'checkboxes',
  110. '#title' => t('Theme exclusions'),
  111. '#options' => drupal_map_assoc(array_keys(list_themes())),
  112. '#default_value' => variable_get('simplemenu_exclusions', array()),
  113. '#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.'),
  114. );
  115. $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Hide delay'),
  118. '#size' => 4,
  119. '#default_value' => variable_get('simplemenu_hide_delay', 800),
  120. '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
  121. );
  122. $form['default_menu']['advanced']['simplemenu_effect'] = array(
  123. '#type' => 'radios',
  124. '#title' => t('Show effect'),
  125. '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
  126. '#default_value' => variable_get('simplemenu_effect', 'opacity'),
  127. '#description' => t('The effect used when displaying a menu.')
  128. );
  129. $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
  130. '#type' => 'radios',
  131. '#title' => t('Show speed'),
  132. '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
  133. '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
  134. '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
  135. );
  136. return system_settings_form($form);
  137. }
  138. /**
  139. * Render an HTML list of links for a given menu.
  140. */
  141. function simplemenu_get_menu() {
  142. $output = '';
  143. // if a user turned off menu module but SimpleMenu was previously set
  144. // reset variable so a menu appears
  145. if (module_exists('menu')) {
  146. $menu = simplemenu_menu_tree(variable_get('simplemenu_menu', 1));
  147. }
  148. else {
  149. $menu = simplemenu_menu_tree(1);
  150. }
  151. if (!$menu) {
  152. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  153. }
  154. $output .= simplemenu_get_devel();
  155. $output .= $menu;
  156. return $output;
  157. }
  158. /**
  159. * Custom implementation of menu_tree().
  160. * We want to retrieve the entire menu structure for a given menu,
  161. * regardless of whether or not the menu item is expanded or not.
  162. */
  163. function simplemenu_menu_tree($pid = 1) {
  164. $menu = menu_get_menu();
  165. $output = '';
  166. if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
  167. foreach ($menu['visible'][$pid]['children'] as $mid) {
  168. $type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : NULL;
  169. $children = isset($menu['visible'][$mid]['children']) ? $menu['visible'][$mid]['children'] : NULL;
  170. $output .= theme('menu_item', $mid, simplemenu_theme_menu_tree($mid), count($children) == 0);
  171. }
  172. }
  173. return $output;
  174. }
  175. /**
  176. * Custom implementation of theme_menu_tree() to call our custom menu above.
  177. */
  178. function simplemenu_theme_menu_tree($pid = 1) {
  179. if ($tree = simplemenu_menu_tree($pid)) {
  180. return '<ul>'. $tree .'</ul>';
  181. }
  182. }
  183. /**
  184. * Return a list of devel module links if the module is enabled
  185. * and the user has access to this module.
  186. */
  187. function simplemenu_get_devel() {
  188. $output = '';
  189. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  190. if (user_access('access devel information')) {
  191. $links[] = l('Devel settings', 'admin/settings/devel', array('title' => t('Adjust module settings for devel module')));
  192. $links[] = l('Empty cache', 'devel/cache/clear', array('title' => t('Clear the database cache tables which store page, menu, node, and variable caches.')), drupal_get_destination());
  193. $links[] = l('Phpinfo()', 'admin/logs/status/php');
  194. $links[] = l('Function reference', 'devel/reference', array('title' => t('View a list of currently defined user functions with documentation links')));
  195. $links[] = l('Reinstall modules', 'devel/reinstall', array('title' => t('Re-run hook_install() for a given module')));
  196. $links[] = l('Reset menus', 'devel/menu/reset', array('title' => t('Resets all menu items to their default settings')));
  197. $links[] = l('Variable editor', 'devel/variable', array('title' => t('Edit and delete site variables')));
  198. $links[] = l('Session viewer', 'devel/session', array('title' => t('List the contents of $_SESSION')));
  199. if (function_exists('devel_node_access_perm') && user_access(DNA_ACCESS_VIEW)) {
  200. // True only if devel_node_access enabled.
  201. $links[] = l('Node access summary', 'devel/node_access/summary');
  202. }
  203. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a><ul>';
  204. $output .= '<li class="leaf">'. implode($links, '</li><li class="leaf">') .'</li>';
  205. $output .= '</ul></li>';
  206. }
  207. }
  208. return $output;
  209. }