simplemenu.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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])) && _simplemenu_page_visibility()) {
  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. 'detectPopup' => variable_get('simplemenu_detect_popop', 1),
  42. );
  43. drupal_add_js(array('simplemenu' => $settings), 'setting');
  44. drupal_add_js('var simplemenu = '. drupal_to_js(simplemenu_get_menu()) .';', 'inline');
  45. drupal_add_js($path .'/simplemenu.js');
  46. drupal_add_js($path .'/superfish.js');
  47. }
  48. }
  49. /**
  50. * Implementation of hook_perm().
  51. */
  52. function simplemenu_perm() {
  53. return array('view simplemenu', 'administer simplemenu');
  54. }
  55. /**
  56. * SimpleMenu settings page.
  57. */
  58. function simplemenu_admin_settings() {
  59. if (module_exists('menu')) {
  60. $form['default_menu']['simplemenu_menu'] = array(
  61. '#type' => 'select',
  62. '#title' => t('Menu'),
  63. '#options' => menu_parent_options(menu_get_menus(), array( 'mlid' => 0 )), // return complete tree;
  64. '#default_value' => variable_get('simplemenu_menu', 'navigation:0'),
  65. '#description' => t('Select the menu to display.')
  66. );
  67. }
  68. if (module_exists('devel')) {
  69. $form['default_menu']['simplemenu_devel'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Add devel module links'),
  72. '#default_value' => variable_get('simplemenu_devel', 0),
  73. '#description' => t('Add devel module links for those users that can access the devel module.')
  74. );
  75. }
  76. $form['default_menu']['simplemenu_theme'] = array(
  77. '#type' => 'select',
  78. '#title' => t('Theme'),
  79. '#options' => array(
  80. 'original' => t('original'),
  81. 'blackblue' => t('black & blue'),
  82. 'custom' => t('custom'),
  83. ),
  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' => array(
  104. 'prepend' => t('Prepend'),
  105. 'append' => t('Append'),
  106. ),
  107. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  108. '#description' => t('Choose how the menu should be attached to the above selector.'),
  109. '#required' => TRUE
  110. );
  111. $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
  112. '#type' => 'checkboxes',
  113. '#title' => t('Theme exclusions'),
  114. '#options' => drupal_map_assoc(array_keys(list_themes())),
  115. '#default_value' => variable_get('simplemenu_exclusions', array()),
  116. '#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.'),
  117. );
  118. $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Hide delay'),
  121. '#size' => 4,
  122. '#default_value' => variable_get('simplemenu_hide_delay', 800),
  123. '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
  124. );
  125. $form['default_menu']['advanced']['simplemenu_effect'] = array(
  126. '#type' => 'radios',
  127. '#title' => t('Show effect'),
  128. '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
  129. '#default_value' => variable_get('simplemenu_effect', 'opacity'),
  130. '#description' => t('The effect used when displaying a menu.')
  131. );
  132. $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
  133. '#type' => 'radios',
  134. '#title' => t('Show speed'),
  135. '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
  136. '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
  137. '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
  138. );
  139. $form['default_menu']['advanced']['simplemenu_detect_popop'] = array(
  140. '#type' => 'checkbox',
  141. '#title' => t('Detect pop-up windows'),
  142. '#default_value' => variable_get('simplemenu_detect_popop', 1),
  143. '#description' => t("Choose whether SimpleMenu should attempt to detect if it is inside of a pop-up window. If enabled, SimpleMenu will not display if it is inside of a pop-up window."),
  144. );
  145. $form['default_menu']['advanced']['simplemenu_visibility_operator'] = array(
  146. '#type' => 'radios',
  147. '#title' => t('Show block on specific pages'),
  148. '#default_value' => variable_get('simplemenu_visibility_operator', 0),
  149. '#options' => array(
  150. 0 => t('Show on every page except the listed pages.'),
  151. 1 => t('Show on only the listed pages.')
  152. ),
  153. );
  154. $form['default_menu']['advanced']['simplemenu_visibility_pages'] = array(
  155. '#type' => 'textarea',
  156. '#title' => t('Pages'),
  157. '#default_value' => variable_get('simplemenu_visibility_pages', ''),
  158. '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
  159. );
  160. return system_settings_form($form);
  161. }
  162. /**
  163. * Render an HTML list of links for a given menu.
  164. */
  165. function simplemenu_get_menu() {
  166. $output = '';
  167. // if a user turned off menu module but SimpleMenu was previously set
  168. // reset variable so a menu appears
  169. $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  170. $menu = simplemenu_menu_tree($menu_name);
  171. if (!$menu) {
  172. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  173. }
  174. // This is ugly, I know, but it is the only way I can see to get the additional
  175. // links inside the <ul> tags
  176. if($devel = simplemenu_get_devel()) {
  177. $pos = strpos($menu, '>') + 1;
  178. $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
  179. }
  180. $output .= $menu;
  181. return $output;
  182. }
  183. /**
  184. * Custom implementation of menu_tree().
  185. * We want to retrieve the entire menu structure for a given menu,
  186. * regardless of whether or not the menu item is expanded or not.
  187. */
  188. function simplemenu_menu_tree($menu_name = 'navigation:0') {
  189. static $menu_output = array();
  190. if (!isset($menu_output[$menu_name])) {
  191. $tree = simplemenu_tree_all_data($menu_name);
  192. $menu_output[$menu_name] = menu_tree_output($tree);
  193. }
  194. return $menu_output[$menu_name];
  195. }
  196. /**
  197. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  198. * (which can be *any* menu item, not just the root of a custom menu).
  199. *
  200. * @param $root_menu
  201. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  202. *
  203. * @todo we don't actually need $menu_name, $mlid would be sufficient
  204. */
  205. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  206. static $tree = array();
  207. list($menu_name, $mlid) = explode(':', $root_menu);
  208. // Generate the cache ID.
  209. // "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"
  210. $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
  211. if (!isset($tree[$cid])) {
  212. // If the static variable doesn't have the data, check {cache_menu}.
  213. $cache = cache_get($cid, 'cache_menu');
  214. if ($cache && isset($cache->data)) {
  215. $data = $cache->data;
  216. }
  217. else {
  218. // Build and run the query, and build the tree.
  219. if ($mlid > 0) {
  220. $item = menu_link_load($mlid);
  221. // The tree is a subtree of $menu_name, so we need to restrict the query to
  222. // this subtree.
  223. $px = "p$item[depth]";
  224. $where = " AND ml.$px = %d AND ml.mlid != %d";
  225. $args = array($item[$px], $mlid);
  226. }
  227. else {
  228. // Get all links in this menu.
  229. $where = '';
  230. $args = array();
  231. }
  232. array_unshift($args, $menu_name);
  233. // Select the links from the table, and recursively build the tree. We
  234. // LEFT JOIN since there is no match in {menu_router} for an external
  235. // link.
  236. $data['tree'] = menu_tree_data(db_query("
  237. 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.*
  238. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  239. WHERE ml.menu_name = '%s'". $where ."
  240. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  241. $data['node_links'] = array();
  242. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  243. // Cache the data.
  244. cache_set($cid, $data, 'cache_menu');
  245. }
  246. // Check access for the current user to each item in the tree.
  247. menu_tree_check_access($data['tree'], $data['node_links']);
  248. $tree[$cid] = $data['tree'];
  249. }
  250. return $tree[$cid];
  251. }
  252. /**
  253. * Return a list of devel module links if the module is enabled
  254. * and the user has access to this module.
  255. */
  256. function simplemenu_get_devel() {
  257. $output = '';
  258. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  259. if (user_access('access devel information')) {
  260. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
  261. $output .= simplemenu_menu_tree('devel');
  262. $output .= '</li>';
  263. }
  264. }
  265. return $output;
  266. }
  267. /**
  268. * Determine if simplemenu should be displayed based on visibility settings.
  269. *
  270. * @return boolean
  271. */
  272. function _simplemenu_page_visibility() {
  273. $operator = variable_get('simplemenu_visibility_operator', 0);
  274. $pages = variable_get('simplemenu_visibility_pages', '');
  275. if ($pages) {
  276. $path = drupal_get_path_alias($_GET['q']);
  277. // Compare with the internal and path alias (if any).
  278. $page_match = drupal_match_path($path, $pages);
  279. if ($path != $_GET['q']) {
  280. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  281. }
  282. // When $operator has a value of 0, the menu is displayed on
  283. // all pages except those listed in $pages. When set to 1, it
  284. // is displayed only on those pages listed in $pages.
  285. $page_match = !($operator xor $page_match);
  286. }
  287. else {
  288. $page_match = TRUE;
  289. }
  290. return $page_match;
  291. }