simplemenu.module 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Creates a simplemenu.
  6. */
  7. /**
  8. * Implementation of hook_menu().
  9. */
  10. function simplemenu_menu() {
  11. $items = array();
  12. $items['admin/settings/simplemenu'] = array(
  13. 'title' => 'SimpleMenu',
  14. 'description' => 'Select the menu to display.',
  15. 'page callback' => 'drupal_get_form',
  16. 'page arguments' => array('simplemenu_admin_settings'),
  17. 'access arguments' => array('administer simplemenu')
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implementation of hook_init()
  23. */
  24. function simplemenu_init() {
  25. global $theme;
  26. $exclusions = variable_get('simplemenu_exclusions', array());
  27. if (user_access('view simplemenu') && (!isset($exclusions[$theme])) ) {
  28. global $theme, $custom_theme;
  29. $path = drupal_get_path('module', 'simplemenu');
  30. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  31. drupal_add_css($path .'/simplemenu.css');
  32. if ($simplemenu_theme != 'custom') {
  33. drupal_add_css($path .'/themes/'. $simplemenu_theme .'/'. $simplemenu_theme .'.css');
  34. }
  35. $settings = array(
  36. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  37. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  38. 'element' => variable_get('simplemenu_element', 'body'),
  39. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  40. 'placement' => variable_get('simplemenu_element_method', 'prepend'),
  41. );
  42. drupal_add_js(array('simplemenu' => $settings), 'setting');
  43. drupal_add_js('var simplemenu = '. drupal_to_js(simplemenu_get_menu()) .';', 'inline');
  44. drupal_add_js($path .'/simplemenu.js');
  45. drupal_add_js($path .'/superfish.js');
  46. }
  47. }
  48. /**
  49. * Implementation of hook_perm().
  50. */
  51. function simplemenu_perm() {
  52. return array('view simplemenu', 'administer simplemenu');
  53. }
  54. /**
  55. * SimpleMenu settings page.
  56. */
  57. function simplemenu_admin_settings() {
  58. if (module_exists('menu')) {
  59. $form['default_menu']['simplemenu_menu'] = array(
  60. '#type' => 'select',
  61. '#title' => t('Menu'),
  62. '#options' => menu_parent_options(menu_get_menus(), array( 'mlid' => 0 )), // return complete tree;
  63. '#default_value' => variable_get('simplemenu_menu', 'navigation:0'),
  64. '#description' => t('Select the menu to display.')
  65. );
  66. }
  67. if (module_exists('devel')) {
  68. $form['default_menu']['simplemenu_devel'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Add devel module links'),
  71. '#default_value' => variable_get('simplemenu_devel', 0),
  72. '#description' => t('Add devel module links for those users that can access the devel module.')
  73. );
  74. }
  75. $form['default_menu']['simplemenu_theme'] = array(
  76. '#type' => 'select',
  77. '#title' => t('Theme'),
  78. '#options' => array(
  79. 'original' => t('original'),
  80. 'blackblue' => t('black & blue'),
  81. 'custom' => t('custom'),
  82. ),
  83. '#default_value' => variable_get('simplemenu_theme', 'original'),
  84. '#description' => t('Select which theme to use. If you specify custom, you need to define CSS in your theme.')
  85. );
  86. $form['default_menu']['advanced'] = array(
  87. '#type' => 'fieldset',
  88. '#title' => t('Advanced settings'),
  89. '#collapsible' => TRUE,
  90. '#collapsed' => TRUE
  91. );
  92. $form['default_menu']['advanced']['simplemenu_element'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('CSS selector to attach menu to'),
  95. '#default_value' => variable_get('simplemenu_element', 'body'),
  96. '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
  97. '#required' => TRUE
  98. );
  99. $form['default_menu']['advanced']['simplemenu_element_method'] = array(
  100. '#type' => 'radios',
  101. '#title' => t('Attach method'),
  102. '#options' => drupal_map_assoc(array('prepend', 'append')),
  103. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  104. '#description' => t('Choose how the menu should be attached to the above selector.'),
  105. '#required' => TRUE
  106. );
  107. $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
  108. '#type' => 'checkboxes',
  109. '#title' => t('Theme exclusions'),
  110. '#options' => drupal_map_assoc(array_keys(list_themes())),
  111. '#default_value' => variable_get('simplemenu_exclusions', array()),
  112. '#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.'),
  113. );
  114. $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
  115. '#type' => 'textfield',
  116. '#title' => t('Hide delay'),
  117. '#size' => 4,
  118. '#default_value' => variable_get('simplemenu_hide_delay', 800),
  119. '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
  120. );
  121. $form['default_menu']['advanced']['simplemenu_effect'] = array(
  122. '#type' => 'radios',
  123. '#title' => t('Show effect'),
  124. '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
  125. '#default_value' => variable_get('simplemenu_effect', 'opacity'),
  126. '#description' => t('The effect used when displaying a menu.')
  127. );
  128. $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
  129. '#type' => 'radios',
  130. '#title' => t('Show speed'),
  131. '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
  132. '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
  133. '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
  134. );
  135. return system_settings_form($form);
  136. }
  137. /**
  138. * Render an HTML list of links for a given menu.
  139. */
  140. function simplemenu_get_menu() {
  141. $output = '';
  142. // if a user turned off menu module but SimpleMenu was previously set
  143. // reset variable so a menu appears
  144. $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  145. $menu = simplemenu_menu_tree($menu_name);
  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. // This is ugly, I know, but it is the only way I can see to get the additional
  150. // links inside the <ul> tags
  151. if($devel = simplemenu_get_devel()) {
  152. $pos = strpos($menu, '>') + 1;
  153. $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
  154. }
  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($menu_name = 'navigation:0') {
  164. static $menu_output = array();
  165. if (!isset($menu_output[$menu_name])) {
  166. $tree = simplemenu_tree_all_data($menu_name);
  167. $menu_output[$menu_name] = menu_tree_output($tree);
  168. }
  169. return $menu_output[$menu_name];
  170. }
  171. /**
  172. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  173. * (which can be *any* menu item, not just the root of a custom menu).
  174. *
  175. * @param $root_menu
  176. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  177. *
  178. * @todo we don't actually need $menu_name, $mlid would be sufficient
  179. */
  180. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  181. static $tree = array();
  182. list($menu_name, $mlid) = explode(':', $root_menu);
  183. // Generate the cache ID.
  184. // "links:navigation:all:2" means "all from root to 2" (what the ...), so for "all from 2 down" we do "links:navigation:all:2:all"
  185. $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
  186. if (!isset($tree[$cid])) {
  187. // If the static variable doesn't have the data, check {cache_menu}.
  188. $cache = cache_get($cid, 'cache_menu');
  189. if ($cache && isset($cache->data)) {
  190. $data = $cache->data;
  191. }
  192. else {
  193. // Build and run the query, and build the tree.
  194. if ($mlid > 0) {
  195. $item = menu_link_load($mlid);
  196. // The tree is a subtree of $menu_name, so we need to restrict the query to
  197. // this subtree.
  198. $px = "p$item[depth]";
  199. $where = " AND ml.$px = %d AND ml.mlid != %d";
  200. $args = array($item[$px], $mlid);
  201. }
  202. else {
  203. // Get all links in this menu.
  204. $where = '';
  205. $args = array();
  206. }
  207. array_unshift($args, $menu_name);
  208. // Select the links from the table, and recursively build the tree. We
  209. // LEFT JOIN since there is no match in {menu_router} for an external
  210. // link.
  211. $data['tree'] = menu_tree_data(db_query("
  212. SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  213. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  214. WHERE ml.menu_name = '%s'". $where ."
  215. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  216. $data['node_links'] = array();
  217. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  218. // Cache the data.
  219. cache_set($cid, $data, 'cache_menu');
  220. }
  221. // Check access for the current user to each item in the tree.
  222. menu_tree_check_access($data['tree'], $data['node_links']);
  223. $tree[$cid] = $data['tree'];
  224. }
  225. return $tree[$cid];
  226. }
  227. /**
  228. * Return a list of devel module links if the module is enabled
  229. * and the user has access to this module.
  230. */
  231. function simplemenu_get_devel() {
  232. $output = '';
  233. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  234. if (user_access('access devel information')) {
  235. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
  236. $output .= simplemenu_menu_tree('devel');
  237. $output .= '</li>';
  238. }
  239. }
  240. return $output;
  241. }