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

/**
 * @file
6
 * Administrative page callbacks for the path module.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 */

/**
 * 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.
  $output = drupal_get_form('path_admin_filter_form', $keys);
  // Enable language column if locale is enabled or if we have any alias with language
  $count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE language != ''"));
  $multilanguage = (module_exists('locale') || $count);

  if ($keys) {
22
    // Replace wildcards with PDO wildcards.
23
    $keys = preg_replace('!\*+!', '%', $keys);
24
25
    $sql = "SELECT * FROM {url_alias} WHERE dst LIKE :keys";
    $args = array(':keys' => '%'. $keys .'%');
26
27
28
  }
  else {
    $sql = 'SELECT * FROM {url_alias}';
29
    $args = array();
30
31
32
33
34
35
36
  }
  $header = array(
    array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
    array('data' => t('System'), 'field' => 'src'),
    array('data' => t('Operations'), 'colspan' => '2')
  );
  if ($multilanguage) {
37
    array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
38
39
  }
  $sql .= tablesort_sql($header);
40
  $result = pager_query($sql, 50, 0 , NULL, $args);
41
42
43
44
45
46

  $rows = array();
  $destination = drupal_get_destination();
  while ($data = db_fetch_object($result)) {
    $row = array(check_plain($data->dst), check_plain($data->src), l(t('edit'), "admin/build/path/edit/$data->pid", array('query' => $destination)), l(t('delete'), "admin/build/path/delete/$data->pid", array('query' => $destination)));
    if ($multilanguage) {
47
      array_splice($row, 2, 0, module_invoke('locale', 'language_name', $data->language));
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    }
    $rows[] = $row;
  }

  if (empty($rows)) {
    $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases available.') ;
    $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
  }

  $output .= theme('table', $header, $rows);
  $output .= theme('pager', NULL, 50, 0);

  return $output;
}

/**
 * Menu callback; handles pages for creating and editing URL aliases.
 */
66
function path_admin_edit($pid = 0) {
67
68
69
  if ($pid) {
    $alias = path_load($pid);
    drupal_set_title(check_plain($alias['dst']));
70
    $output = drupal_get_form('path_admin_form', $alias);
71
72
  }
  else {
73
    $output = drupal_get_form('path_admin_form');
74
75
76
77
78
79
80
81
82
  }

  return $output;
}

/**
 * Return a form for editing or creating an individual URL alias.
 *
 * @ingroup forms
83
84
 * @see path_admin_form_validate()
 * @see path_admin_form_submit()
85
86
 */
function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' => '', 'language' => '', 'pid' => NULL)) {
87

88
89
90
91
92
93
94
95
96
  $form['#alias'] = $edit;

  $form['src'] = array(
    '#type' => 'textfield',
    '#title' => t('Existing system path'),
    '#default_value' => $edit['src'],
    '#maxlength' => 64,
    '#size' => 45,
    '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
97
98
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
99
100
101
102
103
104
105
106
  );
  $form['dst'] = array(
    '#type' => 'textfield',
    '#title' => t('Path alias'),
    '#default_value' => $edit['dst'],
    '#maxlength' => 64,
    '#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.'),
107
108
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
    '#required' => TRUE,
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  );
  // This will be a hidden value unless locale module is enabled
  $form['language'] = array(
    '#type' => 'value',
    '#value' => $edit['language']
  );
  if ($edit['pid']) {
    $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
    $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
  }
  else {
    $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
  }

  return $form;
}


/**
 * Verify that a new URL alias is valid
 */
function path_admin_form_validate($form, &$form_state) {
  $src = $form_state['values']['src'];
  $dst = $form_state['values']['dst'];
  $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'] : '';

  if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) {
    form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst)));
  }
140
  $item = menu_get_item($src);
141
142
  if (!$item) {
    form_set_error('src', t("The path '@link_path' is invalid.", array('@link_path' => $src)));
143
  }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}

/**
 * Save a new URL alias to the database.
 */
function path_admin_form_submit($form, &$form_state) {
  // Language is only set if locale module is enabled
  path_set_alias($form_state['values']['src'], $form_state['values']['dst'], isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0, isset($form_state['values']['language']) ? $form_state['values']['language'] : '');

  drupal_set_message(t('The alias has been saved.'));
  $form_state['redirect'] = 'admin/build/path';
  return;
}

/**
 * Menu callback; confirms deleting an URL alias
160
 */
161
function path_admin_delete_confirm($form_state, $pid) {
162
163
164
165
166
167
168
169
170
171
172
173
  $path = path_load($pid);
  if (user_access('administer url aliases')) {
    $form['pid'] = array('#type' => 'value', '#value' => $pid);
    $output = confirm_form($form,
      t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])),
      isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path');
  }
  return $output;
}

/**
 * Execute URL alias deletion
174
 */
175
176
177
178
179
180
181
182
183
184
185
186
187
function path_admin_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    path_admin_delete($form_state['values']['pid']);
    $form_state['redirect'] = 'admin/build/path';
    return;
  }
}


/**
 * Return a form to filter URL aliases.
 *
 * @ingroup forms
188
 * @see path_admin_filter_form_submit()
189
190
191
192
193
194
195
196
197
198
199
200
201
202
 */
function path_admin_filter_form(&$form_state, $keys = '') {
  $form['#attributes'] = array('class' => 'search-form');
  $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,
    '#maxlength' => 64,
    '#size' => 25,
  );
203
204
205
206
207
208
209
210
211
212
213
214
  $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'),
    );
  }
215
216
217
218
  return $form;
}

/**
219
 * Process filter form submission when the Filter button is pressed.
220
 */
221
function path_admin_filter_form_submit_filter($form, &$form_state) {
222
  $form_state['redirect'] = 'admin/build/path/list/' . trim($form_state['values']['filter']);
223
224
}

225
226
227
228
229
230
231
232
/**
 * Process filter form submission when the Reset button is pressed.
 */
function path_admin_filter_form_submit_reset($form, &$form_state) {
  $form_state['redirect'] = 'admin/build/path/list';
}


233
234
235
236
237
238
239
240
/**
 * Helper function for grabbing filter keys.
 */
function path_admin_filter_get_keys() {
  // Extract keys as remainder of path
  $path = explode('/', $_GET['q'], 5);
  return count($path) == 5 ? $path[4] : '';
}