path.module 9.68 KB
Newer Older
Dries's avatar
Dries committed
1
2
3
4
<?php
/* $Id$ */

function path_admin() {
Dries's avatar
   
Dries committed
5
  $op = $_POST["op"];
Dries's avatar
Dries committed
6
7
  $edit = $_POST["edit"];

Dries's avatar
   
Dries committed
8
  if (user_access("administer url aliases")) {
Dries's avatar
Dries committed
9
10
11
12
13
14

    if (empty($op)) {
      $op = arg(2);
    }

    switch ($op) {
Dries's avatar
   
Dries committed
15
      case "add":
Dries's avatar
Dries committed
16
17
18
        $output = path_form();
        break;

Dries's avatar
   
Dries committed
19
      case "edit":
Dries's avatar
   
Dries committed
20
21
22
23
24
        $output = path_form(path_load(arg(3)));
        break;

      case "help":
        $output = path_help();
Dries's avatar
Dries committed
25
26
        break;

Dries's avatar
   
Dries committed
27
      case "delete":
Dries's avatar
   
Dries committed
28
29
        $output  = status(path_delete(arg(3)));
        $output .= path_overview();
Dries's avatar
Dries committed
30
31
        break;

Dries's avatar
   
Dries committed
32
33
      case t("Create new alias"):
      case t("Update alias"):
Dries's avatar
   
Dries committed
34
35
        $output .= status(path_save($edit));
        break;
Dries's avatar
Dries committed
36
37

      default:
Dries's avatar
   
Dries committed
38
        $output .= path_overview();
Dries's avatar
Dries committed
39
40
    }

Dries's avatar
   
Dries committed
41
    print theme("page", $output);
Dries's avatar
Dries committed
42
43
  }
  else {
Dries's avatar
   
Dries committed
44
    print theme("page", message_access());
Dries's avatar
Dries committed
45
46
47
  }
}

Dries's avatar
   
Dries committed
48
49
function path_set_alias($path = NULL, $alias = NULL) {
  if ($path && !$alias) {
Dries's avatar
   
Dries committed
50
    db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
Dries's avatar
   
Dries committed
51
52
53
    drupal_rebuild_path_map();
  }
  else if (!$path && $alias) {
Dries's avatar
   
Dries committed
54
    db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
Dries's avatar
   
Dries committed
55
56
57
    drupal_rebuild_path_map();
  }
  else if ($path && $alias) {
Dries's avatar
   
Dries committed
58
59
    $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
    $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
Dries's avatar
   
Dries committed
60
61
62

    // We have an insert:
    if ($path_count == 0 && $alias_count == 0) {
Dries's avatar
   
Dries committed
63
      db_query("INSERT INTO {url_alias} SET src = '%s', dst = '%s'", $path, $alias);
Dries's avatar
   
Dries committed
64
65
66
      drupal_rebuild_path_map();
    }
    else if ($path_count == 1 && $alias_count == 0) {
Dries's avatar
   
Dries committed
67
      db_query("UPDATE {url_alias} SET dst = '%s' WHERE src = '%s'", $alias, $path);
Dries's avatar
   
Dries committed
68
69
70
      drupal_rebuild_path_map();
    }
    else if ($path_count == 0 && $alias_count == 1) {
Dries's avatar
   
Dries committed
71
      db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
Dries's avatar
   
Dries committed
72
73
74
75
76
77
78
79
80
      drupal_rebuild_path_map();
    }
    else if ($path_count == 1 && $alias_count == 1) {
      // This will delete the path that alias was originally pointing to:
      path_set_alias(NULL, $alias);
      path_set_alias($path);
      path_set_alias($path, $alias);
    }
  }
Dries's avatar
Dries committed
81
82
}

Dries's avatar
   
