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

Line #Times calledCode
1
<?php
2
// $Id: path.admin.inc,v 1.14 2008/10/13 00:33:03 webchick 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 PDO wildcards.
230
    $keys = preg_replace('!\*+!', '%', $keys);
240
    $sql = "SELECT * FROM {url_alias} WHERE dst LIKE :keys";
250
    $args = array(':keys' => '%'. $keys .'%');
260
  }
27
  else {
283
    $sql = 'SELECT * FROM {url_alias}';
293
    $args = array();
30
  }
31
  $header = array(
323
    array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
333
    array('data' => t('System'), 'field' => 'src'),
343
    array('data' => t('Operations'), 'colspan' => '2')
353
  );
363
  if ($multilanguage) {
370
    array_splice($header, 2, 0, array(array('data' => t('Language'),
'field' => 'language')));
380
  }
393
  $sql .= tablesort_sql($header);
403
  $result = pager_query($sql, 50, 0 , NULL, $args);
41
423
  $rows = array();
433
  $destination = drupal_get_destination();
443
  while ($data = db_fetch_object($result)) {
452
    $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)));
462
    if ($multilanguage) {
470
      array_splice($row, 2, 0, module_invoke('locale', 'language_name',
$data->language));
480
    }
492
    $rows[] = $row;
502
  }
51
523
  if (empty($rows)) {
531
    $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases
available.') ;
541
    $rows[] = array(array('data' => $empty_message, 'colspan' =>
($multilanguage ? 5 : 4)));
551
  }
56
573
  $output .= theme('table', $header, $rows);
583
  $output .= theme('pager', NULL, 50, 0);
59
603
  return $output;
610
}
62
63
/**
64
 * Menu callback; handles pages for creating and editing URL aliases.
65
 */
6611
function path_admin_edit($pid = 0) {
676
  if ($pid) {
682
    $alias = path_load($pid);
692
    drupal_set_title($alias['dst']);
702
    $output = drupal_get_form('path_admin_form', $alias);
711
  }
72
  else {
734
    $output = drupal_get_form('path_admin_form');
74
  }
75
764
  return $output;
770
}
78
79
/**
80
 * Return a form for editing or creating an individual URL alias.
81
 *
82
 * @ingroup forms
83
 * @see path_admin_form_validate()
84
 * @see path_admin_form_submit()
85
 */
8611
function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' =>
'', 'language' => '', 'pid' => NULL)) {
87
886
  $form['#alias'] = $edit;
89
906
  $form['src'] = array(
916
    '#type' => 'textfield',
926
    '#title' => t('Existing system path'),
936
    '#default_value' => $edit['src'],
946
    '#maxlength' => 64,
956
    '#size' => 45,
966
    '#description' => t('Specify the existing path you wish to alias. For
example: node/28, forum/1, taxonomy/term/1+2.'),
976
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) .
(variable_get('clean_url', 0) ? '' : '?q='),
986
    '#required' => TRUE,
99
  );
1006
  $form['dst'] = array(
1016
    '#type' => 'textfield',
1026
    '#title' => t('Path alias'),
1036
    '#default_value' => $edit['dst'],
1046
    '#maxlength' => 64,
1056
    '#size' => 45,
1066
    '#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.'),
1076
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) .
(variable_get('clean_url', 0) ? '' : '?q='),
1086
    '#required' => TRUE,
109
  );
110
  // This will be a hidden value unless locale module is enabled
1116
  $form['language'] = array(
1126
    '#type' => 'value',
1136
    '#value' => $edit['language']
1146
  );
