path.admin.inc 10.4 KB
Newer Older
1 2 3 4
<?php

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

/**
9
 * Returns a listing of all defined URL aliases.
10
 *
11 12 13 14 15
 * 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.
16
  $build['path_admin_filter_form'] = drupal_get_form('path_admin_filter_form', $keys);
17 18
  // Enable language column if language.module is enabled or if we have any
  // alias with a language.
19
  $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE langcode <> :langcode', 0, 1, array(':langcode' => LANGUAGE_NOT_SPECIFIED))->fetchField();
20
  $multilanguage = (module_exists('language') || $alias_exists);
21

22 23 24
  $header = array();
  $header[] = array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc');
  $header[] = array('data' => t('System'), 'field' => 'source');
25
  if ($multilanguage) {
26
    $header[] = array('data' => t('Language'), 'field' => 'langcode');
27
  }
28
  $header[] = array('data' => t('Operations'));
29 30 31 32

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

  $rows = array();
  $destination = drupal_get_destination();
43
  foreach ($result as $data) {
44 45 46 47
    $row = array();
    $row['data']['alias'] = l($data->alias, $data->source);
    $row['data']['source'] = l($data->source, $data->source, array('alias' => TRUE));
    if ($multilanguage) {
48
      $row['data']['language_name'] = language_name($data->langcode);
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    }

    $operations = array();
    $operations['edit'] = array(
      'title' => t('edit'),
      'href' => "admin/config/search/path/edit/$data->pid",
      'query' => $destination,
    );
    $operations['delete'] = array(
      'title' => t('delete'),
      'href' => "admin/config/search/path/delete/$data->pid",
      'query' => $destination,
    );
    $row['data']['operations'] = array(
63
      'data' => array(
64 65 66
        '#theme' => 'links',
        '#links' => $operations,
        '#attributes' => array('class' => array('links', 'inline', 'nowrap')),
67 68
      ),
    );
69

70 71
    // If the system path maps to a different URL alias, highlight this table
    // row to let the user know of old aliases.
72
    if ($data->alias != drupal_get_path_alias($data->source, $data->langcode)) {
73 74
      $row['class'] = array('warning');
    }
75

76 77 78
    $rows[] = $row;
  }

79
  $build['path_table'] = array(
80 81
    '#theme' => 'table',
    '#header' => $header,
82
    '#rows' => $rows,
83
    '#empty' => t('No URL aliases available. <a href="@link">Add URL alias</a>.', array('@link' => url('admin/config/search/path/add'))),
84 85
  );
  $build['path_pager'] = array('#theme' => 'pager');
86

87
  return $build;
88 89 90
}

/**
91 92 93 94 95 96 97 98 99
 * Page callback: Returns a form creating or editing a path alias.
 *
 * @param $path
 *   An array containing the path ID, source, alias, and language code.
 *
 * @return
 *   A form for adding or editing a URL alias.
 *
 * @see path_menu()
100
 */
101 102 103 104
function path_admin_edit($path = array()) {
  if ($path) {
    drupal_set_title($path['alias']);
    $output = drupal_get_form('path_admin_form', $path);
105 106
  }
  else {
107
    $output = drupal_get_form('path_admin_form');
108 109 110 111 112 113
  }

  return $output;
}

/**
114 115 116 117
 * Form constructor for the path administration form.
 *
 * @param $path
 *   An array containing the path ID, source, alias, and language code.
118 119
 *
 * @ingroup forms
120 121
 * @see path_admin_form_validate()
 * @see path_admin_form_submit()
122
 * @see path_admin_form_delete_submit()
123
 */
124
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'langcode' => LANGUAGE_NOT_SPECIFIED, 'pid' => NULL)) {
125
  $form['source'] = array(
126 127
    '#type' => 'textfield',
    '#title' => t('Existing system path'),
128
    '#default_value' => $path['source'],
129
    '#maxlength' => 255,
130
    '#size' => 45,
131
    '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'),
132 133
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
134
  );
135
  $form['alias'] = array(
136 137
    '#type' => 'textfield',
    '#title' => t('Path alias'),
138
    '#default_value' => $path['alias'],
139
    '#maxlength' => 255,
140 141
    '#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.'),
142 143
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
144
  );
145

146 147
  // A hidden value unless language.module is enabled.
  if (module_exists('language')) {
148
    $languages = language_list();
149 150 151 152
    foreach ($languages as $langcode => $language) {
      $language_options[$langcode] = $language->name;
    }

153
    $form['langcode'] = array(
154 155
      '#type' => 'select',
      '#title' => t('Language'),
156
      '#options' => $language_options,
157
      '#empty_value' => LANGUAGE_NOT_SPECIFIED,
158
      '#empty_option' => t('- None -'),
159
      '#default_value' => $path['langcode'],
160
      '#weight' => -10,
161
      '#description' => t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set as <em>- None -</em>.'),
162 163 164
    );
  }
  else {
165
    $form['langcode'] = array(
166
      '#type' => 'value',
167
      '#value' => $path['langcode']
168 169
    );
  }
170

171
  $form['actions'] = array('#type' => 'actions');