Dries committed
83
function path_form($edit = "", $error = "") {
Dries's avatar
Dries committed
84

Dries's avatar
   
Dries committed
85
86
  if ($error) {
    foreach ($error as $message) {
Dries's avatar
   
Dries committed
87
      $form .= theme("error", $message);
Dries's avatar
   
Dries committed
88
89
    }
  }
Dries's avatar
Dries committed
90

Dries's avatar
   
Dries committed
91
92
  $form .= form_textfield(t("Existing path"), "src", $edit["src"], 50, 64, t("Specify the existing path you wish to alias. For example: node/view/28, forum/1, taxonomy/page/or/1,2."));
  $form .= form_textfield(t("New path alias"), "dst", $edit["dst"], 50, 64, 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."));
Dries's avatar
Dries committed
93

Dries's avatar
   
Dries committed
94
95
96
97
98
99
100
101
102
  if ($edit["pid"]) {
    $form .= form_hidden("pid", $edit["pid"]);
    $form .= form_submit(t("Update alias"));
  }
  else {
    $form .= form_submit(t("Create new alias"));
  }

  return form($form);
Dries's avatar
Dries committed
103
104
}

Dries's avatar
   
Dries committed
105
function path_help($section = "admin/help#path") {
Dries's avatar
   
Dries committed
106
107
  $output = "";

Dries's avatar
Dries committed
108
  switch ($section) {
Dries's avatar
   
Dries committed
109
110
    case "admin/system/modules#description":
      $output = t("Enables users to create custom URLs.");
Dries's avatar
Dries committed
111
      break;
Dries's avatar
   
Dries committed
112
    case "admin/path":
Dries's avatar
   
Dries committed
113
      $output = t("Drupal provides users complete control over URLs through aliasing. While the original Drupal URLs are always created and accessible, advanced users have the option to override these normal paths.");
Dries's avatar
   
Dries committed
114
115
      break;
    case "admin/path/add":
Dries's avatar
   
Dries committed
116
      $output = t("Enter the path you wish to create the alias for, followed by the name of the new alias. Each path can be associated with only one alias.");
Dries's avatar
Dries committed
117
      break;
Dries's avatar
   
Dries committed
118
    case "admin/help#path":
Steven Wittens's avatar
Typoes.    
Steven Wittens committed
119
      $output .= "<h3>Background</h3><p>URL aliasing gives users the ability to have control over all Drupal paths. This functionality integrates seamlessly into node forms and also provides the administrator an interface to view all aliases that have been created.</p><p>Aliases have a 1 to 1 relationship with their original Drupal URLs. In other words you cannot have an alias map to more than one path. Likewise, a Drupal URL can't be mapped to more than one alias.</p>";
Dries's avatar
   
Dries committed
120
      $output .= "<h3>Permissions</h3><p>Two new permissions are introduced for aliasing URLs: <i>create url aliases</i> and <i>administer url aliases</i>.</p>";
Dries's avatar
   
Dries committed
121
122
123
124
125
      $output .= "<ol>";
      $output .= "<li><b>create url aliases</b> - Allows users to create aliases for nodes. Enabling this permission will display a new path field to the user in any node form, allowing them to enter an alias for that node. They will be able to edit/delete the alias after it is created using the same form.</li>";
      $output .= "<li><b>administer url aliases</b> - Allows users to access the alias administration interface.  They must also have the <i>access administration pages</i> permission set as well. This interface displays all aliases and provides a way to create and modify them as well. This is also the location to build aliases for things other than nodes.  For example, you can create an alias for a taxonomy URL or even re-map the admin path (although the original admin path will still be accessible since aliases do not cancel out original paths).</li>";
      $output .= "</ol>";
      $output = t($output);
Dries's avatar
Dries committed
126
127
128
      break;
  }

Dries's avatar
   
Dries committed
129
  return $output;
Dries's avatar
Dries committed
130
131
}

Dries's avatar
   
Dries committed
132
133
function path_link($type, $node = NULL) {
  if ($type == "system" && user_access("administer url aliases")) {
Dries's avatar
   
Dries committed
134
135
136
    menu("admin/path", t("url aliasing"), "path_admin", 4);
    menu("admin/path/add", t("new alias"), "path_admin");
    menu("admin/path/help", t("help"), "path_admin", 9);
Dries's avatar
   
Dries committed
137
138
   }
}
Dries's avatar
Dries committed
139

Dries's avatar
   
Dries committed
140
function path_nodeapi(&$node, $op, $arg) {
Dries's avatar
   
Dries committed
141
  if (user_access("create url aliases") || user_access("administer url aliases")) {
Dries's avatar
Dries committed
142

Dries's avatar
   
Dries committed
143
144
145
146
147
    switch ($op) {
      case "validate":
        // is_null provides a mechanism for us to determine if this is the first
        // viewing of the form.  If it is the first time, load the alias, if it isn't
        // (i.e., user has clicked preview) let them work with their current form alias.
148
149
        if (is_null($node->path)) {
          $node->path = drupal_get_path_alias("node/view/$node->nid");
Dries's avatar
   
Dries committed
150
151
        }
        else {
152
153
154
          $node->path = trim($node->path);
          if ($node->path && !valid_url($node->path)) {
            $error["path"] = t("The path is invalid.");
Dries's avatar
   
Dries committed
155
156
            return $error;
          }
Dries's avatar
   
Dries committed
157
          else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/view/$node->nid"))) {
158
            $error["path"] = t("The path is already in use.");
Dries's avatar
   
Dries committed
159
160
161
162
            return $error;
          }
        }
        break;
Dries's avatar
Dries committed
163

Dries's avatar
   
Dries committed
164
      case "form pre":
165
        return form_textfield(t("Path alias"), "path", $node->path, 60, 250, 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.") . theme_error($arg["path"]));
Dries's avatar
   
Dries committed
166
167

      case "insert":
Dries's avatar
   
Dries committed
168
169
170
171
172
173
174
175
        /*
        ** Don't try to insert if path is NULL.  We may have already set
        ** the alias ahead of time.
        */
        if ($node->path) {
          path_set_alias("node/view/$node->nid", $node->path);
        }
        break;
Dries's avatar
   
Dries committed
176
      case "update":
177
        path_set_alias("node/view/$node->nid", $node->path);
Dries's avatar
   
Dries committed
178
179
180
181
182
183
184
185
        break;

      case "delete":
        if ($alias = drupal_get_path_alias("node/view/$node->nid")) {
          path_set_alias("node/view/$node->nid");
        }
        break;
    }
Dries's avatar
Dries committed
186
187
188
189
  }
}

function path_perm() {
Dries's avatar
   
Dries committed
190
  return array("create url aliases", "administer url aliases");
Dries's avatar
Dries committed
191
192
193
}

function path_overview() {
Dries's avatar
   
Dries committed
194
  $sql = "SELECT * FROM {url_alias}";
Dries's avatar
Dries committed
195
  $header = array(
Dries's avatar
   
Dries committed
196
    array ("data" => t("alias"), "field" => "dst", "sort" => "asc"),
Dries's avatar
   
Dries committed
197
198
    array ("data" => t("normal"), "field" => "src"),
    array ("data" => t("operations"), "colspan" => 2)
Dries's avatar
Dries committed
199
200
201
202
203
  );
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);

  while ($data = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
204
    $rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid"));
Dries's avatar
Dries committed
205
206
  }

Dries's avatar
   
Dries committed
207
  if ($pager = pager_display(NULL, 50, 0, "admin", tablesort_pager())) {
Dries's avatar
   
Dries committed
208
    $rows[] = array(array("data" => $pager, "colspan" => "4"));
Dries's avatar
Dries committed
209
210
  }

Dries's avatar
   
Dries committed
211
  if (!$rows) {
Dries's avatar
   
Dries committed
212
    $rows[] = array(array("data" => t("No URL aliases available."), "colspan" => "4"));
Dries's avatar
   
Dries committed
213
214
  }

Dries's avatar
   
Dries committed
215
  return theme("table", $header, $rows);
Dries's avatar
Dries committed
216
217
}