1156
  if ($edit['pid']) {
1162
    $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
1172
    $form['submit'] = array('#type' => 'submit', '#value' => t('Update
alias'));
1182
  }
119
  else {
1204
    $form['submit'] = array('#type' => 'submit', '#value' => t('Create new
alias'));
121
  }
122
1236
  return $form;
1240
}
125
126
127
/**
128
 * Verify that a new URL alias is valid
129
 */
13011
function path_admin_form_validate($form, &$form_state) {
1313
  $src = $form_state['values']['src'];
1323
  $dst = $form_state['values']['dst'];
1333
  $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid']
: 0;
134
  // Language is only set if locale module is enabled, otherwise save for
all languages.
1353
  $language = isset($form_state['values']['language']) ?
$form_state['values']['language'] : '';
136
1373
  if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid !=
%d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) {
1381
    form_set_error('dst', t('The alias %alias is already in use in this
language.', array('%alias' => $dst)));
1391
  }
1403
  $item = menu_get_item($src);
1413
  if (!$item) {
1420
    form_set_error('src', t("The path '@link_path' is invalid.",
array('@link_path' => $src)));
1430
  }
1443
}
145
146
/**
147
 * Save a new URL alias to the database.
148
 */
14911
function path_admin_form_submit($form, &$form_state) {
150
  // Language is only set if locale module is enabled
1512
  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'] : '');
152
1532
  drupal_set_message(t('The alias has been saved.'));
1542
  $form_state['redirect'] = 'admin/build/path';
1552
  return;
1560
}
157
158
/**
159
 * Menu callback; confirms deleting an URL alias
160
 */
16111
function path_admin_delete_confirm($form_state, $pid) {
1622
  $path = path_load($pid);
1632
  if (user_access('administer url aliases')) {
1642
    $form['pid'] = array('#type' => 'value', '#value' => $pid);
1652
    $output = confirm_form($form,
1662
      t('Are you sure you want to delete path alias %title?',
array('%title' => $path['dst'])),
1672
      isset($_GET['destination']) ? $_GET['destination'] :
'admin/build/path');
1682
  }
1692
  return $output;
1700
}
171
172
/**
173
 * Execute URL alias deletion
174
 */
17511
function path_admin_delete_confirm_submit($form, &$form_state) {
1761
  if ($form_state['values']['confirm']) {
1771
    path_admin_delete($form_state['values']['pid']);
1781
    $form_state['redirect'] = 'admin/build/path';
1791
    return;
1800
  }
1810
}
182
183
184
/**
185
 * Return a form to filter URL aliases.
186
 *
187
 * @ingroup forms
188
 * @see path_admin_filter_form_submit()
189
 */
19011
function path_admin_filter_form(&$form_state, $keys = '') {
1913
  $form['#attributes'] = array('class' => 'search-form');
1923
  $form['basic'] = array('#type' => 'fieldset',
1933
    '#title' => t('Filter aliases')
1943
  );
1953
  $form['basic']['inline'] = array('#prefix' => '<div
class="container-inline">', '#suffix' => '</div>');
1963
  $form['basic']['inline']['filter'] = array(
1973
    '#type' => 'textfield',
1983
    '#title' => '',
1993
    '#default_value' => $keys,
2003
    '#maxlength' => 64,
2013
    '#size' => 25,
202
  );
2033
  $form['basic']['inline']['submit'] = array(
2043
    '#type' => 'submit',
2053
    '#value' => t('Filter'),
2063
    '#submit' => array('path_admin_filter_form_submit_filter'),
207
    );
2083
  if ($keys) {
2090
    $form['basic']['inline']['reset'] = array(
2100
      '#type' => 'submit',
2110
      '#value' => t('Reset'),
2120
      '#submit' => array('path_admin_filter_form_submit_reset'),
213
    );
2140
  }
2153
  return $form;
2160
}
217
218
/**
219
 * Process filter form submission when the Filter button is pressed.
220
 */
22111
function path_admin_filter_form_submit_filter($form, &$form_state) {
2220
  $form_state['redirect'] = 'admin/build/path/list/' .
trim($form_state['values']['filter']);
2230
}
224
225
/**
226
 * Process filter form submission when the Reset button is pressed.
227
 */
22811
function path_admin_filter_form_submit_reset($form, &$form_state) {
2290
  $form_state['redirect'] = 'admin/build/path/list';
2300
}
231
232
233
/**
234
 * Helper function for grabbing filter keys.
235
 */
23611
function path_admin_filter_get_keys() {
237
  // Extract keys as remainder of path
2380
  $path = explode('/', $_GET['q'], 5);
2390
  return count($path) == 5 ? $path[4] : '';
2400
}
24111