Code coverage for /20080809/modules/path/path.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: path.admin.inc,v 1.9 2008/08/03 05:21:20 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
 * When filter key passed, perform a standard search on the given key,
12
 * and return the list of matching URL aliases.
13
 */
1411
function path_admin_overview($keys = NULL) {
15
  // Add the filter form above the overview table.
163
  $output = drupal_get_form('path_admin_filter_form', $keys);
17
  // Enable language column if locale is enabled or if we have any alias
with language
183
  $count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE
language != ''"));
193
  $multilanguage = (module_exists('locale') || $count);
20
213
  if ($keys) {
22
    // Replace wildcards with MySQL/PostgreSQL wildcards.
230
    $keys = preg_replace('!\*+!', '%', $keys);
240
    $sql = "SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'";
250
  }
26
  else {
273
    $sql = 'SELECT * FROM {url_alias}';
28
  }
29
  $header = array(
303
    array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
313
    array('data' => t('System'), 'field' => 'src'),
323
    array('data' => t('Operations'), 'colspan' => '2')
333
  );
343
  if ($multilanguage) {
350
    $header[3] = $header[2];
360
    $header[2] = array('data' => t('Language'), 'field' => 'language');
370
  }
383
  $sql .= tablesort_sql($header);
393
  $result = pager_query($sql, 50, 0 , NULL, $keys);
40
413
  $rows = array();
423
  $destination = drupal_get_destination();
433
  while ($data = db_fetch_object($result)) {
442
    $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)));
452
    if ($multilanguage) {
460
      $row[4] = $row[3];
470
      $row[3] = $row[2];
480
      $row[2] = module_invoke('locale', 'language_name', $data->language);
490
    }
502
    $rows[] = $row;
512
  }
52
533
  if (empty($rows)) {
541
    $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases
available.') ;
551
    $rows[] = array(array('data' => $empty_message, 'colspan' =>
($multilanguage ? 5 : 4)));
561
  }
57
583
  $output .= theme('table', $header, $rows);
593
  $output .= theme('pager', NULL, 50, 0);
60
613
  return $output;
620
}
63
64
/**
65
 * Menu callback; handles pages for creating and editing URL aliases.
66
 */
6711
function path_admin_edit($pid = 0) {
686
  if ($pid) {
692
    $alias = path_load($pid);
702
    drupal_set_title(check_plain($alias['dst']));
712
    $output = drupal_get_form('path_admin_form', $alias);
721
  }
73
  else {
744
    $output = drupal_get_form('path_admin_form');
75
  }
76
774
  return $output;
780
}
79
80
/**
81
 * Return a form for editing or creating an individual URL alias.
82
 *
83
 * @ingroup forms
84
 * @see path_admin_form_validate()
85
 * @see path_admin_form_submit()
86
 */
8711
function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' =>
'', 'language' => '', 'pid' => NULL)) {
88
896
  $form['#alias'] = $edit;
90
916
  $form['src'] = array(
926
    '#type' => 'textfield',
936
    '#title' => t('Existing system path'),
946
    '#default_value' => $edit['src'],
956
    '#maxlength' => 64,
966
    '#size' => 45,
976
    '#description' => t('Specify the existing path you wish to alias. For
example: node/28, forum/1, taxonomy/term/1+2.'),
986
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) .
(variable_get('clean_url', 0) ? '' : '?q='),
996
    '#required' => TRUE,
100
  );
1016
  $form['dst'] = array(
1026
    '#type' => 'textfield',
1036
    '#title' => t('Path alias'),
1046
    '#default_value' => $edit['dst'],
1056
    '#maxlength' => 64,
1066
    '#size' => 45,
1076
    '#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.'),
1086
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) .
(variable_get('clean_url', 0) ? '' : '?q='),
1096
    '#required' => TRUE,
110
  );
111
  // This will be a hidden value unless locale module is enabled
1126
  $form['language'] = array(
1136
    '#type' => 'value',
1146
    '#value' => $edit['language']
1156
  );
