path.admin.inc 8.2 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * @file
Dries's avatar
Dries committed
6
 * Administrative page callbacks for the path module.
7 8 9 10
 */

/**
 * Return a listing of all defined URL aliases.
11
 *
12 13 14 15 16
 * When filter key passed, perform a standard search on the given key,
 * and return the list of matching URL aliases.
 */
function path_admin_overview($keys = NULL) {
  // Add the filter form above the overview table.
17
  $build['path_admin_filter_form'] = drupal_get_form('path_admin_filter_form', $keys);
18
  // Enable language column if locale is enabled or if we have any alias with language
19
  $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', 0, 1, array(':language' => ''))->fetchField();
20
  $multilanguage = (module_exists('locale') || $alias_exists);
21 22

  $header = array(
23 24
    array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
    array('data' => t('System'), 'field' => 'source'),
25 26 27
    array('data' => t('Operations'), 'colspan' => '2')
  );
  if ($multilanguage) {
28
    array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
29
  }
30 31 32 33

  $query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
  if ($keys) {
    // Replace wildcards with PDO wildcards.
34
    $query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
35 36 37
  }
  $result = $query
    ->fields('url_alias')
38
    ->orderByHeader($header)
39 40
    ->limit(50)
    ->execute();
41 42 43

  $rows = array();
  $destination = drupal_get_destination();
44
  foreach ($result as $data) {
45 46
    $row = array(
      'data' => array(
47 48
        l($data->alias, $data->source),
        l($data->source, $data->source, array('alias' => TRUE)),
49 50
        l(t('edit'), "admin/config/search/path/edit/$data->pid", array('query' => $destination)),
        l(t('delete'), "admin/config/search/path/delete/$data->pid", array('query' => $destination)),
51 52
      ),
    );
53 54
    // If the system path maps to a different URL alias, highlight this table
    // row to let the user know of old aliases.
55
    if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
56 57
      $row['class'] = array('warning');
    }
58
    if ($multilanguage) {
59
      array_splice($row['data'], 2, 0, module_invoke('locale', 'language_name', $data->language));
60 61 62 63 64
    }
    $rows[] = $row;
  }

  if (empty($rows)) {
65
    $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases available. <a href="@link">Add alias</a>.', array('@link' => url('admin/config/search/path/add'))) ;
66 67 68
    $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
  }

69
  $build['path_table'] = array(
70 71
    '#theme' => 'table',
    '#header' => $header,
72 73 74
    '#rows' => $rows
  );
  $build['path_pager'] = array('#theme' => 'pager');
75

76
  return $build;
77 78 79 80 81
}

/**
 * Menu callback; handles pages for creating and editing URL aliases.
 */
82 83 84 85
function path_admin_edit($path = array()) {
  if ($path) {
    drupal_set_title($path['alias']);
    $output = drupal_get_form('path_admin_form', $path);
86 87
  }
  else {
88
    $output = drupal_get_form('path_admin_form');
89 90 91 92 93 94 95 96 97
  }

  return $output;
}

/**
 * Return a form for editing or creating an individual URL alias.
 *
 * @ingroup forms
98 99
 * @see path_admin_form_validate()
 * @see path_admin_form_submit()
100
 */
101 102
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => '', 'pid' => NULL)) {
  $form['source'] = array(
103 104
    '#type' => 'textfield',
    '#title' => t('Existing system path'),
105
    '#default_value' => $path['source'],
106
    '#maxlength' => 255,
107
    '#size' => 45,
108
    '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'),
109 110
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
111
  );
112
  $form['alias'] = array(
113 114
    '#type' => 'textfield',
    '#title' => t('Path alias'),
115
    '#default_value' => $path['alias'],
116
    '#maxlength' => 255,
117 118
    '#size' => 45,
    '#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
119 120
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
121
  );
122 123

  // This will be a hidden value unless locale module is enabled.
