simplemenu.module 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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' => array(
  103. 'prepend' => t('Prepend'),
  104. 'append' => t('Append'),
  105. ),
  106. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  107. '#description' => t('Choose how the menu should be attached to the above selector.'),
  108. '#required' => TRUE
  109. );
  110. $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
  111. '#type' => 'checkboxes',
  112. '#title' => t('Theme exclusions'),
  113. '#options' => drupal_map_assoc(array_keys(list_themes())),
  114. '#default_value' => variable_get('simplemenu_exclusions', array()),
  115. '#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.'),
  116. );
  117. $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Hide delay'),
  120. '#size' => 4,
  121. '#default_value' => variable_get('simplemenu_hide_delay', 800),
  122. '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
  123. );
  124. $form['default_menu']['advanced']['simplemenu_effect'] = array(
  125. '#type' => 'radios',
  126. '#title' => t('Show effect'),
  127. '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
  128. '#default_value' => variable_get('simplemenu_effect', 'opacity'),
  129. '#description' => t('The effect used when displaying a menu.')
  130. );
  131. $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
  132. '#type' => 'radios',
  133. '#title' => t('Show speed'),
  134. '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
  135. '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
  136. '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
  137. );
  138. return system_settings_form($form);
  139. }
  140. /**
  141. * Render an HTML list of links for a given menu.
  142. */
  143. function simplemenu_get_menu() {
  144. $output = '';
  145. // if a user turned off menu module but SimpleMenu was previously set
  146. // reset variable so a menu appears
  147. $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  148. $menu = simplemenu_menu_tree($menu_name);
  149. if (!$menu) {
  150. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  151. }
  152. // This is ugly, I know, but it is the only way I can see to get the additional
  153. // links inside the <ul> tags
  154. if($devel = simplemenu_get_devel()) {
  155. $pos = strpos($menu, '>') + 1;
  156. $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
  157. }
  158. $output .= $menu;
  159. return $output;
  160. }
  161. /**
  162. * Custom implementation of menu_tree().
  163. * We want to retrieve the entire menu structure for a given menu,
  164. * regardless of whether or not the menu item is expanded or not.
  165. */
  166. function simplemenu_menu_tree($menu_name = 'navigation:0') {
  167. static $menu_output = array();
  168. if (!isset($menu_output[$menu_name])) {
  169. $tree = simplemenu_tree_all_data($menu_name);
  170. $menu_output[$menu_name] = menu_tree_output($tree);
  171. }
  172. return $menu_output[$menu_name];
  173. }
  174. /**
  175. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  176. * (which can be *any* menu item, not just the root of a custom menu).
  177. *
  178. * @param $root_menu
  179. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  180. *
  181. * @todo we don't actually need $menu_name, $mlid would be sufficient
  182. */
  183. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  184. static $tree = array();
  185. list($menu_name, $mlid) = explode(':', $root_menu);
  186. // Generate the cache ID.
  187. // "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"
  188. $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
  189. if (!isset($tree[$cid])) {
  190. // If the static variable doesn't have the data, check {cache_menu}.
  191. $cache = cache_get($cid, 'cache_menu');
  192. if ($cache && isset($cache->data)) {
  193. $data = $cache->data;
  194. }
  195. else {
  196. // Build and run the query, and build the tree.
  197. if ($mlid > 0) {
  198. $item = menu_link_load($mlid);
  199. // The tree is a subtree of $menu_name, so we need to restrict the query to
  200. // this subtree.
  201. $px = "p$item[depth]";
  202. $where = " AND ml.$px = %d AND ml.mlid != %d";
  203. $args = array($item[$px], $mlid);
  204. }
  205. else {
  206. // Get all links in this menu.
  207. $where = '';
  208. $args = array();
  209. }
  210. array_unshift($args, $menu_name);
  211. // Select the links from the table, and recursively build the tree. We
  212. // LEFT JOIN since there is no match in {menu_router} for an external
  213. // link.
  214. $data['tree'] = menu_tree_data(db_query("
  215. 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.*
  216. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  217. WHERE ml.menu_name = '%s'". $where ."
  218. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  219. $data['node_links'] = array();
  220. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  221. // Cache the data.
  222. cache_set($cid, $data, 'cache_menu');
  223. }
  224. // Check access for the current user to each item in the tree.
  225. menu_tree_check_access($data['tree'], $data['node_links']);
  226. $tree[$cid] = $data['tree'];
  227. }
  228. return $tree[$cid];
  229. }
  230. /**
  231. * Return a list of devel module links if the module is enabled
  232. * and the user has access to this module.
  233. */
  234. function simplemenu_get_devel() {
  235. $output = '';
  236. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  237. if (user_access('access devel information')) {
  238. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
  239. $output .= simplemenu_menu_tree('devel');
  240. $output .= '</li>';
  241. }
  242. }
  243. return $output;
  244. }