simplemenu.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. /**
  3. * @file
  4. * Creates a simplemenu.
  5. */
  6. /**
  7. * Implementation of hook_menu().
  8. */
  9. function simplemenu_menu() {
  10. $items = array();
  11. $items['admin/config/simplemenu'] = array(
  12. 'title' => 'SimpleMenu',
  13. 'description' => 'Select the menu to display.',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('simplemenu_admin_settings'),
  16. 'access arguments' => array('administer simplemenu'),
  17. 'file' => 'simplemenu.admin.inc',
  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 = (!isset($exclusions[$theme]) || !$exclusions[$theme])
  30. && user_access('view simplemenu')
  31. && _simplemenu_page_visibility()
  32. && _simplemenu_superuser_active();
  33. }
  34. return $enabled;
  35. }
  36. /**
  37. * Implementation of hook_init().
  38. */
  39. function simplemenu_init() {
  40. // do a simple access check here, since theme isn't available to check yet
  41. if (simplemenu_enabled()) {
  42. _simplemenu_add_menu();
  43. _simplemenu_add_css(); // basic CSS must be before _simplemenu_add_theme()
  44. _simplemenu_add_theme();
  45. _simplemenu_add_js();
  46. }
  47. }
  48. /** \brief Add the simplemenu variable with the menu to be displayed.
  49. *
  50. * This function loads the menu to be displayed and transforms it so
  51. * it works with superfish.
  52. *
  53. * If the cache version of the simplemenu JavaScript string cannot be
  54. * created, then it is sent inline whether or not the user asked for it
  55. * to be sent inline.
  56. */
  57. function _simplemenu_add_menu() {
  58. // XXX -- should we put that in the settings instead? why put it in its own variable?
  59. $simplemenu = 'var simplemenu=' . drupal_json_encode(simplemenu_get_menu()) . ';';
  60. $has_file = variable_get('simplemenu_cache_menu', TRUE);
  61. if ($has_file) {
  62. $js_hash = drupal_hash_base64($simplemenu);
  63. $js_path = 'public://js'; // same path as concatenated Core JS
  64. $js_filename = $js_path . '/simplemenu_' . $js_hash . '.js';
  65. if (!file_exists($js_filename)) {
  66. file_prepare_directory($js_path, FILE_CREATE_DIRECTORY);
  67. if (!file_unmanaged_save_data($simplemenu, $js_filename, FILE_EXISTS_REPLACE)) {
  68. $has_file = FALSE;
  69. }
  70. }
  71. }
  72. $options = array(
  73. 'scope' => variable_get('simplemenu_menu_scope', 'footer'),
  74. // 'version' => ?, -- could we make use of the version?
  75. );
  76. if ($has_file) {
  77. //$options['type'] = 'file'; -- default
  78. drupal_add_js($js_filename, $options);
  79. }
  80. else {
  81. // inline adds the value as is (untouched)
  82. $options['type'] = 'inline';
  83. drupal_add_js($simplemenu, $options);
  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 = 'public://css'; // same path as concatenated Core CSS
  101. if (file_prepare_directory($css_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  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_hash = hash('sha256', $css);
  125. $output_filename = $css_path . '/simplemenu-' . $css_hash . '.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), array('type' => 'setting'));
  210. // Simplemenu
  211. drupal_add_js($simplemenu_path . '/simplemenu.js', array('version' => '1.2'));
  212. // Superfish
  213. $superfish = variable_get('simplemenu_superfish_version', 'superfish-1.4.1.js');
  214. if ($superfish != 'custom') {
  215. $sf_version = str_replace(array('superfish-', '.js'), '', $superfish);
  216. drupal_add_js($simplemenu_path . '/' . $superfish, array('version' => $sf_version));
  217. }
  218. }
  219. /**
  220. * \brief Retrieve the zindex for the CSS files.
  221. *
  222. * This function retrieves a z-index from a Drupal variable and
  223. * transform it to fit in a CSS file.
  224. *
  225. * \param[in] $name The name of the z-index variable to read.
  226. * \param[in] $default The default value to use when the variable is not defined.
  227. *
  228. * \return A string representing the current value of the specified z-index.
  229. */
  230. function simplemnu_get_zindex($name, $default) {
  231. $zindex = variable_get($name, $default);
  232. if ($zindex == -1) {
  233. $zindex = '';
  234. }
  235. else {
  236. $zindex = 'z-index: ' . $zindex . ';';
  237. }
  238. return $zindex;
  239. }
  240. /**
  241. * Implementation of hook_perm().
  242. */
  243. function simplemenu_perm() {
  244. return array('view simplemenu', 'administer simplemenu');
  245. }
  246. /**
  247. * Render an HTML list of links for a given menu.
  248. */
  249. function simplemenu_get_menu() {
  250. variable_set('simplemenu_running', TRUE);
  251. // if a user turned off menu module but SimpleMenu was previously set
  252. // reset variable so a menu appears
  253. $all_menus = array(variable_get('simplemenu_menu', 'management:0'));
  254. drupal_alter('simplemenu_menus', $all_menus);
  255. if (count($all_menus) > 1) {
  256. // if menu is not enable then we cannot have a count other than 1
  257. $menu_titles = menu_get_menus();
  258. $tree = array();
  259. foreach ($all_menus as $full_name) {
  260. list($menu_name, $mlid) = explode(':', $full_name);
  261. $tree[] = array(
  262. 'link' => array(
  263. 'simplemenu_multi_menu_root' => TRUE,
  264. 'mlid' => $mlid,
  265. 'menu_name' => $full_name,
  266. 'hidden' => FALSE,
  267. 'title' => $menu_titles[$menu_name],
  268. 'href' => 'admin/settings/simplemenu', /// ??? -- we should not have a link here
  269. 'in_active_trail' => FALSE,
  270. 'has_children' => TRUE,
  271. 'localized_options' => array(
  272. 'attributes' => array('class' => 'simplemenu-top-level'),
  273. ),
  274. ),
  275. 'below' => simplemenu_menu_tree($full_name),
  276. );
  277. }
  278. }
  279. else {
  280. reset($all_menus);
  281. $tree = simplemenu_menu_tree(current($all_menus));
  282. }
  283. // allow other modules to modify the menu tree
  284. drupal_alter('simplemenu_tree', $tree);
  285. $tree = simplemenu_tree_remove_hidden($tree);
  286. // now generate the output
  287. $menu_form = menu_tree_output($tree);
  288. $menu = drupal_render($menu_form);
  289. if (!$menu) {
  290. $menu = '<ul class="menu"><li><a href="' . url('admin/settings/simplemenu') . '">'
  291. . t('No menu items found. Try a different menu as the default.') . '</a></li></ul>';
  292. }
  293. // add the id to the UL tag here instead of the JavaScript
  294. // otherwise it could be added to the <div> tag instead...
  295. $pos = strpos($menu, '>');
  296. $menu = str_replace('class="menu', 'class="menu clear-block', substr($menu, 0, $pos))
  297. . ' id="simplemenu"' . substr($menu, $pos);
  298. variable_set('simplemenu_running', FALSE);
  299. return '<div class="simplemenu-block">' . $menu . '&nbsp;</div>';
  300. }
  301. /**
  302. * At this point (May 31, 2010) the menu tree includes
  303. * many 'below' that should be considered empty but
  304. * aren't... unless we make sure we remove the children
  305. * ourselves.
  306. */
  307. function simplemenu_tree_remove_hidden($tree) {
  308. $clean = array();
  309. foreach ($tree as $key => $data) {
  310. if (!$data['link']['hidden']) {
  311. if ($data['below']) {
  312. $data['below'] = simplemenu_tree_remove_hidden($data['below']);
  313. if (count($data['below']) == 0) {
  314. $data['below'] = 0;
  315. }
  316. }
  317. $clean[] = $data;
  318. }
  319. }
  320. return $clean;
  321. }
  322. /**
  323. * Custom implementation of menu_tree().
  324. * We want to retrieve the entire menu structure for a given menu,
  325. * regardless of whether or not the menu item is expanded or not.
  326. */
  327. function simplemenu_menu_tree($menu_name = 'management:0') {
  328. static $menu_tree = array();
  329. if (!isset($menu_output[$menu_name])) {
  330. //$menu_tree[$menu_name] = simplemenu_tree_all_data($menu_name);
  331. $menu_tree[$menu_name] = menu_tree_all_data('management');
  332. }
  333. return $menu_tree[$menu_name];
  334. }
  335. /**
  336. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  337. * (which can be *any* menu item, not just the root of a custom menu).
  338. *
  339. * @param $root_menu
  340. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  341. *
  342. * @todo we don't actually need $menu_name, $mlid would be sufficient
  343. */
  344. function simplemenu_tree_all_data($root_menu = 'management:0') {
  345. static $tree = array();
  346. list($menu_name, $mlid) = explode(':', $root_menu);
  347. // Generate the cache ID for Drupal 7.
  348. $cid = 'links:' . $menu_name . ':all-cid:' . $mlid . ':' . $GLOBALS['language']->language . ':0';
  349. if (!isset($tree[$cid])) {
  350. // // If the static variable doesn't have the data, check {cache_menu}.
  351. // $cache = cache_get($cid, 'cache_menu');
  352. // if ($cache && isset($cache->data)) {
  353. // $data = $cache->data;
  354. // }
  355. // else {
  356. // // Build and run the query, and build the tree.
  357. // $where = '';
  358. // $args = array(':menu_name' => $menu_name);
  359. // if ($mlid > 0) {
  360. // $item = menu_link_load($mlid);
  361. // if ($item) {
  362. // // The tree is a subtree of $menu_name, so we need to restrict the query to
  363. // // this subtree.
  364. // $px = "p" . (int) $item['depth'];
  365. // $where = " AND ml.$px = :ml AND ml.mlid != :mlid";
  366. // $args[':ml'] = $item[$px];
  367. // $args[':mlid'] = $mlid;
  368. // }
  369. // }
  370. // // Select the links from the table, and recursively build the tree. We
  371. // // LEFT JOIN since there is no match in {menu_router} for an external
  372. // // link.
  373. // $result = db_query("SELECT m.load_functions, m.to_arg_functions, m.access_callback,
  374. // m.access_arguments, m.page_callback, m.page_arguments,
  375. // m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  376. // FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  377. // WHERE ml.menu_name = :menu_name$where
  378. // ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args);
  379. // $data['tree'] = menu_tree_data($result->fetchAssoc());
  380. // $data['node_links'] = array();
  381. // menu_tree_collect_node_links($data['tree'], $data['node_links']);
  382. // // Cache the data.
  383. // cache_set($cid, $data, 'cache_menu');
  384. // }
  385. // // Check access for the current user to each item in the tree.
  386. // menu_tree_check_access($data['tree'], $data['node_links']);
  387. // $tree[$cid] = $data['tree'];
  388. // If the static variable doesn't have the data, check {cache_menu}.
  389. $cache = cache_get($cid, 'cache_menu');
  390. if ($cache && isset($cache->data)) {
  391. // If the cache entry exists, it will just be the cid for the actual data.
  392. // This avoids duplication of large amounts of data.
  393. $cache = cache_get($cache->data, 'cache_menu');
  394. if ($cache && isset($cache->data)) {
  395. $data = $cache->data;
  396. }
  397. }
  398. // If the tree data was not in the cache, $data will be NULL.
  399. if (!isset($data)) {
  400. // Build the query using a LEFT JOIN since there is no match in
  401. // {menu_router} for an external link.
  402. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  403. $query->addTag('translatable');
  404. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  405. $query->fields('ml');
  406. $query->fields('m', array(
  407. 'load_functions',
  408. 'to_arg_functions',
  409. 'access_callback',
  410. 'access_arguments',
  411. 'page_callback',
  412. 'page_arguments',
  413. 'delivery_callback',
  414. 'title',
  415. 'title_callback',
  416. 'title_arguments',
  417. 'theme_callback',
  418. 'theme_arguments',
  419. 'type',
  420. 'description',
  421. ));
  422. for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
  423. $query->orderBy('p' . $i, 'ASC');
  424. }
  425. $query->condition('ml.menu_name', $menu_name);
  426. if (isset($max_depth)) {
  427. $query->condition('ml.depth', $max_depth, '<=');
  428. }
  429. if ($mlid) {
  430. // The tree is for a single item, so we need to match the values in its
  431. // p columns and 0 (the top level) with the plid values of other links.
  432. $args = array(0);
  433. for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
  434. $args[] = $link["p$i"];
  435. }
  436. $args = array_unique($args);
  437. $query->condition('ml.plid', $args, 'IN');
  438. $parents = $args;
  439. $parents[] = $link['mlid'];
  440. }
  441. else {
  442. // Get all links in this menu.
  443. $parents = array();
  444. }
  445. // Select the links from the table, and build an ordered array of links
  446. // using the query result object.
  447. $links = array();
  448. foreach ($query->execute() as $item) {
  449. $links[] = $item;
  450. }
  451. $data['tree'] = menu_tree_data($links, $parents);
  452. $data['node_links'] = array();
  453. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  454. // Cache the data, if it is not already in the cache.
  455. $tree_cid = _menu_tree_cid($menu_name, $data);
  456. if (!cache_get($tree_cid, 'cache_menu')) {
  457. cache_set($tree_cid, $data, 'cache_menu');
  458. }
  459. // Cache the cid of the (shared) data using the menu and item-specific cid.
  460. cache_set($cid, $tree_cid, 'cache_menu');
  461. }
  462. // Check access for the current user to each item in the tree.
  463. menu_tree_check_access($data['tree'], $data['node_links']);
  464. $tree[$cid] = $data['tree'];
  465. }
  466. return $tree[$cid];
  467. }
  468. /**
  469. * Determine if simplemenu should be displayed based on visibility settings.
  470. *
  471. * @return boolean
  472. */
  473. function _simplemenu_page_visibility() {
  474. $operator = variable_get('simplemenu_visibility_operator', 0);
  475. $pages = variable_get('simplemenu_visibility_pages', '');
  476. if ($pages) {
  477. $path = drupal_get_path_alias($_GET['q']);
  478. // Compare with the internal and path alias (if any).
  479. $page_match = drupal_match_path($path, $pages);
  480. if ($path != $_GET['q']) {
  481. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  482. }
  483. // When $operator has a value of 0, the menu is displayed on
  484. // all pages except those listed in $pages. When set to 1, it
  485. // is displayed only on those pages listed in $pages.
  486. $page_match = !($operator ^ $page_match);
  487. }
  488. else {
  489. $page_match = TRUE;
  490. }
  491. return $page_match;
  492. }
  493. /**
  494. * Check whether the superuser/admin should be shown simplemenu.
  495. */
  496. function _simplemenu_superuser_active() {
  497. global $user;
  498. return $user->uid != 1 || variable_get('simplemenu_uid1', 1) == 1;
  499. }
  500. // vim: ts=2 sw=2 et syntax=php