xmlsitemap_custom.admin.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the xmlsitemap_custom.
  5. */
  6. /**
  7. * List Links.
  8. */
  9. function xmlsitemap_custom_list_links() {
  10. $header = array(
  11. 'loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'),
  12. 'priority' => array('data' => t('Priority'), 'field' => 'priority'),
  13. 'changefreq' => array('data' => t('Change frequency'), 'field' => 'changefreq'),
  14. 'language' => array('data' => t('Language'), 'field' => 'language'),
  15. 'operations' => array('data' => t('Operations')),
  16. );
  17. // Do not include the language column if locale is disabled.
  18. if (!module_exists('locale')) {
  19. unset($header['language']);
  20. }
  21. $rows = array();
  22. $destination = drupal_get_destination();
  23. $query = db_select('xmlsitemap')
  24. ->extend('PagerDefault')
  25. ->extend('TableSort');
  26. $query->fields('xmlsitemap');
  27. $query->condition('type', 'custom');
  28. $query->limit(25);
  29. $query->orderByHeader($header);
  30. $result = $query->execute();
  31. foreach ($result as $link) {
  32. $row = array();
  33. $row['loc'] = l($link->loc, $link->loc);
  34. $row['priority'] = number_format($link->priority, 1);
  35. $row['changefreq'] = $link->changefreq ? drupal_ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None');
  36. if (isset($header['language'])) {
  37. $row['language'] = module_invoke('locale', 'language_name', $link->language);
  38. }
  39. $operations = array();
  40. $operations['edit'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/custom/edit/' . $link->id, array('title' => t('Edit'), 'modal' => TRUE));
  41. $operations['delete'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/custom/delete/' . $link->id, array('title' => t('Delete'), 'modal' => TRUE));
  42. $row['operations'] = array(
  43. 'data' => array(
  44. '#theme' => 'links',
  45. '#links' => $operations,
  46. '#attributes' => array('class' => array('links', 'inline')),
  47. ),
  48. );
  49. $rows[] = $row;
  50. }
  51. // @todo Convert to tableselect
  52. $build['xmlsitemap_custom_table'] = array(
  53. '#theme' => 'table',
  54. '#header' => $header,
  55. '#rows' => $rows,
  56. '#empty' => t('No custom links available.') . ' ' . l(t('Add custom link'), 'admin/config/search/xmlsitemap/custom/add', array('query' => $destination)),
  57. );
  58. $build['xmlsitemap_custom_pager'] = array('#theme' => 'pager');
  59. return $build;
  60. }
  61. /**
  62. * Edit Link Form.
  63. */
  64. function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) {
  65. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  66. _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');
  67. $link += array(
  68. 'id' => db_query("SELECT MAX(id) FROM {xmlsitemap} WHERE type = 'custom'")->fetchField() + 1,
  69. 'loc' => '',
  70. 'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
  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' => 'actions',
  116. );
  117. $form['actions']['submit'] = array(
  118. '#type' => 'submit',
  119. '#value' => t('Save'),
  120. '#weight' => 5,
  121. );
  122. $form['actions']['cancel'] = array(
  123. '#markup' => l(t('Cancel'), 'admin/config/search/xmlsitemap/custom'),
  124. '#weight' => 10,
  125. );
  126. return $form;
  127. }
  128. /**
  129. * Edit Link Form Validate.
  130. */
  131. function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
  132. $link = &$form_state['values'];
  133. // Make sure we trim and normalize the path first.
  134. $link['loc'] = trim($link['loc']);
  135. $link['loc'] = drupal_get_normal_path($link['loc'], $link['language']);
  136. // Test anonymous user access to the custom link paths.
  137. xmlsitemap_switch_user(0);
  138. $menu_item = menu_get_item($link['loc']);
  139. xmlsitemap_restore_user();
  140. // Since the menu item access results are cached, manually check the current
  141. // path.
  142. if ($menu_item && strpos($link['loc'], 'admin/config/search/xmlsitemap/custom') === 0 && !user_access('administer xmlsitemap', drupal_anonymous_user())) {
  143. $menu_item['access'] = FALSE;
  144. }
  145. 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(
  146. ':loc' => $link['loc'],
  147. ':languages' => array(LANGUAGE_NONE, $link['language']),
  148. ))->fetchField()) {
  149. form_set_error('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
  150. }
  151. elseif (empty($menu_item['access']) && !is_readable('./' . $link['loc'])) {
  152. // @todo Harden this file exists check to make sure we can't link to files
  153. // like .htaccess.
  154. form_set_error('loc', t('The custom link %link is either invalid or it cannot be accessed by anonymous users.', array('%link' => $link['loc'])));
  155. }
  156. }
  157. /**
  158. * Edit Link Form Submit.
  159. */
  160. function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) {
  161. $link = $form_state['values'];
  162. xmlsitemap_link_save($link);
  163. drupal_set_message(t('The custom link for %loc was saved.', array('%loc' => $link['loc'])));
  164. $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
  165. }
  166. /**
  167. * Delete Link Form.
  168. */
  169. function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
  170. // @todo Remove when https://www.drupal.org/node/576290 is fixed.
  171. _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');
  172. $form['#link'] = $link;
  173. $form['id'] = array(
  174. '#type' => 'value',
  175. '#value' => $link['id'],
  176. );
  177. $form['link'] = array(
  178. '#type' => 'value',
  179. '#value' => $link,
  180. );
  181. return confirm_form(
  182. $form,
  183. t('Are you sure you want to delete the custom link for %loc?', array('%loc' => $link['loc'])),
  184. 'admin/config/search/xmlsitemap/custom',
  185. t('This action cannot be undone.'),
  186. t('Delete'),
  187. t('Cancel')
  188. );
  189. }
  190. /**
  191. * Delete Link Form Submit.
  192. */
  193. function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) {
  194. $link = $form_state['values']['link'];
  195. xmlsitemap_link_delete('custom', $link['id']);
  196. drupal_set_message(t('The custom link for %loc has been deleted.', array('%loc' => $link['loc'])));
  197. watchdog('xmlsitemap', 'The custom link for %loc has been deleted.', array('%loc' => $link['loc']), WATCHDOG_NOTICE);
  198. $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
  199. }