comparison modules/path/path.module @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 2427550111ae
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: path.module,v 1.138 2008/02/03 19:20:35 goba 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 'type' => MENU_CALLBACK,
50 'file' => 'path.admin.inc',
51 );
52 $items['admin/build/path/delete'] = array(
53 'title' => 'Delete alias',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('path_admin_delete_confirm'),
56 'type' => MENU_CALLBACK,
57 'file' => 'path.admin.inc',
58 );
59 $items['admin/build/path/list'] = array(
60 'title' => 'List',
61 'type' => MENU_DEFAULT_LOCAL_TASK,
62 'weight' => -10,
63 );
64 $items['admin/build/path/add'] = array(
65 'title' => 'Add alias',
66 'page callback' => 'path_admin_edit',
67 'access arguments' => array('administer url aliases'),
68 'type' => MENU_LOCAL_TASK,
69 'file' => 'path.admin.inc',
70 );
71
72 return $items;
73 }
74
75 /**
76 * Post-confirmation; delete an URL alias.
77 */
78 function path_admin_delete($pid = 0) {
79 db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
80 drupal_set_message(t('The alias has been deleted.'));
81 }
82
83 /**
84 * Set an aliased path for a given Drupal path, preventing duplicates.
85 */
86 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
87 $path = urldecode($path);
88 $alias = urldecode($alias);
89 // First we check if we deal with an existing alias and delete or modify it based on pid.
90 if ($pid) {
91 // An existing alias.
92 if (!$path || !$alias) {
93 // Delete the alias based on pid.
94 db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
95 }
96 else {
97 // Update the existing alias.
98 db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
99 }
100 }
101 else if ($path && $alias) {
102 // Check for existing aliases.
103 if ($alias == drupal_get_path_alias($path, $language)) {
104 // There is already such an alias, neutral or in this language.
105 // Update the alias based on alias; setting the language if not yet done.
106 db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
107 }
108 else {
109 // A new alias. Add it to the database.
110 db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
111 }
112 }
113 else {
114 // Delete the alias.
115 if ($alias) {
116 db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
117 }
118 else {
119 db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
120 }
121 }
122 drupal_clear_path_cache();
123 }
124
125
126 /**
127 * Implementation of hook_nodeapi().
128 *
129 * Allows URL aliases for nodes to be specified at node edit time rather
130 * than through the administrative interface.
131 */
132 function path_nodeapi(&$node, $op, $arg) {
133 // Permissions are required for everything except node loading.
134 if (user_access('create url aliases') || user_access('administer url aliases') || ($op == 'load')) {
135 $language = isset($node->language) ? $node->language : '';
136 switch ($op) {
137 case 'validate':
138 $node->path = trim($node->path);
139 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))) {
140 form_set_error('path', t('The path is already in use.'));
141 }
142 break;
143
144 case 'load':
145 $path = 'node/'. $node->nid;
146 $alias = drupal_get_path_alias($path, $language);
147 if ($path != $alias) {
148 $node->path = $alias;
149 }
150 break;
151
152 case 'insert':
153 // Don't try to insert if path is NULL. We may have already set
154 // the alias ahead of time.
155 if (isset($node->path)) {
156 path_set_alias('node/'. $node->nid, $node->path, NULL, $language);
157 }
158 break;
159
160 case 'update':
161 path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
162 break;
163
164 case 'delete':
165 $path = 'node/'. $node->nid;
166 if (drupal_get_path_alias($path) != $path) {
167 path_set_alias($path);
168 }
169 break;
170 }
171 }
172 }
173
174 /**
175 * Implementation of hook_form_alter().
176 */
177 function path_form_alter(&$form, $form_state, $form_id) {
178 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
179 $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
180 $form['path'] = array(
181 '#type' => 'fieldset',
182 '#title' => t('URL path settings'),
183 '#collapsible' => TRUE,
184 '#collapsed' => empty($path),
185 '#access' => user_access('create url aliases'),
186 '#weight' => 30,
187 );
188 $form['path']['path'] = array(
189 '#type' => 'textfield',
190 '#default_value' => $path,
191 '#maxlength' => 250,
192 '#collapsible' => TRUE,
193 '#collapsed' => TRUE,
194 '#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.'),
195 );
196 if ($path) {
197 $form['path']['pid'] = array(
198 '#type' => 'value',
199 '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
200 );
201 }
202 }
203 }
204
205 /**
206 * Implementation of hook_perm().
207 */
208 function path_perm() {
209 return array('create url aliases', 'administer url aliases');
210 }
211
212 /**
213 * Fetch a specific URL alias from the database.
214 */
215 function path_load($pid) {
216 return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
217 }