[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/path/ -> path.module (source)

   1  <?php
   2  // $Id: path.module,v 1.105.2.1 2007/05/21 00:52:28 drumm Exp $
   3  
   4  /**
   5   * @file
   6   * Enables users to rename URLs.
   7   */
   8  
   9  /**
  10   * Implementation of hook_help().
  11   */
  12  function path_help($section) {
  13    switch ($section) {
  14      case 'admin/help#path':
  15        $output = '<p>'. t('The path module allows you to specify aliases for Drupal URLs. Such aliases improve readability of URLs for your users and may help internet search engines to index your content more effectively. More than one alias may be created for a given page.') .'</p>';
  16        $output .= t('<p>Some examples of URL aliases are:</p>
  17  <ul>
  18  <li>user/login =&gt; login</li>
  19  <li>image/tid/16 =&gt; store</li>
  20  <li>taxonomy/term/7+19+20+21 =&gt; store/products/whirlygigs</li>
  21  <li>node/3 =&gt; contact</li>
  22  </ul>
  23  ');
  24        $output .= '<p>'. t('The path module enables an extra field for aliases in all node input and editing forms (when users have the appropriate permissions). It also provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are "administer url aliases" and "create url aliases". ') .'</p>';
  25        $output .= '<p>'. t('This module also comes with user-defined mass URL aliasing capabilities, which is useful if you wish to uniformly use URLs different from the default. For example, you may want to have your URLs presented in a different language. Access to the Drupal source code on the web server is required to set up these kinds of aliases. ') .'</p>';
  26        $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@path">Path page</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
  27        return $output;
  28      case 'admin/build/path':
  29        return '<p>'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'</p>';
  30      case 'admin/build/path/add':
  31        return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>';
  32    }
  33  }
  34  
  35  /**
  36   * Implementation of hook_menu().
  37   */
  38  function path_menu($may_cache) {
  39    $items = array();
  40  
  41    if ($may_cache) {
  42      $items[] = array('path' => 'admin/build/path', 'title' => t('URL aliases'),
  43        'description' => t('Change your site\'s URL paths by aliasing them.'),
  44        'callback' => 'path_admin',
  45        'access' => user_access('administer url aliases'));
  46      $items[] = array('path' => 'admin/build/path/edit', 'title' => t('Edit alias'),
  47        'callback' => 'drupal_get_form',
  48        'callback arguments' => array('path_admin_edit'),
  49        'access' => user_access('administer url aliases'),
  50        'type' => MENU_CALLBACK);
  51      $items[] = array('path' => 'admin/build/path/delete', 'title' => t('Delete alias'),
  52        'callback' => 'drupal_get_form',
  53        'callback arguments' => array('path_admin_delete_confirm'),
  54        'access' => user_access('administer url aliases'),
  55        'type' => MENU_CALLBACK);
  56      $items[] = array('path' => 'admin/build/path/list', 'title' => t('List'),
  57        'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
  58      $items[] = array('path' => 'admin/build/path/add', 'title' => t('Add alias'),
  59        'callback' => 'drupal_get_form',
  60        'callback arguments' => array('path_admin_edit'),
  61        'access' => user_access('administer url aliases'),
  62        'type' => MENU_LOCAL_TASK);
  63    }
  64  
  65    return $items;
  66  }
  67  
  68  /**
  69   * Menu callback; presents an overview of all URL aliases.
  70   */
  71  function path_admin() {
  72    return path_overview();
  73  }
  74  
  75  /**
  76   * Menu callback; handles pages for creating and editing URL aliases.
  77   */
  78  function path_admin_edit($pid = 0) {
  79    if ($pid) {
  80      $alias = path_load($pid);
  81      drupal_set_title(check_plain($alias['dst']));
  82      $output = path_form($alias);
  83    }
  84    else {
  85      $output = path_form();
  86    }
  87  
  88    return $output;
  89  }
  90  
  91  /**
  92   * Menu callback; confirms deleting an URL alias
  93   **/
  94  function path_admin_delete_confirm($pid) {
  95    $path = path_load($pid);
  96    if (user_access('administer url aliases')) {
  97      $form['pid'] = array('#type' => 'value', '#value' => $pid);
  98      $output = confirm_form($form,
  99    t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])),
 100     isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path', t('This action cannot be undone.'),
 101    t('Delete'), t('Cancel') );
 102    }
 103  
 104    return $output;
 105  }
 106  
 107  /**
 108   * Execute URL alias deletion
 109   **/
 110  function path_admin_delete_confirm_submit($form_id, $form_values) {
 111    if ($form_values['confirm']) {
 112      path_admin_delete($form_values['pid']);
 113      return 'admin/build/path';
 114    }
 115  }
 116  
 117  /**
 118   * Post-confirmation; delete an URL alias.
 119   */
 120  function path_admin_delete($pid = 0) {
 121    db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
 122    drupal_set_message(t('The alias has been deleted.'));
 123  }
 124  
 125  
 126  
 127  /**
 128   * Set an aliased path for a given Drupal path, preventing duplicates.
 129   */
 130  function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
 131    if ($path && !$alias) {
 132      db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
 133      drupal_clear_path_cache();
 134    }
 135    else if (!$path && $alias) {
 136      db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
 137      drupal_clear_path_cache();
 138    }
 139    else if ($path && $alias) {
 140      $path = urldecode($path);
 141      $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
 142      $alias = urldecode($alias);
 143      $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
 144  
 145      // We have an insert:
 146      if ($path_count == 0 && $alias_count == 0) {
 147        db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
 148        drupal_clear_path_cache();
 149      }
 150      else if ($path_count >= 1 && $alias_count == 0) {
 151        if ($pid) {
 152          db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid);
 153        }
 154        else {
 155          db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
 156        }
 157        drupal_clear_path_cache();
 158      }
 159      else if ($path_count == 0 && $alias_count == 1) {
 160        db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
 161        drupal_clear_path_cache();
 162      }
 163      else if ($path_count == 1 && $alias_count == 1) {
 164        // This will delete the path that alias was originally pointing to:
 165        path_set_alias(NULL, $alias);
 166        path_set_alias($path);
 167        path_set_alias($path, $alias);
 168      }
 169    }
 170  }
 171  
 172  /**
 173   * Return a form for editing or creating an individual URL alias.
 174   */
 175  function path_form($edit = '') {
 176    $form['#base'] = 'path_form';
 177  
 178    $form['src'] = array(
 179      '#type' => 'textfield',
 180      '#title' => t('Existing system path'),
 181      '#default_value' => $edit['src'],
 182      '#maxlength' => 64,
 183      '#size' => 45,
 184      '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
 185      '#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q=')
 186    );
 187    $form['dst'] = array(
 188      '#type' => 'textfield',
 189      '#default_value' => $edit['dst'],
 190      '#maxlength' => 64,
 191      '#size' => 45,
 192      '#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.'),
 193      '#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q=')
 194    );
 195  
 196    if ($edit['pid']) {
 197      $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
 198      $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
 199    }
 200    else {
 201      $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
 202    }
 203  
 204    return $form;
 205  }
 206  
 207  /**
 208   * Implementation of hook_nodeapi().
 209   *
 210   * Allows URL aliases for nodes to be specified at node edit time rather
 211   * than through the administrative interface.
 212   */
 213  function path_nodeapi(&$node, $op, $arg) {
 214    if (user_access('create url aliases') || user_access('administer url aliases')) {
 215      switch ($op) {
 216        case 'validate':
 217          $node->path = trim($node->path);
 218          if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
 219            form_set_error('path', t('The path is already in use.'));
 220          }
 221          break;
 222  
 223        case 'load':
 224          $path = "node/$node->nid";
 225          // We don't use drupal_get_path_alias() to avoid custom rewrite functions.
 226          // We only care about exact aliases.
 227          $result = db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path);
 228          if (db_num_rows($result)) {
 229            $node->path = db_result($result);
 230          }
 231          break;
 232  
 233        case 'insert':
 234          // Don't try to insert if path is NULL. We may have already set
 235          // the alias ahead of time.
 236          if ($node->path) {
 237            path_set_alias("node/$node->nid", $node->path);
 238          }
 239          break;
 240  
 241        case 'update':
 242          path_set_alias("node/$node->nid", $node->path, $node->pid);
 243          break;
 244  
 245        case 'delete':
 246          $path = "node/$node->nid";
 247          if (drupal_get_path_alias($path) != $path) {
 248            path_set_alias($path);
 249          }
 250          break;
 251      }
 252    }
 253  }
 254  
 255  /**
 256   * Implementation of hook_form_alter().
 257   */
 258  function path_form_alter($form_id, &$form) {
 259    if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
 260      $path = $form['#node']->path;
 261      $form['path'] = array(
 262        '#type' => 'fieldset',
 263        '#title' => t('URL path settings'),
 264        '#collapsible' => TRUE,
 265        '#collapsed' => empty($path),
 266        '#access' => user_access('create url aliases'),
 267        '#weight' => 30,
 268      );
 269      $form['path']['path'] = array(
 270        '#type' => 'textfield',
 271        '#default_value' => $path,
 272        '#maxlength' => 250,
 273        '#collapsible' => TRUE,
 274        '#collapsed' => TRUE,
 275        '#description' => t('Optionally specify an alternative URL by which this node 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.'),
 276      );
 277      if ($path) {
 278        $form['path']['pid'] = array(
 279          '#type' => 'value',
 280          '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $path))
 281        );
 282      }
 283    }
 284  }
 285  
 286  
 287  /**
 288   * Implementation of hook_perm().
 289   */
 290  function path_perm() {
 291    return array('create url aliases', 'administer url aliases');
 292  }
 293  
 294  /**
 295   * Return a listing of all defined URL aliases.
 296   */
 297  function path_overview() {
 298    $sql = 'SELECT * FROM {url_alias}';
 299    $header = array(
 300      array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
 301      array('data' => t('System'), 'field' => 'src'),
 302      array('data' => t('Operations'), 'colspan' => '2')
 303    );
 304    $sql .= tablesort_sql($header);
 305    $result = pager_query($sql, 50);
 306  
 307    $destination = drupal_get_destination();
 308    while ($data = db_fetch_object($result)) {
 309      $rows[] = array(check_plain($data->dst), check_plain($data->src), l(t('edit'), "admin/build/path/edit/$data->pid", array(), $destination), l(t('delete'), "admin/build/path/delete/$data->pid", array(), $destination));
 310    }
 311  
 312    if (!$rows) {
 313      $rows[] = array(array('data' => t('No URL aliases available.'), 'colspan' => '4'));
 314    }
 315  
 316    $output = theme('table', $header, $rows);
 317    $output .= theme('pager', NULL, 50, 0);
 318    return $output;
 319  }
 320  
 321  /**
 322   * Fetch a specific URL alias from the database.
 323   */
 324  function path_load($pid) {
 325    return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
 326  }
 327  
 328  /**
 329   * Verify that a new URL alias is valid
 330   */
 331  function path_form_validate($form_id, $form_values) {
 332    $src = $form_values['src'];
 333    $dst = $form_values['dst'];
 334    $pid = $form_values['pid'];
 335  
 336    if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s'", $pid, $dst))) {
 337      form_set_error('dst', t('The alias %alias is already in use.', array('%alias' => $dst)));
 338    }
 339  }
 340  
 341  /**
 342   * Save a new URL alias to the database.
 343   */
 344  function path_form_submit($form_id, $form_values) {
 345    path_set_alias($form_values['src'], $form_values['dst'], $form_values['pid']);
 346  
 347    drupal_set_message(t('The alias has been saved.'));
 348    return 'admin/build/path';
 349  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics