simplemenu.module 13 KB

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