simplemenu.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/config/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. // XXX -- should we put that in the settings instead? why put it in its own variable?
  60. $simplemenu = 'var simplemenu=' . drupal_json_encode(simplemenu_get_menu()) . ';';
  61. $has_file = variable_get('simplemenu_cache_menu', TRUE);
  62. if ($has_file) {
  63. $js_hash = drupal_hash_base64($simplemenu);
  64. $js_path = 'public://js'; // same path as concatenated Core JS
  65. $js_filename = $js_path . '/simplemenu_' . $js_hash . '.js';
  66. if (!file_exists($js_filename)) {
  67. file_prepare_directory($js_path, FILE_CREATE_DIRECTORY);
  68. if (!file_unmanaged_save_data($simplemenu, $js_filename, FILE_EXISTS_REPLACE)) {
  69. $has_file = FALSE;
  70. }
  71. }
  72. }
  73. $options = array(
  74. 'scope' => variable_get('simplemenu_menu_scope', 'footer'),
  75. // 'version' => ?, -- could we make use of the version?
  76. );
  77. if ($has_file) {
  78. //$options['type'] = 'file'; -- default
  79. drupal_add_js($js_filename, $options);
  80. }
  81. else {
  82. // inline adds the value as is (untouched)
  83. $options['type'] = 'inline';
  84. drupal_add_js($simplemenu, $options);
  85. }
  86. }
  87. /** \brief Generate the CSS and add it to the page.
  88. *
  89. * This function generates the dynamic CSS and then insert that to
  90. * the header of the page.
  91. *
  92. * The function regenerates the CSS only when the settings were
  93. * modified. Otherwise, it uses the cached version.
  94. *
  95. * The function has a fall back, in case the dynamic CSS cannot
  96. * be created.
  97. */
  98. function _simplemenu_add_css() {
  99. global $user;
  100. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  101. $css_path = 'public://css'; // same path as concatenated Core CSS
  102. if (file_prepare_directory($css_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  103. $fix = variable_get('simplemenu_fix', 'scroll');
  104. // XXX add a variable simplemenu_update which is set to TRUE whenever
  105. // the settings get modified and false here
  106. $output_filename = variable_get('simplemenu_css_filename', '');
  107. if (!$output_filename) {
  108. $tags = array(
  109. '@MENUBAR_ZINDEX@' => simplemnu_get_zindex('simplemenu_menubar_zindex', 9999),
  110. '@DROPDOWN_ZINDEX@' => simplemnu_get_zindex('simplemenu_dropdown_zindex', 9999),
  111. );
  112. switch ($fix) {
  113. case 'top':
  114. $tags['@FIX@'] = "position: fixed;\n top: 0;";
  115. break;
  116. case 'bottom':
  117. $tags['@FIX@'] = "position: fixed;\n bottom: 0;";
  118. break;
  119. default: // scroll
  120. $tags['@FIX@'] = 'position: relative;';
  121. break;
  122. }
  123. $css = file_get_contents($simplemenu_path . '/simplemenu.css.tpl');
  124. $css = strtr($css, $tags);
  125. $css_hash = hash('sha256', $css);
  126. $output_filename = $css_path . '/simplemenu-' . $css_hash . '.css';
  127. if (!file_exists($output_filename)) {
  128. // new content, create a new file
  129. file_put_contents($output_filename, $css);
  130. }
  131. else {
  132. // this call is rather ugly, but we must make sure that the
  133. // system cache will take the current Simplemenu CSS in account
  134. _drupal_flush_css_js();
  135. }
  136. //variable_set('simplemenu_css_filename', $output_filename);
  137. }
  138. drupal_add_css($output_filename);
  139. }
  140. else {
  141. // in case we cannot create the dynamic CSS
  142. $last_msg = variable_get('simplemenu_css_error', 0);
  143. if (($last_msg != -1 && $last_msg + 3600 > time()) || $user->uid == 1) {
  144. // avoid displaying the error on each page... only once per hour.
  145. // (unless you are the admin, in which case you probably want to know!)
  146. variable_set('simplemenu_css_error', time());
  147. drupal_set_message(t('Simplemenu could not create the folder @path in order to save the dynamic CSS data.',
  148. array('@path' => $css_path)), 'warning');
  149. }
  150. // use a default that cannot react to the dynamic changes...
  151. drupal_add_css($simplemenu_path .'/simplemenu.css');
  152. }
  153. }
  154. /** \brief Add the module theme.
  155. *
  156. * This function adds a theme for the Simplemenu look.
  157. *
  158. * By default, the original theme is used. The module also offers the
  159. * blackblue theme. It is also possible to create new themes or use
  160. * the theming of the current theme for simplemenu (so the menu fits
  161. * perfectly for that theme.)
  162. */
  163. function _simplemenu_add_theme() {
  164. // we want to put the simplemenu theme CSS first
  165. // so we can change some CSS entries dynamically
  166. // but at this time the simplemenu.css is used to
  167. // reset many of the CSS entries... Hmmm...
  168. $simplemenu_theme = variable_get('simplemenu_theme', 'original');
  169. if ($simplemenu_theme != 'custom') {
  170. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  171. $theme_file = $simplemenu_path . '/themes/' . $simplemenu_theme
  172. . '/' . $simplemenu_theme . '.css';
  173. if (is_file($theme_file)) {
  174. drupal_add_css($theme_file);
  175. }
  176. }
  177. }
  178. /** \brief Add the JavaScript that makes it all work.
  179. *
  180. * This function adds the Simplemenu JavaScript, the Superfish JavaScript
  181. * and settings from the user.
  182. */
  183. function _simplemenu_add_js() {
  184. $simplemenu_path = drupal_get_path('module', 'simplemenu');
  185. // Settings
  186. $fix = variable_get('simplemenu_fix', 'scroll');
  187. switch ($fix) {
  188. case 'top':
  189. $element = 'body';
  190. $placement = 'prepend';
  191. break;
  192. case 'bottom':
  193. $element = 'body';
  194. $placement = 'append';
  195. break;
  196. default: // 'scroll'
  197. // let user defined other elements when not fixed
  198. $element = variable_get('simplemenu_element', 'body');
  199. $placement = variable_get('simplemenu_element_method', 'prepend');
  200. break;
  201. }
  202. $settings = array(
  203. 'effect' => variable_get('simplemenu_effect', 'opacity'),
  204. 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
  205. 'element' => $element,
  206. 'placement' => $placement,
  207. 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
  208. 'detectPopup' => variable_get('simplemenu_detect_popup', 1),
  209. );
  210. drupal_add_js(array('simplemenu' => $settings), array('type' => 'setting'));
  211. // Simplemenu
  212. drupal_add_js($simplemenu_path . '/simplemenu.js', array('version' => '1.2'));
  213. // Superfish
  214. $superfish = variable_get('simplemenu_superfish_version', 'superfish-1.4.1.js');
  215. if ($superfish != 'custom') {
  216. $sf_version = str_replace(array('superfish-', '.js'), '', $superfish);
  217. drupal_add_js($simplemenu_path . '/' . $superfish, array('version' => $sf_version));
  218. }
  219. }
  220. /**
  221. * \brief Retrieve the zindex for the CSS files.
  222. *
  223. * This function retrieves a z-index from a Drupal variable and
  224. * transform it to fit in a CSS file.
  225. *
  226. * \param[in] $name The name of the z-index variable to read.
  227. * \param[in] $default The default value to use when the variable is not defined.
  228. *
  229. * \return A string representing the current value of the specified z-index.
  230. */
  231. function simplemnu_get_zindex($name, $default) {
  232. $zindex = variable_get($name, $default);
  233. if ($zindex == -1) {
  234. $zindex = '';
  235. }
  236. else {
  237. $zindex = 'z-index: ' . $zindex . ';';
  238. }
  239. return $zindex;
  240. }
  241. /**
  242. * Implementation of hook_perm().
  243. */
  244. function simplemenu_perm() {
  245. return array('view simplemenu', 'administer simplemenu');
  246. }
  247. /**
  248. * Render an HTML list of links for a given menu.
  249. */
  250. function simplemenu_get_menu() {
  251. variable_set('simplemenu_running', TRUE);
  252. // if a user turned off menu module but SimpleMenu was previously set
  253. // reset variable so a menu appears
  254. $all_menus = array(variable_get('simplemenu_menu', 'management:0'));
  255. drupal_alter('simplemenu_menus', $all_menus);
  256. if (count($all_menus) > 1) {
  257. // if menu is not enable then we cannot have a count other than 1
  258. $menu_titles = menu_get_menus();
  259. $tree = array();
  260. foreach ($all_menus as $full_name) {
  261. list($menu_name, $mlid) = explode(':', $full_name);
  262. $tree[] = array(
  263. 'link' => array(
  264. 'simplemenu_multi_menu_root' => TRUE,
  265. 'mlid' => $mlid,
  266. 'menu_name' => $full_name,
  267. 'hidden' => FALSE,
  268. 'title' => $menu_titles[$menu_name],
  269. 'href' => 'admin/settings/simplemenu', /// ??? -- we should not have a link here
  270. 'in_active_trail' => FALSE,
  271. 'has_children' => TRUE,
  272. 'localized_options' => array(
  273. 'attributes' => array('class' => 'simplemenu-top-level'),
  274. ),
  275. ),
  276. 'below' => simplemenu_menu_tree($full_name),
  277. );
  278. }
  279. }
  280. else {
  281. reset($all_menus);
  282. $tree = simplemenu_menu_tree(current($all_menus));
  283. }
  284. // allow other modules to modify the menu tree
  285. drupal_alter('simplemenu_tree', $tree);
  286. $tree = simplemenu_tree_remove_hidden($tree);
  287. // now generate the output
  288. $menu_form = menu_tree_output($tree);
  289. $menu = drupal_render($menu_form);
  290. if (!$menu) {
  291. $menu = '<ul class="menu"><li><a href="' . url('admin/settings/simplemenu') . '">'
  292. . t('No menu items found. Try a different menu as the default.') . '</a></li></ul>';
  293. }
  294. // add the id to the UL tag here instead of the JavaScript
  295. // otherwise it could be added to the <div> tag instead...
  296. $pos = strpos($menu, '>');
  297. $menu = str_replace('class="menu', 'class="menu clear-block', substr($menu, 0, $pos))
  298. . ' id="simplemenu"' . substr($menu, $pos);
  299. variable_set('simplemenu_running', FALSE);
  300. return '<div class="simplemenu-block">' . $menu . '&nbsp;</div>';
  301. }
  302. /**
  303. * At this point (May 31, 2010) the menu tree includes
  304. * many 'below' that should be considered empty but
  305. * aren't... unless we make sure we remove the children
  306. * ourselves.
  307. */
  308. function simplemenu_tree_remove_hidden($tree) {
  309. $clean = array();
  310. foreach ($tree as $key => $data) {
  311. if (!$data['link']['hidden']) {
  312. if ($data['below']) {
  313. $data['below'] = simplemenu_tree_remove_hidden($data['below']);
  314. if (count($data['below']) == 0) {
  315. $data['below'] = 0;
  316. }
  317. }
  318. $clean[] = $data;
  319. }
  320. }
  321. return $clean;
  322. }
  323. /**
  324. * Custom implementation of menu_tree().
  325. * We want to retrieve the entire menu structure for a given menu,
  326. * regardless of whether or not the menu item is expanded or not.
  327. */
  328. function simplemenu_menu_tree($menu_name = 'management:0') {
  329. static $menu_tree = array();
  330. if (!isset($menu_output[$menu_name])) {
  331. //$menu_tree[$menu_name] = simplemenu_tree_all_data($menu_name);
  332. $menu_tree[$menu_name] = menu_tree_all_data('management');
  333. }
  334. return $menu_tree[$menu_name];
  335. }
  336. /**
  337. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  338. * (which can be *any* menu item, not just the root of a custom menu).
  339. *
  340. * @param $root_menu
  341. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  342. *
  343. * @todo we don't actually need $menu_name, $mlid would be sufficient
  344. */
  345. function simplemenu_tree_all_data($root_menu = 'management:0') {
  346. static $tree = array();
  347. list($menu_name, $mlid) = explode(':', $root_menu);
  348. // Generate the cache ID for Drupal 7.
  349. $cid = 'links:' . $menu_name . ':all-cid:' . $mlid . ':' . $GLOBALS['language']->language . ':0';
  350. if (!isset($tree[$cid])) {
  351. // // If the static variable doesn't have the data, check {cache_menu}.
  352. // $cache = cache_get($cid, 'cache_menu');
  353. // if ($cache && isset($cache->data)) {
  354. // $data = $cache->data;
  355. // }
  356. // else {
  357. // // Build and run the query, and build the tree.
  358. // $where = '';
  359. // $args = array(':menu_name' => $menu_name);
  360. // if ($mlid > 0) {
  361. // $item = menu_link_load($mlid);
  362. // if ($item) {
  363. // // The tree is a subtree of $menu_name, so we need to restrict the query to
  364. // // this subtree.
  365. // $px = "p" . (int) $item['depth'];
  366. // $where = " AND ml.$px = :ml AND ml.mlid != :mlid";
  367. // $args[':ml'] = $item[$px];
  368. // $args[':mlid'] = $mlid;
  369. // }
  370. // }
  371. // // Select the links from the table, and recursively build the tree. We
  372. // // LEFT JOIN since there is no match in {menu_router} for an external
  373. // // link.
  374. // $result = db_query("SELECT m.load_functions, m.to_arg_functions, m.access_callback,
  375. // m.access_arguments, m.page_callback, m.page_arguments,
  376. // m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  377. // FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  378. // WHERE ml.menu_name = :menu_name$where
  379. // ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args);
  380. // $data['tree'] = menu_tree_data($result->fetchAssoc());
  381. // $data['node_links'] = array();
  382. // menu_tree_collect_node_links($data['tree'], $data['node_links']);
  383. // // Cache the data.
  384. // cache_set($cid, $data, 'cache_menu');
  385. // }
  386. // // Check access for the current user to each item in the tree.
  387. // menu_tree_check_access($data['tree'], $data['node_links']);
  388. // $tree[$cid] = $data['tree'];
  389. // If the static variable doesn't have the data, check {cache_menu}.
  390. $cache = cache_get($cid, 'cache_menu');
  391. if ($cache && isset($cache->data)) {
  392. // If the cache entry exists, it will just be the cid for the actual data.
  393. // This avoids duplication of large amounts of data.
  394. $cache = cache_get($cache->data, 'cache_menu');
  395. if ($cache && isset($cache->data)) {
  396. $data = $cache->data;
  397. }
  398. }
  399. // If the tree data was not in the cache, $data will be NULL.
  400. if (!isset($data)) {
  401. // Build the query using a LEFT JOIN since there is no match in
  402. // {menu_router} for an external link.
  403. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  404. $query->addTag('translatable');
  405. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  406. $query->fields('ml');
  407. $query->fields('m', array(
  408. 'load_functions',
  409. 'to_arg_functions',
  410. 'access_callback',
  411. 'access_arguments',
  412. 'page_callback',
  413. 'page_arguments',
  414. 'delivery_callback',
  415. 'title',
  416. 'title_callback',
  417. 'title_arguments',
  418. 'theme_callback',
  419. 'theme_arguments',
  420. 'type',
  421. 'description',
  422. ));
  423. for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
  424. $query->orderBy('p' . $i, 'ASC');
  425. }
  426. $query->condition('ml.menu_name', $menu_name);
  427. if (isset($max_depth)) {
  428. $query->condition('ml.depth', $max_depth, '<=');
  429. }
  430. if ($mlid) {
  431. // The tree is for a single item, so we need to match the values in its
  432. // p columns and 0 (the top level) with the plid values of other links.
  433. $args = array(0);
  434. for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
  435. $args[] = $link["p$i"];
  436. }
  437. $args = array_unique($args);
  438. $query->condition('ml.plid', $args, 'IN');
  439. $parents = $args;
  440. $parents[] = $link['mlid'];
  441. }
  442. else {
  443. // Get all links in this menu.
  444. $parents = array();
  445. }
  446. // Select the links from the table, and build an ordered array of links
  447. // using the query result object.
  448. $links = array();
  449. foreach ($query->execute() as $item) {
  450. $links[] = $item;
  451. }
  452. $data['tree'] = menu_tree_data($links, $parents);
  453. $data['node_links'] = array();
  454. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  455. // Cache the data, if it is not already in the cache.
  456. $tree_cid = _menu_tree_cid($menu_name, $data);
  457. if (!cache_get($tree_cid, 'cache_menu')) {
  458. cache_set($tree_cid, $data, 'cache_menu');
  459. }
  460. // Cache the cid of the (shared) data using the menu and item-specific cid.
  461. cache_set($cid, $tree_cid, 'cache_menu');
  462. }
  463. // Check access for the current user to each item in the tree.
  464. menu_tree_check_access($data['tree'], $data['node_links']);
  465. $tree[$cid] = $data['tree'];
  466. }
  467. return $tree[$cid];
  468. }
  469. /**
  470. * Determine if simplemenu should be displayed based on visibility settings.
  471. *
  472. * @return boolean
  473. */
  474. function _simplemenu_page_visibility() {
  475. $operator = variable_get('simplemenu_visibility_operator', 0);
  476. $pages = variable_get('simplemenu_visibility_pages', '');
  477. if ($pages) {
  478. $path = drupal_get_path_alias($_GET['q']);
  479. // Compare with the internal and path alias (if any).
  480. $page_match = drupal_match_path($path, $pages);
  481. if ($path != $_GET['q']) {
  482. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  483. }
  484. // When $operator has a value of 0, the menu is displayed on
  485. // all pages except those listed in $pages. When set to 1, it
  486. // is displayed only on those pages listed in $pages.
  487. $page_match = !($operator ^ $page_match);
  488. }
  489. else {
  490. $page_match = TRUE;
  491. }
  492. return $page_match;
  493. }
  494. /**
  495. * Check whether the superuser/admin should be shown simplemenu.
  496. */
  497. function _simplemenu_superuser_active() {
  498. global $user;
  499. return $user->uid != 1 || variable_get('simplemenu_uid1', 1) == 1;
  500. }
  501. // vim: ts=2 sw=2 et syntax=php