simplemenu.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 = (!isset($exclusions[$theme]) || !$exclusions[$theme])
  31. && user_access('view simplemenu')
  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 (simplemenu_enabled()) {
  43. _simplemenu_add_menu();
  44. _simplemenu_add_css(); // basic CSS must be before _simplemenu_add_theme()
  45. _simplemenu_add_theme();
  46. _simplemenu_add_js();
  47. }
  48. }
  49. /** \brief Add the simplemenu variable with the menu to be displayed.
  50. *
  51. * This function loads the menu to be displayed and transforms it so
  52. * it works with superfish.
  53. *
  54. * If the cache version of the simplemenu JavaScript string cannot be
  55. * created, then it is sent inline whether or not the user asked for it
  56. * to be sent inline.
  57. */
  58. function _simplemenu_add_menu() {
  59. $simplemenu = 'var simplemenu=' . drupal_to_js(simplemenu_get_menu()) . ';';
  60. $has_file = variable_get('simplemenu_cache_menu', TRUE);
  61. if ($has_file) {
  62. $js_path = file_create_path('js'); // same path as concatenated Core JS
  63. $js_md5 = md5($simplemenu); // this is a lot faster than transferring the menu for each page!
  64. $js_filename = $js_path . '/simplemenu-' . $js_md5 . '.js';
  65. $has_file = file_check_directory($js_path, FILE_CREATE_DIRECTORY);
  66. if ($has_file) {
  67. // The old way was to send the whole menu each time
  68. if (!file_exists($js_filename)) {
  69. // use LOCK so concurrent writes don't mess up the file
  70. @file_put_contents($js_filename, $simplemenu);
  71. $has_file = file_exists($js_filename);
  72. }
  73. else {
  74. $has_file = TRUE;
  75. }
  76. }
  77. }
  78. $scope = variable_get('simplemenu_menu_scope', 'footer');
  79. if ($has_file) {
  80. drupal_add_js($js_filename, 'module', $scope);
  81. }
  82. else {
  83. drupal_add_js($simplemenu, 'inline', $scope);
  84. }
  85. }
  86. /** \brief Generate the CSS and add it to the page.
  87. *
  88. * This function generates the dynamic CSS and then insert that to
  89. * the header of the page.
  90. *
  91. * The function regenerates the CSS only when the settings were
  92. * modified. Otherwise, it uses the cached version.
  93. *
  94. * The function has a fall back, in case the dynamic CSS cannot
  95. * be created.
  96. */
  97. function _simplemenu_add_css() {
  98. global $user;
  99. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  100. $css_path = file_create_path('css'); // same path as concatenated Core CSS
  101. if (file_check_directory($css_path, FILE_CREATE_DIRECTORY)) {
  102. $fix = variable_get('simplemenu_fix', 'scroll');
  103. // XXX add a variable simplemenu_update which is set to TRUE whenever
  104. // the settings get modified and false here
  105. $output_filename = variable_get('simplemenu_css_filename', '');
  106. if (!$output_filename) {
  107. $tags = array(
  108. '@MENUBAR_ZINDEX@' => simplemnu_get_zindex('simplemenu_menubar_zindex', 9999),
  109. '@DROPDOWN_ZINDEX@' => simplemnu_get_zindex('simplemenu_dropdown_zindex', 9999),
  110. );
  111. switch ($fix) {
  112. case 'top':
  113. $tags['@FIX@'] = "position: fixed;\n top: 0;";
  114. break;
  115. case 'bottom':
  116. $tags['@FIX@'] = "position: fixed;\n bottom: 0;";
  117. break;
  118. default: // scroll
  119. $tags['@FIX@'] = 'position: relative;';
  120. break;
  121. }
  122. $css = file_get_contents($simplemenu_path . '/simplemenu.css.tpl');
  123. $css = strtr($css, $tags);
  124. $css_md5 = md5($css);
  125. $output_filename = $css_path . '/simplemenu-' . $css_md5 . '.css';
  126. if (!file_exists($output_filename)) {
  127. // new content, create a new file
  128. file_put_contents($output_filename, $css);
  129. }
  130. else {
  131. // this call is rather ugly, but we must make sure that the
  132. // system cache will take the current Simplemenu CSS in account
  133. _drupal_flush_css_js();
  134. }
  135. //variable_set('simplemenu_css_filename', $output_filename);
  136. }
  137. drupal_add_css($output_filename);
  138. }
  139. else {
  140. // in case we cannot create the dynamic CSS
  141. $last_msg = variable_get('simplemenu_css_error', 0);
  142. if (($last_msg != -1 && $last_msg + 3600 > time()) || $user->uid == 1) {
  143. // avoid displaying the error on each page... only once per hour.
  144. // (unless you are the admin, in which case you probably want to know!)
  145. variable_set('simplemenu_css_error', time());
  146. drupal_set_message(t('Simplemenu could not create the folder @path in order to save the dynamic CSS data.',
  147. array('@path' => $css_path)), 'warning');
  148. }
  149. // use a default that cannot react to the dynamic changes...
  150. drupal_add_css($simplemenu_path .'/simplemenu.css');
  151. }
  152. }
  153. /** \brief Add the module theme.
  154. *
  155. * This function adds a theme for the Simplemenu look.
  156. *
  157. * By default, the original theme is used. The module also offers the
  158. * blackblue theme. It is also possible to create new themes or use
  159. * the theming of the current theme for simplemenu (so the menu fits
  160. * perfectly for that theme.)
  161. */
  162. function _simplemenu_add_theme() {
  163. // we want to put the simplemenu theme CSS first
  164. // so we can change some CSS entries dynamically
  165. // but at this time the simplemenu.css is used to
  166. // reset many of the CSS entries... Hmmm...
  167. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  168. if ($simplemenu_theme != 'custom') {
  169. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  170. $theme_file = $simplemenu_path . '/themes/' . $simplemenu_theme
  171. . '/' . $simplemenu_theme . '.css';
  172. if (is_file($theme_file)) {
  173. drupal_add_css($theme_file);
  174. }
  175. }
  176. }
  177. /** \brief Add the JavaScript that makes it all work.
  178. *
  179. * This function adds the Simplemenu JavaScript, the Superfish JavaScript
  180. * and settings from the user.
  181. */
  182. function _simplemenu_add_js() {
  183. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  184. // Settings
  185. $fix = variable_get('simplemenu_fix', 'scroll');
  186. switch ($fix) {
  187. case 'top':
  188. $element = 'body';
  189. $placement = 'prepend';
  190. break;
  191. case 'bottom':
  192. $element = 'body';
  193. $placement = 'append';
  194. break;
  195. default: // 'scroll'
  196. // let user defined other elements when not fixed
  197. $element = variable_get('simplemenu_element', 'body');
  198. $placement = variable_get('simplemenu_element_method', 'prepend');
  199. break;
  200. }
  201. $settings = array(
  202. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  203. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  204. 'element' => $element,
  205. 'placement' => $placement,
  206. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  207. 'detectPopup' => variable_get('simplemenu_detect_popup', 1),
  208. );
  209. drupal_add_js(array('simplemenu' => $settings), 'setting');
  210. // Simplemenu
  211. drupal_add_js($simplemenu_path . '/simplemenu.js');
  212. // Superfish
  213. $superfish = variable_get('simplemenu_superfish_version', 'superfish-1.4.1.js');
  214. if ($superfish != 'custom') {
  215. drupal_add_js($simplemenu_path . '/' . $superfish);
  216. }
  217. }
  218. /**
  219. * \brief Retrieve the zindex for the CSS files.
  220. *
  221. * This function retrieves a z-index from a Drupal variable and
  222. * transform it to fit in a CSS file.
  223. *
  224. * \param[in] $name The name of the z-index variable to read.
  225. * \param[in] $default The default value to use when the variable is not defined.
  226. *
  227. * \return A string representing the current value of the specified z-index.
  228. */
  229. function simplemnu_get_zindex($name, $default) {
  230. $zindex = variable_get($name, $default);
  231. if ($zindex == -1) {
  232. $zindex = '';
  233. }
  234. else {
  235. $zindex = 'z-index: ' . $zindex . ';';
  236. }
  237. return $zindex;
  238. }
  239. /**
  240. * Implementation of hook_perm().
  241. */
  242. function simplemenu_perm() {
  243. return array('view simplemenu', 'administer simplemenu');
  244. }
  245. /**
  246. * Render an HTML list of links for a given menu.
  247. */
  248. function simplemenu_get_menu() {
  249. variable_set('simplemenu_running', TRUE);
  250. // if a user turned off menu module but SimpleMenu was previously set
  251. // reset variable so a menu appears
  252. $all_menus = array(variable_get('simplemenu_menu', 'navigation:0'));
  253. drupal_alter('simplemenu_menus', $all_menus);
  254. if (count($all_menus) > 1) {
  255. // if menu is not enable then we cannot have a count other than 1
  256. $menu_titles = menu_get_menus();
  257. $tree = array();
  258. foreach ($all_menus as $full_name) {
  259. list($menu_name, $mlid) = explode(':', $full_name);
  260. $tree[] = array(
  261. 'link' => array(
  262. 'simplemenu_multi_menu_root' => TRUE,
  263. 'mlid' => $mlid,
  264. 'menu_name' => $full_name,
  265. 'hidden' => FALSE,
  266. 'title' => $menu_titles[$menu_name],
  267. 'href' => 'admin/settings/simplemenu', /// ??? -- we should not have a link here
  268. 'in_active_trail' => FALSE,
  269. 'has_children' => TRUE,
  270. 'localized_options' => array(
  271. 'attributes' => array('class' => 'simplemenu-top-level'),
  272. ),
  273. ),
  274. 'below' => simplemenu_menu_tree($full_name),
  275. );
  276. }
  277. }
  278. else {
  279. reset($all_menus);
  280. $tree = simplemenu_menu_tree(current($all_menus));
  281. }
  282. // allow other modules to modify the menu tree
  283. drupal_alter('simplemenu_tree', $tree);
  284. // now generate the output
  285. $menu = menu_tree_output($tree);
  286. if (!$menu) {
  287. $menu = '<ul class="menu"><li><a href="' . url('admin/settings/simplemenu') . '">'
  288. . t('No menu items found. Try a different menu as the default.') . '</a></li></ul>';
  289. }
  290. // add the id to the UL tag here instead of the JavaScript
  291. // otherwise it could be added to the <div> tag instead...
  292. $pos = strpos($menu, '>');
  293. $menu = str_replace('class="menu', 'class="menu clear-block', substr($menu, 0, $pos))
  294. . ' id="simplemenu"' . substr($menu, $pos);
  295. variable_set('simplemenu_running', FALSE);
  296. return '<div class="simplemenu-block">' . $menu . '&nbsp;</div>';
  297. }
  298. /**
  299. * Custom implementation of menu_tree().
  300. * We want to retrieve the entire menu structure for a given menu,
  301. * regardless of whether or not the menu item is expanded or not.
  302. */
  303. function simplemenu_menu_tree($menu_name = 'navigation:0') {
  304. static $menu_tree = array();
  305. if (!isset($menu_output[$menu_name])) {
  306. $menu_tree[$menu_name] = simplemenu_tree_all_data($menu_name);
  307. }
  308. return $menu_tree[$menu_name];
  309. }
  310. /**
  311. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  312. * (which can be *any* menu item, not just the root of a custom menu).
  313. *
  314. * @param $root_menu
  315. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  316. *
  317. * @todo we don't actually need $menu_name, $mlid would be sufficient
  318. */
  319. function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  320. static $tree = array();
  321. list($menu_name, $mlid) = explode(':', $root_menu);
  322. // Generate the cache ID.
  323. // "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"
  324. $cid = "links:$menu_name:all:$mlid" . ($mlid ? ':all' : '');
  325. if (!isset($tree[$cid])) {
  326. // If the static variable doesn't have the data, check {cache_menu}.
  327. $cache = cache_get($cid, 'cache_menu');
  328. if ($cache && isset($cache->data)) {
  329. $data = $cache->data;
  330. }
  331. else {
  332. // Build and run the query, and build the tree.
  333. if ($mlid > 0) {
  334. $item = menu_link_load($mlid);
  335. // The tree is a subtree of $menu_name, so we need to restrict the query to
  336. // this subtree.
  337. $px = "p$item[depth]";
  338. $where = " AND ml.$px = %d AND ml.mlid != %d";
  339. $args = array($item[$px], $mlid);
  340. }
  341. else {
  342. // Get all links in this menu.
  343. $where = '';
  344. $args = array();
  345. }
  346. array_unshift($args, $menu_name);
  347. // Select the links from the table, and recursively build the tree. We
  348. // LEFT JOIN since there is no match in {menu_router} for an external
  349. // link.
  350. $data['tree'] = menu_tree_data(db_query("
  351. 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.*
  352. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  353. WHERE ml.menu_name = '%s'". $where ."
  354. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
  355. $data['node_links'] = array();
  356. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  357. // Cache the data.
  358. cache_set($cid, $data, 'cache_menu');
  359. }
  360. // Check access for the current user to each item in the tree.
  361. menu_tree_check_access($data['tree'], $data['node_links']);
  362. $tree[$cid] = $data['tree'];
  363. }
  364. return $tree[$cid];
  365. }
  366. /**
  367. * Determine if simplemenu should be displayed based on visibility settings.
  368. *
  369. * @return boolean
  370. */
  371. function _simplemenu_page_visibility() {
  372. $operator = variable_get('simplemenu_visibility_operator', 0);
  373. $pages = variable_get('simplemenu_visibility_pages', '');
  374. if ($pages) {
  375. $path = drupal_get_path_alias($_GET['q']);
  376. // Compare with the internal and path alias (if any).
  377. $page_match = drupal_match_path($path, $pages);
  378. if ($path != $_GET['q']) {
  379. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  380. }
  381. // When $operator has a value of 0, the menu is displayed on
  382. // all pages except those listed in $pages. When set to 1, it
  383. // is displayed only on those pages listed in $pages.
  384. $page_match = !($operator ^ $page_match);
  385. }
  386. else {
  387. $page_match = TRUE;
  388. }
  389. return $page_match;
  390. }
  391. /**
  392. * Check whether the superuser/admin should be shown simplemenu.
  393. */
  394. function _simplemenu_superuser_active() {
  395. global $user;
  396. return $user->uid != 1 || variable_get('simplemenu_uid1', 1) == 1;
  397. }
  398. // vim: ts=2 sw=2 et syntax=php