124 125
  $form['language'] = array(
    '#type' => 'value',
126
    '#value' => $path['language']
127
  );
128 129 130 131 132 133 134 135 136
  if ($path['pid']) {
    $form['pid'] = array(
      '#type' => 'hidden',
      '#value' => $path['pid'],
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update alias'),
    );
137 138
  }
  else {
139 140 141 142
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Create new alias'),
    );
143 144 145 146 147 148 149
  }

  return $form;
}


/**
150
 * Verify that a URL alias is valid
151 152
 */
function path_admin_form_validate($form, &$form_state) {
153 154
  $source = &$form_state['values']['source'];
  $source = drupal_get_normal_path($source);
155
  $alias = $form_state['values']['alias'];
156 157 158 159
  $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
  // Language is only set if locale module is enabled, otherwise save for all languages.
  $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';

160
  $has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array(
161 162 163 164 165
      ':pid' => $pid,
      ':alias' => $alias,
      ':language' => $language,
    ))
    ->fetchField();
166 167

  if ($has_alias) {
168
    form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
169
  }
170
  $item = menu_get_item($source);
Dries's avatar
Dries committed
171
  if (!$item || !$item['access']) {
172
    form_set_error('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
173
  }
174 175 176
}

/**
177
 * Save a URL alias to the database.
178 179
 */
function path_admin_form_submit($form, &$form_state) {
180 181 182 183
  // Remove unnecessary values.
  form_state_values_clean($form_state);

  path_save($form_state['values']);
184 185

  drupal_set_message(t('The alias has been saved.'));
186
  $form_state['redirect'] = 'admin/config/search/path';
187 188 189 190
}

/**
 * Menu callback; confirms deleting an URL alias
191
 */
192
function path_admin_delete_confirm($form, &$form_state, $path) {
193
  if (user_access('administer url aliases')) {
194 195 196 197 198 199 200
    $form_state['path'] = $path;
    return confirm_form(
      $form,
      t('Are you sure you want to delete path alias %title?',
      array('%title' => $path['alias'])),
      'admin/config/search/path'
    );
201
  }
202
  return array();
203 204 205 206
}

/**
 * Execute URL alias deletion
207
 */
208 209
function path_admin_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
210
    path_delete($form_state['path']['pid']);
211
    $form_state['redirect'] = 'admin/config/search/path';
212 213 214 215 216 217 218 219
  }
}


/**
 * Return a form to filter URL aliases.
 *
 * @ingroup forms
220
 * @see path_admin_filter_form_submit()
221
 */
222
function path_admin_filter_form($form, &$form_state, $keys = '') {
223
  $form['#attributes'] = array('class' => array('search-form'));
224 225 226 227 228 229 230 231
  $form['basic'] = array('#type' => 'fieldset',
    '#title' => t('Filter aliases')
  );
  $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
  $form['basic']['inline']['filter'] = array(
    '#type' => 'textfield',
    '#title' => '',
    '#default_value' => $keys,
232
    '#maxlength' => 128,
233 234
    '#size' => 25,
  );
235 236 237 238 239 240 241 242 243 244 245 246
  $form['basic']['inline']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#submit' => array('path_admin_filter_form_submit_filter'),
    );
  if ($keys) {
    $form['basic']['inline']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
      '#submit' => array('path_admin_filter_form_submit_reset'),
    );
  }
247 248 249 250
  return $form;
}

/**
251
 * Process filter form submission when the Filter button is pressed.
252
 */
253
function path_admin_filter_form_submit_filter($form, &$form_state) {
254
  $form_state['redirect'] = 'admin/config/search/path/list/' . trim($form_state['values']['filter']);
255 256
}

257 258 259 260
/**
 * Process filter form submission when the Reset button is pressed.
 */
function path_admin_filter_form_submit_reset($form, &$form_state) {
261
  $form_state['redirect'] = 'admin/config/search/path/list';
262
}