2003-09-30 17:03:29 +00:00
<?php
/* $Id$ */
function path_admin() {
2003-10-09 17:20:18 +00:00
$op = $_POST["op"];
2003-09-30 17:03:29 +00:00
$edit = $_POST["edit"];
2003-10-03 14:10:05 +00:00
if (user_access("administer url aliases")) {
2003-09-30 17:03:29 +00:00
if (empty($op)) {
$op = arg(2);
}
switch ($op) {
2003-10-05 17:36:23 +00:00
case "add":
2003-09-30 17:03:29 +00:00
$output = path_form();
break;
2003-10-05 17:36:23 +00:00
case "edit":
2003-10-03 14:10:05 +00:00
$output = path_form(path_load(arg(3)));
break;
case "help":
$output = path_help();
2003-09-30 17:03:29 +00:00
break;
2003-10-05 17:36:23 +00:00
case "delete":
2003-10-03 14:10:05 +00:00
$output = status(path_delete(arg(3)));
$output .= path_overview();
2003-09-30 17:03:29 +00:00
break;
2003-10-05 17:36:23 +00:00
case t("Create new alias"):
case t("Update alias"):
2003-10-03 14:10:05 +00:00
$output .= status(path_save($edit));
break;
2003-09-30 17:03:29 +00:00
default:
2003-10-01 05:18:03 +00:00
$output .= path_overview();
2003-09-30 17:03:29 +00:00
}
2003-11-25 19:26:21 +00:00
print theme("page", $output);
2003-09-30 17:03:29 +00:00
}
else {
2003-11-25 19:26:21 +00:00
print theme("page", message_access());
2003-09-30 17:03:29 +00:00
}
}
2003-10-03 14:10:05 +00:00
function path_set_alias($path = NULL, $alias = NULL) {
if ($path && !$alias) {
2003-10-22 20:20:35 +00:00
db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
2003-10-03 14:10:05 +00:00
drupal_rebuild_path_map();
}
else if (!$path && $alias) {
2003-10-22 20:20:35 +00:00
db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
2003-10-03 14:10:05 +00:00
drupal_rebuild_path_map();
}
else if ($path && $alias) {
2003-10-22 20:20:35 +00:00
$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));
2003-10-03 14:10:05 +00:00
// We have an insert:
if ($path_count == 0 && $alias_count == 0) {
2003-10-22 20:20:35 +00:00
db_query("INSERT INTO {url_alias} SET src = '%s', dst = '%s'", $path, $alias);
2003-10-03 14:10:05 +00:00
drupal_rebuild_path_map();
}
else if ($path_count == 1 && $alias_count == 0) {
2003-10-22 20:20:35 +00:00
db_query("UPDATE {url_alias} SET dst = '%s' WHERE src = '%s'", $alias, $path);
2003-10-03 14:10:05 +00:00
drupal_rebuild_path_map();
}
else if ($path_count == 0 && $alias_count == 1) {
2003-10-22 20:20:35 +00:00
db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
2003-10-03 14:10:05 +00:00
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);
}
}
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
function path_form($edit = "", $error = "") {
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
if ($error) {
foreach ($error as $message) {
2003-11-09 23:27:22 +00:00
$form .= theme("error", $message);
2003-10-03 14:10:05 +00:00
}
}
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
$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."));
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
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);
2003-09-30 17:03:29 +00:00
}
2003-10-09 18:53:22 +00:00
function path_help($section = "admin/help#path") {
2003-10-07 18:16:41 +00:00
$output = "";
2003-09-30 17:03:29 +00:00
switch ($section) {
2003-10-07 18:16:41 +00:00
case "admin/system/modules#description":
$output = t("Enables users to create custom URLs.");
2003-09-30 17:03:29 +00:00
break;
2003-10-03 14:10:05 +00:00
case "admin/path":
2003-10-07 18:16:41 +00:00
$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.");
2003-10-03 14:10:05 +00:00
break;
case "admin/path/add":
2003-10-07 18:16:41 +00:00
$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.");
2003-09-30 17:03:29 +00:00
break;
2003-10-09 18:53:22 +00:00
case "admin/help#path":
2003-10-23 16:41:07 +00:00
$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>";
2003-10-03 14:10:05 +00:00
$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>";
2003-10-07 18:16:41 +00:00
$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);
2003-09-30 17:03:29 +00:00
break;
}
2003-10-07 18:16:41 +00:00
return $output;
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
function path_link($type, $node = NULL) {
if ($type == "system" && user_access("administer url aliases")) {
2003-10-09 18:53:22 +00:00
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);
2003-10-03 14:10:05 +00:00
}
}
2003-09-30 17:03:29 +00:00
2003-10-17 16:00:44 +00:00
function path_nodeapi(&$node, $op, $arg) {
2003-10-03 14:10:05 +00:00
if (user_access("create url aliases") || user_access("administer url aliases")) {
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
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.
2003-10-07 12:17:43 +00:00
if (is_null($node->path)) {
$node->path = drupal_get_path_alias("node/view/$node->nid");
2003-10-03 14:10:05 +00:00
}
else {
2003-10-07 12:17:43 +00:00
$node->path = trim($node->path);
if ($node->path && !valid_url($node->path)) {
$error["path"] = t("The path is invalid.");
2003-10-03 14:10:05 +00:00
return $error;
}
2003-10-22 20:20:35 +00:00
else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/view/$node->nid"))) {
2003-10-07 12:17:43 +00:00
$error["path"] = t("The path is already in use.");
2003-10-03 14:10:05 +00:00
return $error;
}
}
break;
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
case "form pre":
2003-10-07 12:17:43 +00:00
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"]));
2003-10-03 14:10:05 +00:00
case "insert":
2003-10-17 16:00:44 +00:00
/*
** 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;
2003-10-03 14:10:05 +00:00
case "update":
2003-10-07 12:17:43 +00:00
path_set_alias("node/view/$node->nid", $node->path);
2003-10-03 14:10:05 +00:00
break;
case "delete":
if ($alias = drupal_get_path_alias("node/view/$node->nid")) {
path_set_alias("node/view/$node->nid");
}
break;
}
2003-09-30 17:03:29 +00:00
}
}
function path_perm() {
2003-10-03 14:10:05 +00:00
return array("create url aliases", "administer url aliases");
2003-09-30 17:03:29 +00:00
}
function path_overview() {
2003-10-22 20:20:35 +00:00
$sql = "SELECT * FROM {url_alias}";
2003-09-30 17:03:29 +00:00
$header = array(
2003-10-01 05:18:03 +00:00
array ("data" => t("alias"), "field" => "dst", "sort" => "asc"),
2003-10-03 14:10:05 +00:00
array ("data" => t("normal"), "field" => "src"),
array ("data" => t("operations"), "colspan" => 2)
2003-09-30 17:03:29 +00:00
);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
while ($data = db_fetch_object($result)) {
2003-10-01 05:18:03 +00:00
$rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid"));
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
if ($pager = pager_display(NULL, 50, 0, "admin", tablesort_pager())) {
2003-10-04 09:43:28 +00:00
$rows[] = array(array("data" => $pager, "colspan" => "4"));
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
if (!$rows) {
2003-10-04 09:43:28 +00:00
$rows[] = array(array("data" => t("No URL aliases available."), "colspan" => "4"));
2003-10-03 14:10:05 +00:00
}
2003-11-13 19:52:54 +00:00
return theme("table", $header, $rows);
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
function path_load($pid) {
2003-10-22 20:20:35 +00:00
return db_fetch_array(db_query("SELECT * FROM {url_alias} WHERE pid = '%d'", $pid));
2003-10-03 14:10:05 +00:00
}
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
function path_delete($pid) {
2003-10-27 20:54:08 +00:00
db_query("DELETE FROM {url_alias} WHERE pid = '%d'", $pid);
2003-10-03 14:10:05 +00:00
return t("the alias has been deleted.");
}
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
function path_save($edit) {
$src = $edit["src"];
$dst = $edit["dst"];
$pid = $edit["pid"];
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
if (!valid_url($src)) {
$error[] = t("the normal path '%src' is invalid.", array("%src" => $src));
2003-09-30 17:03:29 +00:00
}
2003-10-22 20:20:35 +00:00
if (db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE pid != '%d' AND src = '%s'", $pid, $src))) {
2003-10-03 14:10:05 +00:00
$error[] = t("the normal path '%src' is already aliased.", array("%src" => $src));
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
if (!valid_url($dst)) {
$error[] = t("the alias '%dst' is invalid.", array("%dst" => $dst));
2003-09-30 17:03:29 +00:00
}
2003-10-22 20:20:35 +00:00
if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != '%d' AND dst = '%s'", $pid, $dst))) {
2003-10-03 14:10:05 +00:00
$error[] = t("the alias '%dst' is already in use.", array("%dst" => $dst));
}
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
if ($error) {
return path_form($edit, $error);
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
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) {
2003-10-22 20:20:35 +00:00
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s' WHERE pid = '%d'", $src, $dst, $pid);
2003-10-03 14:10:05 +00:00
}
else {
path_set_alias($src, $dst);
}
2003-09-30 17:03:29 +00:00
}
2003-10-05 17:36:23 +00:00
return t("the alias has been saved.") . path_overview();
2003-09-30 17:03:29 +00:00
}
?>