simplemenu.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_permission().
  242. */
  243. function simplemenu_permission() {
  244. return array(
  245. 'view simplemenu' => array(
  246. 'title' => t('See and use the Simplemenu'),
  247. ),
  248. 'administer simplemenu' => array(
  249. 'title' => t('Administer the Simplemenu'),
  250. ),
  251. );
  252. }
  253. /**
  254. * Render an HTML list of links for a given menu.
  255. */
  256. function simplemenu_get_menu() {
  257. variable_set('simplemenu_running', TRUE);
  258. // if a user turned off menu module but SimpleMenu was previously set
  259. // reset variable so a menu appears
  260. $all_menus = array(variable_get('simplemenu_menu', 'management:0'));
  261. drupal_alter('simplemenu_menus', $all_menus);
  262. if (count($all_menus) > 1) {
  263. // if menu is not enable then we cannot have a count other than 1
  264. $menu_titles = menu_get_menus();
  265. $tree = array();
  266. foreach ($all_menus as $full_name) {
  267. list($menu_name, $mlid) = explode(':', $full_name);
  268. $tree[] = array(
  269. 'link' => array(
  270. 'simplemenu_multi_menu_root' => TRUE,
  271. 'mlid' => $mlid,
  272. 'menu_name' => $full_name,
  273. 'hidden' => FALSE,
  274. 'title' => $menu_titles[$menu_name],
  275. 'href' => 'admin/settings/simplemenu', /// ??? -- we should not have a link here
  276. 'in_active_trail' => FALSE,
  277. 'has_children' => TRUE,
  278. 'localized_options' => array(
  279. 'attributes' => array('class' => 'simplemenu-top-level'),
  280. ),
  281. ),
  282. 'below' => simplemenu_menu_tree($full_name),
  283. );
  284. }
  285. }
  286. else {
  287. reset($all_menus);
  288. $tree = simplemenu_menu_tree(current($all_menus));
  289. }
  290. // allow other modules to modify the menu tree
  291. drupal_alter('simplemenu_tree', $tree);
  292. $tree = simplemenu_tree_remove_hidden($tree);
  293. // now generate the output
  294. $menu_form = menu_tree_output($tree);
  295. $menu = drupal_render($menu_form);
  296. if (!$menu) {
  297. $menu = '<ul class="menu"><li><a href="' . url('admin/settings/simplemenu') . '">'
  298. . t('No menu items found. Try a different menu as the default.') . '</a></li></ul>';
  299. }
  300. // add the id to the UL tag here instead of the JavaScript
  301. // otherwise it could be added to the <div> tag instead...
  302. $pos = strpos($menu, '>');
  303. $menu = str_replace('class="menu', 'class="menu clear-block', substr($menu, 0, $pos))
  304. . ' id="simplemenu"' . substr($menu, $pos);
  305. variable_set('simplemenu_running', FALSE);
  306. return '<div class="simplemenu-block">' . $menu . '&nbsp;</div>';
  307. }
  308. /**
  309. * At this point (May 31, 2010) the menu tree includes
  310. * many 'below' that should be considered empty but
  311. * aren't... unless we make sure we remove the children
  312. * ourselves.
  313. */
  314. function simplemenu_tree_remove_hidden($tree) {
  315. $clean = array();
  316. foreach ($tree as $key => $data) {
  317. if (!$data['link']['hidden']) {
  318. if ($data['below']) {
  319. $data['below'] = simplemenu_tree_remove_hidden($data['below']);
  320. if (count($data['below']) == 0) {
  321. $data['below'] = 0;
  322. }
  323. }
  324. $clean[] = $data;
  325. }
  326. }
  327. return $clean;
  328. }
  329. /**
  330. * Custom implementation of menu_tree().
  331. * We want to retrieve the entire menu structure for a given menu,
  332. * regardless of whether or not the menu item is expanded or not.
  333. */
  334. function simplemenu_menu_tree($menu_name = 'management:0') {
  335. static $menu_tree = array();
  336. if (!isset($menu_output[$menu_name])) {
  337. //$menu_tree[$menu_name] = simplemenu_tree_all_data($menu_name);
  338. $menu_tree[$menu_name] = menu_tree_all_data('management');
  339. }
  340. return $menu_tree[$menu_name];
  341. }
  342. /**
  343. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  344. * (which can be *any* menu item, not just the root of a custom menu).
  345. *
  346. * @param $root_menu
  347. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  348. *
  349. * @todo we don't actually need $menu_name, $mlid would be sufficient
  350. */
  351. function simplemenu_tree_all_data($root_menu = 'management:0') {
  352. static $tree = array();
  353. list($menu_name, $mlid) = explode(':', $root_menu);
  354. // Generate the cache ID for Drupal 7.
  355. $cid = 'links:' . $menu_name . ':all-cid:' . $mlid . ':' . $GLOBALS['language']->language . ':0';
  356. if (!isset($tree[$cid])) {
  357. // // If the static variable doesn't have the data, check {cache_menu}.
  358. // $cache = cache_get($cid, 'cache_menu');
  359. // if ($cache && isset($cache->data)) {
  360. // $data = $cache->data;
  361. // }
  362. // else {
  363. // // Build and run the query, and build the tree.
  364. // $where = '';
  365. // $args = array(':menu_name' => $menu_name);
  366. // if ($mlid > 0) {
  367. // $item = menu_link_load($mlid);
  368. // if ($item) {
  369. // // The tree is a subtree of $menu_name, so we need to restrict the query to
  370. // // this subtree.
  371. // $px = "p" . (int) $item['depth'];
  372. // $where = " AND ml.$px = :ml AND ml.mlid != :mlid";
  373. // $args[':ml'] = $item[$px];
  374. // $args[':mlid'] = $mlid;
  375. // }
  376. // }
  377. // // Select the links from the table, and recursively build the tree. We
  378. // // LEFT JOIN since there is no match in {menu_router} for an external
  379. // // link.
  380. // $result = db_query("SELECT m.load_functions, m.to_arg_functions, m.access_callback,
  381. // m.access_arguments, m.page_callback, m.page_arguments,
  382. // m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  383. // FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  384. // WHERE ml.menu_name = :menu_name$where
  385. // ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args);
  386. // $data['tree'] = menu_tree_data($result->fetchAssoc());
  387. // $data['node_links'] = array();
  388. // menu_tree_collect_node_links($data['tree'], $data['node_links']);
  389. // // Cache the data.
  390. // cache_set($cid, $data, 'cache_menu');
  391. // }
  392. // // Check access for the current user to each item in the tree.
  393. // menu_tree_check_access($data['tree'], $data['node_links']);
  394. // $tree[$cid] = $data['tree'];
  395. // If the static variable doesn't have the data, check {cache_menu}.
  396. $cache = cache_get($cid, 'cache_menu');
  397. if ($cache && isset($cache->data)) {
  398. // If the cache entry exists, it will just be the cid for the actual data.
  399. // This avoids duplication of large amounts of data.
  400. $cache = cache_get($cache->data, 'cache_menu');
  401. if ($cache && isset($cache->data)) {
  402. $data = $cache->data;
  403. }
  404. }
  405. // If the tree data was not in the cache, $data will be NULL.
  406. if (!isset($data)) {
  407. // Build the query using a LEFT JOIN since there is no match in
  408. // {menu_router} for an external link.
  409. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  410. $query->addTag('translatable');
  411. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  412. $query->fields('ml');
  413. $query->fields('m', array(
  414. 'load_functions',
  415. 'to_arg_functions',
  416. 'access_callback',
  417. 'access_arguments',
  418. 'page_callback',
  419. 'page_arguments',
  420. 'delivery_callback',
  421. 'title',
  422. 'title_callback',
  423. 'title_arguments',
  424. 'theme_callback',
  425. 'theme_arguments',
  426. 'type',
  427. 'description',
  428. ));
  429. for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
  430. $query->orderBy('p' . $i, 'ASC');
  431. }
  432. $query->condition('ml.menu_name', $menu_name);
  433. if (isset($max_depth)) {
  434. $query->condition('ml.depth', $max_depth, '<=');
  435. }
  436. if ($mlid) {
  437. // The tree is for a single item, so we need to match the values in its
  438. // p columns and 0 (the top level) with the plid values of other links.
  439. $args = array(0);
  440. for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
  441. $args[] = $link["p$i"];
  442. }
  443. $args = array_unique($args);
  444. $query->condition('ml.plid', $args, 'IN');
  445. $parents = $args;
  446. $parents[] = $link['mlid'];
  447. }
  448. else {
  449. // Get all links in this menu.
  450. $parents = array();
  451. }
  452. // Select the links from the table, and build an ordered array of links
  453. // using the query result object.
  454. $links = array();
  455. foreach ($query->execute() as $item) {
  456. $links[] = $item;
  457. }
  458. $data['tree'] = menu_tree_data($links, $parents);
  459. $data['node_links'] = array();
  460. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  461. // Cache the data, if it is not already in the cache.
  462. $tree_cid = _menu_tree_cid($menu_name, $data);
  463. if (!cache_get($tree_cid, 'cache_menu')) {
  464. cache_set($tree_cid, $data, 'cache_menu');
  465. }
  466. // Cache the cid of the (shared) data using the menu and item-specific cid.
  467. cache_set($cid, $tree_cid, 'cache_menu');
  468. }
  469. // Check access for the current user to each item in the tree.
  470. menu_tree_check_access($data['tree'], $data['node_links']);
  471. $tree[$cid] = $data['tree'];
  472. }
  473. return $tree[$cid];
  474. }
  475. /**
  476. * Determine if simplemenu should be displayed based on visibility settings.
  477. *
  478. * @return boolean
  479. */
  480. function _simplemenu_page_visibility() {
  481. $operator = variable_get('simplemenu_visibility_operator', 0);
  482. $pages = variable_get('simplemenu_visibility_pages', '');
  483. if ($pages) {
  484. $path = drupal_get_path_alias($_GET['q']);
  485. // Compare with the internal and path alias (if any).
  486. $page_match = drupal_match_path($path, $pages);
  487. if ($path != $_GET['q']) {
  488. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  489. }
  490. // When $operator has a value of 0, the menu is displayed on
  491. // all pages except those listed in $pages. When set to 1, it
  492. // is displayed only on those pages listed in $pages.
  493. $page_match = !($operator ^ $page_match);
  494. }
  495. else {
  496. $page_match = TRUE;
  497. }
  498. return $page_match;
  499. }
  500. /**
  501. * Check whether the superuser/admin should be shown simplemenu.
  502. */
  503. function _simplemenu_superuser_active() {
  504. global $user;
  505. return $user->uid != 1 || variable_get('simplemenu_uid1', 1) == 1;
  506. }
  507. // vim: ts=2 sw=2 et syntax=php