xmlsitemap_custom.admin.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // $Id: xmlsitemap_custom.admin.inc,v 1.10 2010/01/24 03:57:14 davereid Exp $
  3. /**
  4. * @file
  5. * Administrative page callbacks for the xmlsitemap_custom module.
  6. */
  7. function xmlsitemap_custom_list_links() {
  8. $header = array(
  9. 'loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'),
  10. 'priority' => array('data' => t('Priority'), 'field' => 'priority'),
  11. 'changefreq' => array('data' => t('Change frequency'), 'field' => 'changefreq'),
  12. 'language' => array('data' => t('Language'), 'field' => 'language'),
  13. 'operations' => array('data' => t('Operations')),
  14. );
  15. // Do not include the language column if locale is disabled.
  16. if (!module_exists('locale')) {
  17. unset($header['language']);
  18. }
  19. $rows = array();
  20. $destination = drupal_get_destination();
  21. $query = db_select('xmlsitemap');
  22. $query->fields('xmlsitemap');
  23. $query->condition('type', 'custom');
  24. $query->extend('PagerDefault')->limit(50);
  25. $query->extend('TableSort')->orderByHeader($header);
  26. $result = $query->execute();
  27. foreach ($result as $link) {
  28. $row = array();
  29. $row['loc'] = l($link->loc, $link->loc);
  30. $row['priority'] = number_format($link->priority, 1);
  31. $row['changefreq'] = $link->changefreq ? drupal_ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None');
  32. if (isset($header['language'])) {
  33. $row['language'] = module_invoke('locale', 'language_name', $link->language);
  34. }
  35. $operations = array();
  36. $operations['edit'] = array(
  37. 'title' => t('Edit'),
  38. 'href' => 'admin/config/search/xmlsitemap/custom/edit/' . $link->id,
  39. 'query' => $destination,
  40. );
  41. $operations['delete'] = array(
  42. 'title' => t('Delete'),
  43. 'href' => 'admin/config/search/xmlsitemap/custom/delete/' . $link->id,
  44. 'query' => $destination,
  45. );
  46. $row['operations'] = array(
  47. 'data' => array(
  48. '#theme' => 'links',
  49. '#links' => $operations,
  50. '#attributes' => array('class' => array('links', 'inline')),
  51. ),
  52. );
  53. $rows[] = $row;
  54. }
  55. // @todo Convert to tableselect
  56. $build['xmlsitemap_custom_table'] = array(
  57. '#theme' => 'table',
  58. '#header' => $header,
  59. '#rows' => $rows,
  60. '#empty' => t('No custom links available. <a href="@link">Add custom link</a>.', array('@link' => url('admin/config/search/xmlsitemap/custom/add', array('query' => $destination)))),
  61. );
  62. $build['xmlsitemap_custom_pager'] = array('#theme' => 'pager');
  63. return $build;
  64. }
  65. function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) {
  66. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  67. $link += array(
  68. 'id' => db_query("SELECT MAX(id) FROM {xmlsitemap} WHERE type = 'custom'")->fetchField() + 1,
  69. 'loc' => '',
  70. 'priority' => 0.5,
  71. 'lastmod' => 0,
  72. 'changefreq' => 0,
  73. 'changecount' => 0,
  74. 'language' => LANGUAGE_NONE,
  75. );
  76. $form['type'] = array(
  77. '#type' => 'value',
  78. '#value' => 'custom',
  79. );
  80. $form['id'] = array(
  81. '#type' => 'value',
  82. '#value' => $link['id'],
  83. );
  84. $form['loc'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Path to link'),
  87. '#field_prefix' => url('', array('absolute' => TRUE)),
  88. '#default_value' => $link['loc'] ? drupal_get_path_alias($link['loc'], $link['language']) : '',
  89. '#required' => TRUE,
  90. '#size' => 30,
  91. );
  92. $form['priority'] = array(
  93. '#type' => 'select',
  94. '#title' => t('Priority'),
  95. '#options' => xmlsitemap_get_priority_options(),
  96. '#default_value' => number_format($link['priority'], 1),
  97. '#description' => t('The priority of this URL relative to other URLs on your site.'),
  98. );
  99. $form['changefreq'] = array(
  100. '#type' => 'select',
  101. '#title' => t('Change frequency'),
  102. '#options' => array(0 => t('None')) + xmlsitemap_get_changefreq_options(),
  103. '#default_value' => $link['changefreq'],
  104. '#description' => t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
  105. );
  106. $languages = module_exists('locale') ? locale_language_list() : array();
  107. $form['language'] = array(
  108. '#type' => 'select',
  109. '#title' => t('Language'),
  110. '#default_value' => $link['language'],
  111. '#options' => array(LANGUAGE_NONE => t('Language neutral')) + $languages,
  112. '#access' => $languages,
  113. );
  114. $form['actions'] = array(
  115. '#type' => 'container',
  116. '#attributes' => array('class' => array('form-actions')),
  117. '#weight' => 100,
  118. );
  119. $form['actions']['submit'] = array(
  120. '#type' => 'submit',
  121. '#value' => t('Save'),
  122. '#weight' => 5,
  123. );
  124. $form['actions']['cancel'] = array(
  125. '#markup' => l(t('Cancel'), 'admin/config/search/xmlsitemap/custom'),
  126. '#weight' => 10,
  127. );
  128. return $form;
  129. }
  130. function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
  131. $link = &$form_state['values'];
  132. // Make sure we trim and normalize the path first.
  133. $link['loc'] = trim($link['loc']);
  134. $link['loc'] = drupal_get_normal_path($link['loc'], $link['language']);
  135. // Test anonymous user access to the custom link paths.
  136. xmlsitemap_switch_user(0);
  137. $menu_item = menu_get_item($link['loc']);
  138. xmlsitemap_restore_user();
  139. // Since the menu item access results are cached, manually check the current path.
  140. if ($menu_item && strpos($link['loc'], 'admin/config/search/xmlsitemap/custom') === 0 && !user_access('administer xmlsitemap', drupal_anonymous_user())) {
  141. $menu_item['access'] = FALSE;
  142. }
  143. if (db_query_range("SELECT 1 FROM {xmlsitemap} WHERE type <> 'custom' AND loc = :loc AND status = 1 AND access = 1 AND language IN (:languages)", 0, 1, array(':loc' => $link['loc'], ':languages' => array(LANGUAGE_NONE, $link['language'])))->fetchField()) {
  144. form_set_error('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
  145. }
  146. elseif (empty($menu_item['access']) && !is_readable('./' . $link['loc'])) {
  147. // @todo Harden this file exists check to make sure we can't link to files
  148. // like .htaccess.
  149. form_set_error('loc', t('The custom link %link is either invalid or it cannot be accessed by anonymous users.', array('%link' => $link['loc'])));
  150. }
  151. }
  152. function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) {
  153. $link = $form_state['values'];
  154. xmlsitemap_save_link($link);
  155. drupal_set_message(t('The custom link for %loc was saved.', array('%loc' => $link['loc'])));
  156. $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
  157. }
  158. function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
  159. $form['link'] = array(
  160. '#type' => 'value',
  161. '#value' => $link,
  162. );
  163. return confirm_form(
  164. $form,
  165. t('Are you sure you want to delete the custom link for %loc?', array('%loc' => $link['loc'])),
  166. 'admin/config/search/xmlsitemap/custom',
  167. t('This action cannot be undone.'),
  168. t('Delete'),
  169. t('Cancel')
  170. );
  171. }
  172. function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) {
  173. $link = $form_state['values']['link'];
  174. xmlsitemap_delete_link(array('type' => 'custom', 'id' => $link['id']));
  175. drupal_set_message(t('The custom link for %loc has been deleted.', array('%loc' => $link['loc'])));
  176. watchdog('xmlsitemap', 'The custom link for %loc has been deleted.', array('%loc' => $link['loc']), WATCHDOG_NOTICE);
  177. $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
  178. }