web/drupal/modules/path/path.admin.inc
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: path.admin.inc,v 1.7.2.1 2008/11/22 10:49:15 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  */
       
    14 function path_admin_overview($keys = NULL) {
       
    15   // Add the filter form above the overview table.
       
    16   $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
       
    18   $count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE language != ''"));
       
    19   $multilanguage = (module_exists('locale') || $count);
       
    20 
       
    21   if ($keys) {
       
    22     // Replace wildcards with MySQL/PostgreSQL wildcards.
       
    23     $keys = preg_replace('!\*+!', '%', $keys);
       
    24     $sql = "SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'";
       
    25   }
       
    26   else {
       
    27     $sql = 'SELECT * FROM {url_alias}';
       
    28   }
       
    29   $header = array(
       
    30     array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
       
    31     array('data' => t('System'), 'field' => 'src'),
       
    32     array('data' => t('Operations'), 'colspan' => '2')
       
    33   );
       
    34   if ($multilanguage) {
       
    35     $header[3] = $header[2];
       
    36     $header[2] = array('data' => t('Language'), 'field' => 'language');
       
    37   }
       
    38   $sql .= tablesort_sql($header);
       
    39   $result = pager_query($sql, 50, 0 , NULL, $keys);
       
    40 
       
    41   $rows = array();
       
    42   $destination = drupal_get_destination();
       
    43   while ($data = db_fetch_object($result)) {
       
    44     $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)));
       
    45     if ($multilanguage) {
       
    46       $row[4] = $row[3];
       
    47       $row[3] = $row[2];
       
    48       $row[2] = module_invoke('locale', 'language_name', $data->language);
       
    49     }
       
    50     $rows[] = $row;
       
    51   }
       
    52 
       
    53   if (empty($rows)) {
       
    54     $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases available.') ;
       
    55     $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
       
    56   }
       
    57 
       
    58   $output .= theme('table', $header, $rows);
       
    59   $output .= theme('pager', NULL, 50, 0);
       
    60 
       
    61   return $output;
       
    62 }
       
    63 
       
    64 /**
       
    65  * Menu callback; handles pages for creating and editing URL aliases.
       
    66  */
       
    67 function path_admin_edit($pid = 0) {
       
    68   if ($pid) {
       
    69     $alias = path_load($pid);
       
    70     drupal_set_title(check_plain($alias['dst']));
       
    71     $output = drupal_get_form('path_admin_form', $alias);
       
    72   }
       
    73   else {
       
    74     $output = drupal_get_form('path_admin_form');
       
    75   }
       
    76 
       
    77   return $output;
       
    78 }
       
    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  */
       
    87 function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' => '', 'language' => '', 'pid' => NULL)) {
       
    88 
       
    89   $form['#alias'] = $edit;
       
    90 
       
    91   $form['src'] = array(
       
    92     '#type' => 'textfield',
       
    93     '#title' => t('Existing system path'),
       
    94     '#default_value' => $edit['src'],
       
    95     '#maxlength' => 128,
       
    96     '#size' => 45,
       
    97     '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
       
    98     '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
       
    99     '#required' => TRUE,
       
   100   );
       
   101   $form['dst'] = array(
       
   102     '#type' => 'textfield',
       
   103     '#title' => t('Path alias'),
       
   104     '#default_value' => $edit['dst'],
       
   105     '#maxlength' => 128,
       
   106     '#size' => 45,
       
   107     '#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.'),
       
   108     '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
       
   109     '#required' => TRUE,
       
   110   );
       
   111   // This will be a hidden value unless locale module is enabled
       
   112   $form['language'] = array(
       
   113     '#type' => 'value',
       
   114     '#value' => $edit['language']
       
   115   );
       
   116   if ($edit['pid']) {
       
   117     $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
       
   118     $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
       
   119   }
       
   120   else {
       
   121     $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
       
   122   }
       
   123 
       
   124   return $form;
       
   125 }
       
   126 
       
   127 
       
   128 /**
       
   129  * Verify that a new URL alias is valid
       
   130  */
       
   131 function path_admin_form_validate($form, &$form_state) {
       
   132   $src = $form_state['values']['src'];
       
   133   $dst = $form_state['values']['dst'];
       
   134   $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.
       
   136   $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
       
   137 
       
   138   if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) {
       
   139     form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst)));
       
   140   }
       
   141   $item = menu_get_item($src);
       
   142   if (!$item || !$item['access']) {
       
   143     form_set_error('src', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $src)));
       
   144   }
       
   145 }
       
   146 
       
   147 /**
       
   148  * Save a new URL alias to the database.
       
   149  */
       
   150 function path_admin_form_submit($form, &$form_state) {
       
   151   // Language is only set if locale module is enabled
       
   152   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 
       
   154   drupal_set_message(t('The alias has been saved.'));
       
   155   $form_state['redirect'] = 'admin/build/path';
       
   156   return;
       
   157 }
       
   158 
       
   159 /**
       
   160  * Menu callback; confirms deleting an URL alias
       
   161  */
       
   162 function path_admin_delete_confirm($form_state, $pid) {
       
   163   $path = path_load($pid);
       
   164   if (user_access('administer url aliases')) {
       
   165     $form['pid'] = array('#type' => 'value', '#value' => $pid);
       
   166     $output = confirm_form($form,
       
   167       t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])),
       
   168       isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path');
       
   169   }
       
   170   return $output;
       
   171 }
       
   172 
       
   173 /**
       
   174  * Execute URL alias deletion
       
   175  */
       
   176 function path_admin_delete_confirm_submit($form, &$form_state) {
       
   177   if ($form_state['values']['confirm']) {
       
   178     path_admin_delete($form_state['values']['pid']);
       
   179     $form_state['redirect'] = 'admin/build/path';
       
   180     return;
       
   181   }
       
   182 }
       
   183 
       
   184 
       
   185 /**
       
   186  * Return a form to filter URL aliases.
       
   187  *
       
   188  * @ingroup forms
       
   189  * @see path_admin_filter_form_submit()
       
   190  */
       
   191 function path_admin_filter_form(&$form_state, $keys = '') {
       
   192   $form['#attributes'] = array('class' => 'search-form');
       
   193   $form['basic'] = array('#type' => 'fieldset',
       
   194     '#title' => t('Filter aliases')
       
   195   );
       
   196   $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
       
   197   $form['basic']['inline']['filter'] = array(
       
   198     '#type' => 'textfield',
       
   199     '#title' => '',
       
   200     '#default_value' => $keys,
       
   201     '#maxlength' => 128,
       
   202     '#size' => 25,
       
   203   );
       
   204   $form['basic']['inline']['submit'] = array(
       
   205     '#type' => 'submit',
       
   206     '#value' => t('Filter'),
       
   207     '#submit' => array('path_admin_filter_form_submit_filter'),
       
   208     );
       
   209   if ($keys) {
       
   210     $form['basic']['inline']['reset'] = array(
       
   211       '#type' => 'submit',
       
   212       '#value' => t('Reset'),
       
   213       '#submit' => array('path_admin_filter_form_submit_reset'),
       
   214     );
       
   215   }
       
   216   return $form;
       
   217 }
       
   218 
       
   219 /**
       
   220  * Process filter form submission when the Filter button is pressed.
       
   221  */
       
   222 function path_admin_filter_form_submit_filter($form, &$form_state) {
       
   223   $form_state['redirect'] = 'admin/build/path/list/'. trim($form_state['values']['filter']);
       
   224 }
       
   225 
       
   226 /**
       
   227  * Process filter form submission when the Reset button is pressed.
       
   228  */
       
   229 function path_admin_filter_form_submit_reset($form, &$form_state) {
       
   230   $form_state['redirect'] = 'admin/build/path/list';
       
   231 }
       
   232 
       
   233 
       
   234 /**
       
   235  * Helper function for grabbing filter keys.
       
   236  */
       
   237 function path_admin_filter_get_keys() {
       
   238   // Extract keys as remainder of path
       
   239   $path = explode('/', $_GET['q'], 5);
       
   240   return count($path) == 5 ? $path[4] : '';
       
   241 }