summaryrefslogtreecommitdiff
path: root/modules/path/path.module
blob: 9a8ea9ff083ed54998e7adaf1552cc3a2a30d2e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/* $Id$ */

function path_admin() {
  $op = $_POST["op"];
  $edit = $_POST["edit"];

  if (user_access("administer url aliases")) {

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

    switch ($op) {
      case "add":
        $output = path_form();
        break;

      case "edit":
        $output = path_form(path_load(arg(3)));
        break;

      case "help":
        $output = path_help();
        break;

      case "delete":
        $output  = status(path_delete(arg(3)));
        $output .= path_overview();
        break;

      case t("Create new alias"):
      case t("Update alias"):
        $output .= status(path_save($edit));
        break;

      default:
        $output .= path_overview();
    }

    return $output;
  }
  else {
    return message_access();
  }
}

function path_set_alias($path = NULL, $alias = NULL) {
  if ($path && !$alias) {
    db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
    drupal_rebuild_path_map();
  }
  else if (!$path && $alias) {
    db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
    drupal_rebuild_path_map();
  }
  else if ($path && $alias) {
    $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));

    // We have an insert:
    if ($path_count == 0 && $alias_count == 0) {
      db_query("INSERT INTO {url_alias} SET src = '%s', dst = '%s'", $path, $alias);
      drupal_rebuild_path_map();
    }
    else if ($path_count == 1 && $alias_count == 0) {
      db_query("UPDATE {url_alias} SET dst = '%s' WHERE src = '%s'", $alias, $path);
      drupal_rebuild_path_map();
    }
    else if ($path_count == 0 && $alias_count == 1) {
      db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
      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);
    }
  }
}

function path_form($edit = "", $error = "") {

  if ($error) {
    foreach ($error as $message) {
      $form .= theme("error", $message);
    }
  }

  $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."));

  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);
}

function path_help($section = "admin/help#path") {
  $output = "";

  switch ($section) {
    case "admin/system/modules#description":
      $output = t("Enables users to create custom URLs.");
      break;
    case "admin/path":
      $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.");
      break;
    case "admin/path/add":
      $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.");
      break;
    case "admin/help#path":
      $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>";
      $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>";
      $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);
      break;
  }

  return $output;
}

function path_link($type, $node = NULL) {
  if ($type == "system" && user_access("administer url aliases")) {
    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);
   }
}

function path_nodeapi(&$node, $op, $arg) {
  if (user_access("create url aliases") || user_access("administer url aliases")) {

    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.
        if (is_null($node->path)) {
          $node->path = drupal_get_path_alias("node/view/$node->nid");
        }
        else {
          $node->path = trim($node->path);
          if ($node->path && !valid_url($node->path)) {
            $error["path"] = t("The path is invalid.");
            return $error;
          }
          else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/view/$node->nid"))) {
            $error["path"] = t("The path is already in use.");
            return $error;
          }
        }
        break;

      case "form pre":
        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"]));

      case "insert":
        /*
        ** 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;
      case "update":
        path_set_alias("node/view/$node->nid", $node->path);
        break;

      case "delete":
        if ($alias = drupal_get_path_alias("node/view/$node->nid")) {
          path_set_alias("node/view/$node->nid");
        }
        break;
    }
  }
}

function path_perm() {
  return array("create url aliases", "administer url aliases");
}

function path_overview() {
  $sql = "SELECT * FROM {url_alias}";
  $header = array(
    array ("data" => t("alias"), "field" => "dst", "sort" => "asc"),
    array ("data" => t("normal"), "field" => "src"),
    array ("data" => t("operations"), "colspan" => 2)
  );
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);

  while ($data = db_fetch_object($result)) {
    $rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid"));
  }

  if ($pager = pager_display(NULL, 50, 0, "admin", tablesort_pager())) {
    $rows[] = array(array("data" => $pager, "colspan" => "4"));
  }

  if (!$rows) {
    $rows[] = array(array("data" => t("No URL aliases available."), "colspan" => "4"));
  }

  return theme("table", $header, $rows);
}

function path_load($pid) {
  return db_fetch_array(db_query("SELECT * FROM {url_alias} WHERE pid = '%d'", $pid));
}

function path_delete($pid) {
  db_query("DELETE FROM {url_alias} WHERE pid = '%d'", $pid);
  return t("the alias has been deleted.");
}

function path_save($edit) {
  $src = $edit["src"];
  $dst = $edit["dst"];
  $pid = $edit["pid"];

  if (!valid_url($src)) {
    $error[] = t("the normal path '%src' is invalid.", array("%src" => $src));
  }

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

  if (!valid_url($dst)) {
    $error[] = t("the alias '%dst' is invalid.", array("%dst" => $dst));
  }

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

  if ($error) {
    return path_form($edit, $error);
  }
  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) {
      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s' WHERE pid = '%d'", $src, $dst, $pid);
    }
    else {
      path_set_alias($src, $dst);
    }
  }

  return t("the alias has been saved.") . path_overview();
}

?>