simplemenu.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * Is simplemenu enabled for this page request?
  23. */
  24. function simplemenu_enabled() {
  25. static $enabled;
  26. if(!isset($enabled)) {
  27. global $theme;
  28. $exclusions = variable_get('simplemenu_exclusions', array());
  29. $enabled = (user_access('view simplemenu')
  30. && (!isset($exclusions[$theme]) || !$exclusions[$theme])
  31. && _simplemenu_page_visibility());
  32. }
  33. return $enabled;
  34. }
  35. /**
  36. * Implementation of hook_init().
  37. */
  38. function simplemenu_init() {
  39. // do a simple access check here, since theme isn't available to check yet
  40. if (user_access('view simplemenu')) {
  41. $path = drupal_get_path('module', 'simplemenu');
  42. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  43. drupal_add_css($path .'/simplemenu.css');
  44. if ($simplemenu_theme != 'custom') {
  45. drupal_add_css($path .'/themes/'. $simplemenu_theme .'/'. $simplemenu_theme .'.css');
  46. }
  47. $settings = array(
  48. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  49. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  50. 'element' => variable_get('simplemenu_element', 'body'),
  51. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  52. 'placement' => variable_get('simplemenu_element_method', 'prepend'),
  53. 'detectPopup' => variable_get('simplemenu_detect_popop', 1),
  54. );
  55. drupal_add_js(array('simplemenu' => $settings), 'setting');
  56. drupal_add_js($path .'/simplemenu.js');
  57. drupal_add_js($path .'/superfish.js');
  58. }
  59. }
  60. /**
  61. * Implementation of hook_footer().
  62. *
  63. * This has been broken off of simplemenu_init() because simplemenu_get_menu()
  64. * calls simplemenu_menu_tree() which calls menu_tree_output() which has several
  65. * calls to theme(). This initializes the theme system too early causing hard
  66. * to track bugs.
  67. *
  68. * @see http://drupal.org/node/219910
  69. */
  70. function simplemenu_footer() {
  71. if(simplemenu_enabled()) {
  72. $simplemenu = drupal_to_js(simplemenu_get_menu());
  73. $path = base_path() . drupal_get_path('module', 'simplemenu');
  74. $output = "<script type=\"text/javascript\">var simplemenu = $simplemenu;</script>\n";
  75. return $output;
  76. }
  77. }
  78. /**
  79. * Implementation of hook_perm().
  80. */
  81. function simplemenu_perm() {
  82. return array('view simplemenu', 'administer simplemenu');
  83. }
  84. /**
  85. * SimpleMenu settings page.
  86. */
  87. function simplemenu_admin_settings() {
  88. if (module_exists('menu')) {
  89. $form['default_menu']['simplemenu_menu'] = array(
  90. '#type' => 'select',
  91. '#title' => t('Menu'),
  92. '#options' => menu_parent_options(menu_get_menus(), array( 'mlid' => 0 )), // return complete tree;
  93. '#default_value' => variable_get('simplemenu_menu', 'navigation:0'),
  94. '#description' => t('Select the menu to display.')
  95. );
  96. }
  97. if (module_exists('devel')) {
  98. $form['default_menu']['simplemenu_devel'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Add devel module links'),
  101. '#default_value' => variable_get('simplemenu_devel', 0),
  102. '#description' => t('Add devel module links for those users that can access the devel module.')
  103. );
  104. }
  105. $form['default_menu']['simplemenu_theme'] = array(
  106. '#type' => 'select',
  107. '#title' => t('Theme'),
  108. '#options' => array(
  109. 'original' => t('original'),
  110. 'blackblue' => t('black & blue'),
  111. 'custom' => t('custom'),
  112. ),
  113. '#default_value' => variable_get('simplemenu_theme', 'original'),
  114. '#description' => t('Select which theme to use. If you specify custom, you need to define CSS in your theme.')
  115. );
  116. $form['default_menu']['advanced'] = array(
  117. '#type' => 'fieldset',
  118. '#title' => t('Advanced settings'),
  119. '#collapsible' => TRUE,
  120. '#collapsed' => TRUE
  121. );
  122. $form['default_menu']['advanced']['simplemenu_element'] = array(
  123. '#type' => 'textfield',
  124. '#title' => t('CSS selector to attach menu to'),
  125. '#default_value' => variable_get('simplemenu_element', 'body'),
  126. '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
  127. '#required' => TRUE
  128. );
  129. $form['default_menu']['advanced']['simplemenu_element_method'] = array(
  130. '#type' => 'radios',
  131. '#title' => t('Attach method'),
  132. '#options' => array(
  133. 'prepend' => t('Prepend'),
  134. 'append' => t('Append'),
  135. ),
  136. '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
  137. '#description' => t('Choose how the menu should be attached to the above selector.'),
  138. '#required' => TRUE
  139. );
  140. $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
  141. '#type' => 'checkboxes',
  142. '#title' => t('Theme exclusions'),
  143. '#options' => drupal_map_assoc(array_keys(list_themes())),
  144. '#default_value' => variable_get('simplemenu_exclusions', array()),
  145. '#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.'),
  146. );
  147. $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
  148. '#type' => 'textfield',
  149. '#title' => t('Hide delay'),
  150. '#size' => 4,
  151. '#default_value' => variable_get('simplemenu_hide_delay', 800),
  152. '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
  153. );
  154. $form['default_menu']['advanced']['simplemenu_effect'] = array(
  155. '#type' => 'radios',
  156. '#title' => t('Show effect'),
  157. '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
  158. '#default_value' => variable_get('simplemenu_effect', 'opacity'),
  159. '#description' => t('The effect used when displaying a menu.')
  160. );
  161. $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
  162. '#type' => 'radios',
  163. '#title' => t('Show speed'),
  164. '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
  165. '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
  166. '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
  167. );
  168. $form['default_menu']['advanced']['simplemenu_detect_popop'] = array(
  169. '#type' => 'checkbox',
  170. '#title' => t('Detect pop-up windows'),
  171. '#default_value' => variable_get('simplemenu_detect_popop', 1),
  172. '#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."),
  173. );
  174. $form['default_menu']['advanced']['simplemenu_visibility_operator'] = array(
  175. '#type' => 'radios',
  176. '#title' => t('Show block on specific pages'),
  177. '#default_value' => variable_get('simplemenu_visibility_operator', 0),
  178. '#options' => array(
  179. 0 => t('Show on every page except the listed pages.'),
  180. 1 => t('Show on only the listed pages.')
  181. ),
  182. );
  183. $form['default_menu']['advanced']['simplemenu_visibility_pages'] = array(
  184. '#type' => 'textarea',
  185. '#title' => t('Pages'),
  186. '#default_value' => variable_get('simplemenu_visibility_pages', ''),
  187. '#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>')),
  188. );
  189. return system_settings_form($form);
  190. }
  191. /**
  192. * Render an HTML list of links for a given menu.
  193. */
  194. function simplemenu_get_menu() {
  195. $output = '';
  196. // if a user turned off menu module but SimpleMenu was previously set
  197. // reset variable so a menu appears
  198. $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  199. $menu = simplemenu_menu_tree($menu_name);
  200. if (!$menu) {
  201. $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
  202. }
  203. // This is ugly, I know, but it is the only way I can see to get the additional
  204. // links inside the <ul> tags
  205. if($devel = simplemenu_get_devel()) {
  206. $pos = strpos($menu, '>') + 1;
  207. $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
  208. }
  209. $output .= $menu;
  210. return $output;
  211. }
  212. /**
  213. * Custom implementation of menu_tree().
  214. * We want to retrieve the entire menu structure for a given menu,
  215. * regardless of whether or not the menu item is expanded or not.
  216. */
  217. function simplemenu_menu_tree($menu_name = 'navigation:0') {
  218. static $menu_output = array();
  219. if (!isset($menu_output[$menu_name])) {
  220. $tree = simplemenu_tree_all_data($menu_name);
  221. $menu_output[$menu_name] = menu_tree_output($tree);
  222. }
  223. return $menu_output[$menu_name];
  224. }
  225. /**
  226. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  227. * (which can be *any* menu item, not just the root of a custom menu).
  228. *
  229. * @param $root_menu
  230. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  231. *
  232. * @todo we don't actually need $menu_name, $mlid would be sufficient
  233. */
  234. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  235. static $tree = array();
  236. list($menu_name, $mlid) = explode(':', $root_menu);
  237. // Generate the cache ID.
  238. // "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"
  239. $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
  240. if (!isset($tree[$cid])) {
  241. // If the static variable doesn't have the data, check {cache_menu}.
  242. $cache = cache_get($cid, 'cache_menu');
  243. if ($cache && isset($cache->data)) {
  244. $data = $cache->data;
  245. }
  246. else {
  247. // Build and run the query, and build the tree.
  248. if ($mlid > 0) {
  249. $item = menu_link_load($mlid);
  250. // The tree is a subtree of $menu_name, so we need to restrict the query to
  251. // this subtree.
  252. $px = "p$item[depth]";
  253. $where = " AND ml.$px = %d AND ml.mlid != %d";
  254. $args = array($item[$px], $mlid);
  255. }
  256. else {
  257. // Get all links in this menu.
  258. $where = '';
  259. $args = array();
  260. }
  261. array_unshift($args, $menu_name);
  262. // Select the links from the table, and recursively build the tree. We
  263. // LEFT JOIN since there is no match in {menu_router} for an external
  264. // link.
  265. $data['tree'] = menu_tree_data(db_query("
  266. 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.*
  267. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  268. WHERE ml.menu_name = '%s'". $where ."
  269. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  270. $data['node_links'] = array();
  271. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  272. // Cache the data.
  273. cache_set($cid, $data, 'cache_menu');
  274. }
  275. // Check access for the current user to each item in the tree.
  276. menu_tree_check_access($data['tree'], $data['node_links']);
  277. $tree[$cid] = $data['tree'];
  278. }
  279. return $tree[$cid];
  280. }
  281. /**
  282. * Return a list of devel module links if the module is enabled
  283. * and the user has access to this module.
  284. */
  285. function simplemenu_get_devel() {
  286. $output = '';
  287. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  288. if (user_access('access devel information')) {
  289. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
  290. $output .= simplemenu_menu_tree('devel');
  291. $output .= '</li>';
  292. }
  293. }
  294. return $output;
  295. }
  296. /**
  297. * Determine if simplemenu should be displayed based on visibility settings.
  298. *
  299. * @return boolean
  300. */
  301. function _simplemenu_page_visibility() {
  302. $operator = variable_get('simplemenu_visibility_operator', 0);
  303. $pages = variable_get('simplemenu_visibility_pages', '');
  304. if ($pages) {
  305. $path = drupal_get_path_alias($_GET['q']);
  306. // Compare with the internal and path alias (if any).
  307. $page_match = drupal_match_path($path, $pages);
  308. if ($path != $_GET['q']) {
  309. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  310. }
  311. // When $operator has a value of 0, the menu is displayed on
  312. // all pages except those listed in $pages. When set to 1, it
  313. // is displayed only on those pages listed in $pages.
  314. $page_match = !($operator xor $page_match);
  315. }
  316. else {
  317. $page_match = TRUE;
  318. }
  319. return $page_match;
  320. }