xmlsitemap_custom.admin.inc 7.4 KB

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