xmlsitemap_menu.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. // $Id: xmlsitemap_menu.module,v 1.7 2010/01/20 03:34:33 davereid Exp $
  3. /**
  4. * Implements hook_cron().
  5. *
  6. * Process old menu links not found in the {xmlsitemap} table.
  7. */
  8. function xmlsitemap_menu_cron() {
  9. xmlsitemap_menu_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
  10. }
  11. /**
  12. * Implements hook_xmlsitemap_index_links().
  13. */
  14. function xmlsitemap_menu_xmlsitemap_index_links($limit) {
  15. if ($menus = xmlsitemap_menu_get_menus()) {
  16. // Set the global user variable to the anonymous user.
  17. xmlsitemap_switch_user(0);
  18. $sql = "SELECT ml.mlid FROM {menu_links} ml LEFT JOIN {xmlsitemap} x ON x.type = 'menu' AND ml.mlid = x.id WHERE x.id IS NULL AND ml.menu_name IN (:menus) ORDER BY ml.mlid DESC";
  19. $mlids = db_query_range($sql, 0, $limit, array(':menus' => $menus));
  20. foreach ($mlids as $mlid) {
  21. $menu_item = xmlsitemap_menu_menu_link_load($mlid);
  22. $link = xmlsitemap_menu_create_link($menu_item);
  23. xmlsitemap_save_link($link);
  24. }
  25. // Set the global user variable back to the original user.
  26. xmlsitemap_restore_user();
  27. }
  28. }
  29. /**
  30. * Implements hook_xmlsitemap_links().
  31. */
  32. function xmlsitemap_menu_xmlsitemap_links($offset = 0, $limit = 0) {
  33. $links = array();
  34. if ($menus = xmlsitemap_menu_get_menus()) {
  35. // Set the global user variable to the anonymous user.
  36. xmlsitemap_switch_user(0);
  37. $sql = "SELECT ml.mlid FROM {menu_links} ml WHERE ml.mlid > :mlid AND ml.menu_name IN (:menus) ORDER BY ml.mlid";
  38. $args = array(':mlid' => $offset, ':menus' => $menus);
  39. $mlids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
  40. foreach ($mlids as $mlid) {
  41. $menu_item = xmlsitemap_menu_menu_link_load($mlid);
  42. $links[] = xmlsitemap_menu_create_link($menu_item);
  43. }
  44. // Set the global user variable back to the original user.
  45. xmlsitemap_restore_user();
  46. }
  47. return $links;
  48. }
  49. /**
  50. * Implements hook_xmlsitemap_links_batch_info().
  51. */
  52. function xmlsitemap_menu_xmlsitemap_links_batch_info() {
  53. $menus = xmlsitemap_menu_get_menus();
  54. return array(
  55. 'max' => $menus ? db_query("SELECT COUNT(ml.mlid) FROM {menu_links} ml WHERE ml.menu_name IN (:menus)", array(':menus' => $menus))->fetchField() : 0,
  56. );
  57. }
  58. /**
  59. * Implements hook_xmlsitemap_link_info().
  60. */
  61. function xmlsitemap_menu_xmlsitemap_link_info() {
  62. return array(
  63. 'menu' => array(
  64. 'purge' => TRUE,
  65. 'table' => 'menu_links',
  66. 'id' => 'mlid',
  67. 'subtype' => 'menu_name',
  68. 'subtypes' => xmlsitemap_menu_get_menus(),
  69. ),
  70. );
  71. }
  72. /**
  73. * Load a menu link and its associated sitemap link data.
  74. */
  75. function xmlsitemap_menu_menu_link_load($mlid) {
  76. $menu_item = menu_link_load($mlid);
  77. if ($data = xmlsitemap_load_link(array('type' => 'menu', 'id' => $mlid))) {
  78. $menu_item['xmlsitemap'] = $data;
  79. }
  80. return $menu_item;
  81. }
  82. /**
  83. * Implements hook_form_FORM_ID_alter().
  84. *
  85. * Show a summary of menus on the XML sitemap settings page.
  86. */
  87. function xmlsitemap_menu_form_xmlsitemap_settings_form_alter(&$form, $form_state) {
  88. $type = array(
  89. 'type' => 'menu',
  90. 'title' => t('Menus'),
  91. 'item_title' => t('Menu'),
  92. 'access' => user_access('administer menu'),
  93. );
  94. $menus = menu_get_menus();
  95. foreach ($menus as $menu => $name) {
  96. $menus[$menu] = array(
  97. 'name' => $name,
  98. 'link' => 'admin/build/menu-customize/' . $menu . '/edit',
  99. 'status' => variable_get('xmlsitemap_menu_status_' . $menu, 0),
  100. 'priority' => variable_get('xmlsitemap_menu_priority_' . $menu, 0.5),
  101. );
  102. }
  103. xmlsitemap_add_form_type_summary($form, $type, $menus);
  104. $form['menu']['#weight'] = 40;
  105. }
  106. /**
  107. * Implements hook_form_FORM_ID_alter().
  108. *
  109. * @see menu_edit_menu()
  110. * @see xmlsitemap_menu_menu_edit_menu_submit()
  111. */
  112. function xmlsitemap_menu_form_menu_edit_menu_alter(&$form, $form_state) {
  113. $menu = isset($form['menu_name']['#value']) ? $form['menu_name']['#value'] : '';
  114. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  115. $options = array(
  116. 'status' => variable_get('xmlsitemap_menu_status_' . $menu, 0),
  117. 'priority' => variable_get('xmlsitemap_menu_priority_' . $menu, 0.5),
  118. );
  119. xmlsitemap_add_form_type_options($form, 'menu', $options);
  120. // @todo Enable this feature:
  121. //$form['xmlsitemap']['xmlsitemap_menu_calculate_priority'] = array(
  122. // '#type' => 'checkbox',
  123. // '#title' => t('Calculate priority based on menu item depth and weight.'),
  124. // '#default_value' => variable_get('xmlsitemap_menu_calculate_priority_' . $menu, FALSE),
  125. //);
  126. $form['submit'] += array('#weight' => 50);
  127. if (isset($form['delete'])) {
  128. $form['delete'] += array('#weight' => 51);
  129. }
  130. $form['#submit'][] = 'xmlsitemap_menu_menu_edit_menu_submit';
  131. }
  132. /**
  133. * Form submit handler; update settings when a menu is saved.
  134. */
  135. function xmlsitemap_menu_menu_edit_menu_submit($form, $form_state) {
  136. $menu = $form_state['values']['menu_name'];
  137. $new_priority = $form_state['values']['xmlsitemap_menu_priority'];
  138. $new_status = $form_state['values']['xmlsitemap_menu_status'];
  139. if ($new_status != variable_get('xmlsitemap_menu_status_' . $menu, 0)) {
  140. xmlsitemap_update_links(array('status' => $new_status), array('type' => 'menu', 'subtype' => $menu, 'status_override' => 0));
  141. }
  142. if ($new_priority != variable_get('xmlsitemap_menu_priority_' . $menu, 0.5)) {
  143. xmlsitemap_update_links(array('priority' => $new_priority), array('type' => 'menu', 'subtype' => $menu, 'priority_override' => 0));
  144. }
  145. variable_set('xmlsitemap_menu_priority_' . $menu, $new_priority);
  146. variable_set('xmlsitemap_menu_status_' . $menu, $new_status);
  147. }
  148. /**
  149. * Implements hook_form_FORM_ID_alter().
  150. *
  151. * @see menu_delete_menu_confirm()
  152. * @see xmlsitemap_menu_form_menu_delete_menu_confirm_submit()
  153. */
  154. function xmlsitemap_menu_form_menu_delete_menu_confirm_alter(&$form, $form_state) {
  155. $form['#submit'][] = 'xmlsitemap_menu_form_menu_delete_menu_confirm_submit';
  156. }
  157. /**
  158. * Form submit handler; delete sitemap links when a menu is deleted.
  159. */
  160. function xmlsitemap_menu_form_menu_delete_menu_confirm_submit($form, $form_state) {
  161. $menu = $form['#menu']['menu_name'];
  162. xmlsitemap_delete_link(array('type' => 'menu', 'subtype' => $menu));
  163. variable_del('xmlsitemap_menu_status_' . $menu);
  164. variable_del('xmlsitemap_menu_priority_ ' . $menu);
  165. }
  166. //function xmlsitemap_menu_form_menu_overview_form_alter(&$form, $form_state) {
  167. // $form['#submit'][] = 'xmlsitemap_menu_menu_overview_form_submit';
  168. //}
  169. //
  170. //function xmlsitemap_menu_menu_overview_form_submit($form, $form_state) {
  171. // foreach (element_children($form) as $mlid) {
  172. // if (isset($form[$mlid]['#item'])) {
  173. // $menu_item = menu_link_load($form[$mlid]['#item']['mlid'], TRUE);
  174. // xmlsitemap_menu_item_update($menu_item);
  175. // }
  176. // }
  177. //}
  178. /**
  179. * Implements hook_form_FORM_ID_alter().
  180. *
  181. * @see menu_item_delete_form()
  182. */
  183. function xmlsitemap_menu_form_menu_item_delete_form_alter(&$form, $form_state) {
  184. $form['#submit'][] = 'xmlsitemap_menu_menu_item_delete_form_submit';
  185. }
  186. /**
  187. * Form submit callback; delete the sitemap link when a menu item is deleted.
  188. */
  189. function xmlsitemap_menu_menu_item_delete_form_submit($form, $form_state) {
  190. xmlsitemap_menu_item_delete($form['#item']);
  191. }
  192. /**
  193. * Implements hook_form_FORM_ID_alter().
  194. *
  195. * @see menu_edit_item()
  196. */
  197. function xmlsitemap_menu_form_menu_edit_item_alter(&$form, $form_state) {
  198. $form['#submit'][] = 'xmlsitemap_menu_menu_edit_item_submit';
  199. }
  200. /**
  201. * Form submit callback; update the sitemap link when a menu item is updated.
  202. */
  203. function xmlsitemap_menu_menu_edit_item_submit($form, $form_state) {
  204. xmlsitemap_switch_user(0);
  205. $menu_item = xmlsitemap_menu_menu_link_load($form_state['values']['menu']['mlid']);
  206. xmlsitemap_restore_user();
  207. xmlsitemap_menu_item_update($menu_item);
  208. }
  209. function xmlsitemap_menu_item_update(array $menu_item) {
  210. $link = xmlsitemap_menu_create_link($menu_item);
  211. xmlsitemap_save_link($link);
  212. }
  213. function xmlsitemap_menu_item_delete(array $menu_item) {
  214. xmlsitemap_delete_link(array('type' => 'menu', 'id' => $menu_item['mlid']));
  215. }
  216. /**
  217. * Fetch an array of menus to be included in the sitemap.
  218. */
  219. function xmlsitemap_menu_get_menus() {
  220. $menus = array_keys(menu_get_menus());
  221. foreach ($menus as $index => $menu) {
  222. if (!variable_get('xmlsitemap_menu_status_' . $menu, 0)) {
  223. unset($menus[$index]);
  224. }
  225. }
  226. return $menus;
  227. }
  228. /**
  229. * Create a sitemap link from a menu item.
  230. *
  231. * @param $menu_item
  232. * A loaded menu item.
  233. */
  234. function xmlsitemap_menu_create_link(array $menu_item) {
  235. if (!isset($menu_item['xmlsitemap'])) {
  236. $menu_item['xmlsitemap'] = array();
  237. }
  238. $menu_item['xmlsitemap'] += array(
  239. 'type' => 'menu',
  240. 'id' => $menu_item['mlid'],
  241. 'loc' => $menu_item['link_path'],
  242. 'status' => variable_get('xmlsitemap_menu_status_' . $menu_item['menu_name'], 0),
  243. 'status_default' => variable_get('xmlsitemap_menu_status_' . $menu_item['menu_name'], 0),
  244. 'status_override' => 0,
  245. 'priority' => variable_get('xmlsitemap_menu_priority_' . $menu_item['menu_name'], 0.5),
  246. 'priority_default' => variable_get('xmlsitemap_menu_priority_' . $menu_item['menu_name'], 0.5),
  247. 'priority_override' => 0,
  248. );
  249. // The following values must always be checked because they are volatile.
  250. $menu_item['xmlsitemap']['subtype'] = $menu_item['menu_name'];
  251. $menu_item['xmlsitemap']['access'] = $menu_item['access'] && !$menu_item['external'] && !$menu_item['hidden'];
  252. $menu_item['xmlsitemap']['language'] = isset($menu_item['options']['langcode']) ? $menu_item['options']['langcode'] : LANGUAGE_NONE;
  253. return $menu_item['xmlsitemap'];
  254. }
  255. /**
  256. * Calculate the priority of a menu link based on depth and weight.
  257. */
  258. function xmlsitemap_menu_calculate_priority(array $menu_item) {
  259. $priority = (MENU_MAX_DEPTH - $menu_item['depth'] + 1) / MENU_MAX_DEPTH;
  260. $priority -= (50 + $menu_item['weight']) / (100 * (MENU_MAX_DEPTH + 1));
  261. return $priority;
  262. }
  263. /**
  264. * Internal default variables for template_var().
  265. */
  266. function xmlsitemap_menu_variables() {
  267. $defaults = array(
  268. // Deprecated variables set to NULL so they are still removed on uninstall.
  269. 'xmlsitemap_menu_menus' => NULL,
  270. 'xmlsitemap_menu_calculate_priority' => NULL,
  271. );
  272. $menus = array_keys(menu_get_menus());
  273. foreach ($menus as $menu) {
  274. $defaults['xmlsitemap_menu_status_' . $menu] = 0;
  275. $defaults['xmlsitemap_menu_priority_' . $menu] = 0.5;
  276. $defaults['xmlsitemap_menu_calculate_priority_' . $menu] = FALSE;
  277. }
  278. return $defaults;
  279. }
  280. /**
  281. * Internal implementation of variable_get().
  282. */
  283. function xmlsitemap_menu_var($name, $default = NULL) {
  284. static $defaults = NULL;
  285. if (!isset($defaults)) {
  286. $defaults = xmlsitemap_menu_variables();
  287. }
  288. $name = 'xmlsitemap_menu_' . $name;
  289. // @todo Remove when stable.
  290. if (!isset($defaults[$name])) {
  291. trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
  292. }
  293. return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
  294. }