Commit 23ca7a2d authored by Dries's avatar Dries

- Another batch of updates/improvements:

   + introduced basic node permissions ("create", "delete", "update" and
     "view") at the node level: it's up to the "<$node->type>_module" to
     hide gory details (if any).

   + made the "blog it"-feature in the blog and import module work with
     the new node system, in specific with the new centralized forms.

   + made it possible to update blogs.

   + made the page module work with the new node system.

   + various smaller improvements.
parent 3d47ad35
<?php
// $Id$
/*
** Loading and storing nodes:
** - load: called when a node is being loaded
** - save: called before a node gets saved and used to define default
** values
** - insert: called when inserting a node to the node table
** - delete: called when deleting a node from the node table
** - update: called when updating a node in the noe table
**
** Viewing and editing nodes:
** - view: called to display a node on the screen
** - form: called to display a node's form
**
** - status
*/
// TODO: still used by themes, yet doesn't return anything at the moment
function node_index() {
}
function node_access($node) {
global $user;
return ($node->status == 1) || user_access("administer nodes");
}
function node_get_comments($nid) {
$comment = db_fetch_object(db_query("SELECT COUNT(c.lid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.lid WHERE n.nid = '$nid' GROUP BY n.nid"));
return $comment->number ? $comment->number : 0;
......@@ -210,21 +189,21 @@ function node_save($node, $filter) {
}
function node_delete($conditions) {
function node_delete($node) {
if ($node = node_load($conditions)) {
// delete the node and its comments:
db_query("DELETE FROM node WHERE nid = '$node->nid'");
db_query("DELETE FROM comments WHERE lid = '$node->nid'");
db_query("DELETE FROM moderate WHERE nid = '$node->nid'");
if (is_array($node)) {
$node = node_object($node);
}
// call the node specific callback (if any):
module_invoke($node->type, "delete", &$node);
// delete the node and its comments:
db_query("DELETE FROM node WHERE nid = '$node->nid'");
db_query("DELETE FROM comments WHERE lid = '$node->nid'");
db_query("DELETE FROM moderate WHERE nid = '$node->nid'");
watchdog("special", "node: deleted '$node->title'");
}
// call the node specific callback (if any):
module_invoke($node->type, "delete", &$node);
watchdog("special", "node: deleted '$node->title'");
}
function node_view($node, $main = 0) {
......
......@@ -497,7 +497,7 @@ function import_page_last() {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
if ($item->link) {
......@@ -530,7 +530,7 @@ function import_page_feed($fid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
if ($item->link) {
......@@ -564,7 +564,7 @@ function import_page_bundle($bid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
......
......@@ -497,7 +497,7 @@ function import_page_last() {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
if ($item->link) {
......@@ -530,7 +530,7 @@ function import_page_feed($fid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
if ($item->link) {
......@@ -564,7 +564,7 @@ function import_page_bundle($bid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
......
<?php
// $Id$
function blog_node($field) {
global $user;
$info = array("name" => "personal blog");
return $info[$field];
}
function blog_access($op, $node) {
global $user;
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
if ($op == "create") {
return $user->uid;
}
if ($op == "update") {
return user_access("administer nodes") || ($user->uid == $node->uid);
}
if ($op == "delete") {
return user_access("administer nodes") || ($user->uid == $node->uid);
}
}
function blog_help() {
?>
<p>Drupal's blog module allows registered users to maintain an online blog or diary. It provides easy-to-write and easy-to-read online diaries or journals that can be filled with daily thoughts, poetry, boneless blabber, spiritual theories, intimate details, valuable experiences, cynical rants, semi-coherent comments, writing experiments, artistic babblings, critics on current facts, fresh insights, diverse dreams, chronicles and mumbling madness available for public consumption.</p>
......@@ -94,7 +123,7 @@ function blog_page_user($uid = 0, $date = 0) {
}
if ($user->uid && user_access("post blogs")) {
$links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&nid=$blog->nid\">". t("blog it") ."</a>";
}
if ($blog->comment) {
......@@ -131,7 +160,7 @@ function blog_page_last() {
}
if ($user->uid && user_access("post blogs")) {
$links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&nid=$blog->nid\">". t("blog it") ."</a>";
}
if ($blog->comment) {
......@@ -150,6 +179,17 @@ function blog_page_last() {
}
function blog_form($edit) {
global $nid, $iid;
if (!$edit->body) {
if ($nid && $blog = node_load(array("nid" => $nid))) {
$edit->body = "<i>". $blog->body ."</i> [<a href=\"module.php?mod=blog&id=$blog->uid&date=$blog->created\">$blog->name</a>]";
}
if ($iid && $item = db_fetch_object(db_query("SELECT i.*, f.title as ftitle, f.link as flink FROM item i, feed f WHERE i.iid = '". check_input($iid) ."' AND i.fid = f.fid"))) {
$edit->body = "<a href=\"$item->link\">$item->title</a> - <i>". check_output($item->description) ."</i> [<a href=\"$item->flink\">$item->ftitle</a>]\n";
}
}
$output = form_textarea(t("Body"), "body", $edit->body, 60, 15, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
......@@ -157,30 +197,14 @@ function blog_form($edit) {
}
function blog_save($node) {
global $user;
if ($node->nid) {
/*
** Load the original blog from the database to make sure that only
** original author can update his blog.
*/
$blog = node_load(array("nid" => $node->nid));
if ($user->uid && $user->uid == $node->uid && $user->uid == $blog->uid) {
return array();
}
else if (user_access("adminster nodes")) {
return array();
}
else {
return 0;
}
return array();
}
else {
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
}
function blog_page() {
......
<?php
// $Id$
function blog_node($field) {
global $user;
$info = array("name" => "personal blog");
return $info[$field];
}
function blog_access($op, $node) {
global $user;
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
if ($op == "create") {
return $user->uid;
}
if ($op == "update") {
return user_access("administer nodes") || ($user->uid == $node->uid);
}
if ($op == "delete") {
return user_access("administer nodes") || ($user->uid == $node->uid);
}
}
function blog_help() {
?>
<p>Drupal's blog module allows registered users to maintain an online blog or diary. It provides easy-to-write and easy-to-read online diaries or journals that can be filled with daily thoughts, poetry, boneless blabber, spiritual theories, intimate details, valuable experiences, cynical rants, semi-coherent comments, writing experiments, artistic babblings, critics on current facts, fresh insights, diverse dreams, chronicles and mumbling madness available for public consumption.</p>
......@@ -94,7 +123,7 @@ function blog_page_user($uid = 0, $date = 0) {
}
if ($user->uid && user_access("post blogs")) {
$links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&nid=$blog->nid\">". t("blog it") ."</a>";
}
if ($blog->comment) {
......@@ -131,7 +160,7 @@ function blog_page_last() {
}
if ($user->uid && user_access("post blogs")) {
$links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&nid=$blog->nid\">". t("blog it") ."</a>";
}
if ($blog->comment) {
......@@ -150,6 +179,17 @@ function blog_page_last() {
}
function blog_form($edit) {
global $nid, $iid;
if (!$edit->body) {
if ($nid && $blog = node_load(array("nid" => $nid))) {
$edit->body = "<i>". $blog->body ."</i> [<a href=\"module.php?mod=blog&id=$blog->uid&date=$blog->created\">$blog->name</a>]";
}
if ($iid && $item = db_fetch_object(db_query("SELECT i.*, f.title as ftitle, f.link as flink FROM item i, feed f WHERE i.iid = '". check_input($iid) ."' AND i.fid = f.fid"))) {
$edit->body = "<a href=\"$item->link\">$item->title</a> - <i>". check_output($item->description) ."</i> [<a href=\"$item->flink\">$item->ftitle</a>]\n";
}
}
$output = form_textarea(t("Body"), "body", $edit->body, 60, 15, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
......@@ -157,30 +197,14 @@ function blog_form($edit) {
}
function blog_save($node) {
global $user;
if ($node->nid) {
/*
** Load the original blog from the database to make sure that only
** original author can update his blog.
*/
$blog = node_load(array("nid" => $node->nid));
if ($user->uid && $user->uid == $node->uid && $user->uid == $blog->uid) {
return array();
}
else if (user_access("adminster nodes")) {
return array();
}
else {
return 0;
}
return array();
}
else {
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
}
function blog_page() {
......
<?php
// $Id$
function book_node($field) {
global $user;
$info = array("name" => "book page");
return $info[$field];
}
function book_access($op, $node) {
global $user;
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
if ($op == "create") {
return 1;
}
if ($op == "update") {
return 1;
}
if ($op == "delete") {
return user_access("adminster nodes");
}
}
function book_link($type) {
if ($type == "page" && user_access("access content")) {
$links[] = "<a href=\"module.php?mod=book\">". t("collaborative book") ."</a>";
......@@ -36,17 +65,11 @@ function book_delete($node) {
}
function book_save($node) {
if ($node->nid) {
if (user_access("administer nodes")) {
return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
}
else {
// TODO: temporay disabled book page updates by regular users
return 0;
}
if ($node->nid && user_access("administer nodes")) {
return array("pid", "log", "parent", "weight");
}
else {
return array("pid", "log", "parent", "weight");
return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
}
}
......@@ -80,8 +103,7 @@ function book_view($node, $main = 0) {
$indent .= "-";
}
$output .= " <TR><TD COLSPAN=\"3\">$location</TD></TR>";
// TODO: <TD ALIGN=\"right\">". node_control($node) ."</TD></TR>\n";
$output .= " <TR><TD COLSPAN=\"2\">$location</TD><TD ALIGN=\"right\"><A HREF=\"module.php?mod=node&op=edit&id=$node->nid\">update</A></TD></TR>\n";
$output .= " <TR><TD COLSPAN=\"3\"><HR></TD></TR>";
$output .= " <TR><TD COLSPAN=\"3\"><B><BIG>". check_output($node->title) ."</BIG></B>". ($node->body ? "<BR><SMALL><I>Last updated by ". format_name($node) ." on ". format_date($node->created) ."</I></SMALL> " : "") ."</TD></TR>\n";
}
......
<?php
// $Id$
function book_node($field) {
global $user;
$info = array("name" => "book page");
return $info[$field];
}
function book_access($op, $node) {
global $user;
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
if ($op == "create") {
return 1;
}
if ($op == "update") {
return 1;
}
if ($op == "delete") {
return user_access("adminster nodes");
}
}
function book_link($type) {
if ($type == "page" && user_access("access content")) {
$links[] = "<a href=\"module.php?mod=book\">". t("collaborative book") ."</a>";
......@@ -36,17 +65,11 @@ function book_delete($node) {
}
function book_save($node) {
if ($node->nid) {
if (user_access("administer nodes")) {
return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
}
else {
// TODO: temporay disabled book page updates by regular users
return 0;
}
if ($node->nid && user_access("administer nodes")) {
return array("pid", "log", "parent", "weight");
}
else {
return array("pid", "log", "parent", "weight");
return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
}
}
......@@ -80,8 +103,7 @@ function book_view($node, $main = 0) {
$indent .= "-";
}
$output .= " <TR><TD COLSPAN=\"3\">$location</TD></TR>";
// TODO: <TD ALIGN=\"right\">". node_control($node) ."</TD></TR>\n";
$output .= " <TR><TD COLSPAN=\"2\">$location</TD><TD ALIGN=\"right\"><A HREF=\"module.php?mod=node&op=edit&id=$node->nid\">update</A></TD></TR>\n";
$output .= " <TR><TD COLSPAN=\"3\"><HR></TD></TR>";
$output .= " <TR><TD COLSPAN=\"3\"><B><BIG>". check_output($node->title) ."</BIG></B>". ($node->body ? "<BR><SMALL><I>Last updated by ". format_name($node) ." on ". format_date($node->created) ."</I></SMALL> " : "") ."</TD></TR>\n";
}
......
<?php
// $Id$
function forum_node($field) {
$info = array("name" => "discussion forum");
return $info[$field];
}
function forum_access($op, $node) {
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
return user_access("adminster nodes");
}
function forum_link($type) {
if ($type == "page" && user_access("access content")) {
$links[] = "<a href=\"module.php?mod=forum\">". t("forum") ."</a>";
......@@ -24,18 +39,12 @@ function forum_form($node) {
function forum_save() {
if (user_access("administer nodes")) {
if ($node->nid) {
return array();
}
else {
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
if ($node->nid) {
return array();
}
else {
return 0;
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
}
function forum_num_comments($nid) {
......
<?php
// $Id$
function forum_node($field) {
$info = array("name" => "discussion forum");
return $info[$field];
}
function forum_access($op, $node) {
if ($op == "view") {
return $node->nid && $node->status && !$node->moderate;
}
return user_access("adminster nodes");
}
function forum_link($type) {
if ($type == "page" && user_access("access content")) {
$links[] = "<a href=\"module.php?mod=forum\">". t("forum") ."</a>";
......@@ -24,18 +39,12 @@ function forum_form($node) {
function forum_save() {
if (user_access("administer nodes")) {
if ($node->nid) {
return array();
}
else {
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
if ($node->nid) {
return array();
}
else {
return 0;
return array("promote" => 0, "moderate" => 0, "status" => 1);
}
}
function forum_num_comments($nid) {
......
......@@ -497,7 +497,7 @@ function import_page_last() {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
if ($item->link) {
......@@ -530,7 +530,7 @@ function import_page_feed($fid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
if ($item->link) {
......@@ -564,7 +564,7 @@ function import_page_bundle($bid) {
$output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
while ($item = db_fetch_object($result)) {
$links[] = "<a href=\"submit.php?mod=blog&type=import&id=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=node&op=add&type=blog&iid=$item->iid\">". t("blog it") ."</a>";
$links[] = "<a href=\"module.php?mod=import&op=feed&id=$item->fid\">". t("feed") ."</a>";
$links[] = "<a href=\"$item->link\">". t("visit") ."</a>";
......
......@@ -14,6 +14,30 @@ function node_help() {
}
}
function node_access($op, $node = 0) {
/*
** Convert the node to an object if necessary:
*/
if (is_array($node)) {
$node = node_object($node);
}
/*
** Construct a function:
*/
$function = $node->type ."_access";
if (function_exists($function)) {
return $function($op, $node);
}
else {
return 0;
}
}
function node_perm() {
return array("administer nodes", "access content", "post content");
}
......@@ -375,7 +399,7 @@ function node_form($edit) {
$output .= form_submit(t("Submit"));
}
if ($edit->nid && user_access("administer nodes")) {
if ($edit->nid && node_access("delete", $edit)) {
$output .= form_submit(t("Delete"));
}
......@@ -407,15 +431,13 @@ function node_add($type) {
}
else {
// TODO: make building a node list generic and find an easy way to associate post permissions with them
if (user_access("adminster nodes")) {
$nodes = array("book" => "book page", "blog" => "personal blog entry", "poll" => "online survey", "story" => "story", "page" => "static page", "forum" => "discussion forum");
}
else {
$nodes = array("book" => "book page", "blog" => "personal blog entry", "poll" => "online survey", "story" => "story");
}
foreach ($nodes as $type => $name) {
$links[] = "<a href=\"module.php?mod=node&op=add&type=$type\">". t($name) ."</a>";
$links = array();
foreach (module_list() as $name) {
if (($info = module_invoke($name, "node", "name")) && node_access("create", array("type" => $name))) {
$links[] = "<a href=\"module.php?mod=node&op=add&type=$name\">". t($info) ."</a>";
}
}
$output .= sprintf(t("Submit a %s."), implode(", ", $links));
......@@ -430,7 +452,14 @@ function node_edit($id) {
$node = node_load(array("nid" => $id));
// TODO: make this generic for all node types - temporary solution
return node_form($node);
if (node_access("update", $node)) {