menu_clone.module 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Implements hook_help().
  4. */
  5. function menu_clone_help($path, $arg) {
  6. $output = '';
  7. switch ($path) {
  8. case 'admin/help#menu_clone':
  9. $output = '<p>'. t('This module provides you with the ability to clone complete menus, much like <a href="@node_clone" rel="external">Node Clone</a> does with nodes. The module adds a <em>Clone menu</em> tab to every <a href="@detail_page">detail page</a> of a menu. Clicking on the <em>Clone menu</em> tab brings you to the <a href="@menu_clone_detail">menu clone page</a>, displaying all settings needed to properly create a clone of this menu.', array('@detail_page' => url('admin/structure/menu/manage/navigation'), '@node_clone' => 'http://drupal.org/project/node_clone', '@menu_clone_detail' => url('admin/structure/menu/manage/navigation/clone'))) .'</p>';
  10. $output .= '<p>'. t('The optional <em><a href="@i18n" rel="external">Internationalization</a></em> (i18n) module together with the i18n submodule <em>Menu translation</em> will activate additional Language options. The <em>multilingual options</em> of the cloned menu will be set to <em>Translate and Localize</em>.', array('@i18n' => 'http://drupal.org/project/i18n')) .'</p>';
  11. $output .= '<p>'. t('The module provides inline help topics. For further info, read the help on the <a href="@menu_clone">menu clone page</a>.', array('@menu_clone' => url('admin/structure/menu/manage/navigation/clone'))) .'</p>';
  12. break;
  13. case 'admin/structure/menu/manage/%/clone':
  14. $output = '<p>'. t('Next to the basic menu properties such as <em>Menu name</em>, <em>Title</em> and <em>Description</em>, this form allows you to customise the menu itself before cloning it.') .'</p>';
  15. $output .= '<p>';
  16. $output .= t('The optional <em><a href="@i18n" rel="external">Internationalization</a></em> (i18n) module together with the i18n submodule <em>Menu translation</em> will activate the <em>Change language</em> option. This select box allows you to change the language of <em>all</em> menu items during the cloning process. The <em>multilingual options</em> of the cloned menu will be set to <em>Translate and Localize</em>.', array('@i18n' => 'http://drupal.org/project/i18n'));
  17. if (module_exists('i18n') && !module_exists('i18n_menu')) {
  18. $output .= '<br />'. t('<strong>NOTE</strong>: You <em>do</em> have the <em><a href="@i18n" rel="external">Internationalization</a></em> module installed, but the <em>Menu translation</em> submodule is not enabled. You can enable it on the <a href="@modules">modules page</a>.', array('@i18n' => 'http://drupal.org/project/i18n', '@modules' => url('admin/modules')));
  19. }
  20. else if(!module_exists('i18n')) {
  21. $output .= '<br />'. t('<strong>NOTE</strong>: You <em>do not</em> have the <em><a href="@i18n" rel="external">Internationalization</a></em> module installed or enabled, you can download it from the <a href="@i18n" rel="external">project page</a> and enable it on the <a href="@modules">modules page</a>.', array('@i18n' => 'http://drupal.org/project/i18n', '@modules' => url('admin/modules')));
  22. }
  23. $output .= '</p>';
  24. $output .= '<p>'. t('In <em>Edit Menu</em> you can customise the menu tree before cloning it. Next to the <em>Enabled</em> and <em>Expanded</em> checkboxes, the menu tree can be rearranged as well. To rearrange menu items, grab a drag-and-drop handle under the <em>Menu item</em> column and drag the items (or group of items) to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.<br />Everything under the <em>Ignore</em> row will <em>not</em> be copied over to the new menu. It does not matter on what depth the <em>Ignore</em> row resides, <em>everything</em> below it will simply be ignored.') .'</p>';
  25. break;
  26. }
  27. return $output;
  28. }
  29. /**
  30. * Implements hook_permission().
  31. */
  32. function menu_clone_permission() {
  33. return array(
  34. 'access menu clone' => array(
  35. 'title' => t('Access menu clone'),
  36. 'description' => t('Grants access to the "clone menu" tab.'),
  37. ),
  38. );
  39. }
  40. /**
  41. * Implements hook_menu().
  42. */
  43. function menu_clone_menu() {
  44. $items = array();
  45. $items['admin/structure/menu/manage/%menu_clone/clone'] = array(
  46. 'title' => 'Clone menu',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('menu_clone_clone_form', 4),
  49. 'access arguments' => array('access menu clone'),
  50. 'type' => MENU_LOCAL_TASK,
  51. 'file' => 'includes/menu_clone.admin.inc',
  52. 'weight' => 20,
  53. );
  54. return $items;
  55. }
  56. /**
  57. * Implements hook_theme().
  58. */
  59. function menu_clone_theme() {
  60. return array(
  61. 'menu_clone_clone_form' => array(
  62. 'render element' => 'form',
  63. 'file' => 'includes/menu_clone.admin.inc',
  64. ),
  65. );
  66. }
  67. /**
  68. * Implements hook_load().
  69. * We create a new menu name for the menu being cloned and check if this name is
  70. * unique. If not unique, add a number to the 'clone-' prefix and retry.
  71. * Next we get the full menu tree data, using part of the menu_overview_form()
  72. * function from the Drupal core Menu module.
  73. *
  74. * @param menu_name machine-readable name of menu being cloned
  75. * @return an associative array containing all the menu info
  76. * @see menu_clone_clone_form()
  77. * @see Drupal core menu.admin.inc menu_tree_data()
  78. *
  79. * TODO: Check why this one is also seems to be executed on
  80. * admin/structure/menu/manage/<menu_name> (without the trailing /clone)
  81. */
  82. function menu_clone_load($menu_name) {
  83. global $menu_admin;
  84. // Get basic menu data.
  85. // SELECT * FROM {menu_custom} WHERE menu_name = '%s'
  86. $menu = db_select('menu_custom', 'm')
  87. ->fields('m')
  88. ->condition('menu_name', $menu_name, '=')
  89. ->execute()
  90. ->fetchAssoc();
  91. // No menu? Bail out triggering a 404.
  92. if (empty($menu)) {
  93. return FALSE;
  94. }
  95. // Create a unique name
  96. // Remove 'menu-' prefix if there is any.
  97. if (strpos($menu['menu_name'], 'menu-') === 0) {
  98. $menu['menu_name'] = drupal_substr($menu['menu_name'], 5);
  99. }
  100. $new_name = drupal_substr('clone-' . $menu['menu_name'], 0, MENU_MAX_MENU_NAME_LENGTH_UI);
  101. $sql = "SELECT menu_name FROM {menu_custom} WHERE menu_name = ':name'";
  102. $result = db_query($sql, array(':name' => 'menu-' . $new_name))->fetchField();
  103. $i = 1;
  104. while (!empty($result)) {
  105. $new_name = drupal_substr('clone' . $i . '-' . $menu['menu_name'], 0, MENU_MAX_MENU_NAME_LENGTH_UI);
  106. $result = db_query($sql, array(':name' => 'menu-' . $new_name))->fetchField();
  107. $i++;
  108. }
  109. // Begin code taken from menu_overview_form().
  110. $sql = "
  111. SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.delivery_callback, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  112. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  113. WHERE ml.menu_name = :menu
  114. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
  115. $result = db_query($sql, array(':menu' => $menu_name), array('fetch' => PDO::FETCH_ASSOC));
  116. $links = array();
  117. foreach ($result as $item) {
  118. $links[] = $item;
  119. }
  120. $tree = menu_tree_data($links);
  121. $node_links = array();
  122. menu_tree_collect_node_links($tree, $node_links);
  123. // We indicate that a menu administrator is running the menu access check.
  124. $menu_admin = TRUE;
  125. menu_tree_check_access($tree, $node_links);
  126. $menu_admin = FALSE;
  127. // End code taken from menu_overview_form().
  128. // Set new unique name.
  129. $menu['menu_name'] = $new_name;
  130. $menu['title'] = t('Clone of') . ' ' . $menu['title'];
  131. $menu['tree'] = $tree;
  132. return $menu;
  133. }