xmlsitemap_custom.admin.inc 7.56 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Administrative page callbacks for the xmlsitemap_custom.
6 7
 */

8 9 10
/**
 * List Links.
 */
11 12
function xmlsitemap_custom_list_links() {
  $header = array(
13 14 15 16
    'loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'),
    'priority' => array('data' => t('Priority'), 'field' => 'priority'),
    'changefreq' => array('data' => t('Change frequency'), 'field' => 'changefreq'),
    'language' => array('data' => t('Language'), 'field' => 'language'),
17
    'operations' => array('data' => t('Operations')),
18
  );
19 20 21 22 23
  // Do not include the language column if locale is disabled.
  if (!module_exists('locale')) {
    unset($header['language']);
  }

24
  $rows = array();
25 26
  $destination = drupal_get_destination();

27 28 29
  $query = db_select('xmlsitemap')
    ->extend('PagerDefault')
    ->extend('TableSort');
30 31
  $query->fields('xmlsitemap');
  $query->condition('type', 'custom');
32 33
  $query->limit(25);
  $query->orderByHeader($header);
34
  $result = $query->execute();
35 36 37 38 39

  foreach ($result as $link) {
    $row = array();
    $row['loc'] = l($link->loc, $link->loc);
    $row['priority'] = number_format($link->priority, 1);
40 41 42 43
    $row['changefreq'] = $link->changefreq ? drupal_ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None');
    if (isset($header['language'])) {
      $row['language'] = module_invoke('locale', 'language_name', $link->language);
    }
44
    $operations = array();
45 46
    $operations['edit'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/custom/edit/' . $link->id, array('title' => t('Edit'), 'modal' => TRUE));
    $operations['delete'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/custom/delete/' . $link->id, array('title' => t('Delete'), 'modal' => TRUE));
47 48 49 50 51 52 53 54
    $row['operations'] = array(
      'data' => array(
        '#theme' => 'links',
        '#links' => $operations,
        '#attributes' => array('class' => array('links', 'inline')),
      ),
    );
    $rows[] = $row;
55 56
  }

57
  // @todo Convert to tableselect
58 59 60 61
  $build['xmlsitemap_custom_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
62
    '#empty' => t('No custom links available.') . ' ' . l(t('Add custom link'), 'admin/config/search/xmlsitemap/custom/add', array('query' => $destination)),
63
  );
64 65
  $build['xmlsitemap_custom_pager'] = array('#theme' => 'pager');
  return $build;
66 67
}

68 69 70
/**
 * Edit Link Form.
 */
71
function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) {
72
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
73 74
  _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');

75
  $link += array(
76
    'id' => db_query("SELECT MAX(id) FROM {xmlsitemap} WHERE type = 'custom'")->fetchField() + 1,
77
    'loc' => '',
78
    'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
79 80 81
    'lastmod' => 0,
    'changefreq' => 0,
    'changecount' => 0,
82
    'language' => LANGUAGE_NONE,
83 84 85 86 87 88 89 90 91 92 93 94 95 96
  );

  $form['type'] = array(
    '#type' => 'value',
    '#value' => 'custom',
  );
  $form['id'] = array(
    '#type' => 'value',
    '#value' => $link['id'],
  );
  $form['loc'] = array(
    '#type' => 'textfield',
    '#title' => t('Path to link'),
    '#field_prefix' => url('', array('absolute' => TRUE)),
97
    '#default_value' => $link['loc'] ? drupal_get_path_alias($link['loc'], $link['language']) : '',
98 99 100
    '#required' => TRUE,
    '#size' => 30,
  );
101
  $form['priority'] = array(
102 103 104 105 106 107
    '#type' => 'select',
    '#title' => t('Priority'),
    '#options' => xmlsitemap_get_priority_options(),
    '#default_value' => number_format($link['priority'], 1),
    '#description' => t('The priority of this URL relative to other URLs on your site.'),
  );
108
  $form['changefreq'] = array(
109 110
    '#type' => 'select',
    '#title' => t('Change frequency'),
111
    '#options' => array(0 => t('None')) + xmlsitemap_get_changefreq_options(),
112 113 114 115 116 117 118 119
    '#default_value' => $link['changefreq'],
    '#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.'),
  );
  $languages = module_exists('locale') ? locale_language_list() : array();
  $form['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#default_value' => $link['language'],
120
    '#options' => array(LANGUAGE_NONE => t('Language neutral')) + $languages,
121 122 123
    '#access' => $languages,
  );

124
  $form['actions'] = array(
125
    '#type' => 'actions',
126 127
  );
  $form['actions']['submit'] = array(
128 129
    '#type' => 'submit',
    '#value' => t('Save'),
130
    '#weight' => 5,
131
  );
132 133 134
  $form['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), 'admin/config/search/xmlsitemap/custom'),
    '#weight' => 10,
135 136 137 138 139
  );

  return $form;
}

140 141 142
/**
 * Edit Link Form Validate.
 */
143
function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
144 145 146 147
  $link = &$form_state['values'];

  // Make sure we trim and normalize the path first.
  $link['loc'] = trim($link['loc']);
148
  $link['loc'] = drupal_get_normal_path($link['loc'], $link['language']);
149 150 151 152 153 154

  // Test anonymous user access to the custom link paths.
  xmlsitemap_switch_user(0);
  $menu_item = menu_get_item($link['loc']);
  xmlsitemap_restore_user();

155 156
  // Since the menu item access results are cached, manually check the current
  // path.
157
  if ($menu_item && strpos($link['loc'], 'admin/config/search/xmlsitemap/custom') === 0 && !user_access('administer xmlsitemap', drupal_anonymous_user())) {
158 159 160
    $menu_item['access'] = FALSE;
  }

161 162 163 164
  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()) {
165 166 167 168 169 170 171 172 173
    form_set_error('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
  }
  elseif (empty($menu_item['access']) && !is_readable('./' . $link['loc'])) {
    // @todo Harden this file exists check to make sure we can't link to files
    // like .htaccess.
    form_set_error('loc', t('The custom link %link is either invalid or it cannot be accessed by anonymous users.', array('%link' => $link['loc'])));
  }
}

174 175 176
/**
 * Edit Link Form Submit.
 */
177
function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) {
178
  $link = $form_state['values'];
179
  xmlsitemap_link_save($link);
180
  drupal_set_message(t('The custom link for %loc was saved.', array('%loc' => $link['loc'])));
181
  $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
182 183
}

184 185 186
/**
 * Delete Link Form.
 */
187
function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
188 189 190
  // @todo Remove when http://drupal.org/node/576290 is fixed.
  _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');

191 192 193 194 195
  $form['#link'] = $link;
  $form['id'] = array(
    '#type' => 'value',
    '#value' => $link['id'],
  );
196 197 198 199 200 201 202 203
  $form['link'] = array(
    '#type' => 'value',
    '#value' => $link,
  );

  return confirm_form(
    $form,
    t('Are you sure you want to delete the custom link for %loc?', array('%loc' => $link['loc'])),
204
    'admin/config/search/xmlsitemap/custom',
205 206 207 208 209 210
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

211 212 213
/**
 * Delete Link Form Submit.
 */
214 215
function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) {
  $link = $form_state['values']['link'];
216
  xmlsitemap_link_delete('custom', $link['id']);
217 218
  drupal_set_message(t('The custom link for %loc has been deleted.', array('%loc' => $link['loc'])));
  watchdog('xmlsitemap', 'The custom link for %loc has been deleted.', array('%loc' => $link['loc']), WATCHDOG_NOTICE);
219
  $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
220
}