| 1 |
<?php
|
| 2 |
// $Id: path.admin.inc,v 1.35 2009/10/20 01:24:34 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administrative page callbacks for the path module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Return a listing of all defined URL aliases.
|
| 11 |
*
|
| 12 |
* When filter key passed, perform a standard search on the given key,
|
| 13 |
* and return the list of matching URL aliases.
|
| 14 |
*/
|
| 15 |
function path_admin_overview($keys = NULL) {
|
| 16 |
// 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 |
array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
|
| 24 |
array('data' => t('System'), 'field' => 'source'),
|
| 25 |
array('data' => t('Operations'), 'colspan' => '2')
|
| 26 |
);
|
| 27 |
if ($multilanguage) {
|
| 28 |
array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
|
| 29 |
}
|
| 30 |
|
| 31 |
$query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
|
| 32 |
if ($keys) {
|
| 33 |
// Replace wildcards with PDO wildcards.
|
| 34 |
$query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
|
| 35 |
}
|
| 36 |
$result = $query
|
| 37 |
->fields('url_alias')
|
| 38 |
->orderByHeader($header)
|
| 39 |
->limit(50)
|
| 40 |
->execute();
|
| 41 |
|
| 42 |
$rows = array();
|
| 43 |
$destination = drupal_get_destination();
|
| 44 |
foreach ($result as $data) {
|
| 45 |
$row = array(
|
| 46 |
'data' => array(
|
| 47 |
l($data->alias, $data->source),
|
| 48 |
l($data->source, $data->source, array('alias' => TRUE)),
|
| 49 |
l(t('edit'), "admin/config/search/path/edit/$data->pid", array('query' => $destination)),
|
| 50 |
l(t('delete'), "admin/config/search/path/delete/$data->pid", array('query' => $destination)),
|
| 51 |
),
|
| 52 |
);
|
| 53 |
// If the system path maps to a different URL alias, highlight this table
|
| 54 |
// row to let the user know of old aliases.
|
| 55 |
if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
|
| 56 |
$row['class'] = array('warning');
|
| 57 |
}
|
| 58 |
if ($multilanguage) {
|
| 59 |
array_splice($row['data'], 2, 0, module_invoke('locale', 'language_name', $data->language));
|
| 60 |
}
|
| 61 |
$rows[] = $row;
|
| 62 |
}
|
| 63 |
|
| 64 |
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 |
$rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
|
| 67 |
}
|
| 68 |
|
| 69 |
$build['path_table'] = array(
|
| 70 |
'#theme' => 'table',
|
| 71 |
'#header' => $header,
|
| 72 |
'#rows' => $rows
|
| 73 |
);
|
| 74 |
$build['path_pager'] = array('#theme' => 'pager');
|
| 75 |
|
| 76 |
return $build;
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Menu callback; handles pages for creating and editing URL aliases.
|
| 81 |
*/
|
| 82 |
function path_admin_edit($path = array()) {
|
| 83 |
if ($path) {
|
| 84 |
drupal_set_title($path['alias']);
|
| 85 |
$output = drupal_get_form('path_admin_form', $path);
|
| 86 |
}
|
| 87 |
else {
|
| 88 |
$output = drupal_get_form('path_admin_form');
|
| 89 |
}
|
| 90 |
|
| 91 |
return $output;
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Return a form for editing or creating an individual URL alias.
|
| 96 |
*
|
| 97 |
* @ingroup forms
|
| 98 |
* @see path_admin_form_validate()
|
| 99 |
* @see path_admin_form_submit()
|
| 100 |
*/
|
| 101 |
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => '', 'pid' => NULL)) {
|
| 102 |
$form['source'] = array(
|
| 103 |
'#type' => 'textfield',
|
| 104 |
'#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 |
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
|
| 110 |
'#required' => TRUE,
|
| 111 |
);
|
| 112 |
$form['alias'] = array(
|
| 113 |
'#type' => 'textfield',
|
| 114 |
'#title' => t('Path alias'),
|
| 115 |
'#default_value' => $path['alias'],
|
| 116 |
'#maxlength' => 255,
|
| 117 |
'#size' => 45,
|
| 118 |
'#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 |
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
|
| 120 |
'#required' => TRUE,
|
| 121 |
);
|
| 122 |
|
| 123 |
// This will be a hidden value unless locale module is enabled.
|
| 124 |
$form['language'] = array(
|
| 125 |
'#type' => 'value',
|
| 126 |
'#value' => $path['language']
|
| 127 |
);
|
| 128 |
if ($path['pid']) {
|
| 129 |
$form['pid'] = array(
|
| 130 |
'#type' => 'hidden',
|
| 131 |
'#value' => $path['pid'],
|
| 132 |
);
|
| 133 |
$form['submit'] = array(
|
| 134 |
'#type' => 'submit',
|
| 135 |
'#value' => t('Update alias'),
|
| 136 |
);
|
| 137 |
}
|
| 138 |
else {
|
| 139 |
$form['submit'] = array(
|
| 140 |
'#type' => 'submit',
|
| 141 |
'#value' => t('Create new alias'),
|
| 142 |
);
|
| 143 |
}
|
| 144 |
|
| 145 |
return $form;
|
| 146 |
}
|
| 147 |
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Verify that a URL alias is valid
|
| 151 |
*/
|
| 152 |
function path_admin_form_validate($form, &$form_state) {
|
| 153 |
$source = &$form_state['values']['source'];
|
| 154 |
$source = drupal_get_normal_path($source);
|
| 155 |
$alias = $form_state['values']['alias'];
|
| 156 |
$pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
|
| 157 |
// Language is only set if locale module is enabled, otherwise save for all languages.
|
| 158 |
$language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
|
| 159 |
|
| 160 |
$has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array(
|
| 161 |
':pid' => $pid,
|
| 162 |
':alias' => $alias,
|
| 163 |
':language' => $language,
|
| 164 |
))
|
| 165 |
->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);
|
| 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 |
// Remove unnecessary values.
|
| 181 |
form_state_values_clean($form_state);
|
| 182 |
|
| 183 |
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 |
$form_state['path'] = $path;
|
| 195 |
return confirm_form(
|
| 196 |
$form,
|
| 197 |
t('Are you sure you want to delete path alias %title?',
|
| 198 |
array('%title' => $path['alias'])),
|
| 199 |
'admin/config/search/path'
|
| 200 |
);
|
| 201 |
}
|
| 202 |
return array();
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Execute URL alias deletion
|
| 207 |
*/
|
| 208 |
function path_admin_delete_confirm_submit($form, &$form_state) {
|
| 209 |
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 |
* Return a form to filter URL aliases.
|
| 218 |
*
|
| 219 |
* @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 |
$form['basic'] = array('#type' => 'fieldset',
|
| 225 |
'#title' => t('Filter aliases')
|
| 226 |
);
|
| 227 |
$form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
|
| 228 |
$form['basic']['inline']['filter'] = array(
|
| 229 |
'#type' => 'textfield',
|
| 230 |
'#title' => '',
|
| 231 |
'#default_value' => $keys,
|
| 232 |
'#maxlength' => 128,
|
| 233 |
'#size' => 25,
|
| 234 |
);
|
| 235 |
$form['basic']['inline']['submit'] = array(
|
| 236 |
'#type' => 'submit',
|
| 237 |
'#value' => t('Filter'),
|
| 238 |
'#submit' => array('path_admin_filter_form_submit_filter'),
|
| 239 |
);
|
| 240 |
if ($keys) {
|
| 241 |
$form['basic']['inline']['reset'] = array(
|
| 242 |
'#type' => 'submit',
|
| 243 |
'#value' => t('Reset'),
|
| 244 |
'#submit' => array('path_admin_filter_form_submit_reset'),
|
| 245 |
);
|
| 246 |
}
|
| 247 |
return $form;
|
| 248 |
}
|
| 249 |
|
| 250 |
/**
|
| 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 |
* Process filter form submission when the Reset button is pressed.
|
| 259 |
*/
|
| 260 |
function path_admin_filter_form_submit_reset($form, &$form_state) {
|
| 261 |
$form_state['redirect'] = 'admin/config/search/path/list';
|
| 262 |
}
|