drupal/modules/path.module

227 lines
5.7 KiB
Plaintext
Raw Normal View History

2003-09-30 17:03:29 +00:00
<?php
/* $Id$ */
function path_admin() {
$op = strtolower($_POST["op"]);
$edit = $_POST["edit"];
if (user_access("alias urls")) {
if (empty($op)) {
$op = arg(2);
}
switch ($op) {
case t("add"):
$output = path_form();
break;
case t("edit"):
$output = path_form(object2array(get_path_from_id(arg(3))));
break;
case t("delete"):
if ($edit["confirm"]) {
if (path_delete($edit['pid'])) {
$output .= status("Deleted path '". $edit['dst'] ."'");
2003-09-30 17:03:29 +00:00
}
}
else {
$output .= path_confirm_delete(arg(3));
}
break;
case t("create new alias"):
$output = status(path_save($edit));
// fall-through
default:
$output .= path_overview();
2003-09-30 17:03:29 +00:00
}
return $output;
}
else {
return message_access();
}
}
/**
* Returns a path that is acceptable as an url.
*/
function path_clean($path) {
global $base_url;
/*
** Replace absolute URL for this site with relative URL.
*/
$path = str_replace($base_url, "", $path);
/*
** Only allow alpha numeric characters, slashes and underscores.
*/
$path = preg_replace("'[^a-zA-Z0-9/_.]'", " ", $path);
/*
** Remove all whitespace.
*/
$path = str_replace(" ", "", $path);
/*
** Replace two or more sequential slashes with only one slashes.
*/
$path = preg_replace("'//*'","/",$path);
/*
** Remove beginning and trailing slashes.
*/
$path = trim($path, "/");
return $path;
}
function path_confirm_delete($id) {
$path = get_path_from_id($id);
$form .= form_hidden("confirm", 1);
$form .= form_hidden("pid", $id);
$form .= form_hidden("src", $path->src);
$form .= form_hidden("dst", $path->dst);
2003-09-30 17:03:29 +00:00
$form .= form_submit(t("Delete"));
$form .= form_submit(t("Cancel"));
return form(form_item(t("Delete alias '%dst' that maps to '%src'", array("%dst" => $path->dst, "%src" => $path->src)), $form, t("Are you sure you want to delete this alias?")));
2003-09-30 17:03:29 +00:00
}
function path_delete($pid) {
return db_query("DELETE FROM {path} WHERE pid = '%d'", $pid);
}
function path_help($section = "admin/path/help") {
switch ($section) {
case "admin/system/modules":
$output = "Enables users to create custom URLs.";
break;
case "admin/system/modules/path":
$output = "Documentation yet to be written.";
break;
case "admin/path/help":
$output = "Documentation yet to be written.";
break;
}
return t($output);
}
function path_form($edit = "") {
$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."));
2003-09-30 17:03:29 +00:00
$form .= form_hidden("pid", $edit["pid"]);
$form .= form_submit(t("Create new alias"));
if ($edit["pid"]) {
$form .= form_submit(t("Delete"));
}
return form($form);
}
function path_insert($edit) {
return db_query("INSERT INTO {path} SET src = '%s', dst = '%s'", $edit["src"], $edit["dst"]);
2003-09-30 17:03:29 +00:00
}
# DELETE
function path_is_unique($path, $type) {
if ($type == "dst") {
return !(get_src_url($path));
2003-09-30 17:03:29 +00:00
}
elseif ($type == "src") {
2003-09-30 17:03:29 +00:00
return !(get_url_alias($path));
}
}
function path_link($type, $node = NULL) {
if ($type == "system" && user_access("alias urls")) {
menu("admin/path", t("url aliasing"), "path_admin", "", 4);
menu("admin/path/add", t("new alias"), "path_admin", "");
}
}
function path_perm() {
return array("alias urls");
}
function path_overview() {
$sql = "SELECT * FROM {path}";
$header = array(
array ("data" => t("alias"), "field" => "dst", "sort" => "asc"),
array ("data" => t("maps to"), "field" => "src"),
2003-09-30 17:03:29 +00:00
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"));
2003-09-30 17:03:29 +00:00
}
$pager = pager_display(NULL, 50, 0, "admin", tablesort_pager());
if (!empty($pager)) {
$rows[] = array(array("data" => $pager, "colspan" => 3));
}
return table($header, $rows);
}
function path_save($edit) {
$dst = path_clean($edit["dst"]);
$src = path_clean($edit["src"]);
2003-09-30 17:03:29 +00:00
if ($src == NULL || !valid_url($src)) {
return t("the specified path is not valid.");
2003-09-30 17:03:29 +00:00
}
if (db_result(db_query("SELECT COUNT(src) FROM {path} WHERE src = '%s'", $src))) {
return t("the specified path is already aliased.");
2003-09-30 17:03:29 +00:00
}
if ($dst == NULL || !valid_url($dst)) {
return t("the specified path alias is not valid.");
2003-09-30 17:03:29 +00:00
}
if (db_result(db_query("SELECT COUNT(dst) FROM {path} WHERE dst = '%s'", $dst))) {
return t("the specified alias is already in use.");
2003-09-30 17:03:29 +00:00
}
if ($edit["pid"]) {
path_update($src, $dst);
2003-09-30 17:03:29 +00:00
}
else { //Update the path
path_insert($edit);
}
return t("you may access %src via %dst.", array("%src" => url($src), "%dst" => l($dst, url($dst))));
2003-09-30 17:03:29 +00:00
}
function path_system($field) {
$system["description"] = path_help("admin/system/modules");
$system["admin_help"] = path_help("admin/system/modules/path");
return $system[$field];
}
function path_update($edit) {
if ($edit["pid"]) {
return db_query("UPDATE {path} SET src = '%s', dst = '%s' WHERE pid = '%d'", $edit["src"], $edit["dst"], $edit["pid"]);
2003-09-30 17:03:29 +00:00
}
else { // intended for nodes
return db_query("UPDATE {path} SET dst = '%s' WHERE src = '%s'", $edit["dst"], $edit["src"]);
2003-09-30 17:03:29 +00:00
}
}
function get_path_from_id($id) {
return db_fetch_object(db_query("SELECT * FROM {path} WHERE pid = '%d'", $id));
}
?>