simplemenu.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. 'file' => 'simplemenu.admin.inc',
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Is simplemenu enabled for this page request?
  24. */
  25. function simplemenu_enabled() {
  26. static $enabled;
  27. if (!isset($enabled)) {
  28. global $theme;
  29. $exclusions = variable_get('simplemenu_exclusions', array());
  30. $enabled = (user_access('view simplemenu')
  31. && (!isset($exclusions[$theme]) || !$exclusions[$theme])
  32. && _simplemenu_page_visibility()
  33. && _simplemenu_superuser_active());
  34. }
  35. return $enabled;
  36. }
  37. /**
  38. * Implementation of hook_init().
  39. */
  40. function simplemenu_init() {
  41. // do a simple access check here, since theme isn't available to check yet
  42. if (user_access('view simplemenu') && simplemenu_enabled()) {
  43. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  44. $css_path = file_create_path('css'); // same path as concatenated Core CSS
  45. if (file_check_directory($css_path, FILE_CREATE_DIRECTORY)) {
  46. // The old way had a "static" CSS which meant that we could not easily
  47. // offer options to the users.
  48. //drupal_add_css($simplemenu_path .'/simplemenu.css');
  49. $fix = variable_get('simplemenu_fix', 'scroll');
  50. // XXX add a variable simplemenu_update which is set to TRUE whenever
  51. // the settings get modified and false here
  52. $output_filename = variable_get('simplemenu_css_filename', '');
  53. if (!$output_filename) {
  54. $tags = array(
  55. '@MENUBAR_ZINDEX@' => simplemnu_get_zindex('simplemenu_menubar_zindex', 9999),
  56. '@DROPDOWN_ZINDEX@' => simplemnu_get_zindex('simplemenu_dropdown_zindex', 9999),
  57. );
  58. switch ($fix) {
  59. case 'top':
  60. $tags['@FIX@'] = "position: fixed;\n top: 0;";
  61. break;
  62. case 'bottom':
  63. $tags['@FIX@'] = "position: fixed;\n bottom: 0;";
  64. break;
  65. default: // scroll
  66. $tags['@FIX@'] = 'position: relative;';
  67. break;
  68. }
  69. $css = file_get_contents($simplemenu_path . '/simplemenu.css.tpl');
  70. $css = strtr($css, $tags);
  71. $css_md5 = md5($css);
  72. $output_filename = $css_path . '/simplemenu-' . $css_md5 . '.css';
  73. if (!file_exists($output_filename)) {
  74. // new content, create a new file
  75. file_put_contents($output_filename, $css);
  76. }
  77. else {
  78. // this call is rather ugly, but we must make sure that the
  79. // system cache will take the current Simplemenu CSS in account
  80. _drupal_flush_css_js();
  81. }
  82. //variable_set('simplemenu_css_filename', $output_filename);
  83. }
  84. drupal_add_css($output_filename);
  85. }
  86. else {
  87. drupal_set_message(t('Simplemenu could not create the folder @path in order to save the dynamic CSS data.',
  88. array('@path' => $css_path)), 'error');
  89. // use a default that cannot react to the dynamic changes...
  90. drupal_add_css($simplemenu_path .'/simplemenu.css');
  91. }
  92. // we want to put the simplemenu theme CSS first
  93. // so we can change some CSS entries dynamically
  94. // but at this time the simplemenu.css is used to
  95. // reset many of the CSS entries... Hmmm...
  96. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  97. $theme_file = $simplemenu_path .'/themes/'. $simplemenu_theme .'/'. $simplemenu_theme .'.css';
  98. if (is_file($theme_file)) {
  99. drupal_add_css($theme_file);
  100. }
  101. switch ($fix) {
  102. case 'top':
  103. $element = 'body';
  104. $placement = 'prepend';
  105. break;
  106. case 'bottom':
  107. $element = 'body';
  108. $placement = 'append';
  109. break;
  110. default: // 'scroll'
  111. // let user defined other elements when not fixed
  112. $element = variable_get('simplemenu_element', 'body');
  113. $placement = variable_get('simplemenu_element_method', 'prepend');
  114. break;
  115. }
  116. $settings = array(
  117. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  118. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  119. 'element' => $element,
  120. 'placement' => $placement,
  121. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  122. 'detectPopup' => variable_get('simplemenu_detect_popup', 1),
  123. );
  124. drupal_add_js(array('simplemenu' => $settings), 'setting');
  125. drupal_add_js($simplemenu_path . '/simplemenu.js');
  126. $superfish = variable_get('simplemenu_superfish_version', 'superfish-1.4.1.js');
  127. drupal_add_js($simplemenu_path . '/' . $superfish);
  128. }
  129. }
  130. /**
  131. * \brief Retrieve the zindex for the CSS files.
  132. *
  133. * This function retrieves a z-index from a Drupal variable and
  134. * transform it to fit in a CSS file.
  135. *
  136. * \param[in] $name The name of the z-index variable to read.
  137. * \param[in] $default The default value to use when the variable is not defined.
  138. *
  139. * \return A string representing the current value of the specified z-index.
  140. */
  141. function simplemnu_get_zindex($name, $default) {
  142. $zindex = variable_get($name, $default);
  143. if ($zindex == -1) {
  144. $zindex = '';
  145. }
  146. else {
  147. $zindex = 'z-index: ' . $zindex . ';';
  148. }
  149. return $zindex;
  150. }
  151. /**
  152. * Implementation of hook_footer().
  153. *
  154. * This has been broken off of simplemenu_init() because simplemenu_get_menu()
  155. * calls simplemenu_menu_tree() which calls menu_tree_output() which has several
  156. * calls to theme(). This initializes the theme system too early causing hard
  157. * to track bugs.
  158. *
  159. * @see http://drupal.org/node/219910
  160. */
  161. function simplemenu_footer() {
  162. if (simplemenu_enabled()) {
  163. $simplemenu = drupal_to_js(simplemenu_get_menu());
  164. $path = base_path() . drupal_get_path('module', 'simplemenu');
  165. $output = "<script type=\"text/javascript\">var simplemenu = $simplemenu;</script>\n";
  166. return $output;
  167. }
  168. }
  169. /**
  170. * Implementation of hook_perm().
  171. */
  172. function simplemenu_perm() {
  173. return array('view simplemenu', 'administer simplemenu');
  174. }
  175. /**
  176. * Render an HTML list of links for a given menu.
  177. */
  178. function simplemenu_get_menu() {
  179. // if a user turned off menu module but SimpleMenu was previously set
  180. // reset variable so a menu appears
  181. $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  182. $menu = simplemenu_menu_tree($menu_name);
  183. if (!$menu) {
  184. $menu = '<ul class="menu"><li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li></ul>';
  185. }
  186. // This is ugly, I know, but it is the only way I can see to get the additional
  187. // links inside the <ul> tags
  188. if ($devel = simplemenu_get_devel()) {
  189. $pos = strpos($menu, '>') + 1;
  190. $menu = substr($menu, 0, $pos) . $devel . substr($menu, $pos);
  191. }
  192. // add the class & id to the UL tag here instead of the JavaScript
  193. // otherwise it could be added to the <div> tag instead...
  194. $pos = strpos($menu, '>') + 1;
  195. $menu = '<ul class="menu clear-block" id="simplemenu">' . substr($menu, $pos);
  196. return '<div class="simplemenu-block">' . $menu . '&nbsp;</div>';
  197. }
  198. /**
  199. * Custom implementation of menu_tree().
  200. * We want to retrieve the entire menu structure for a given menu,
  201. * regardless of whether or not the menu item is expanded or not.
  202. */
  203. function simplemenu_menu_tree($menu_name = 'navigation:0') {
  204. static $menu_output = array();
  205. if (!isset($menu_output[$menu_name])) {
  206. $tree = simplemenu_tree_all_data($menu_name);
  207. $menu_output[$menu_name] = menu_tree_output($tree);
  208. }
  209. return $menu_output[$menu_name];
  210. }
  211. /**
  212. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  213. * (which can be *any* menu item, not just the root of a custom menu).
  214. *
  215. * @param $root_menu
  216. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  217. *
  218. * @todo we don't actually need $menu_name, $mlid would be sufficient
  219. */
  220. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  221. static $tree = array();
  222. list($menu_name, $mlid) = explode(':', $root_menu);
  223. // Generate the cache ID.
  224. // "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"
  225. $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
  226. if (!isset($tree[$cid])) {
  227. // If the static variable doesn't have the data, check {cache_menu}.
  228. $cache = cache_get($cid, 'cache_menu');
  229. if ($cache && isset($cache->data)) {
  230. $data = $cache->data;
  231. }
  232. else {
  233. // Build and run the query, and build the tree.
  234. if ($mlid > 0) {
  235. $item = menu_link_load($mlid);
  236. // The tree is a subtree of $menu_name, so we need to restrict the query to
  237. // this subtree.
  238. $px = "p$item[depth]";
  239. $where = " AND ml.$px = %d AND ml.mlid != %d";
  240. $args = array($item[$px], $mlid);
  241. }
  242. else {
  243. // Get all links in this menu.
  244. $where = '';
  245. $args = array();
  246. }
  247. array_unshift($args, $menu_name);
  248. // Select the links from the table, and recursively build the tree. We
  249. // LEFT JOIN since there is no match in {menu_router} for an external
  250. // link.
  251. $data['tree'] = menu_tree_data(db_query("
  252. 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.*
  253. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  254. WHERE ml.menu_name = '%s'". $where ."
  255. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  256. $data['node_links'] = array();
  257. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  258. // Cache the data.
  259. cache_set($cid, $data, 'cache_menu');
  260. }
  261. // Check access for the current user to each item in the tree.
  262. menu_tree_check_access($data['tree'], $data['node_links']);
  263. $tree[$cid] = $data['tree'];
  264. }
  265. return $tree[$cid];
  266. }
  267. /**
  268. * Return a list of devel module links if the module is enabled
  269. * and the user has access to this module.
  270. */
  271. function simplemenu_get_devel() {
  272. $output = '';
  273. if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
  274. if (user_access('access devel information')) {
  275. $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
  276. $output .= simplemenu_menu_tree('devel');
  277. $output .= '</li>';
  278. }
  279. }
  280. return $output;
  281. }
  282. /**
  283. * Determine if simplemenu should be displayed based on visibility settings.
  284. *
  285. * @return boolean
  286. */
  287. function _simplemenu_page_visibility() {
  288. $operator = variable_get('simplemenu_visibility_operator', 0);
  289. $pages = variable_get('simplemenu_visibility_pages', '');
  290. if ($pages) {
  291. $path = drupal_get_path_alias($_GET['q']);
  292. // Compare with the internal and path alias (if any).
  293. $page_match = drupal_match_path($path, $pages);
  294. if ($path != $_GET['q']) {
  295. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  296. }
  297. // When $operator has a value of 0, the menu is displayed on
  298. // all pages except those listed in $pages. When set to 1, it
  299. // is displayed only on those pages listed in $pages.
  300. $page_match = !($operator ^ $page_match);
  301. }
  302. else {
  303. $page_match = TRUE;
  304. }
  305. return $page_match;
  306. }
  307. /**
  308. * Check whether the superuser/admin should be shown simplemenu.
  309. */
  310. function _simplemenu_superuser_active() {
  311. global $user;
  312. return $user->uid != 1 || variable_get('simplemenu_uid1', 1) == 1;
  313. }
  314. // vim: ts=2 sw=2 et syntax=php