Dries's avatar
   
Dries committed
218
function path_load($pid) {
Dries's avatar
   
Dries committed
219
  return db_fetch_array(db_query("SELECT * FROM {url_alias} WHERE pid = '%d'", $pid));
Dries's avatar
   
Dries committed
220
}
Dries's avatar
Dries committed
221

Dries's avatar
   
Dries committed
222
function path_delete($pid) {
Dries's avatar
   
Dries committed
223
  db_query("DELETE FROM {url_alias} WHERE pid = '%d'", $pid);
Dries's avatar
   
Dries committed
224
225
  return t("the alias has been deleted.");
}
Dries's avatar
Dries committed
226

Dries's avatar
   
Dries committed
227
228
229
230
function path_save($edit) {
  $src = $edit["src"];
  $dst = $edit["dst"];
  $pid = $edit["pid"];
Dries's avatar
Dries committed
231

Dries's avatar
   
Dries committed
232
233
  if (!valid_url($src)) {
    $error[] = t("the normal path '%src' is invalid.", array("%src" => $src));
Dries's avatar
Dries committed
234
235
  }

Dries's avatar
   
Dries committed
236
  if (db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE pid != '%d' AND src = '%s'", $pid, $src))) {
Dries's avatar
   
Dries committed
237
    $error[] = t("the normal path '%src' is already aliased.", array("%src" => $src));
Dries's avatar
Dries committed
238
239
  }

Dries's avatar
   
Dries committed
240
241
  if (!valid_url($dst)) {
    $error[] = t("the alias '%dst' is invalid.", array("%dst" => $dst));
Dries's avatar
Dries committed
242
243
  }

Dries's avatar
   
Dries committed
244
  if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != '%d' AND dst = '%s'", $pid, $dst))) {
Dries's avatar
   
Dries committed
245
246
    $error[] = t("the alias '%dst' is already in use.", array("%dst" => $dst));
  }
Dries's avatar
Dries committed
247

Dries's avatar
   
Dries committed
248
249
  if ($error) {
    return path_form($edit, $error);
Dries's avatar
Dries committed
250
  }
Dries's avatar
   
Dries committed
251
252
253
254
255
256
257
258
  else {
    /*
    ** Normally, you would use path_set_alias to update the paths table,
    ** but this is a special case.  We want to modify a specific row and the only
    ** way to do that is with pid.
    */

    if ($pid) {
Dries's avatar
   
Dries committed
259
      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s' WHERE pid = '%d'", $src, $dst, $pid);
Dries's avatar
   
Dries committed
260
261
262
263
    }
    else {
      path_set_alias($src, $dst);
    }
Dries's avatar
Dries committed
264
265
  }

Dries's avatar
   
Dries committed
266
  return t("the alias has been saved.") . path_overview();
Dries's avatar
Dries committed
267
268
269
}

?>