[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

   1  <?php
   2  // $Id: path.module,v 1.138.2.3 2008/11/22 10:49:15 dries Exp $
   3  
   4  /**
   5   * @file
   6   * Enables users to rename URLs.
   7   */
   8  
   9  /**
  10   * Implementation of hook_help().
  11   */
  12  function path_help($path, $arg) {
  13    switch ($path) {
  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 appropriately permissioned users to specify an optional alias in all node input and editing forms, and provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are <em>administer url aliases</em> and <em>create url aliases</em>. ') .'</p>';
  25        $output .= '<p>'. t('This module also provides 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 mass URL aliasing. ') .'</p>';
  26        $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
  27        return $output;
  28      case 'admin/build/path':
  29        return '<p>'. t("Drupal provides complete control over URLs through aliasing, which is often used to make URLs more readable or easy to remember. For example, the alias 'about' may be mapped onto the post at the system path 'node/1', creating a more meaningful URL. 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() {
  39    $items['admin/build/path'] = array(
  40      'title' => 'URL aliases',
  41      'description' => "Change your site's URL paths by aliasing them.",
  42      'page callback' => 'path_admin_overview',
  43      'access arguments' => array('administer url aliases'),
  44      'file' => 'path.admin.inc',
  45    );
  46    $items['admin/build/path/edit'] = array(
  47      'title' => 'Edit alias',
  48      'page callback' => 'path_admin_edit',
  49      'access arguments' => array('administer url aliases'),
  50      'type' => MENU_CALLBACK,
  51      'file' => 'path.admin.inc',
  52    );
  53    $items['admin/build/path/delete'] = array(
  54      'title' => 'Delete alias',
  55      'page callback' => 'drupal_get_form',
  56      'page arguments' => array('path_admin_delete_confirm'),
  57      'access arguments' => array('administer url aliases'),
  58      'type' => MENU_CALLBACK,
  59      'file' => 'path.admin.inc',
  60    );
  61    $items['admin/build/path/list'] = array(
  62      'title' => 'List',
  63      'type' => MENU_DEFAULT_LOCAL_TASK,
  64      'weight' => -10,
  65    );
  66    $items['admin/build/path/add'] = array(
  67      'title' => 'Add alias',
  68      'page callback' => 'path_admin_edit',
  69      'access arguments' => array('administer url aliases'),
  70      'type' => MENU_LOCAL_TASK,
  71      'file' => 'path.admin.inc',
  72    );
  73  
  74    return $items;
  75  }
  76  
  77  /**
  78   * Post-confirmation; delete an URL alias.
  79   */
  80  function path_admin_delete($pid = 0) {
  81    db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  82    drupal_set_message(t('The alias has been deleted.'));
  83  }
  84  
  85  /**
  86   * Set an aliased path for a given Drupal path, preventing duplicates.
  87   */
  88  function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
  89    $path = urldecode($path);
  90    $alias = urldecode($alias);
  91    // First we check if we deal with an existing alias and delete or modify it based on pid.
  92    if ($pid) {
  93      // An existing alias.
  94      if (!$path || !$alias) {
  95        // Delete the alias based on pid.
  96        db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  97      }
  98      else {
  99        // Update the existing alias.
 100        db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
 101      }
 102    }
 103    else if ($path && $alias) {
 104      // Check for existing aliases.
 105      if ($alias == drupal_get_path_alias($path, $language)) {
 106        // There is already such an alias, neutral or in this language.
 107        // Update the alias based on alias; setting the language if not yet done.
 108        db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
 109      }
 110      else {
 111        // A new alias. Add it to the database.
 112        db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
 113      }
 114    }
 115    else {
 116      // Delete the alias.
 117      if ($alias) {
 118        db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
 119      }
 120      else {
 121        db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
 122      }
 123    }
 124    drupal_clear_path_cache();
 125  }
 126  
 127  
 128  /**
 129   * Implementation of hook_nodeapi().
 130   *
 131   * Allows URL aliases for nodes to be specified at node edit time rather
 132   * than through the administrative interface.
 133   */
 134  function path_nodeapi(&$node, $op, $arg) {
 135    // Permissions are required for everything except node loading.
 136    if (user_access('create url aliases') || user_access('administer url aliases') || ($op == 'load')) {
 137      $language = isset($node->language) ? $node->language : '';
 138      switch ($op) {
 139        case 'validate':
 140          if (isset($node->path)) {
 141            $node->path = trim($node->path);
 142            if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s' AND language = '%s'", $node->path, "node/$node->nid", $language))) {
 143              form_set_error('path', t('The path is already in use.'));
 144            }
 145          }
 146          break;
 147  
 148        case 'load':
 149          $path = 'node/'. $node->nid;
 150          $alias = drupal_get_path_alias($path, $language);
 151          if ($path != $alias) {
 152            $node->path = $alias;
 153          }
 154          break;
 155  
 156        case 'insert':
 157          // Don't try to insert if path is NULL. We may have already set
 158          // the alias ahead of time.
 159          if (isset($node->path)) {
 160            path_set_alias('node/'. $node->nid, $node->path, NULL, $language);
 161          }
 162          break;
 163  
 164        case 'update':
 165          path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
 166          break;
 167  
 168        case 'delete':
 169          $path = 'node/'. $node->nid;
 170          if (drupal_get_path_alias($path) != $path) {
 171            path_set_alias($path);
 172          }
 173          break;
 174      }
 175    }
 176  }
 177  
 178  /**
 179   * Implementation of hook_form_alter().
 180   */
 181  function path_form_alter(&$form, $form_state, $form_id) {
 182    if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
 183      $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
 184      $form['path'] = array(
 185        '#type' => 'fieldset',
 186        '#title' => t('URL path settings'),
 187        '#collapsible' => TRUE,
 188        '#collapsed' => empty($path),
 189        '#access' => user_access('create url aliases'),
 190        '#weight' => 30,
 191      );
 192      $form['path']['path'] = array(
 193        '#type' => 'textfield',
 194        '#default_value' => $path,
 195        '#maxlength' => 128,
 196        '#collapsible' => TRUE,
 197        '#collapsed' => TRUE,
 198        '#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.'),
 199      );
 200      if ($path) {
 201        $form['path']['pid'] = array(
 202          '#type' => 'value',
 203          '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
 204        );
 205      }
 206    }
 207  }
 208  
 209  /**
 210   * Implementation of hook_perm().
 211   */
 212  function path_perm() {
 213    return array('create url aliases', 'administer url aliases');
 214  }
 215  
 216  /**
 217   * Fetch a specific URL alias from the database.
 218   */
 219  function path_load($pid) {
 220    return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
 221  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7