172 173 174 175
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
176 177 178 179 180
  if ($path['pid']) {
    $form['pid'] = array(
      '#type' => 'hidden',
      '#value' => $path['pid'],
    );
181
    $form['actions']['delete'] = array(
182
      '#type' => 'submit',
183 184
      '#value' => t('Delete'),
      '#submit' => array('path_admin_form_delete_submit'),
185
    );
186 187 188 189 190
  }

  return $form;
}

191
/**
192 193 194 195
 * Form submission handler for the 'Delete' button on path_admin_form().
 *
 * @see path_admin_form_validate()
 * @see path_admin_form_submit()
196 197 198 199 200 201 202 203 204
 */
function path_admin_form_delete_submit($form, &$form_state) {
  $destination = array();
  if (isset($_GET['destination'])) {
    $destination = drupal_get_destination();
    unset($_GET['destination']);
  }
  $form_state['redirect'] = array('admin/config/search/path/delete/' . $form_state['values']['pid'], array('query' => $destination));
}
205 206

/**
207 208 209 210
 * Form validation handler for path_admin_form().
 *
 * @see path_admin_form_submit()
 * @see path_admin_form_delete_submit()
211 212
 */
function path_admin_form_validate($form, &$form_state) {
213 214
  $source = &$form_state['values']['source'];
  $source = drupal_get_normal_path($source);
215
  $alias = $form_state['values']['alias'];
216
  $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
217 218
  // Language is only set if language.module is enabled, otherwise save for all
  // languages.
219
  $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : LANGUAGE_NOT_SPECIFIED;
220

221
  $has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND langcode = :langcode", array(
222 223
      ':pid' => $pid,
      ':alias' => $alias,
224
      ':langcode' => $langcode,
225 226
    ))
    ->fetchField();
227 228

  if ($has_alias) {
229
    form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
230
  }
231
  if (!drupal_valid_path($source)) {
232
    form_set_error('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
233
  }
234 235 236
}

/**
237 238 239 240
 * Form submission handler for path_admin_form().
 *
 * @see path_admin_form_validate()
 * @see path_admin_form_delete_submit()
241 242
 */
function path_admin_form_submit($form, &$form_state) {
243 244 245 246
  // Remove unnecessary values.
  form_state_values_clean($form_state);

  path_save($form_state['values']);
247 248

  drupal_set_message(t('The alias has been saved.'));
249
  $form_state['redirect'] = 'admin/config/search/path';
250 251 252
}

/**
253 254 255 256 257 258
 * Form constructor for the path deletion form.
 *
 * @param $path
 *   The path alias that will be deleted.
 *
 * @see path_admin_delete_confirm_submit()
259
 */
260
function path_admin_delete_confirm($form, &$form_state, $path) {
261
  if (user_access('administer url aliases')) {
262 263 264 265 266 267 268
    $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'
    );
269
  }
270
  return array();
271 272 273
}

/**
274
 * Form submission handler for path_admin_delete_confirm().
275
 */
276 277
function path_admin_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
278
    path_delete($form_state['path']['pid']);
279
    $form_state['redirect'] = 'admin/config/search/path';
280 281 282 283
  }
}

/**
284
 * Form constructor for the path admin overview filter form.
285 286
 *
 * @ingroup forms
287 288
 * @see path_admin_filter_form_submit_filter()
 * @see path_admin_filter_form_submit_reset()
289
 */
290
function path_admin_filter_form($form, &$form_state, $keys = '') {
291
  $form['#attributes'] = array('class' => array('search-form'));
292
  $form['basic'] = array('#type' => 'fieldset',
293 294
    '#title' => t('Filter aliases'),
    '#attributes' => array('class' => array('container-inline')),
295
  );
296
  $form['basic']['filter'] = array(
297
    '#type' => 'search',
298 299
    '#title' => 'Path alias',
    '#title_display' => 'invisible',
300
    '#default_value' => $keys,
301
    '#maxlength' => 128,
302 303
    '#size' => 25,
  );
304
  $form['basic']['submit'] = array(
305 306 307 308 309
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#submit' => array('path_admin_filter_form_submit_filter'),
    );
  if ($keys) {
310
    $form['basic']['reset'] = array(
311 312 313 314 315
      '#type' => 'submit',
      '#value' => t('Reset'),
      '#submit' => array('path_admin_filter_form_submit_reset'),
    );
  }
316 317 318 319
  return $form;
}

/**
320 321 322
 * Form submission handler for the path_admin_filter_form() Filter button.
 *
 * @see path_admin_filter_form_submit_reset()
323
 */
324
function path_admin_filter_form_submit_filter($form, &$form_state) {
325
  $form_state['redirect'] = 'admin/config/search/path/list/' . trim($form_state['values']['filter']);
326 327
}

328
/**
329 330 331
 * Form submission handler for the path_admin_filter_form() Reset button.
 *
 * @see path_admin_filter_form_submit_filter()
332 333
 */
function path_admin_filter_form_submit_reset($form, &$form_state) {
334
  $form_state['redirect'] = 'admin/config/search/path/list';
335
}