editmenu.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /**
  3. * @file
  4. * Creates a editmenu.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function editmenu_menu() {
  10. $items = array();
  11. $items['admin/config/user-interface/editmenu'] = array(
  12. 'title' => 'EditMenu',
  13. 'description' => 'Select the menu to display.',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('editmenu_admin_settings'),
  16. 'access arguments' => array('administer editmenu'),
  17. 'file' => 'editmenu.admin.inc',
  18. 'type' => MENU_NORMAL_ITEM,
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Is editmenu enabled for this page request?
  24. */
  25. function editmenu_enabled() {
  26. $enabled = &drupal_static(__FUNCTION__);
  27. if (!isset($enabled)) {
  28. global $theme;
  29. $is_overlay = FALSE;
  30. if (function_exists('overlay_get_mode')) {
  31. $is_overlay = (overlay_get_mode() == 'child') ? TRUE : FALSE;
  32. }
  33. $exclusions = variable_get('editmenu_exclusions', array());
  34. $enabled = (!isset($exclusions[$theme]) || !$exclusions[$theme])
  35. && user_access('view editmenu')
  36. && _editmenu_page_visibility()
  37. && _editmenu_superuser_active()
  38. && !$is_overlay;
  39. }
  40. return $enabled;
  41. }
  42. /**
  43. * Implements hook_init().
  44. */
  45. function editmenu_init() {
  46. // do a simple access check here, since theme isn't available to check yet
  47. if (editmenu_enabled()) {
  48. _editmenu_add_menu();
  49. _editmenu_add_css(); // basic CSS must be before _editmenu_add_theme()
  50. _editmenu_add_theme();
  51. _editmenu_add_js();
  52. }
  53. }
  54. /** \brief Add the editmenu variable with the menu to be displayed.
  55. *
  56. * This function loads the menu to be displayed and transforms it so
  57. * it works with superfish.
  58. *
  59. * If the cache version of the editmenu JavaScript string cannot be
  60. * created, then it is sent inline whether or not the user asked for it
  61. * to be sent inline.
  62. */
  63. function _editmenu_add_menu() {
  64. // XXX -- should we put that in the settings instead? why put it in its own variable?
  65. $editmenu = 'var editmenu=' . drupal_json_encode(editmenu_get_menu()) . ';';
  66. $has_file = variable_get('editmenu_cache_menu', TRUE);
  67. if ($has_file) {
  68. $js_hash = drupal_hash_base64($editmenu);
  69. $js_path = 'public://js'; // same path as concatenated Core JS
  70. $js_filename = $js_path . '/editmenu_' . $js_hash . '.js';
  71. if (!file_exists($js_filename)) {
  72. file_prepare_directory($js_path, FILE_CREATE_DIRECTORY);
  73. if (!file_unmanaged_save_data($editmenu, $js_filename, FILE_EXISTS_REPLACE)) {
  74. $has_file = FALSE;
  75. }
  76. }
  77. }
  78. $options = array(
  79. 'scope' => variable_get('editmenu_menu_scope', 'footer'),
  80. // 'version' => ?, -- could we make use of the version?
  81. );
  82. if ($has_file) {
  83. //$options['type'] = 'file'; -- default
  84. drupal_add_js($js_filename, $options);
  85. }
  86. else {
  87. // inline adds the value as is (untouched)
  88. $options['type'] = 'inline';
  89. drupal_add_js($editmenu, $options);
  90. }
  91. }
  92. /** \brief Generate the CSS and add it to the page.
  93. *
  94. * This function generates the dynamic CSS and then insert that to
  95. * the header of the page.
  96. *
  97. * The function regenerates the CSS only when the settings were
  98. * modified. Otherwise, it uses the cached version.
  99. *
  100. * The function has a fall back, in case the dynamic CSS cannot
  101. * be created.
  102. */
  103. function _editmenu_add_css() {
  104. global $user;
  105. $editmenu_path = drupal_get_path('module', 'editmenu');
  106. $css_path = 'public://css'; // same path as concatenated Core CSS
  107. if (file_prepare_directory($css_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  108. $fix = variable_get('editmenu_fix', 'scroll');
  109. // XXX add a variable editmenu_update which is set to TRUE whenever
  110. // the settings get modified and false here
  111. $output_filename = variable_get('editmenu_css_filename', '');
  112. if (!$output_filename) {
  113. $tags = array(
  114. '@MENUBAR_ZINDEX@' => simplemnu_get_zindex('editmenu_menubar_zindex', 9999),
  115. '@DROPDOWN_ZINDEX@' => simplemnu_get_zindex('editmenu_dropdown_zindex', 9999),
  116. );
  117. switch ($fix) {
  118. case 'top':
  119. $tags['@FIX@'] = "position: fixed;\n top: 0; left: 0;";
  120. break;
  121. case 'bottom':
  122. $tags['@FIX@'] = "position: fixed;\n bottom: 0; left: 0;";
  123. break;
  124. default: // scroll
  125. $tags['@FIX@'] = 'position: relative;';
  126. break;
  127. }
  128. $css = file_get_contents($editmenu_path . '/editmenu.css.tpl');
  129. $css = strtr($css, $tags);
  130. $css_hash = hash('sha256', $css);
  131. $output_filename = $css_path . '/editmenu-' . $css_hash . '.css';
  132. if (!file_exists($output_filename)) {
  133. // new content, create a new file
  134. file_put_contents($output_filename, $css);
  135. }
  136. else {
  137. // this call is rather ugly, but we must make sure that the
  138. // system cache will take the current Editmenu CSS in account
  139. _drupal_flush_css_js();
  140. }
  141. //variable_set('editmenu_css_filename', $output_filename);
  142. }
  143. drupal_add_css($output_filename);
  144. }
  145. else {
  146. // in case we cannot create the dynamic CSS
  147. $last_msg = variable_get('editmenu_css_error', 0);
  148. if (($last_msg != -1 && $last_msg + 3600 > time()) || $user->uid == 1) {
  149. // avoid displaying the error on each page... only once per hour.
  150. // (unless you are the admin, in which case you probably want to know!)
  151. variable_set('editmenu_css_error', time());
  152. drupal_set_message(t('Editmenu could not create the folder @path in order to save the dynamic CSS data.',
  153. array('@path' => $css_path)), 'warning');
  154. }
  155. // use a default that cannot react to the dynamic changes...
  156. drupal_add_css($editmenu_path .'/editmenu.css');
  157. }
  158. }
  159. /** \brief Add the module theme.
  160. *
  161. * This function adds a theme for the Editmenu look.
  162. *
  163. * By default, the original theme is used. The module also offers the
  164. * blackblue theme. It is also possible to create new themes or use
  165. * the theming of the current theme for editmenu (so the menu fits
  166. * perfectly for that theme.)
  167. */
  168. function _editmenu_add_theme() {
  169. // we want to put the editmenu theme CSS first
  170. // so we can change some CSS entries dynamically
  171. // but at this time the editmenu.css is used to
  172. // reset many of the CSS entries... Hmmm...
  173. $editmenu_theme = variable_get('editmenu_theme', 'original');
  174. if ($editmenu_theme != 'custom') {
  175. $editmenu_path = drupal_get_path('module', 'editmenu');
  176. $theme_file = $editmenu_path . '/themes/' . $editmenu_theme
  177. . '/' . $editmenu_theme . '.css';
  178. if (is_file($theme_file)) {
  179. drupal_add_css($theme_file);
  180. }
  181. }
  182. }
  183. /** \brief Add the JavaScript that makes it all work.
  184. *
  185. * This function adds the Editmenu JavaScript, the Superfish JavaScript
  186. * and settings from the user.
  187. */
  188. function _editmenu_add_js() {
  189. $editmenu_path = drupal_get_path('module', 'editmenu');
  190. // Settings
  191. $fix = variable_get('editmenu_fix', 'scroll');
  192. switch ($fix) {
  193. case 'top':
  194. $element = 'body';
  195. $placement = 'prepend';
  196. break;
  197. case 'bottom':
  198. $element = 'body';
  199. $placement = 'append';
  200. break;
  201. default: // 'scroll'
  202. // let user defined other elements when not fixed
  203. $element = variable_get('editmenu_element', 'body');
  204. $placement = variable_get('editmenu_element_method', 'prepend');
  205. break;
  206. }
  207. $settings = array(
  208. 'effect' => variable_get('editmenu_effect', 'opacity'),
  209. 'effectSpeed' => variable_get('editmenu_effect_speed', 'fast'),
  210. 'element' => $element,
  211. 'placement' => $placement,
  212. 'hideDelay' => variable_get('editmenu_hide_delay', 800),
  213. 'detectPopup' => variable_get('editmenu_detect_popup', 1),
  214. );
  215. drupal_add_js(array('editmenu' => $settings), array('type' => 'setting'));
  216. // Editmenu
  217. drupal_add_js($editmenu_path . '/editmenu.js', array('version' => '1.2'));
  218. // Superfish
  219. $superfish = variable_get('editmenu_superfish_version', 'superfish-1.4.1.js');
  220. if ($superfish != 'custom') {
  221. $sf_version = str_replace(array('superfish-', '.js'), '', $superfish);
  222. drupal_add_js($editmenu_path . '/' . $superfish, array('version' => $sf_version));
  223. }
  224. }
  225. /**
  226. * \brief Retrieve the zindex for the CSS files.
  227. *
  228. * This function retrieves a z-index from a Drupal variable and
  229. * transform it to fit in a CSS file.
  230. *
  231. * \param[in] $name The name of the z-index variable to read.
  232. * \param[in] $default The default value to use when the variable is not defined.
  233. *
  234. * \return A string representing the current value of the specified z-index.
  235. */
  236. function simplemnu_get_zindex($name, $default) {
  237. $zindex = variable_get($name, $default);
  238. if ($zindex == -1) {
  239. $zindex = '';
  240. }
  241. else {
  242. $zindex = 'z-index: ' . $zindex . ';';
  243. }
  244. return $zindex;
  245. }
  246. /**
  247. * Implements hook_permission().
  248. */
  249. function editmenu_permission() {
  250. return array(
  251. 'view editmenu' => array(
  252. 'title' => t('View EditMenu'),
  253. 'description' => t('Display EditMenu to this user.'),
  254. ),
  255. 'administer editmenu' => array(
  256. 'title' => t('Administer EditMenu'),
  257. 'description' => t('Change settings of EditMenu.'),
  258. ),
  259. );
  260. }
  261. /**
  262. * Render an HTML list of links for a given menu.
  263. */
  264. function editmenu_get_menu() {
  265. variable_set('editmenu_running', TRUE);
  266. // if a user turned off menu module but EditMenu was previously set
  267. // reset variable so a menu appears
  268. $all_menus = array(variable_get('editmenu_menu', 'management:0'));
  269. drupal_alter('editmenu_menus', $all_menus);
  270. if (count($all_menus) > 1) {
  271. // if menu is not enable then we cannot have a count other than 1
  272. $menu_titles = menu_get_menus();
  273. $tree = array();
  274. foreach ($all_menus as $full_name) {
  275. list($menu_name, $mlid) = explode(':', $full_name);
  276. $tree[] = array(
  277. 'link' => array(
  278. 'editmenu_multi_menu_root' => TRUE,
  279. 'mlid' => $mlid,
  280. 'menu_name' => $full_name,
  281. 'hidden' => FALSE,
  282. 'title' => $menu_titles[$menu_name],
  283. 'href' => 'admin/settings/editmenu', /// ??? -- we should not have a link here
  284. 'in_active_trail' => FALSE,
  285. 'has_children' => TRUE,
  286. 'localized_options' => array(
  287. 'attributes' => array('class' => 'editmenu-top-level'),
  288. ),
  289. ),
  290. 'below' => editmenu_menu_tree($full_name),
  291. );
  292. }
  293. }
  294. else {
  295. reset($all_menus);
  296. $tree = editmenu_menu_tree(current($all_menus));
  297. }
  298. // allow other modules to modify the menu tree
  299. drupal_alter('editmenu_tree', $tree);
  300. $tree = editmenu_tree_remove_hidden($tree);
  301. // now generate the output
  302. $menu_form = menu_tree_output($tree);
  303. $menu = drupal_render($menu_form);
  304. if (!$menu) {
  305. $menu = '<ul class="menu"><li><a href="' . url('admin/settings/editmenu') . '">'
  306. . t('No menu items found. Try a different menu as the default.') . '</a></li></ul>';
  307. }
  308. // add the id to the UL tag here instead of the JavaScript
  309. // otherwise it could be added to the <div> tag instead...
  310. $pos = strpos($menu, '>');
  311. $menu = str_replace('class="menu', 'class="menu clear-block', substr($menu, 0, $pos))
  312. . ' id="editmenu"' . substr($menu, $pos);
  313. variable_set('editmenu_running', FALSE);
  314. return '<div class="editmenu-block">' . $menu . '&nbsp;</div>';
  315. }
  316. /**
  317. * At this point (May 31, 2010) the menu tree includes
  318. * many 'below' that should be considered empty but
  319. * aren't... unless we make sure we remove the children
  320. * ourselves.
  321. */
  322. function editmenu_tree_remove_hidden($tree) {
  323. $clean = array();
  324. foreach ($tree as $key => $data) {
  325. if (!$data['link']['hidden']) {
  326. if ($data['below']) {
  327. $data['below'] = editmenu_tree_remove_hidden($data['below']);
  328. if (count($data['below']) == 0) {
  329. $data['below'] = 0;
  330. }
  331. }
  332. $clean[] = $data;
  333. }
  334. }
  335. return $clean;
  336. }
  337. /**
  338. * Custom implementation of menu_tree().
  339. * We want to retrieve the entire menu structure for a given menu,
  340. * regardless of whether or not the menu item is expanded or not.
  341. */
  342. function editmenu_menu_tree($menu_name = 'management:0') {
  343. $menu_tree = &drupal_static(__FUNCTION__, array());
  344. // until we take $mlid in account, we can use $mname
  345. // for the rest of the function
  346. list($mname, $mlid) = explode(':', $menu_name);
  347. if (!isset($menu_tree[$mname])) {
  348. $menu_tree[$mname] = menu_tree_all_data($mname);
  349. }
  350. return $menu_tree[$mname];
  351. }
  352. /**
  353. * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
  354. * (which can be *any* menu item, not just the root of a custom menu).
  355. *
  356. * @param $root_menu
  357. * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
  358. *
  359. * @todo we don't actually need $menu_name, $mlid would be sufficient
  360. */
  361. function editmenu_tree_all_data($root_menu = 'management:0') {
  362. $tree = &drupal_static(__FUNCTION__, array());
  363. list($menu_name, $mlid) = explode(':', $root_menu);
  364. // Generate the cache ID for Drupal 7.
  365. $max_depth = NULL;
  366. $cid = 'links:' . $menu_name . ':all:' . $mlid . ':' . $GLOBALS['language']->language . ':' . (int) $max_depth;
  367. if (!isset($tree[$cid])) {
  368. $cache = cache_get($cid, 'cache_menu');
  369. if ($cache && isset($cache->data)) {
  370. $data = $cache->data;
  371. }
  372. else {
  373. // Build the query using a LEFT JOIN since there is no match in
  374. // {menu_router} for an external link.
  375. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  376. $query->addTag('translatable');
  377. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  378. $query->fields('ml');
  379. $query->fields('m', array(
  380. 'load_functions',
  381. 'to_arg_functions',
  382. 'access_callback',
  383. 'access_arguments',
  384. 'page_callback',
  385. 'page_arguments',
  386. 'delivery_callback',
  387. 'tab_parent',
  388. 'tab_root',
  389. 'title',
  390. 'title_callback',
  391. 'title_arguments',
  392. 'theme_callback',
  393. 'theme_arguments',
  394. 'type',
  395. 'description',
  396. ));
  397. for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
  398. $query->orderBy('p' . $i, 'ASC');
  399. }
  400. $query->condition('ml.menu_name', $menu_name);
  401. if ($mlid > 0) {
  402. $item = menu_link_load($mlid);
  403. if ($item) {
  404. // The tree is a subtree of $menu_name, so we need to restrict the query to
  405. // this subtree.
  406. $px = "p" . (int) $item['depth'];
  407. $and = db_and()->condition("ml.$px", $item[$px])->condition("ml.mlid", $mlid, '!=');
  408. $query->condition($and);
  409. }
  410. }
  411. // Build an ordered array of links using the query result object.
  412. $links = array();
  413. foreach ($query->execute() as $item) {
  414. $links[] = $item;
  415. }
  416. $data['tree'] = menu_tree_data($links);
  417. if (count($data['tree']) == 1) {
  418. // Move the menu items from below to root
  419. $key = key($data['tree']);
  420. foreach ($data['tree'][$key]['below'] as $id => $item) {
  421. $data['tree'][$id] = $item;
  422. unset($data['tree'][$key]['below'][$id]);
  423. }
  424. }
  425. $data['node_links'] = array();
  426. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  427. menu_tree_check_access($data['tree'], $data['node_links']);
  428. cache_set($cid, $data, 'cache_menu');
  429. }
  430. $tree[$cid] = $data['tree'];
  431. }
  432. return $tree[$cid];
  433. }
  434. /**
  435. * Determine if editmenu should be displayed based on visibility settings.
  436. *
  437. * @return boolean
  438. */
  439. function _editmenu_page_visibility() {
  440. $operator = variable_get('editmenu_visibility_operator', 0);
  441. $pages = variable_get('editmenu_visibility_pages', '');
  442. if ($pages) {
  443. $path = drupal_get_path_alias($_GET['q']);
  444. // Compare with the internal and path alias (if any).
  445. $page_match = drupal_match_path($path, $pages);
  446. if ($path != $_GET['q']) {
  447. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  448. }
  449. // When $operator has a value of 0, the menu is displayed on
  450. // all pages except those listed in $pages. When set to 1, it
  451. // is displayed only on those pages listed in $pages.
  452. $page_match = !($operator ^ $page_match);
  453. }
  454. else {
  455. $page_match = TRUE;
  456. }
  457. return $page_match;
  458. }
  459. /**
  460. * Check whether the superuser/admin should be shown editmenu.
  461. */
  462. function _editmenu_superuser_active() {
  463. global $user;
  464. return $user->uid != 1 || variable_get('editmenu_uid1', 1) == 1;
  465. }