path.admin.inc 8.27 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
11
12
13
14
15
 */

/**
 * Return a listing of all defined URL aliases.
 * 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
  // Enable language column if locale is enabled or if we have any alias with language
18
  $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', 0, 1, array(':language' => ''))->fetchField();
19
  $multilanguage = (module_exists('locale') || $alias_exists);
20
21

  $header = array(
22
23
    array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
    array('data' => t('System'), 'field' => 'source'),
24
25
26
    array('data' => t('Operations'), 'colspan' => '2')
  );
  if ($multilanguage) {
27
    array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
28
  }
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
    $row = array(
      'data' => array(
46
47
        l($data->alias, $data->source),
        l($data->source, $data->source, array('alias' => TRUE)),
48
49
        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)),
50
51
      ),
    );
52
53
    // If the system path maps to a different URL alias, highlight this table
    // row to let the user know of old aliases.
54
    if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
55
56
      $row['class'] = array('warning');
    }
57
    if ($multilanguage) {
58
      array_splice($row['data'], 2, 0, module_invoke('locale', 'language_name', $data->language));
59
60
61
62
63
    }
    $rows[] = $row;
  }

  if (empty($rows)) {
64
    $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'))) ;
65
66
67
    $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
  }

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

75
  return $build;
76
77
78
79
80
}

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

  return $output;
}

/**
 * Return a form for editing or creating an individual URL alias.
 *
 * @ingroup forms
97
98
 * @see path_admin_form_validate()
 * @see path_admin_form_submit()
99
 */
100
101
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => '', 'pid' => NULL)) {
  $form['source'] = array(
102
103
    '#type' => 'textfield',
    '#title' => t('Existing system path'),
104
    '#default_value' => $path['source'],
105
    '#maxlength' => 255,
106
107
    '#size' => 45,
    '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
108
109
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
110
  );
111
  $form['alias'] = array(
112
113
    '#type' => 'textfield',
    '#title' => t('Path alias'),
114
    '#default_value' => $path['alias'],
115
    '#maxlength' => 255,
116
117
    '#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.'),
118
119
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
120
  );
121
122

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

  return $form;
}


/**
149
 * Verify that a URL alias is valid
150
151
 */
function path_admin_form_validate($form, &$form_state) {
152
153
  $source = $form_state['values']['source'];
  $alias = $form_state['values']['alias'];
154
155
156
157
  $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'] : '';

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

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

/**
175
 * Save a URL alias to the database.
176
177
 */
function path_admin_form_submit($form, &$form_state) {
178
179
180
181
182
183
184
  $path = array();
  foreach (array('source', 'alias', 'pid', 'language') as $key) {
    if (isset($form_state['values'][$key])) {
      $path[$key] = $form_state['values'][$key];
    }
  }
  path_save($path);
185
186

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

/**
 * Menu callback; confirms deleting an URL alias
193
 */
194
function path_admin_delete_confirm($form, &$form_state, $path) {
195
  if (user_access('administer url aliases')) {
196
197
198
199
200
201
202
    $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'
    );
203
  }
204
  return array();
205
206
207
208
}

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


/**
 * Return a form to filter URL aliases.
 *
 * @ingroup forms
223
 * @see path_admin_filter_form_submit()
224
 */
225
function path_admin_filter_form($form, &$form_state, $keys = '') {
226
  $form['#attributes'] = array('class' => array('search-form'));
227
228
229
230
231
232
233
234
  $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,
235
    '#maxlength' => 128,
236
237
    '#size' => 25,
  );
238
239
240
241
242
243
244
245
246
247
248
249
  $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'),
    );
  }
250
251
252
253
  return $form;
}

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

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