1166
  if ($edit['pid']) {
1172
    $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
1182
    $form['submit'] = array('#type' => 'submit', '#value' => t('Update
alias'));
1192
  }
120
  else {
1214
    $form['submit'] = array('#type' => 'submit', '#value' => t('Create new
alias'));
122
  }
123
1246
  return $form;
1250
}
126
127
128
/**
129
 * Verify that a new URL alias is valid
130
 */
13111
function path_admin_form_validate($form, &$form_state) {
1323
  $src = $form_state['values']['src'];
1333
  $dst = $form_state['values']['dst'];
1343
  $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid']
: 0;
135
  // Language is only set if locale module is enabled, otherwise save for
all languages.
1363
  $language = isset($form_state['values']['language']) ?
$form_state['values']['language'] : '';
137
1383
  if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid !=
%d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) {
1391
    form_set_error('dst', t('The alias %alias is already in use in this
language.', array('%alias' => $dst)));
1401
  }
1413
  $item = menu_get_item($src);
1423
  if (!$item) {
1430
    form_set_error('src', t("The path '@link_path' is invalid.",
array('@link_path' => $src)));
1440
  }
1453
}
146
147
/**
148
 * Save a new URL alias to the database.
149
 */
15011
function path_admin_form_submit($form, &$form_state) {
151
  // Language is only set if locale module is enabled
1522
  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'] : '');
153
1542
  drupal_set_message(t('The alias has been saved.'));
1552
  $form_state['redirect'] = 'admin/build/path';
1562
  return;
1570
}
158
159
/**
160
 * Menu callback; confirms deleting an URL alias
161
 */
16211
function path_admin_delete_confirm($form_state, $pid) {
1632
  $path = path_load($pid);
1642
  if (user_access('administer url aliases')) {
1652
    $form['pid'] = array('#type' => 'value', '#value' => $pid);
1662
    $output = confirm_form($form,
1672
      t('Are you sure you want to delete path alias %title?',
array('%title' => $path['dst'])),
1682
      isset($_GET['destination']) ? $_GET['destination'] :
'admin/build/path');
1692
  }
1702
  return $output;
1710
}
172
173
/**
174
 * Execute URL alias deletion
175
 */
17611
function path_admin_delete_confirm_submit($form, &$form_state) {
1771
  if ($form_state['values']['confirm']) {
1781
    path_admin_delete($form_state['values']['pid']);
1791
    $form_state['redirect'] = 'admin/build/path';
1801
    return;
1810
  }
1820
}
183
184
185
/**
186
 * Return a form to filter URL aliases.
187
 *
188
 * @ingroup forms
189
 * @see path_admin_filter_form_submit()
190
 */
19111
function path_admin_filter_form(&$form_state, $keys = '') {
1923
  $form['#attributes'] = array('class' => 'search-form');
1933
  $form['basic'] = array('#type' => 'fieldset',
1943
    '#title' => t('Filter aliases')
1953
  );
1963
  $form['basic']['inline'] = array('#prefix' => '<div
class="container-inline">', '#suffix' => '</div>');
1973
  $form['basic']['inline']['filter'] = array(
1983
    '#type' => 'textfield',
1993
    '#title' => '',
2003
    '#default_value' => $keys,
2013
    '#maxlength' => 64,
2023
    '#size' => 25,
203
  );
2043
  $form['basic']['inline']['submit'] = array(
2053
    '#type' => 'submit',
2063
    '#value' => t('Filter'),
2073
    '#submit' => array('path_admin_filter_form_submit_filter'),
208
    );
2093
  if ($keys) {
2100
    $form['basic']['inline']['reset'] = array(
2110
      '#type' => 'submit',
2120
      '#value' => t('Reset'),
2130
      '#submit' => array('path_admin_filter_form_submit_reset'),
214
    );
2150
  }
2163
  return $form;
2170
}
218
219
/**
220
 * Process filter form submission when the Filter button is pressed.
221
 */
22211
function path_admin_filter_form_submit_filter($form, &$form_state) {
2230
  $form_state['redirect'] = 'admin/build/path/list/' .
trim($form_state['values']['filter']);
2240
}
225
226
/**
227
 * Process filter form submission when the Reset button is pressed.
228
 */
22911
function path_admin_filter_form_submit_reset($form, &$form_state) {
2300
  $form_state['redirect'] = 'admin/build/path/list';
2310
}
232
233
234
/**
235
 * Helper function for grabbing filter keys.
236
 */
23711
function path_admin_filter_get_keys() {
238
  // Extract keys as remainder of path
2390
  $path = explode('/', $_GET['q'], 5);
2400
  return count($path) == 5 ? $path[4] : '';
2410
}
24211