moving the onelab www to a fresh location
[plewww.git] / modules / path.module
1 <?php
2 // $Id: path.module 144 2007-03-28 07:52:20Z thierry $
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 are related to URL aliasing are "administer a list of URL aliases" and "add 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 .= t('<p>You can</p>
27 <ul>
28 <li>set the path for a post with the path module.</li>
29 <li>add a URL alias: <a href="%admin-path-add">administer &gt;&gt; url aliases &gt;&gt; add alias</a>.</li>
30 <li>administer the list of URL aliases: <a href="%admin-path">administer &gt;&gt; url aliases</a>.</li>
31 <li>read how to <a href="%external-http-drupal-org-node-15365">configure clean URLs</a> for your webserver.
32 <li>enable clean url\'s to remove the =? at <a href="%admin-settings">administer &gt;&gt; settings</a>.</li>
33 </ul>
34 ', array('%admin-path-add' => url('admin/path/add'), '%admin-path' => url('admin/path'), '%external-http-drupal-org-node-15365' => 'http://drupal.org/node/15365', '%admin-settings' => url('admin/settings')));
35       $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>';
36       return $output;
37     case 'admin/modules#description':
38       return t('Allows users to rename URLs.');
39     case 'admin/path':
40       return t("<p>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>");
41     case 'admin/path/add':
42       return t('<p>Enter the path you wish to create the alias for, followed by the name of the new alias.</p>');
43   }
44 }
45
46 /**
47  * Implementation of hook_menu().
48  */
49 function path_menu($may_cache) {
50   $items = array();
51
52   if ($may_cache) {
53     $items[] = array('path' => 'admin/path', 'title' => t('url aliases'),
54       'callback' => 'path_admin',
55       'access' => user_access('administer url aliases'));
56     $items[] = array('path' => 'admin/path/edit', 'title' => t('edit alias'),
57       'callback' => 'path_admin_edit',
58       'access' => user_access('administer url aliases'),
59       'type' => MENU_CALLBACK);
60     $items[] = array('path' => 'admin/path/delete', 'title' => t('delete alias'),
61       'callback' => 'path_admin_delete_confirm',
62       'access' => user_access('administer url aliases'),
63       'type' => MENU_CALLBACK);
64     $items[] = array('path' => 'admin/path/list', 'title' => t('list'),
65       'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
66     $items[] = array('path' => 'admin/path/add', 'title' => t('add alias'),
67       'callback' => 'path_admin_edit',
68       'access' => user_access('administer url aliases'),
69       'type' => MENU_LOCAL_TASK);
70   }
71
72   return $items;
73 }
74
75 /**
76  * Menu callback; presents an overview of all URL aliases.
77  */
78 function path_admin() {
79   return path_overview();
80 }
81
82 /**
83  * Menu callback; handles pages for creating and editing URL aliases.
84  */
85 function path_admin_edit($pid = 0) {
86   if ($pid) {
87     $alias = path_load($pid);
88     drupal_set_title($alias['dst']);
89     $output = path_form(path_load($pid));
90   }
91   else {
92     $output = path_form();
93   }
94
95   return $output;
96 }
97
98 /**
99  * Menu callback; confirms deleting an URL alias
100  **/
101 function path_admin_delete_confirm($pid) {
102   $path = path_load($pid);
103   if (user_access('administer url aliases')) {
104     $form['pid'] = array('#type' => 'value', '#value' => $pid);
105     $output = confirm_form('path_admin_delete_confirm', $form,
106   t('Are you sure you want to delete path alias %title?', array('%title' => theme('placeholder', $path['dst']))),
107    $_GET['destination'] ? $_GET['destination'] : 'admin/path', t('This action cannot be undone.'),
108   t('Delete'), t('Cancel') );
109   }
110
111   return $output;
112 }
113
114 /**
115  * Execute URL alias deletion
116  **/
117 function path_admin_delete_confirm_submit($form_id, $form_values) {
118   if ($form_values['confirm']) {
119     path_admin_delete($form_values['pid']);
120     return 'admin/path';
121   }
122 }
123
124 /**
125  * Post-confirmation; delete an URL alias.
126  */
127 function path_admin_delete($pid = 0) {
128   db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
129   drupal_set_message(t('The alias has been deleted.'));
130 }
131
132
133
134 /**
135  * Set an aliased path for a given Drupal path, preventing duplicates.
136  */
137 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
138   if ($path && !$alias) {
139     db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
140     drupal_clear_path_cache();
141   }
142   else if (!$path && $alias) {
143     db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
144     drupal_clear_path_cache();
145   }
146   else if ($path && $alias) {
147     $path = urldecode($path);
148     $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
149     $alias = urldecode($alias);
150     $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
151
152     // We have an insert:
153     if ($path_count == 0 && $alias_count == 0) {
154       db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
155       drupal_clear_path_cache();
156     }
157     else if ($path_count >= 1 && $alias_count == 0) {
158       if ($pid) {
159         db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid);
160       }
161       else {
162         db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
163       }
164       drupal_clear_path_cache();
165     }
166     else if ($path_count == 0 && $alias_count == 1) {
167       db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
168       drupal_clear_path_cache();
169     }
170     else if ($path_count == 1 && $alias_count == 1) {
171       // This will delete the path that alias was originally pointing to:
172       path_set_alias(NULL, $alias);
173       path_set_alias($path);
174       path_set_alias($path, $alias);
175     }
176   }
177 }
178
179 /**
180  * Return a form for editing or creating an individual URL alias.
181  */
182 function path_form($edit = '') {
183
184   $form['src'] = array('#type' => 'textfield', '#title' => t('Existing system path'), '#default_value' => $edit['src'], '#maxlength' => 64, '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'));
185   $form['dst'] = array('#type' => 'textfield', '#default_value' => $edit['dst'], '#maxlength' => 64, '#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.'));
186
187   if ($edit['pid']) {
188     $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
189     $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
190   }
191   else {
192     $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
193   }
194
195   return drupal_get_form('path_form', $form);
196 }
197
198 /**
199  * Implementation of hook_nodeapi().
200  *
201  * Allows URL aliases for nodes to be specified at node edit time rather
202  * than through the administrative interface.
203  */
204 function path_nodeapi(&$node, $op, $arg) {
205   if (user_access('create url aliases') || user_access('administer url aliases')) {
206     switch ($op) {
207       case 'validate':
208         $node->path = trim($node->path);
209         if ($node->path && !valid_url($node->path)) {
210           form_set_error('path', t('The path is invalid.'));
211         }
212         else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
213           form_set_error('path', t('The path is already in use.'));
214         }
215         break;
216
217       case 'load':
218         $path = "node/$node->nid";
219         // We don't use drupal_get_path_alias() to avoid custom rewrite functions.
220         // We only care about exact aliases.
221         $result = db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path);
222         if (db_num_rows($result)) {
223           $node->path = db_result($result);
224         }
225         break;
226
227       case 'insert':
228         // Don't try to insert if path is NULL.  We may have already set
229         // the alias ahead of time.
230         if ($node->path) {
231           path_set_alias("node/$node->nid", $node->path);
232         }
233         break;
234
235       case 'update':
236         path_set_alias("node/$node->nid", $node->path, $node->pid);
237         break;
238
239       case 'delete':
240         $path = "node/$node->nid";
241         if (drupal_get_path_alias($path) != $path) {
242           path_set_alias($path);
243         }
244         break;
245     }
246   }
247 }
248
249 /**
250  * Implementation of hook_form_alter().
251  */
252 function path_form_alter($form_id, &$form) {
253   if (user_access('create url aliases') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
254     $path = $form['#node']->path;
255     $form['path'] = array(
256       '#type' => 'fieldset',
257       '#title' => t('URL path settings'),
258       '#collapsible' => TRUE,
259       '#collapsed' => empty($path),
260       '#weight' => 30,
261     );
262     $form['path']['path'] = array(
263       '#type' => 'textfield',
264       '#default_value' => $path,
265       '#maxlength' => 250,
266       '#collapsible' => TRUE,
267       '#collapsed' => TRUE,
268       '#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.'),
269     );
270     if ($path) {
271       $form['path']['pid'] = array(
272         '#type' => 'value',
273         '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $path))
274       );
275     }
276   }
277 }
278
279
280 /**
281  * Implementation of hook_perm().
282  */
283 function path_perm() {
284   return array('create url aliases', 'administer url aliases');
285 }
286
287 /**
288  * Return a listing of all defined URL aliases.
289  */
290 function path_overview() {
291   $sql = 'SELECT * FROM {url_alias}';
292   $header = array(
293     array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
294     array('data' => t('System'), 'field' => 'src'),
295     array('data' => t('Operations'), 'colspan' => '2')
296   );
297   $sql .= tablesort_sql($header);
298   $result = pager_query($sql, 50);
299
300   $destination = drupal_get_destination();
301   while ($data = db_fetch_object($result)) {
302     $rows[] = array($data->dst, $data->src, l(t('edit'), "admin/path/edit/$data->pid", array(), $destination), l(t('delete'), "admin/path/delete/$data->pid", array(), $destination));
303   }
304
305   if (!$rows) {
306     $rows[] = array(array('data' => t('No URL aliases available.'), 'colspan' => '4'));
307   }
308
309   $output = theme('table', $header, $rows);
310   $output .= theme('pager', NULL, 50, 0);
311   return $output;
312 }
313
314 /**
315  * Fetch a specific URL alias from the database.
316  */
317 function path_load($pid) {
318   return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
319 }
320
321 /**
322  * Verify that a new URL alias is valid, and save it to the database.
323  */
324 function path_form_submit() {
325   $edit = $GLOBALS['form_values'];
326   $src = $edit['src'];
327   $dst = $edit['dst'];
328   $pid = $edit['pid'];
329
330   if (!valid_url($src)) {
331     form_set_error('src', t('The system path %path is invalid.', array('%path' => theme('placeholder', $src))));
332   }
333
334   if (!valid_url($dst)) {
335     form_set_error('dst', t('The alias %alias is invalid.', array('%alias' => theme('placeholder', $dst))));
336   }
337
338   if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s'", $pid, $dst))) {
339     form_set_error('dst', t('The alias %alias is already in use.', array('%alias' => theme('placeholder', $dst))));
340   }
341
342   if (form_get_errors()) {
343     return path_form($edit);
344   }
345   else {
346     path_set_alias($src, $dst, $pid);
347
348     drupal_set_message(t('The alias has been saved.'));
349     return 'admin/path';
350   }
351 }
352
353