diff options
author | Dries Buytaert <dries@buytaert.net> | 2004-05-09 19:28:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2004-05-09 19:28:43 +0000 |
commit | a158eca1b0d984a974999b6888c3f983d9111239 (patch) | |
tree | de18741dfb291a281b6aa5d5d1d6c0bd365475b8 | |
parent | 3de9d33e67999a7dc30664d1c49a5a9313d00a00 (diff) | |
download | brdo-a158eca1b0d984a974999b6888c3f983d9111239.tar.gz brdo-a158eca1b0d984a974999b6888c3f983d9111239.tar.bz2 |
- Patch #7577 by JonBob: code improvements to the book, forum, page and poll module.
-rw-r--r-- | modules/book.module | 397 | ||||
-rw-r--r-- | modules/book/book.module | 397 | ||||
-rw-r--r-- | modules/forum.module | 125 | ||||
-rw-r--r-- | modules/forum/forum.module | 125 | ||||
-rw-r--r-- | modules/page.module | 74 | ||||
-rw-r--r-- | modules/page/page.module | 74 | ||||
-rw-r--r-- | modules/poll.module | 78 | ||||
-rw-r--r-- | modules/poll/poll.module | 78 |
8 files changed, 786 insertions, 562 deletions
diff --git a/modules/book.module b/modules/book.module index a845d8404..7658a9251 100644 --- a/modules/book.module +++ b/modules/book.module @@ -1,18 +1,27 @@ <?php // $Id$ +/** + * Implementation of hook_node_name(). + */ function book_node_name($node) { - return t("book page"); + return t('book page'); } +/** + * Implementation of hook_perm(). + */ function book_perm() { - return array("maintain books"); + return array('maintain books'); } +/** + * Implementation of hook_access(). + */ function book_access($op, $node) { global $user; - if ($op == "view") { + if ($op == 'view') { /* ** Everyone can access all published book pages whether these pages ** are still waiting for approval or not. We might not always want @@ -23,16 +32,16 @@ function book_access($op, $node) { return $node->status; } - if ($op == "create") { + if ($op == 'create') { /* ** Only registered users can create book pages. Given the nature ** of the book module this is considered to be a good/safe idea. */ - return user_access("maintain books"); + return user_access('maintain books'); } - if ($op == "update") { + if ($op == 'update') { /* ** Only registered users can update book pages. Given the nature ** of the book module this is considered to be a good/safe idea. @@ -43,7 +52,7 @@ function book_access($op, $node) { ** are allowed. */ - return user_access("maintain books") && !$node->moderate && !$node->format && $node->revision; + return user_access('maintain books') && !$node->moderate && !$node->format && $node->revision; } } @@ -68,27 +77,35 @@ function book_link($type, $node = 0, $main = 0) { } if ($type == 'system') { - menu('node/add/book', t('book page'), user_access('maintain books') ? 'node_page' : MENU_DENIED, 0); + menu('node/add/book', t('book page'), user_access('maintain books') ? MENU_FALLTHROUGH : MENU_DENIED, 0); menu('admin/node/book', t('books'), user_access('administer nodes') ? 'book_admin' : MENU_DENIED, 4); menu('admin/node/book/orphan', t('orphan pages'), user_access('administer nodes') ? 'book_admin_orphan' : MENU_DENIED, 8); menu('admin/node/book/help', t('help'), user_access('administer nodes') ? 'book_help_page' : MENU_DENIED, 9); - $result = db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 ORDER BY b.weight, n.title'); while ($book = db_fetch_object($result)) { - menu("admin/node/book/$book->nid", t('"%title" book', array('%title' => $book->title)), user_access('administer nodes') ? 'book_admin' : MENU_DENIED); + menu("admin/node/book/$book->nid", t('"%title" book', array('%title' => $book->title)), MENU_FALLTHROUGH); } - menu('book', t('books'), user_access('access content') ? 'book_page' : MENU_DENIED, 0, MENU_HIDE); + menu('book', t('books'), user_access('access content') ? 'book_render' : MENU_DENIED, 0, MENU_HIDE); + menu('book/view', t('view'), user_access('access content') ? 'book_view_page' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED); + menu('book/print', t('printer-friendly version'), user_access('access content') ? 'book_print' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED); } return $links; } +/** + * Implementation of hook_block(). + * + * Displays the book table of contents in a block when a node is being + * viewed using a "book/view" path. + */ function book_block($op = 'list', $delta = 0) { // Only display this block when the user is browsing a book: if (arg(0) == 'book' && arg(1) == 'view' && $nid = arg(2)) { - $page = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d", $nid)); + $page = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $nid)); $path = book_location($page); $path[] = $page; @@ -112,12 +129,15 @@ function book_block($op = 'list', $delta = 0) { return $block; } +/** + * Implementation of hook_load(). + */ function book_load($node) { global $user; - $book = db_fetch_object(db_query("SELECT format, parent, weight, log FROM {book} WHERE nid = %d", $node->nid)); + $book = db_fetch_object(db_query('SELECT format, parent, weight, log FROM {book} WHERE nid = %d', $node->nid)); - if (arg(1) == 'edit' && !user_access("administer nodes")) { + if (arg(1) == 'edit' && !user_access('administer nodes')) { /* ** If a user is about to update a book page, we overload some ** fields to reflect the changes. @@ -129,14 +149,14 @@ function book_load($node) { } else { $book->uid = 0; - $book->name = ""; + $book->name = ''; } } /* ** We set the revision field to indicate that we have to create ** a new revision when updating this book page. We enable this - ** always such that the "edit this page"-links appear. + ** always such that the "edit this page" links appear. */ $book->revision = 1; @@ -144,52 +164,67 @@ function book_load($node) { return $book; } +/** + * Implementation of hook_insert(). + */ function book_insert($node) { db_query("INSERT INTO {book} (nid, format, parent, weight, log) VALUES (%d, %d, %d, %d, '%s')", $node->nid, $node->format, $node->parent, $node->weight, $node->log); } +/** + * Implementation of hook_update(). + */ function book_update($node) { db_query("UPDATE {book} SET format = %d, parent = %d, weight = %d, log = '%s' WHERE nid = %d", $node->format, $node->parent, $node->weight, $node->log, $node->nid); } +/** + * Implementation of hook_delete(). + */ function book_delete(&$node) { - db_query("DELETE FROM {book} WHERE nid = %d", $node->nid); + db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); } +/** + * Implementation of hook_validate(). + */ function book_validate(&$node) { - if ($node->format && user_access("create php content")) { - // Do not filter PHP code, do not auto-extract a teaser + if ($node->format && user_access('create php content')) { + // Do not filter PHP code. Do not auto-extract a teaser. $node->teaser = $node->body; } else { $node->format = 0; } - // Set default values for non administrators: - if (!user_access("administer nodes")) { + // Set default values for non-administrators. + if (!user_access('administer nodes')) { $node->format = 0; $node->weight = 0; $node->revision = 1; } } +/** + * Implementation of hook_form(). + */ function book_form(&$node, &$error) { global $user; - $op = $_POST["op"]; - $output = form_select(t("Parent"), "parent", $node->parent, book_toc(), t("The parent subject or category the page belongs in.")); + $op = $_POST['op']; + $output = form_select(t('Parent'), 'parent', $node->parent, book_toc(), t('The parent subject or category the page belongs in.')); - if (function_exists("taxonomy_node_form")) { - $output .= implode("", taxonomy_node_form("book", $node)); + if (function_exists('taxonomy_node_form')) { + $output .= implode('', taxonomy_node_form('book', $node)); } - $output .= form_textarea(t("Body"), "body", $node->body, 60, 20, filter_tips_short()); - $output .= form_textarea(t("Log message"), "log", $node->log, 60, 5, t("An explanation of the additions or updates being made to help the group understand your motivations.")); + $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short()); + $output .= form_textarea(t('Log message'), 'log', $node->log, 60, 5, t('An explanation of the additions or updates being made to help the group understand your motivations.')); - if (user_access("administer nodes")) { - $output .= form_weight(t("Weight"), "weight", $node->weight, 15, t("The heavier pages will sink and the lighter pages will be positioned nearer the top.")); - if (user_access("create php content")) { - $output .= form_radios("Type", "format", $node->format, array(0 => "HTML / text", 1 => "PHP")); + if (user_access('administer nodes')) { + $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.')); + if (user_access('create php content')) { + $output .= form_radios('Type', 'format', $node->format, array(0 => 'HTML / text', 1 => 'PHP')); } } else { @@ -198,71 +233,72 @@ function book_form(&$node, &$error) { ** authored by that user: */ - $output .= form_hidden("revision", 1); + $output .= form_hidden('revision', 1); } return $output; } +/** + * Implementation of hook_node_link(). + */ function book_node_link($node = 0) { global $user; - $op = $_POST["op"]; - $edit = $_POST["edit"]; + $op = $_POST['op']; + $edit = $_POST['edit']; - if ($node->type != "book") { + if ($node->type != 'book') { - if ($edit["nid"]) { - $node = node_load(array("nid" => $edit["nid"])); + if ($edit['nid']) { + $node = node_load(array('nid' => $edit['nid'])); } - if ($op == t("Add to book outline")) { - db_query("INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)", $node->nid, $edit["parent"], $edit["weight"]); - drupal_set_message(t("added the node to the book.")); + if ($op == t('Add to book outline')) { + db_query('INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)', $node->nid, $edit['parent'], $edit['weight']); + drupal_set_message(t('added the node to the book.')); } - if ($op == t("Update book outline")) { - db_query("UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d", $edit["parent"], $edit["weight"], $node->nid); - drupal_set_message(t("updated the book outline.")); + if ($op == t('Update book outline')) { + db_query('UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d', $edit['parent'], $edit['weight'], $node->nid); + drupal_set_message(t('updated the book outline.')); } - if ($op == t("Remove from book outline")) { - db_query("DELETE FROM {book} WHERE nid = %d", $node->nid); - drupal_set_message(t("removed the node form the book.")); + if ($op == t('Remove from book outline')) { + db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + drupal_set_message(t('removed the node form the book.')); } - $output .= "<h3>". t("Edit book outline for node %booktitle", array("%booktitle" => "<em>$node->title</em>")) ."</h3>"; + $output .= '<h3>'. t('Edit book outline for node "%booktitle"', array('%booktitle' => "<em>$node->title</em>")) .'</h3>'; - if ($edit["nid"]) { - $page = db_fetch_object(db_query("SELECT * FROM {book} WHERE nid = %d", $node->nid)); + if ($edit['nid']) { + $page = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid)); - $output .= form_select(t("Parent"), "parent", $page->parent, book_toc(), t("The parent subject or category the page belongs in.")); - $output .= form_weight(t("Weight"), "weight", $node->weight, 15, t("The heavier pages will sink and the lighter pages will be positioned nearer the top.")); + $output .= form_select(t('Parent'), 'parent', $page->parent, book_toc(), t('The parent subject or category the page belongs in.')); + $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.')); if ($page->nid) { - $output .= form_submit(t("Update book outline")); - $output .= form_submit(t("Remove from book outline")); + $output .= form_submit(t('Update book outline')); + $output .= form_submit(t('Remove from book outline')); } else { - $output .= form_submit(t("Add to book outline")); + $output .= form_submit(t('Add to book outline')); } } else { - $output .= form_submit(t("Edit book outline")); + $output .= form_submit(t('Edit book outline')); } - $output .= form_hidden("nid", $node->nid); + $output .= form_hidden('nid', $node->nid); - return form($output, "post", url("admin/node/book")); + return form($output, 'post', url('admin/node/book')); } } -/* -** Return the the most recent revision that matches the specified -** conditions. -*/ - +/** + * Return the the most recent revision that matches the specified conditions. + */ function book_revision_load($page, $conditions = array()) { $revisions = array_reverse(node_revision_list($page)); @@ -293,13 +329,13 @@ function book_revision_load($page, $conditions = array()) { } } -/* -** Return the path (call stack) to a certain book page. -*/ +/** + * Return the path (call stack) to a certain book page. + */ function book_location($node, $nodes = array()) { // TODO: eliminate the recursion - $parent = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d", $node->parent)); + $parent = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $node->parent)); if ($parent->title) { $nodes = book_location($parent, $nodes); array_push($nodes, $parent); @@ -310,7 +346,7 @@ function book_location($node, $nodes = array()) { function book_location_down($node, $nodes = array()) { // TODO: eliminate the recursion - $last_direct_child = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight DESC, n.title DESC", $node->nid)); + $last_direct_child = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight DESC, n.title DESC', $node->nid)); if ($last_direct_child) { array_push($nodes, $last_direct_child); $nodes = book_location_down($last_direct_child, $nodes); @@ -318,6 +354,9 @@ function book_location_down($node, $nodes = array()) { return $nodes; } +/** + * Fetch the node object of the previous page of the book. + */ function book_prev($node) { // if the parent is zero, we are at the start of a book so there is no previous if ($node->parent == 0) { @@ -339,6 +378,9 @@ function book_prev($node) { } } +/** + * Fetch the node object of the next page of the book. + */ function book_next($node) { // get first direct child $child = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight ASC, n.title ASC", $node->nid)); @@ -357,8 +399,11 @@ function book_next($node) { } } +/** + * Implementation of hook_content(). + */ function book_content($node, $main = 0) { - $op = $_POST["op"]; + $op = $_POST['op']; /* ** Always display the most recently approved revision of a node @@ -366,8 +411,8 @@ function book_content($node, $main = 0) { ** the moderation queue. */ - if ($op != t("Preview") && $node->moderate && arg(0) != "queue") { - $revision = book_revision_load($node, array("moderate" => 0, "status" => 1)); + if ($op != t('Preview') && $node->moderate && arg(0) != 'queue') { + $revision = book_revision_load($node, array('moderate' => 0, 'status' => 1)); if ($revision) { $node = $revision; @@ -381,7 +426,7 @@ function book_content($node, $main = 0) { if ($node->format == 1) { // Make sure only authorized users can preview PHP pages. - if ($op == t("Preview") && !user_access("create php content")) { + if ($op == t('Preview') && !user_access('create php content')) { return; } @@ -397,22 +442,23 @@ function book_content($node, $main = 0) { return $node; } +/** + * Implementation of hook_view(). + * + * If not displayed on the main page, we render the node as a page in the + * book with extra links to the previous and next pages. + */ function book_view($node, $main = 0, $page = 0) { $node = book_content($node, $main); - /* - ** Display the node. If not displayed on the main page, we render - ** the node as a page in the book with extra links to the previous - ** and the next page. - */ - $output = ""; + $output = ''; if ($main) { - $output .= theme("node", $node, $main, $page); + $output .= theme('node', $node, $main, $page); } else { if ($node->moderate) { - $node->body = $node->body . "<div class=\"log\"><div class=\"title\">". t("Log") .":</div>$node->log</div>"; + $node->body = $node->body . '<div class="log"><div class="title">'. t('Log') .":</div>$node->log</div>"; } // Add the navigation and the breadcrumb if we view a page if ($page) { @@ -421,30 +467,33 @@ function book_view($node, $main = 0, $page = 0) { drupal_set_breadcrumb($node->breadcrumb); } // Print the node - $output .= theme("node", $node, $main, $page); + $output .= theme('node', $node, $main, $page); } return $output; } +/** + * Present a view of a node as a book page. + */ function book_show($node, $cid) { - $output = ""; + $output = ''; - if (node_access("view", $node)) { + if (node_access('view', $node)) { - if ($node->type == "book") { + if ($node->type == 'book') { $output .= book_view($node, 0, 1); } else { - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); /* ** Add the book navigation if the node is in the book. */ - $book = db_fetch_object(db_query("SELECT * FROM {book} WHERE nid = %d", $node->nid)); + $book = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid)); if ($book) { foreach ($book as $key => $value) { @@ -457,24 +506,24 @@ function book_show($node, $cid) { ** make $node->type a book. This is for the links. */ - $node->type = "book"; + $node->type = 'book'; /* ** View the node */ drupal_set_breadcrumb($node->breadcrumb); - $output .= theme("node", $node, 0, 1); + $output .= theme('node', $node, 0, 1); } else { /* ** We can't get the content of the node and just view the node. - ** We don't add breadcrums or links. + ** We don't add breadcrumbs or links. */ $output .= node_view($node, 0, 1); } } - if (function_exists("comment_render") && $node->comment) { + if (function_exists('comment_render') && $node->comment) { $output .= comment_render($node, $cid); } @@ -489,56 +538,59 @@ function book_show($node, $cid) { return $output; } +/** + * Prepares both the custom breadcrumb trail and the forward/backward + * navigation for a node presented as a book page. + */ function book_navigation($node) { - $path = book_location($node); /* ** Construct the breadcrumb: */ - $node->breadcrumb = ""; // Overwrite the trail with a book trail. - $node->breadcrumb[] = l(t("Home"), ""); + $node->breadcrumb = ''; // Overwrite the trail with a book trail. + $node->breadcrumb[] = l(t('Home'), ''); foreach ($path as $level) { $node->breadcrumb[] = l($level->title, "book/view/$level->nid"); } if ($node->nid) { - $output .= "<div class=\"book\">"; + $output .= '<div class="book">'; if ($tree = book_tree($node->nid)) { - $output .= "<div class=\"tree\">". book_tree($node->nid) ."</div>"; + $output .= '<div class="tree">'. book_tree($node->nid) .'</div>'; } if ($prev = book_prev($node)) { - $links .= "<div class=\"prev\">"; - $links .= l(t("previous"), "book/view/$prev->nid", array("title" => t("View the previous page."))); - $links .= "</div>"; + $links .= '<div class="prev">'; + $links .= l(t('previous'), "book/view/$prev->nid", array('title' => t('View the previous page.'))); + $links .= '</div>'; $titles .= "<div class=\"prev\">$prev->title</div>"; } else { - $links .= "<div class=\"prev\"> </div>"; // make an empty div to fill the space + $links .= '<div class="prev"> </div>'; // make an empty div to fill the space } if ($next = book_next($node)) { - $links .= "<div class=\"next\">"; - $links .= l(t("next"), "book/view/$next->nid", array("title" => t("View the next page."))); - $links .= "</div>"; + $links .= '<div class="next">'; + $links .= l(t('next'), "book/view/$next->nid", array('title' => t('View the next page.'))); + $links .= '</div>'; $titles .= "<div class=\"next\">$next->title</div>"; } else { - $links .= "<div class=\"next\"> </div>"; // make an empty div to fill the space + $links .= '<div class="next"> </div>'; // make an empty div to fill the space } if ($node->parent) { - $links .= "<div class=\"up\">"; - $links .= l(t("up"), "book/view/$node->parent", array("title" => t("View this page's parent section."))); - $links .= "</div>"; + $links .= '<div class="up">'; + $links .= l(t('up'), "book/view/$node->parent", array('title' => t("View this page's parent section."))); + $links .= '</div>'; } - $output .= "<div class=\"nav\">"; + $output .= '<div class="nav">'; $output .= " <div class=\"links\">$links</div>"; $output .= " <div class=\"titles\">$titles</div>"; - $output .= "</div>"; - $output .= "</div>"; + $output .= '</div>'; + $output .= '</div>'; } $node->body = $node->body.$output; @@ -558,9 +610,8 @@ function book_toc_recurse($nid, $indent, $toc, $children) { return $toc; } -function book_toc($parent = 0, $indent = "", $toc = array()) { - - $result = db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = '1' ORDER BY b.weight, n.title"); +function book_toc($parent = 0, $indent = '', $toc = array()) { + $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title'); while ($node = db_fetch_object($result)) { if (!$children[$node->parent]) { @@ -574,8 +625,8 @@ function book_toc($parent = 0, $indent = "", $toc = array()) { ** only administrators can start new books. */ - if (user_access("administer nodes")) { - $toc[0] = "<". t("top-level") .">"; + if (user_access('administer nodes')) { + $toc[0] = '<'. t('top-level') .'>'; } $toc = book_toc_recurse($parent, $indent, $toc, $children); @@ -583,7 +634,6 @@ function book_toc($parent = 0, $indent = "", $toc = array()) { return $toc; } - function book_tree_recurse($nid, $depth, $children, $unfold = array()) { if ($depth > 0) { if ($children[$nid]) { @@ -614,10 +664,8 @@ function book_tree_recurse($nid, $depth, $children, $unfold = array()) { return $output; } - function book_tree($parent = 0, $depth = 3, $unfold = array()) { - - $result = db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = '1' AND n.moderate = '0' ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title'); while ($node = db_fetch_object($result)) { $list = $children[$node->parent] ? $children[$node->parent] : array(); @@ -630,63 +678,56 @@ function book_tree($parent = 0, $depth = 3, $unfold = array()) { } } - +/** + * Menu callback. Prints a listing of all books. + */ function book_render() { - $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title'); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); if ($node) { // take the most recent approved revision, extract the page and check output: $node = book_content($node); // output the content: - $output .= "<div class=\"book\">"; - $output .= "<div class=\"title\">". l($node->title, "book/view/$node->nid") ."</div>"; - $output .= "<div class=\"body\">". $node->body ."</div>"; - $output .= "</div>"; + $output .= '<div class="book">'; + $output .= '<div class="title">'. l($node->title, "book/view/$node->nid") .'</div>'; + $output .= '<div class="body">'. $node->body .'</div>'; + $output .= '</div>'; } } - drupal_set_title(t("Books")); - print theme("page", $output); + drupal_set_title(t('Books')); + print theme('page', $output); } -function book_page() { - - if (user_access("access content")) { - switch (arg(1)) { - case "view": - $node = node_load(array("nid" => arg(2))); - $output = book_show($node, arg(3)); - print theme("page", $output, $node->title); - break; - case "print": - print book_print(arg(2), $depth = 1); - break; - default: - book_render(); - } - } - else { - drupal_access_denied(); - } +/** + * Menu callback. Prints a node as a book page, complete with navigation. + */ +function book_view_page($nid = 0, $cid = 0) { + $node = node_load(array('nid' => $nid)); + $output = book_show($node, $cid); + print theme('page', $output, $node->title); } -function book_print($id = "", $depth = 1) { +/** + * Menu callback. Generates printer-friendly book page will all descendants. + */ +function book_print($nid = 0, $depth = 1) { global $base_url; - $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = %d AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title", $id); + $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = %d AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title', $nid); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); if ($node) { // output the content: - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); } $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>"; @@ -696,37 +737,37 @@ function book_print($id = "", $depth = 1) { } } - $output .= book_print_recurse($id, $depth); + $output .= book_print_recurse($nid, $depth); $html = "<html><head><title>$node->title</title>"; $html .= "<base href=\"$base_url/\" />"; $html .= "<style type=\"text/css\">\n@import url(misc/print.css);\n</style>"; - $html .= "</head><body>". $output ."</body></html>"; + $html .= '</head><body>'. $output .'</body></html>'; - return $html; + print $html; } -function book_print_recurse($parent = "", $depth = 1) { +function book_print_recurse($parent = '', $depth = 1) { $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title"); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); // take the most recent approved revision: if ($node->moderate) { - $node = book_revision_load($node, array("moderate" => 0, "status" => 1)); + $node = book_revision_load($node, array('moderate' => 0, 'status' => 1)); } if ($node) { // output the content: - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); } $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>"; if ($node->body) { - $output .= "<ul>". $node->body ."</ul>"; + $output .= '<ul>'. $node->body .'</ul>'; } $output .= book_print_recurse($node->nid, $depth + 1); @@ -737,10 +778,7 @@ function book_print_recurse($parent = "", $depth = 1) { } function book_admin_view_line($node, $depth = 0) { - - $row = array("<div style=\"padding-left: ". (25 * $depth) ."px;\">". form_textfield(NULL, "$node->nid][title", $node->title, 64, 255) ."</div>", form_weight(NULL, "$node->nid][weight", $node->weight), l(t("view node"), "node/view/$node->nid"), l(t("edit node"), "admin/node/edit/$node->nid"), l(t("delete node"), "admin/node/delete/$node->nid")); - - return $row; + return array("<div style=\"padding-left: ". (25 * $depth) ."px;\">". form_textfield(NULL, "$node->nid][title", $node->title, 64, 255) ."</div>", form_weight(NULL, "$node->nid][weight", $node->weight), l(t("view node"), "node/view/$node->nid"), l(t("edit node"), "admin/node/edit/$node->nid"), l(t("delete node"), "admin/node/delete/$node->nid")); } function book_admin_view_book($nid, $depth = 1) { @@ -755,8 +793,10 @@ function book_admin_view_book($nid, $depth = 1) { return $rows; } +/** + * Display an administrative view of the hierarchy of a book. + */ function book_admin_view($nid, $depth = 0) { - if ($nid) { $node = node_load(array("nid" => $nid)); @@ -774,7 +814,6 @@ function book_admin_view($nid, $depth = 0) { } function book_admin_save($nid, $edit = array()) { - if ($nid) { $book = node_load(array("nid" => $nid)); @@ -805,8 +844,10 @@ function book_admin_save($nid, $edit = array()) { } } +/** + * Menu callback. Displays a listing of all orphaned book pages. + */ function book_admin_orphan() { - $result = db_query("SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid"); while ($page = db_fetch_object($result)) { @@ -828,17 +869,13 @@ function book_admin_orphan() { print theme("page", $output); } -function book_admin_links() { -} - -function book_admin() { +/** + * Menu callback. Displays the book administration page. + */ +function book_admin($nid) { $op = $_POST["op"]; $edit = $_POST["edit"]; - if (empty($op)) { - $op = arg(3); - } - switch ($op) { case t("Edit book outline"): case t("Add to book outline"): @@ -846,19 +883,19 @@ function book_admin() { case t("Update book outline"): $output = book_node_link(); break; - case "orphan": - $output = book_admin_orphan(); - break; case t("Save book pages"): - drupal_set_message(book_admin_save(arg(3), $edit)); + drupal_set_message(book_admin_save($nid, $edit)); // fall through: default: - $output .= book_admin_view(arg(3)); + $output .= book_admin_view($nid); break; } print theme("page", $output); } +/** + * Implementation of hook_help(). + */ function book_help($section = "admin/help#book") { $output = ""; diff --git a/modules/book/book.module b/modules/book/book.module index a845d8404..7658a9251 100644 --- a/modules/book/book.module +++ b/modules/book/book.module @@ -1,18 +1,27 @@ <?php // $Id$ +/** + * Implementation of hook_node_name(). + */ function book_node_name($node) { - return t("book page"); + return t('book page'); } +/** + * Implementation of hook_perm(). + */ function book_perm() { - return array("maintain books"); + return array('maintain books'); } +/** + * Implementation of hook_access(). + */ function book_access($op, $node) { global $user; - if ($op == "view") { + if ($op == 'view') { /* ** Everyone can access all published book pages whether these pages ** are still waiting for approval or not. We might not always want @@ -23,16 +32,16 @@ function book_access($op, $node) { return $node->status; } - if ($op == "create") { + if ($op == 'create') { /* ** Only registered users can create book pages. Given the nature ** of the book module this is considered to be a good/safe idea. */ - return user_access("maintain books"); + return user_access('maintain books'); } - if ($op == "update") { + if ($op == 'update') { /* ** Only registered users can update book pages. Given the nature ** of the book module this is considered to be a good/safe idea. @@ -43,7 +52,7 @@ function book_access($op, $node) { ** are allowed. */ - return user_access("maintain books") && !$node->moderate && !$node->format && $node->revision; + return user_access('maintain books') && !$node->moderate && !$node->format && $node->revision; } } @@ -68,27 +77,35 @@ function book_link($type, $node = 0, $main = 0) { } if ($type == 'system') { - menu('node/add/book', t('book page'), user_access('maintain books') ? 'node_page' : MENU_DENIED, 0); + menu('node/add/book', t('book page'), user_access('maintain books') ? MENU_FALLTHROUGH : MENU_DENIED, 0); menu('admin/node/book', t('books'), user_access('administer nodes') ? 'book_admin' : MENU_DENIED, 4); menu('admin/node/book/orphan', t('orphan pages'), user_access('administer nodes') ? 'book_admin_orphan' : MENU_DENIED, 8); menu('admin/node/book/help', t('help'), user_access('administer nodes') ? 'book_help_page' : MENU_DENIED, 9); - $result = db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 ORDER BY b.weight, n.title'); while ($book = db_fetch_object($result)) { - menu("admin/node/book/$book->nid", t('"%title" book', array('%title' => $book->title)), user_access('administer nodes') ? 'book_admin' : MENU_DENIED); + menu("admin/node/book/$book->nid", t('"%title" book', array('%title' => $book->title)), MENU_FALLTHROUGH); } - menu('book', t('books'), user_access('access content') ? 'book_page' : MENU_DENIED, 0, MENU_HIDE); + menu('book', t('books'), user_access('access content') ? 'book_render' : MENU_DENIED, 0, MENU_HIDE); + menu('book/view', t('view'), user_access('access content') ? 'book_view_page' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED); + menu('book/print', t('printer-friendly version'), user_access('access content') ? 'book_print' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED); } return $links; } +/** + * Implementation of hook_block(). + * + * Displays the book table of contents in a block when a node is being + * viewed using a "book/view" path. + */ function book_block($op = 'list', $delta = 0) { // Only display this block when the user is browsing a book: if (arg(0) == 'book' && arg(1) == 'view' && $nid = arg(2)) { - $page = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d", $nid)); + $page = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $nid)); $path = book_location($page); $path[] = $page; @@ -112,12 +129,15 @@ function book_block($op = 'list', $delta = 0) { return $block; } +/** + * Implementation of hook_load(). + */ function book_load($node) { global $user; - $book = db_fetch_object(db_query("SELECT format, parent, weight, log FROM {book} WHERE nid = %d", $node->nid)); + $book = db_fetch_object(db_query('SELECT format, parent, weight, log FROM {book} WHERE nid = %d', $node->nid)); - if (arg(1) == 'edit' && !user_access("administer nodes")) { + if (arg(1) == 'edit' && !user_access('administer nodes')) { /* ** If a user is about to update a book page, we overload some ** fields to reflect the changes. @@ -129,14 +149,14 @@ function book_load($node) { } else { $book->uid = 0; - $book->name = ""; + $book->name = ''; } } /* ** We set the revision field to indicate that we have to create ** a new revision when updating this book page. We enable this - ** always such that the "edit this page"-links appear. + ** always such that the "edit this page" links appear. */ $book->revision = 1; @@ -144,52 +164,67 @@ function book_load($node) { return $book; } +/** + * Implementation of hook_insert(). + */ function book_insert($node) { db_query("INSERT INTO {book} (nid, format, parent, weight, log) VALUES (%d, %d, %d, %d, '%s')", $node->nid, $node->format, $node->parent, $node->weight, $node->log); } +/** + * Implementation of hook_update(). + */ function book_update($node) { db_query("UPDATE {book} SET format = %d, parent = %d, weight = %d, log = '%s' WHERE nid = %d", $node->format, $node->parent, $node->weight, $node->log, $node->nid); } +/** + * Implementation of hook_delete(). + */ function book_delete(&$node) { - db_query("DELETE FROM {book} WHERE nid = %d", $node->nid); + db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); } +/** + * Implementation of hook_validate(). + */ function book_validate(&$node) { - if ($node->format && user_access("create php content")) { - // Do not filter PHP code, do not auto-extract a teaser + if ($node->format && user_access('create php content')) { + // Do not filter PHP code. Do not auto-extract a teaser. $node->teaser = $node->body; } else { $node->format = 0; } - // Set default values for non administrators: - if (!user_access("administer nodes")) { + // Set default values for non-administrators. + if (!user_access('administer nodes')) { $node->format = 0; $node->weight = 0; $node->revision = 1; } } +/** + * Implementation of hook_form(). + */ function book_form(&$node, &$error) { global $user; - $op = $_POST["op"]; - $output = form_select(t("Parent"), "parent", $node->parent, book_toc(), t("The parent subject or category the page belongs in.")); + $op = $_POST['op']; + $output = form_select(t('Parent'), 'parent', $node->parent, book_toc(), t('The parent subject or category the page belongs in.')); - if (function_exists("taxonomy_node_form")) { - $output .= implode("", taxonomy_node_form("book", $node)); + if (function_exists('taxonomy_node_form')) { + $output .= implode('', taxonomy_node_form('book', $node)); } - $output .= form_textarea(t("Body"), "body", $node->body, 60, 20, filter_tips_short()); - $output .= form_textarea(t("Log message"), "log", $node->log, 60, 5, t("An explanation of the additions or updates being made to help the group understand your motivations.")); + $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short()); + $output .= form_textarea(t('Log message'), 'log', $node->log, 60, 5, t('An explanation of the additions or updates being made to help the group understand your motivations.')); - if (user_access("administer nodes")) { - $output .= form_weight(t("Weight"), "weight", $node->weight, 15, t("The heavier pages will sink and the lighter pages will be positioned nearer the top.")); - if (user_access("create php content")) { - $output .= form_radios("Type", "format", $node->format, array(0 => "HTML / text", 1 => "PHP")); + if (user_access('administer nodes')) { + $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.')); + if (user_access('create php content')) { + $output .= form_radios('Type', 'format', $node->format, array(0 => 'HTML / text', 1 => 'PHP')); } } else { @@ -198,71 +233,72 @@ function book_form(&$node, &$error) { ** authored by that user: */ - $output .= form_hidden("revision", 1); + $output .= form_hidden('revision', 1); } return $output; } +/** + * Implementation of hook_node_link(). + */ function book_node_link($node = 0) { global $user; - $op = $_POST["op"]; - $edit = $_POST["edit"]; + $op = $_POST['op']; + $edit = $_POST['edit']; - if ($node->type != "book") { + if ($node->type != 'book') { - if ($edit["nid"]) { - $node = node_load(array("nid" => $edit["nid"])); + if ($edit['nid']) { + $node = node_load(array('nid' => $edit['nid'])); } - if ($op == t("Add to book outline")) { - db_query("INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)", $node->nid, $edit["parent"], $edit["weight"]); - drupal_set_message(t("added the node to the book.")); + if ($op == t('Add to book outline')) { + db_query('INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)', $node->nid, $edit['parent'], $edit['weight']); + drupal_set_message(t('added the node to the book.')); } - if ($op == t("Update book outline")) { - db_query("UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d", $edit["parent"], $edit["weight"], $node->nid); - drupal_set_message(t("updated the book outline.")); + if ($op == t('Update book outline')) { + db_query('UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d', $edit['parent'], $edit['weight'], $node->nid); + drupal_set_message(t('updated the book outline.')); } - if ($op == t("Remove from book outline")) { - db_query("DELETE FROM {book} WHERE nid = %d", $node->nid); - drupal_set_message(t("removed the node form the book.")); + if ($op == t('Remove from book outline')) { + db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + drupal_set_message(t('removed the node form the book.')); } - $output .= "<h3>". t("Edit book outline for node %booktitle", array("%booktitle" => "<em>$node->title</em>")) ."</h3>"; + $output .= '<h3>'. t('Edit book outline for node "%booktitle"', array('%booktitle' => "<em>$node->title</em>")) .'</h3>'; - if ($edit["nid"]) { - $page = db_fetch_object(db_query("SELECT * FROM {book} WHERE nid = %d", $node->nid)); + if ($edit['nid']) { + $page = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid)); - $output .= form_select(t("Parent"), "parent", $page->parent, book_toc(), t("The parent subject or category the page belongs in.")); - $output .= form_weight(t("Weight"), "weight", $node->weight, 15, t("The heavier pages will sink and the lighter pages will be positioned nearer the top.")); + $output .= form_select(t('Parent'), 'parent', $page->parent, book_toc(), t('The parent subject or category the page belongs in.')); + $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.')); if ($page->nid) { - $output .= form_submit(t("Update book outline")); - $output .= form_submit(t("Remove from book outline")); + $output .= form_submit(t('Update book outline')); + $output .= form_submit(t('Remove from book outline')); } else { - $output .= form_submit(t("Add to book outline")); + $output .= form_submit(t('Add to book outline')); } } else { - $output .= form_submit(t("Edit book outline")); + $output .= form_submit(t('Edit book outline')); } - $output .= form_hidden("nid", $node->nid); + $output .= form_hidden('nid', $node->nid); - return form($output, "post", url("admin/node/book")); + return form($output, 'post', url('admin/node/book')); } } -/* -** Return the the most recent revision that matches the specified -** conditions. -*/ - +/** + * Return the the most recent revision that matches the specified conditions. + */ function book_revision_load($page, $conditions = array()) { $revisions = array_reverse(node_revision_list($page)); @@ -293,13 +329,13 @@ function book_revision_load($page, $conditions = array()) { } } -/* -** Return the path (call stack) to a certain book page. -*/ +/** + * Return the path (call stack) to a certain book page. + */ function book_location($node, $nodes = array()) { // TODO: eliminate the recursion - $parent = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d", $node->parent)); + $parent = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $node->parent)); if ($parent->title) { $nodes = book_location($parent, $nodes); array_push($nodes, $parent); @@ -310,7 +346,7 @@ function book_location($node, $nodes = array()) { function book_location_down($node, $nodes = array()) { // TODO: eliminate the recursion - $last_direct_child = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight DESC, n.title DESC", $node->nid)); + $last_direct_child = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight DESC, n.title DESC', $node->nid)); if ($last_direct_child) { array_push($nodes, $last_direct_child); $nodes = book_location_down($last_direct_child, $nodes); @@ -318,6 +354,9 @@ function book_location_down($node, $nodes = array()) { return $nodes; } +/** + * Fetch the node object of the previous page of the book. + */ function book_prev($node) { // if the parent is zero, we are at the start of a book so there is no previous if ($node->parent == 0) { @@ -339,6 +378,9 @@ function book_prev($node) { } } +/** + * Fetch the node object of the next page of the book. + */ function book_next($node) { // get first direct child $child = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight ASC, n.title ASC", $node->nid)); @@ -357,8 +399,11 @@ function book_next($node) { } } +/** + * Implementation of hook_content(). + */ function book_content($node, $main = 0) { - $op = $_POST["op"]; + $op = $_POST['op']; /* ** Always display the most recently approved revision of a node @@ -366,8 +411,8 @@ function book_content($node, $main = 0) { ** the moderation queue. */ - if ($op != t("Preview") && $node->moderate && arg(0) != "queue") { - $revision = book_revision_load($node, array("moderate" => 0, "status" => 1)); + if ($op != t('Preview') && $node->moderate && arg(0) != 'queue') { + $revision = book_revision_load($node, array('moderate' => 0, 'status' => 1)); if ($revision) { $node = $revision; @@ -381,7 +426,7 @@ function book_content($node, $main = 0) { if ($node->format == 1) { // Make sure only authorized users can preview PHP pages. - if ($op == t("Preview") && !user_access("create php content")) { + if ($op == t('Preview') && !user_access('create php content')) { return; } @@ -397,22 +442,23 @@ function book_content($node, $main = 0) { return $node; } +/** + * Implementation of hook_view(). + * + * If not displayed on the main page, we render the node as a page in the + * book with extra links to the previous and next pages. + */ function book_view($node, $main = 0, $page = 0) { $node = book_content($node, $main); - /* - ** Display the node. If not displayed on the main page, we render - ** the node as a page in the book with extra links to the previous - ** and the next page. - */ - $output = ""; + $output = ''; if ($main) { - $output .= theme("node", $node, $main, $page); + $output .= theme('node', $node, $main, $page); } else { if ($node->moderate) { - $node->body = $node->body . "<div class=\"log\"><div class=\"title\">". t("Log") .":</div>$node->log</div>"; + $node->body = $node->body . '<div class="log"><div class="title">'. t('Log') .":</div>$node->log</div>"; } // Add the navigation and the breadcrumb if we view a page if ($page) { @@ -421,30 +467,33 @@ function book_view($node, $main = 0, $page = 0) { drupal_set_breadcrumb($node->breadcrumb); } // Print the node - $output .= theme("node", $node, $main, $page); + $output .= theme('node', $node, $main, $page); } return $output; } +/** + * Present a view of a node as a book page. + */ function book_show($node, $cid) { - $output = ""; + $output = ''; - if (node_access("view", $node)) { + if (node_access('view', $node)) { - if ($node->type == "book") { + if ($node->type == 'book') { $output .= book_view($node, 0, 1); } else { - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); /* ** Add the book navigation if the node is in the book. */ - $book = db_fetch_object(db_query("SELECT * FROM {book} WHERE nid = %d", $node->nid)); + $book = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid)); if ($book) { foreach ($book as $key => $value) { @@ -457,24 +506,24 @@ function book_show($node, $cid) { ** make $node->type a book. This is for the links. */ - $node->type = "book"; + $node->type = 'book'; /* ** View the node */ drupal_set_breadcrumb($node->breadcrumb); - $output .= theme("node", $node, 0, 1); + $output .= theme('node', $node, 0, 1); } else { /* ** We can't get the content of the node and just view the node. - ** We don't add breadcrums or links. + ** We don't add breadcrumbs or links. */ $output .= node_view($node, 0, 1); } } - if (function_exists("comment_render") && $node->comment) { + if (function_exists('comment_render') && $node->comment) { $output .= comment_render($node, $cid); } @@ -489,56 +538,59 @@ function book_show($node, $cid) { return $output; } +/** + * Prepares both the custom breadcrumb trail and the forward/backward + * navigation for a node presented as a book page. + */ function book_navigation($node) { - $path = book_location($node); /* ** Construct the breadcrumb: */ - $node->breadcrumb = ""; // Overwrite the trail with a book trail. - $node->breadcrumb[] = l(t("Home"), ""); + $node->breadcrumb = ''; // Overwrite the trail with a book trail. + $node->breadcrumb[] = l(t('Home'), ''); foreach ($path as $level) { $node->breadcrumb[] = l($level->title, "book/view/$level->nid"); } if ($node->nid) { - $output .= "<div class=\"book\">"; + $output .= '<div class="book">'; if ($tree = book_tree($node->nid)) { - $output .= "<div class=\"tree\">". book_tree($node->nid) ."</div>"; + $output .= '<div class="tree">'. book_tree($node->nid) .'</div>'; } if ($prev = book_prev($node)) { - $links .= "<div class=\"prev\">"; - $links .= l(t("previous"), "book/view/$prev->nid", array("title" => t("View the previous page."))); - $links .= "</div>"; + $links .= '<div class="prev">'; + $links .= l(t('previous'), "book/view/$prev->nid", array('title' => t('View the previous page.'))); + $links .= '</div>'; $titles .= "<div class=\"prev\">$prev->title</div>"; } else { - $links .= "<div class=\"prev\"> </div>"; // make an empty div to fill the space + $links .= '<div class="prev"> </div>'; // make an empty div to fill the space } if ($next = book_next($node)) { - $links .= "<div class=\"next\">"; - $links .= l(t("next"), "book/view/$next->nid", array("title" => t("View the next page."))); - $links .= "</div>"; + $links .= '<div class="next">'; + $links .= l(t('next'), "book/view/$next->nid", array('title' => t('View the next page.'))); + $links .= '</div>'; $titles .= "<div class=\"next\">$next->title</div>"; } else { - $links .= "<div class=\"next\"> </div>"; // make an empty div to fill the space + $links .= '<div class="next"> </div>'; // make an empty div to fill the space } if ($node->parent) { - $links .= "<div class=\"up\">"; - $links .= l(t("up"), "book/view/$node->parent", array("title" => t("View this page's parent section."))); - $links .= "</div>"; + $links .= '<div class="up">'; + $links .= l(t('up'), "book/view/$node->parent", array('title' => t("View this page's parent section."))); + $links .= '</div>'; } - $output .= "<div class=\"nav\">"; + $output .= '<div class="nav">'; $output .= " <div class=\"links\">$links</div>"; $output .= " <div class=\"titles\">$titles</div>"; - $output .= "</div>"; - $output .= "</div>"; + $output .= '</div>'; + $output .= '</div>'; } $node->body = $node->body.$output; @@ -558,9 +610,8 @@ function book_toc_recurse($nid, $indent, $toc, $children) { return $toc; } -function book_toc($parent = 0, $indent = "", $toc = array()) { - - $result = db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = '1' ORDER BY b.weight, n.title"); +function book_toc($parent = 0, $indent = '', $toc = array()) { + $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title'); while ($node = db_fetch_object($result)) { if (!$children[$node->parent]) { @@ -574,8 +625,8 @@ function book_toc($parent = 0, $indent = "", $toc = array()) { ** only administrators can start new books. */ - if (user_access("administer nodes")) { - $toc[0] = "<". t("top-level") .">"; + if (user_access('administer nodes')) { + $toc[0] = '<'. t('top-level') .'>'; } $toc = book_toc_recurse($parent, $indent, $toc, $children); @@ -583,7 +634,6 @@ function book_toc($parent = 0, $indent = "", $toc = array()) { return $toc; } - function book_tree_recurse($nid, $depth, $children, $unfold = array()) { if ($depth > 0) { if ($children[$nid]) { @@ -614,10 +664,8 @@ function book_tree_recurse($nid, $depth, $children, $unfold = array()) { return $output; } - function book_tree($parent = 0, $depth = 3, $unfold = array()) { - - $result = db_query("SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = '1' AND n.moderate = '0' ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title'); while ($node = db_fetch_object($result)) { $list = $children[$node->parent] ? $children[$node->parent] : array(); @@ -630,63 +678,56 @@ function book_tree($parent = 0, $depth = 3, $unfold = array()) { } } - +/** + * Menu callback. Prints a listing of all books. + */ function book_render() { - $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title"); + $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title'); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); if ($node) { // take the most recent approved revision, extract the page and check output: $node = book_content($node); // output the content: - $output .= "<div class=\"book\">"; - $output .= "<div class=\"title\">". l($node->title, "book/view/$node->nid") ."</div>"; - $output .= "<div class=\"body\">". $node->body ."</div>"; - $output .= "</div>"; + $output .= '<div class="book">'; + $output .= '<div class="title">'. l($node->title, "book/view/$node->nid") .'</div>'; + $output .= '<div class="body">'. $node->body .'</div>'; + $output .= '</div>'; } } - drupal_set_title(t("Books")); - print theme("page", $output); + drupal_set_title(t('Books')); + print theme('page', $output); } -function book_page() { - - if (user_access("access content")) { - switch (arg(1)) { - case "view": - $node = node_load(array("nid" => arg(2))); - $output = book_show($node, arg(3)); - print theme("page", $output, $node->title); - break; - case "print": - print book_print(arg(2), $depth = 1); - break; - default: - book_render(); - } - } - else { - drupal_access_denied(); - } +/** + * Menu callback. Prints a node as a book page, complete with navigation. + */ +function book_view_page($nid = 0, $cid = 0) { + $node = node_load(array('nid' => $nid)); + $output = book_show($node, $cid); + print theme('page', $output, $node->title); } -function book_print($id = "", $depth = 1) { +/** + * Menu callback. Generates printer-friendly book page will all descendants. + */ +function book_print($nid = 0, $depth = 1) { global $base_url; - $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = %d AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title", $id); + $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = %d AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title', $nid); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); if ($node) { // output the content: - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); } $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>"; @@ -696,37 +737,37 @@ function book_print($id = "", $depth = 1) { } } - $output .= book_print_recurse($id, $depth); + $output .= book_print_recurse($nid, $depth); $html = "<html><head><title>$node->title</title>"; $html .= "<base href=\"$base_url/\" />"; $html .= "<style type=\"text/css\">\n@import url(misc/print.css);\n</style>"; - $html .= "</head><body>". $output ."</body></html>"; + $html .= '</head><body>'. $output .'</body></html>'; - return $html; + print $html; } -function book_print_recurse($parent = "", $depth = 1) { +function book_print_recurse($parent = '', $depth = 1) { $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title"); while ($page = db_fetch_object($result)) { // load the node: - $node = node_load(array("nid" => $page->nid)); + $node = node_load(array('nid' => $page->nid)); // take the most recent approved revision: if ($node->moderate) { - $node = book_revision_load($node, array("moderate" => 0, "status" => 1)); + $node = book_revision_load($node, array('moderate' => 0, 'status' => 1)); } if ($node) { // output the content: - if (node_hook($node, "content")) { - $node = node_invoke($node, "content"); + if (node_hook($node, 'content')) { + $node = node_invoke($node, 'content'); } $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>"; if ($node->body) { - $output .= "<ul>". $node->body ."</ul>"; + $output .= '<ul>'. $node->body .'</ul>'; } $output .= book_print_recurse($node->nid, $depth + 1); @@ -737,10 +778,7 @@ function book_print_recurse($parent = "", $depth = 1) { } function book_admin_view_line($node, $depth = 0) { - - $row = array("<div style=\"padding-left: ". (25 * $depth) ."px;\">". form_textfield(NULL, "$node->nid][title", $node->title, 64, 255) ."</div>", form_weight(NULL, "$node->nid][weight", $node->weight), l(t("view node"), "node/view/$node->nid"), l(t("edit node"), "admin/node/edit/$node->nid"), l(t("delete node"), "admin/node/delete/$node->nid")); - - return $row; + return array("<div style=\"padding-left: ". (25 * $depth) ."px;\">". form_textfield(NULL, "$node->nid][title", $node->title, 64, 255) ."</div>", form_weight(NULL, "$node->nid][weight", $node->weight), l(t("view node"), "node/view/$node->nid"), l(t("edit node"), "admin/node/edit/$node->nid"), l(t("delete node"), "admin/node/delete/$node->nid")); } function book_admin_view_book($nid, $depth = 1) { @@ -755,8 +793,10 @@ function book_admin_view_book($nid, $depth = 1) { return $rows; } +/** + * Display an administrative view of the hierarchy of a book. + */ function book_admin_view($nid, $depth = 0) { - if ($nid) { $node = node_load(array("nid" => $nid)); @@ -774,7 +814,6 @@ function book_admin_view($nid, $depth = 0) { } function book_admin_save($nid, $edit = array()) { - if ($nid) { $book = node_load(array("nid" => $nid)); @@ -805,8 +844,10 @@ function book_admin_save($nid, $edit = array()) { } } +/** + * Menu callback. Displays a listing of all orphaned book pages. + */ function book_admin_orphan() { - $result = db_query("SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid"); while ($page = db_fetch_object($result)) { @@ -828,17 +869,13 @@ function book_admin_orphan() { print theme("page", $output); } -function book_admin_links() { -} - -function book_admin() { +/** + * Menu callback. Displays the book administration page. + */ +function book_admin($nid) { $op = $_POST["op"]; $edit = $_POST["edit"]; - if (empty($op)) { - $op = arg(3); - } - switch ($op) { case t("Edit book outline"): case t("Add to book outline"): @@ -846,19 +883,19 @@ function book_admin() { case t("Update book outline"): $output = book_node_link(); break; - case "orphan": - $output = book_admin_orphan(); - break; case t("Save book pages"): - drupal_set_message(book_admin_save(arg(3), $edit)); + drupal_set_message(book_admin_save($nid, $edit)); // fall through: default: - $output .= book_admin_view(arg(3)); + $output .= book_admin_view($nid); break; } print theme("page", $output); } +/** + * Implementation of hook_help(). + */ function book_help($section = "admin/help#book") { $output = ""; diff --git a/modules/forum.module b/modules/forum.module index 09dc707f6..549f48d7f 100644 --- a/modules/forum.module +++ b/modules/forum.module @@ -1,40 +1,40 @@ <?php // $Id$ -function forum_help($section = 'admin/help#forum') { - $output = ''; - +/** + * Implementation of hook_help(). + */ +function forum_help($section) { switch ($section) { case 'admin/help#forum': - $output = t(" + return t(" <h3>Creating a forum</h3> <p>The forum module uses taxonomy to organize itself. To create a forum you first have to create a <a href=\"%taxonomy\">taxonomy vocabulary</a>. When doing this, choose a sensible name for it (such as \"fora\") and make sure under \"Types\" that \"forum\" is selected. Once you have done this, <a href=\"%taxo-terms\">add some terms</a> to it. Each term will become a forum. If you fill in the description field, users will be given additonal information about the forum on the main forum page. For example: \"troubleshooting\" - \"Please ask your questions here.\"</p> <p>When you are happy with your vocabulary, go to <a href=\"%forums\">administer » configutation » modules » forum</a> and set <strong>Forum vocabulary</strong> to the one you have just created. There will now be fora active on the site. For users to access them they must have the \"access content\" <a href=\"%permission\">permission</a> and to create a topic they must have the \"create forum topics\" <a href=\"%permission\">permission</a>. These permissions can be set in the <a href=\"%permission\">permission</a> pages.</p> <h4>Icons</h4> <p>To disable icons, set the icon path as blank in <a href=\"%forums\">administer » configutation » modules » forum</a>.</p> - <p>All files in the icon directory are assumed to be images. You may use images of whatever size you wish, but it is recommended to use 15x15 or 16x16.</p>", array("%taxonomy" => url("admin/taxonomy/add/vocabulary"), "%taxo-terms" => url("admin/taxonomy"), "%forums" => url("admin/system/modules/forum"), "%permission" => url("admin/user/permission"))); - break; + <p>All files in the icon directory are assumed to be images. You may use images of whatever size you wish, but it is recommended to use 15x15 or 16x16.</p>", array("%taxonomy" => url('admin/taxonomy/add/vocabulary'), '%taxo-terms' => url('admin/taxonomy'), '%forums' => url('admin/system/modules/forum'), '%permission' => url('admin/user/permission'))); case 'admin/system/modules#description': - $output = t('Enable threaded discussions about general topics.'); - break; + return t('Enable threaded discussions about general topics.'); case 'admin/system/modules/forum': - $output = t("Forums are threaded discussions based on the taxonomy system. For the forums to work, the taxonomy module has to be installed and enabled. When activated, a taxonomy vocabulary (eg. \"forums\") needs to be <a href=\"%created\">created</a> and bound to the node type \"forum topic\".", array('%created' => url('admin/taxonomy/add/vocabulary'))); - break; + return t("Forums are threaded discussions based on the taxonomy system. For the forums to work, the taxonomy module has to be installed and enabled. When activated, a taxonomy vocabulary (eg. \"forums\") needs to be <a href=\"%created\">created</a> and bound to the node type \"forum topic\".", array('%created' => url('admin/taxonomy/add/vocabulary'))); case 'node/add/forum': - $output = variable_get('forum_help', ''); - break; + return variable_get('forum_help', ''); case 'node/add#forum': - $output = t('A forum is a threaded discussion, enabling users to communicate about a particular topic.'); - break; + return t('A forum is a threaded discussion, enabling users to communicate about a particular topic.'); } - - return $output; } +/** + * Implementation of hook_node_name(). + */ function forum_node_name($node) { return t('forum topic'); } +/** + * Implementation of hook_access(). + */ function forum_access($op, $node) { if ($op == 'view') { return $node->status; @@ -44,10 +44,16 @@ function forum_access($op, $node) { } } +/** + * Implementation of hook_perm(). + */ function forum_perm() { return array('create forum topics'); } +/** + * Implementation of hook_settings(). + */ function forum_settings() { if (module_exist('taxonomy')) { @@ -63,7 +69,7 @@ function forum_settings() { $output = form_group(t('Forum structure settings'), $group); $group = form_textarea(t('Explanation or submission guidelines'), 'forum_help', variable_get('forum_help', ''), 70, 5, t('This text will be displayed at the top of the forum submission form. Useful for helping or instructing your users.')); - $group .= form_textfield(t('Forum icon path'), 'forum_icon_path', variable_get('forum_icon_path', ''), 30, 255, t("The path to the forum icons. Leave blank to disable icons. Don't add a trailing slash. Default icons are available in the 'misc' directory.")); + $group .= form_textfield(t('Forum icon path'), 'forum_icon_path', variable_get('forum_icon_path', ''), 30, 255, t('The path to the forum icons. Leave blank to disable icons. Don\'t add a trailing slash. Default icons are available in the "misc" directory.')); $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 10000)); $group .= form_select(t('Hot topic threshold'), 'forum_hot_topic', variable_get('forum_hot_topic', 15), $number, t('The number of posts a topic must have to be considered <strong>hot</strong>.')); $number = drupal_map_assoc(array(10, 25, 50, 75, 100)); @@ -72,7 +78,7 @@ function forum_settings() { $group .= form_radios(t('Default order'), 'forum_order', variable_get('forum_order', '1'), $forder, t('The default display order for topics.')); $output .= form_group(t('Forum viewing options'), $group); - $group = form_textfield(t('Number of topics in block'), 'forum_block_num', variable_get('forum_block_num', '5'), 5, 5, t("The number of topics to show in the \"Forum topics\" block. To enable the block, go to the <a href=\"%block-administration\">block administration</a> page.", array('%block-administration' => url('admin/system/block')))); + $group = form_textfield(t('Number of topics in block'), 'forum_block_num', variable_get('forum_block_num', '5'), 5, 5, t('The number of topics to show in the "Forum topics" block. To enable the block, go to the <a href="%block-administration">block administration</a> page.', array('%block-administration' => url('admin/system/block')))); $output .= form_group(t('"Forum topic" block settings'), $group); } } @@ -80,6 +86,9 @@ function forum_settings() { return $output; } +/** + * Implementation of hook_taxonomy(). + */ function forum_taxonomy($op, $type, $object) { if ($type == 'vocabulary' && ($op == 'insert' || $op == 'update')) { if (variable_get('forum_nav_vocabulary', '') == '' && in_array('forum', $object['nodes'])) { @@ -89,12 +98,21 @@ function forum_taxonomy($op, $type, $object) { } } +/** + * Implementation of hook_load(). + */ function forum_load($node) { $forum = db_fetch_object(db_query('SELECT * FROM {forum} WHERE nid = %d', $node->nid)); return $forum; } +/** + * Implementation of hook_block(). + * + * Generates a block containing the currently active forum topics and the + * most recently added forum topics. + */ function forum_block($op = 'list', $delta = 0) { global $user; @@ -108,7 +126,7 @@ function forum_block($op = 'list', $delta = 0) { $content .= node_title_list(db_query_range("SELECT nid, title FROM {node} WHERE type = 'forum' AND status = 1 ORDER BY nid DESC", 0, variable_get('forum_block_num', '5')), t('New forum topics:')); if ($content) { - $content .= "<div class=\"more-link\">". l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>'; + $content .= '<div class="more-link">'. l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>'; } $blocks['subject'] = t('Forum topics'); @@ -132,7 +150,7 @@ function forum_link($type, $node = 0, $main = 0) { } if ($type == 'system') { - menu('node/add/forum', t('forum topic'), user_access('create forum topics') ? 'node_page' : MENU_DENIED); + menu('node/add/forum', t('forum topic'), user_access('create forum topics') ? MENU_FALLTHROUGH : MENU_DENIED); menu('forum', t('forums'), user_access('access content') ? 'forum_page' : MENU_DENIED, 0, MENU_HIDE); } @@ -168,10 +186,16 @@ function forum_link($type, $node = 0, $main = 0) { return $links; } +/** + * Implementation of hook_content(). + */ function forum_content($node, $main = 0) { return node_prepare($node, $main); } +/** + * Implementation of hook_view(). + */ function forum_view($node, $main = 0, $page = 0) { if ($page) { @@ -190,6 +214,12 @@ function forum_view($node, $main = 0, $page = 0) { return theme('node', forum_content($node, $main), $main, $page); } +/** + * Implementation of hook_validate(). + * + * Check in particular that only a "leaf" term in the associated taxonomy + * vocabulary is selected, not a "container" term. + */ function forum_validate(&$node) { // Make sure all fields are set properly: $node->icon = $node->icon ? $node->icon : ''; @@ -214,6 +244,9 @@ function forum_validate(&$node) { return $error; } +/** + * Implementation of hook_form(). + */ function forum_form(&$node, &$error) { if (!$node->nid) { // new topic @@ -232,14 +265,23 @@ function forum_form(&$node, &$error) { return $output; } +/** + * Implementation of hook_insert(). + */ function forum_insert($node) { db_query('INSERT INTO {forum} (nid, shadow, tid) VALUES (%d, %d, %d)', $node->nid, $node->shadow, $node->tid); } +/** + * Implementation of hook_update(). + */ function forum_update($node) { db_query('UPDATE {forum} SET shadow = %d, tid = %d WHERE nid = %d', $node->shadow, $node->tid, $node->nid); } +/** + * Implementation of hook_delete(). + */ function forum_delete(&$node) { db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); } @@ -376,7 +418,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { } } else { - // you're not logged in eh? + // Do not track "new replies" status for topics if the user is anonymous. $topic->new_replies = 0; $topic->new = 0; } @@ -395,20 +437,21 @@ function _forum_new($tid) { $read[] = $r->nid; } - $nid = db_result(db_query_range("SELECT n.nid FROM {node} n INNER JOIN {forum} f ON n.nid = f.nid WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = %d ". ($read ? "AND NOT (n.nid IN (". implode(',', $read) .")) " : '') ."ORDER BY created", $tid, 0, 1)); + $nid = db_result(db_query_range("SELECT n.nid FROM {node} n INNER JOIN {forum} f ON n.nid = f.nid WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = %d ". ($read ? 'AND NOT (n.nid IN ('. implode(',', $read) .')) ' : '') .'ORDER BY created', $tid, 0, 1)); return $nid ? $nid : 0; } -function forum_page() { +/** + * Menu callback. Prints a forum listing. + */ +function forum_page($tid = 0, $display = 'all') { global $sortby, $forum_per_page, $from, $user; $op = $_POST['op']; if (module_exist('taxonomy')) { - $tid = arg(1); - - if (arg(2) == 'new') { + if ($display == 'new') { if ($nid = _forum_new($tid)) { drupal_goto("node/view/$nid"); } @@ -484,34 +527,34 @@ function theme_forum_display($forums, $topics, $parents, $tid, $sortby, $forum_p } if (count($forums) || count($parents)) { - $output = "<div id=\"forum\">"; - $output .= "<ul>"; + $output = '<div id="forum">'; + $output .= '<ul>'; if (module_exist('tracker')) { if ($user->uid) { - $output .= " <li>". l(t('Track your forum discussions.'), "tracker/$user->uid") ."</li>"; + $output .= ' <li>'. l(t('Track your forum discussions.'), "tracker/$user->uid") .'</li>'; } - $output .= " <li>". l(t('Track active forum discussions.'), 'tracker') ."</li>"; + $output .= ' <li>'. l(t('Track active forum discussions.'), 'tracker') .'</li>'; } if (user_access('create forum topics')) { - $output .= "<li>". l(t('Post new forum topic.'), "node/add/forum/$tid") ."</li>"; + $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>'; } else if ($user->uid) { - $output .= "<li>". t('You are not allowed to post a new forum topic.') .'</li>'; + $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>'; } else { $output .= '<li>'. t('<a href="%login">Login</a> to post a new forum topic.', array('%login' => url('user/login'))) .'</li>'; } - $output .= "</ul>"; + $output .= '</ul>'; $output .= theme('forum_list', $forums, $parents, $tid); if ($tid && !in_array($tid, variable_get('forum_containers', array()))) { $output .= theme('forum_topic_list', $tid, $topics, $sortby, $forum_per_page, $offset); } - $output .= "</div>"; + $output .= '</div>'; } else { $title = t('No forums defined'); @@ -539,8 +582,8 @@ function theme_forum_list($forums, $parents, $tid) { foreach ($forums as $forum) { if ($forum->container) { - $description = "<div style=\"margin-left: ". ($forum->depth * 30) ."px;\">\n"; - $description .= " <div class=\"name\">". l($forum->name, "forum/$forum->tid") ."</div>\n"; + $description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n"; + $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n"; if ($forum->description) { $description .= " <div class=\"description\">$forum->description</div>\n"; @@ -560,20 +603,20 @@ function theme_forum_list($forums, $parents, $tid) { $links = array(); - $description = "<div style=\"margin-left: ". ($forum->depth * 30) ."px;\">\n"; - $description .= " <div class=\"name\">". l($forum->name, "forum/$forum->tid") ."</div>\n"; + $description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n"; + $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n"; if ($forum->description) { $description .= " <div class=\"description\">$forum->description</div>\n"; } if ($links) { - $description .= " <div class=\"links\">". t('Jump to: %links', array('%links' => implode(', ', $links))) .".</div>\n"; + $description .= ' <div class="links">'. t('Jump to: %links', array('%links' => implode(', ', $links))) .".</div>\n"; } $description .= "</div>\n"; $rows[] = array( array('data' => $description, 'class' => 'forum'), - array('data' => $forum->num_topics . ($new_topics ? "<br />". l(t('%a new', array('%a' => $new_topics)), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'), + array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(t('%a new', array('%a' => $new_topics)), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'), array('data' => $forum->num_posts, 'class' => 'posts'), array('data' => _forum_format($forum->last_post), 'class' => 'last-reply')); } @@ -612,7 +655,7 @@ function theme_forum_topic_list($tid, $topics, $sortby, $forum_per_page, $offset $rows[] = array( array('data' => _forum_icon($topic->new, $topic->num_comments, $topic->comment_mode), 'class' => 'icon'), array('data' => l($topic->title, "node/view/$topic->nid"), 'class' => 'topic'), - array('data' => $topic->num_comments . ($topic->new_replies ? "<br />". l(t('%a new', array('%a' => $topic->new_replies)), "node/view/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'), + array('data' => $topic->num_comments . ($topic->new_replies ? '<br />'. l(t('%a new', array('%a' => $topic->new_replies)), "node/view/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'), array('data' => _forum_format($topic), 'class' => 'created'), array('data' => _forum_format($topic->last_reply), 'class' => 'last-reply') ); diff --git a/modules/forum/forum.module b/modules/forum/forum.module index 09dc707f6..549f48d7f 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -1,40 +1,40 @@ <?php // $Id$ -function forum_help($section = 'admin/help#forum') { - $output = ''; - +/** + * Implementation of hook_help(). + */ +function forum_help($section) { switch ($section) { case 'admin/help#forum': - $output = t(" + return t(" <h3>Creating a forum</h3> <p>The forum module uses taxonomy to organize itself. To create a forum you first have to create a <a href=\"%taxonomy\">taxonomy vocabulary</a>. When doing this, choose a sensible name for it (such as \"fora\") and make sure under \"Types\" that \"forum\" is selected. Once you have done this, <a href=\"%taxo-terms\">add some terms</a> to it. Each term will become a forum. If you fill in the description field, users will be given additonal information about the forum on the main forum page. For example: \"troubleshooting\" - \"Please ask your questions here.\"</p> <p>When you are happy with your vocabulary, go to <a href=\"%forums\">administer » configutation » modules » forum</a> and set <strong>Forum vocabulary</strong> to the one you have just created. There will now be fora active on the site. For users to access them they must have the \"access content\" <a href=\"%permission\">permission</a> and to create a topic they must have the \"create forum topics\" <a href=\"%permission\">permission</a>. These permissions can be set in the <a href=\"%permission\">permission</a> pages.</p> <h4>Icons</h4> <p>To disable icons, set the icon path as blank in <a href=\"%forums\">administer » configutation » modules » forum</a>.</p> - <p>All files in the icon directory are assumed to be images. You may use images of whatever size you wish, but it is recommended to use 15x15 or 16x16.</p>", array("%taxonomy" => url("admin/taxonomy/add/vocabulary"), "%taxo-terms" => url("admin/taxonomy"), "%forums" => url("admin/system/modules/forum"), "%permission" => url("admin/user/permission"))); - break; + <p>All files in the icon directory are assumed to be images. You may use images of whatever size you wish, but it is recommended to use 15x15 or 16x16.</p>", array("%taxonomy" => url('admin/taxonomy/add/vocabulary'), '%taxo-terms' => url('admin/taxonomy'), '%forums' => url('admin/system/modules/forum'), '%permission' => url('admin/user/permission'))); case 'admin/system/modules#description': - $output = t('Enable threaded discussions about general topics.'); - break; + return t('Enable threaded discussions about general topics.'); case 'admin/system/modules/forum': - $output = t("Forums are threaded discussions based on the taxonomy system. For the forums to work, the taxonomy module has to be installed and enabled. When activated, a taxonomy vocabulary (eg. \"forums\") needs to be <a href=\"%created\">created</a> and bound to the node type \"forum topic\".", array('%created' => url('admin/taxonomy/add/vocabulary'))); - break; + return t("Forums are threaded discussions based on the taxonomy system. For the forums to work, the taxonomy module has to be installed and enabled. When activated, a taxonomy vocabulary (eg. \"forums\") needs to be <a href=\"%created\">created</a> and bound to the node type \"forum topic\".", array('%created' => url('admin/taxonomy/add/vocabulary'))); case 'node/add/forum': - $output = variable_get('forum_help', ''); - break; + return variable_get('forum_help', ''); case 'node/add#forum': - $output = t('A forum is a threaded discussion, enabling users to communicate about a particular topic.'); - break; + return t('A forum is a threaded discussion, enabling users to communicate about a particular topic.'); } - - return $output; } +/** + * Implementation of hook_node_name(). + */ function forum_node_name($node) { return t('forum topic'); } +/** + * Implementation of hook_access(). + */ function forum_access($op, $node) { if ($op == 'view') { return $node->status; @@ -44,10 +44,16 @@ function forum_access($op, $node) { } } +/** + * Implementation of hook_perm(). + */ function forum_perm() { return array('create forum topics'); } +/** + * Implementation of hook_settings(). + */ function forum_settings() { if (module_exist('taxonomy')) { @@ -63,7 +69,7 @@ function forum_settings() { $output = form_group(t('Forum structure settings'), $group); $group = form_textarea(t('Explanation or submission guidelines'), 'forum_help', variable_get('forum_help', ''), 70, 5, t('This text will be displayed at the top of the forum submission form. Useful for helping or instructing your users.')); - $group .= form_textfield(t('Forum icon path'), 'forum_icon_path', variable_get('forum_icon_path', ''), 30, 255, t("The path to the forum icons. Leave blank to disable icons. Don't add a trailing slash. Default icons are available in the 'misc' directory.")); + $group .= form_textfield(t('Forum icon path'), 'forum_icon_path', variable_get('forum_icon_path', ''), 30, 255, t('The path to the forum icons. Leave blank to disable icons. Don\'t add a trailing slash. Default icons are available in the "misc" directory.')); $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 10000)); $group .= form_select(t('Hot topic threshold'), 'forum_hot_topic', variable_get('forum_hot_topic', 15), $number, t('The number of posts a topic must have to be considered <strong>hot</strong>.')); $number = drupal_map_assoc(array(10, 25, 50, 75, 100)); @@ -72,7 +78,7 @@ function forum_settings() { $group .= form_radios(t('Default order'), 'forum_order', variable_get('forum_order', '1'), $forder, t('The default display order for topics.')); $output .= form_group(t('Forum viewing options'), $group); - $group = form_textfield(t('Number of topics in block'), 'forum_block_num', variable_get('forum_block_num', '5'), 5, 5, t("The number of topics to show in the \"Forum topics\" block. To enable the block, go to the <a href=\"%block-administration\">block administration</a> page.", array('%block-administration' => url('admin/system/block')))); + $group = form_textfield(t('Number of topics in block'), 'forum_block_num', variable_get('forum_block_num', '5'), 5, 5, t('The number of topics to show in the "Forum topics" block. To enable the block, go to the <a href="%block-administration">block administration</a> page.', array('%block-administration' => url('admin/system/block')))); $output .= form_group(t('"Forum topic" block settings'), $group); } } @@ -80,6 +86,9 @@ function forum_settings() { return $output; } +/** + * Implementation of hook_taxonomy(). + */ function forum_taxonomy($op, $type, $object) { if ($type == 'vocabulary' && ($op == 'insert' || $op == 'update')) { if (variable_get('forum_nav_vocabulary', '') == '' && in_array('forum', $object['nodes'])) { @@ -89,12 +98,21 @@ function forum_taxonomy($op, $type, $object) { } } +/** + * Implementation of hook_load(). + */ function forum_load($node) { $forum = db_fetch_object(db_query('SELECT * FROM {forum} WHERE nid = %d', $node->nid)); return $forum; } +/** + * Implementation of hook_block(). + * + * Generates a block containing the currently active forum topics and the + * most recently added forum topics. + */ function forum_block($op = 'list', $delta = 0) { global $user; @@ -108,7 +126,7 @@ function forum_block($op = 'list', $delta = 0) { $content .= node_title_list(db_query_range("SELECT nid, title FROM {node} WHERE type = 'forum' AND status = 1 ORDER BY nid DESC", 0, variable_get('forum_block_num', '5')), t('New forum topics:')); if ($content) { - $content .= "<div class=\"more-link\">". l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>'; + $content .= '<div class="more-link">'. l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>'; } $blocks['subject'] = t('Forum topics'); @@ -132,7 +150,7 @@ function forum_link($type, $node = 0, $main = 0) { } if ($type == 'system') { - menu('node/add/forum', t('forum topic'), user_access('create forum topics') ? 'node_page' : MENU_DENIED); + menu('node/add/forum', t('forum topic'), user_access('create forum topics') ? MENU_FALLTHROUGH : MENU_DENIED); menu('forum', t('forums'), user_access('access content') ? 'forum_page' : MENU_DENIED, 0, MENU_HIDE); } @@ -168,10 +186,16 @@ function forum_link($type, $node = 0, $main = 0) { return $links; } +/** + * Implementation of hook_content(). + */ function forum_content($node, $main = 0) { return node_prepare($node, $main); } +/** + * Implementation of hook_view(). + */ function forum_view($node, $main = 0, $page = 0) { if ($page) { @@ -190,6 +214,12 @@ function forum_view($node, $main = 0, $page = 0) { return theme('node', forum_content($node, $main), $main, $page); } +/** + * Implementation of hook_validate(). + * + * Check in particular that only a "leaf" term in the associated taxonomy + * vocabulary is selected, not a "container" term. + */ function forum_validate(&$node) { // Make sure all fields are set properly: $node->icon = $node->icon ? $node->icon : ''; @@ -214,6 +244,9 @@ function forum_validate(&$node) { return $error; } +/** + * Implementation of hook_form(). + */ function forum_form(&$node, &$error) { if (!$node->nid) { // new topic @@ -232,14 +265,23 @@ function forum_form(&$node, &$error) { return $output; } +/** + * Implementation of hook_insert(). + */ function forum_insert($node) { db_query('INSERT INTO {forum} (nid, shadow, tid) VALUES (%d, %d, %d)', $node->nid, $node->shadow, $node->tid); } +/** + * Implementation of hook_update(). + */ function forum_update($node) { db_query('UPDATE {forum} SET shadow = %d, tid = %d WHERE nid = %d', $node->shadow, $node->tid, $node->nid); } +/** + * Implementation of hook_delete(). + */ function forum_delete(&$node) { db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); } @@ -376,7 +418,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) { } } else { - // you're not logged in eh? + // Do not track "new replies" status for topics if the user is anonymous. $topic->new_replies = 0; $topic->new = 0; } @@ -395,20 +437,21 @@ function _forum_new($tid) { $read[] = $r->nid; } - $nid = db_result(db_query_range("SELECT n.nid FROM {node} n INNER JOIN {forum} f ON n.nid = f.nid WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = %d ". ($read ? "AND NOT (n.nid IN (". implode(',', $read) .")) " : '') ."ORDER BY created", $tid, 0, 1)); + $nid = db_result(db_query_range("SELECT n.nid FROM {node} n INNER JOIN {forum} f ON n.nid = f.nid WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = %d ". ($read ? 'AND NOT (n.nid IN ('. implode(',', $read) .')) ' : '') .'ORDER BY created', $tid, 0, 1)); return $nid ? $nid : 0; } -function forum_page() { +/** + * Menu callback. Prints a forum listing. + */ +function forum_page($tid = 0, $display = 'all') { global $sortby, $forum_per_page, $from, $user; $op = $_POST['op']; if (module_exist('taxonomy')) { - $tid = arg(1); - - if (arg(2) == 'new') { + if ($display == 'new') { if ($nid = _forum_new($tid)) { drupal_goto("node/view/$nid"); } @@ -484,34 +527,34 @@ function theme_forum_display($forums, $topics, $parents, $tid, $sortby, $forum_p } if (count($forums) || count($parents)) { - $output = "<div id=\"forum\">"; - $output .= "<ul>"; + $output = '<div id="forum">'; + $output .= '<ul>'; if (module_exist('tracker')) { if ($user->uid) { - $output .= " <li>". l(t('Track your forum discussions.'), "tracker/$user->uid") ."</li>"; + $output .= ' <li>'. l(t('Track your forum discussions.'), "tracker/$user->uid") .'</li>'; } - $output .= " <li>". l(t('Track active forum discussions.'), 'tracker') ."</li>"; + $output .= ' <li>'. l(t('Track active forum discussions.'), 'tracker') .'</li>'; } if (user_access('create forum topics')) { - $output .= "<li>". l(t('Post new forum topic.'), "node/add/forum/$tid") ."</li>"; + $output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>'; } else if ($user->uid) { - $output .= "<li>". t('You are not allowed to post a new forum topic.') .'</li>'; + $output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>'; } else { $output .= '<li>'. t('<a href="%login">Login</a> to post a new forum topic.', array('%login' => url('user/login'))) .'</li>'; } - $output .= "</ul>"; + $output .= '</ul>'; $output .= theme('forum_list', $forums, $parents, $tid); if ($tid && !in_array($tid, variable_get('forum_containers', array()))) { $output .= theme('forum_topic_list', $tid, $topics, $sortby, $forum_per_page, $offset); } - $output .= "</div>"; + $output .= '</div>'; } else { $title = t('No forums defined'); @@ -539,8 +582,8 @@ function theme_forum_list($forums, $parents, $tid) { foreach ($forums as $forum) { if ($forum->container) { - $description = "<div style=\"margin-left: ". ($forum->depth * 30) ."px;\">\n"; - $description .= " <div class=\"name\">". l($forum->name, "forum/$forum->tid") ."</div>\n"; + $description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n"; + $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n"; if ($forum->description) { $description .= " <div class=\"description\">$forum->description</div>\n"; @@ -560,20 +603,20 @@ function theme_forum_list($forums, $parents, $tid) { $links = array(); - $description = "<div style=\"margin-left: ". ($forum->depth * 30) ."px;\">\n"; - $description .= " <div class=\"name\">". l($forum->name, "forum/$forum->tid") ."</div>\n"; + $description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n"; + $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n"; if ($forum->description) { $description .= " <div class=\"description\">$forum->description</div>\n"; } if ($links) { - $description .= " <div class=\"links\">". t('Jump to: %links', array('%links' => implode(', ', $links))) .".</div>\n"; + $description .= ' <div class="links">'. t('Jump to: %links', array('%links' => implode(', ', $links))) .".</div>\n"; } $description .= "</div>\n"; $rows[] = array( array('data' => $description, 'class' => 'forum'), - array('data' => $forum->num_topics . ($new_topics ? "<br />". l(t('%a new', array('%a' => $new_topics)), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'), + array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(t('%a new', array('%a' => $new_topics)), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'), array('data' => $forum->num_posts, 'class' => 'posts'), array('data' => _forum_format($forum->last_post), 'class' => 'last-reply')); } @@ -612,7 +655,7 @@ function theme_forum_topic_list($tid, $topics, $sortby, $forum_per_page, $offset $rows[] = array( array('data' => _forum_icon($topic->new, $topic->num_comments, $topic->comment_mode), 'class' => 'icon'), array('data' => l($topic->title, "node/view/$topic->nid"), 'class' => 'topic'), - array('data' => $topic->num_comments . ($topic->new_replies ? "<br />". l(t('%a new', array('%a' => $topic->new_replies)), "node/view/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'), + array('data' => $topic->num_comments . ($topic->new_replies ? '<br />'. l(t('%a new', array('%a' => $topic->new_replies)), "node/view/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'), array('data' => _forum_format($topic), 'class' => 'created'), array('data' => _forum_format($topic->last_reply), 'class' => 'last-reply') ); diff --git a/modules/page.module b/modules/page.module index 63e8a5084..09cb73cc8 100644 --- a/modules/page.module +++ b/modules/page.module @@ -2,95 +2,88 @@ // $Id$ /** - * Provide online user help + * Implementation of hook_help(). */ -function page_help($section = 'admin/help#page') { - $output = ""; - +function page_help($section) { switch ($section) { case 'admin/help#page': - $output .= t(" + return t(" <p>The page module is used when you want to create content that optionally inserts a link into your navigation system. You can also, however, create pages that that don't have this link by skipping the link text field in the page form. At this time, not all themes support the link insertion behavior. Some themes, like xtemplate, provide alternative mechanisms for link creation. Pages are also unique in that they shortcut the typical lifecycle of user generated content (i.e. submit -> moderate -> post -> comment). </p> <p>If you enable the <strong>create PHP content</strong> permission for a role, pages may consist of PHP code in addition to HTML and text.</p> <h3>User access permissions for pages</h3> <p><strong>create pages:</strong> Allows a role to create pages. They cannot edit or delete pages, even if they are the authors. You must enable this permission to in order for a role to create a page.</p> <p><strong>maintain personal pages:</strong> Allows a role to add/edit pages if they own the page. Use this permission if you want users to be able to edit and maintain their own pages.</p> "); - break; case 'admin/system/modules#description': - $output = t("Enables the creation of pages that can be added to the navigation system."); - break; + return t('Enables the creation of pages that can be added to the navigation system.'); case 'node/add#page': - $output = t("If you just want to add a page with a link in the menu to your site, this would be the best choice. Unlike a story, a static page by-passes the submission queue."); - break; + return t('If you just want to add a page with a link in the menu to your site, this is the best choice. Unlike a story, a static page bypasses the submission queue.'); } - - return $output; } /** - * Define user permissions. + * Implementation of hook_perm(). */ function page_perm() { return array('create pages', 'maintain personal pages'); } /** - * Define the human-readable name of a node type. + * Implementation of hook_node_name(). */ function page_node_name($node) { return t('page'); } /** - * Define access restrictions + * Implementation of hook_access(). */ function page_access($op, $node) { global $user; - if ($op == "view") { + if ($op == 'view') { return $node->status; } - if ($op == "create") { + if ($op == 'create') { return user_access('create pages'); } - if ($op == "update") { + if ($op == 'update') { return user_access('maintain personal pages') && ($user->uid == $node->uid); } - if ($op == "delete") { + if ($op == 'delete') { return user_access('maintain personal pages') && ($user->uid == $node->uid); } } /** - * Respond to node insertion. + * Implementation of hook_insert(). */ function page_insert($node) { db_query("INSERT INTO {page} (nid, format, link, description) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->format, $node->link, $node->description); } /** - * Respond to node updating. + * Implementation of hook_update(). */ function page_update($node) { db_query("UPDATE {page} SET format = %d, link = '%s', description = '%s' WHERE nid = %d", $node->format, $node->link, $node->description, $node->nid); } /** - * Respond to node deletion + * Implementation of hook_delete(). */ function page_delete(&$node) { - db_query("DELETE FROM {page} WHERE nid = %d", $node->nid); + db_query('DELETE FROM {page} WHERE nid = %d', $node->nid); } /** - * Load node-type-specific information. + * Implementation of hook_load(). */ function page_load($node) { - return db_fetch_object(db_query("SELECT format, link, description FROM {page} WHERE nid = %d", $node->nid)); + return db_fetch_object(db_query('SELECT format, link, description FROM {page} WHERE nid = %d', $node->nid)); } /** @@ -101,7 +94,7 @@ function page_link($type, $node = 0, $main) { $links = array(); if ($type == 'system') { - menu('node/add/page', t('page'), page_access('create', $node) ? 'node_page' : MENU_DENIED, 0); + menu('node/add/page', t('page'), page_access('create', $node) ? MENU_FALLTHROUGH : MENU_DENIED, 0); } if ($type == 'node' && $node->type == 'page') { @@ -115,12 +108,11 @@ function page_link($type, $node = 0, $main) { } /** - * Prepare a node's body content for viewing + * Implementation of hook_content(). + * + * If body is dynamic (using PHP code), the body will be generated. */ function page_content($node, $main = 0) { - /* Extract the page body. If body is dynamic (using PHP code), the body will - be generated. */ - if ($node->format == 1) { // PHP type ob_start(); @@ -136,21 +128,21 @@ function page_content($node, $main = 0) { } /** - * View a node. + * Implementation of hook_view(). */ function page_view($node, $main = 0, $page = 0) { // prepare the node content $node = page_content($node, $main); // print the node - return theme("node", $node, $main, $page); + return theme('node', $node, $main, $page); } /** - * Display a node editing form + * Implementation of hook_form(). */ function page_form(&$node, &$error) { - if (function_exists("taxonomy_node_form")) { - $output .= implode("", taxonomy_node_form("page", $node)); + if (function_exists('taxonomy_node_form')) { + $output .= implode('', taxonomy_node_form('page', $node)); } if (($node->format == 1) && (!user_access('create php content'))) { @@ -162,28 +154,28 @@ function page_form(&$node, &$error) { $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short()); } - $output .= form_textfield(t('Link name'), 'link', $node->link, 60, 64, t("To make the page show up in the navigation links, enter the name of the link, otherwise leave blank.")); + $output .= form_textfield(t('Link name'), 'link', $node->link, 60, 64, t('To make the page show up in the navigation links, enter the name of the link. Otherwise, leave this blank.')); $output .= form_textfield(t('Link description'), 'description', $node->description, 60, 64, t("The description displayed when hovering over the page's link. Leave blank when you don't want a description.")); - $content_type = (user_access('create php content')) ? array(0 => "HTML", 1 => "PHP") : false; + $content_type = (user_access('create php content')) ? array(0 => 'HTML', 1 => 'PHP') : false; if (!$hide_types && $content_type) { - $output .= form_radios(t("Type"), "format", $node->format, $content_type); + $output .= form_radios(t('Type'), 'format', $node->format, $content_type); } return $output; } /** - * Verify a node editing form. + * Implementation of hook_validate(). */ function page_validate(&$node) { - if ($node->format && user_access("create php content")) { + if ($node->format && user_access('create php content')) { // Do not filter PHP code, do not auto-extract a teaser $node->teaser = $node->body; } if (($node->format == 1) && (!user_access('create php content'))) { /* Overwrite the submitted node body since they don't have sufficient privileges. */ - $node->body = db_result(db_query("SELECT body FROM {node} WHERE nid = '%d'", $node->nid)); + $node->body = db_result(db_query('SELECT body FROM {node} WHERE nid = %d', $node->nid)); } } diff --git a/modules/page/page.module b/modules/page/page.module index 63e8a5084..09cb73cc8 100644 --- a/modules/page/page.module +++ b/modules/page/page.module @@ -2,95 +2,88 @@ // $Id$ /** - * Provide online user help + * Implementation of hook_help(). */ -function page_help($section = 'admin/help#page') { - $output = ""; - +function page_help($section) { switch ($section) { case 'admin/help#page': - $output .= t(" + return t(" <p>The page module is used when you want to create content that optionally inserts a link into your navigation system. You can also, however, create pages that that don't have this link by skipping the link text field in the page form. At this time, not all themes support the link insertion behavior. Some themes, like xtemplate, provide alternative mechanisms for link creation. Pages are also unique in that they shortcut the typical lifecycle of user generated content (i.e. submit -> moderate -> post -> comment). </p> <p>If you enable the <strong>create PHP content</strong> permission for a role, pages may consist of PHP code in addition to HTML and text.</p> <h3>User access permissions for pages</h3> <p><strong>create pages:</strong> Allows a role to create pages. They cannot edit or delete pages, even if they are the authors. You must enable this permission to in order for a role to create a page.</p> <p><strong>maintain personal pages:</strong> Allows a role to add/edit pages if they own the page. Use this permission if you want users to be able to edit and maintain their own pages.</p> "); - break; case 'admin/system/modules#description': - $output = t("Enables the creation of pages that can be added to the navigation system."); - break; + return t('Enables the creation of pages that can be added to the navigation system.'); case 'node/add#page': - $output = t("If you just want to add a page with a link in the menu to your site, this would be the best choice. Unlike a story, a static page by-passes the submission queue."); - break; + return t('If you just want to add a page with a link in the menu to your site, this is the best choice. Unlike a story, a static page bypasses the submission queue.'); } - - return $output; } /** - * Define user permissions. + * Implementation of hook_perm(). */ function page_perm() { return array('create pages', 'maintain personal pages'); } /** - * Define the human-readable name of a node type. + * Implementation of hook_node_name(). */ function page_node_name($node) { return t('page'); } /** - * Define access restrictions + * Implementation of hook_access(). */ function page_access($op, $node) { global $user; - if ($op == "view") { + if ($op == 'view') { return $node->status; } - if ($op == "create") { + if ($op == 'create') { return user_access('create pages'); } - if ($op == "update") { + if ($op == 'update') { return user_access('maintain personal pages') && ($user->uid == $node->uid); } - if ($op == "delete") { + if ($op == 'delete') { return user_access('maintain personal pages') && ($user->uid == $node->uid); } } /** - * Respond to node insertion. + * Implementation of hook_insert(). */ function page_insert($node) { db_query("INSERT INTO {page} (nid, format, link, description) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->format, $node->link, $node->description); } /** - * Respond to node updating. + * Implementation of hook_update(). */ function page_update($node) { db_query("UPDATE {page} SET format = %d, link = '%s', description = '%s' WHERE nid = %d", $node->format, $node->link, $node->description, $node->nid); } /** - * Respond to node deletion + * Implementation of hook_delete(). */ function page_delete(&$node) { - db_query("DELETE FROM {page} WHERE nid = %d", $node->nid); + db_query('DELETE FROM {page} WHERE nid = %d', $node->nid); } /** - * Load node-type-specific information. + * Implementation of hook_load(). */ function page_load($node) { - return db_fetch_object(db_query("SELECT format, link, description FROM {page} WHERE nid = %d", $node->nid)); + return db_fetch_object(db_query('SELECT format, link, description FROM {page} WHERE nid = %d', $node->nid)); } /** @@ -101,7 +94,7 @@ function page_link($type, $node = 0, $main) { $links = array(); if ($type == 'system') { - menu('node/add/page', t('page'), page_access('create', $node) ? 'node_page' : MENU_DENIED, 0); + menu('node/add/page', t('page'), page_access('create', $node) ? MENU_FALLTHROUGH : MENU_DENIED, 0); } if ($type == 'node' && $node->type == 'page') { @@ -115,12 +108,11 @@ function page_link($type, $node = 0, $main) { } /** - * Prepare a node's body content for viewing + * Implementation of hook_content(). + * + * If body is dynamic (using PHP code), the body will be generated. */ function page_content($node, $main = 0) { - /* Extract the page body. If body is dynamic (using PHP code), the body will - be generated. */ - if ($node->format == 1) { // PHP type ob_start(); @@ -136,21 +128,21 @@ function page_content($node, $main = 0) { } /** - * View a node. + * Implementation of hook_view(). */ function page_view($node, $main = 0, $page = 0) { // prepare the node content $node = page_content($node, $main); // print the node - return theme("node", $node, $main, $page); + return theme('node', $node, $main, $page); } /** - * Display a node editing form + * Implementation of hook_form(). */ function page_form(&$node, &$error) { - if (function_exists("taxonomy_node_form")) { - $output .= implode("", taxonomy_node_form("page", $node)); + if (function_exists('taxonomy_node_form')) { + $output .= implode('', taxonomy_node_form('page', $node)); } if (($node->format == 1) && (!user_access('create php content'))) { @@ -162,28 +154,28 @@ function page_form(&$node, &$error) { $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short()); } - $output .= form_textfield(t('Link name'), 'link', $node->link, 60, 64, t("To make the page show up in the navigation links, enter the name of the link, otherwise leave blank.")); + $output .= form_textfield(t('Link name'), 'link', $node->link, 60, 64, t('To make the page show up in the navigation links, enter the name of the link. Otherwise, leave this blank.')); $output .= form_textfield(t('Link description'), 'description', $node->description, 60, 64, t("The description displayed when hovering over the page's link. Leave blank when you don't want a description.")); - $content_type = (user_access('create php content')) ? array(0 => "HTML", 1 => "PHP") : false; + $content_type = (user_access('create php content')) ? array(0 => 'HTML', 1 => 'PHP') : false; if (!$hide_types && $content_type) { - $output .= form_radios(t("Type"), "format", $node->format, $content_type); + $output .= form_radios(t('Type'), 'format', $node->format, $content_type); } return $output; } /** - * Verify a node editing form. + * Implementation of hook_validate(). */ function page_validate(&$node) { - if ($node->format && user_access("create php content")) { + if ($node->format && user_access('create php content')) { // Do not filter PHP code, do not auto-extract a teaser $node->teaser = $node->body; } if (($node->format == 1) && (!user_access('create php content'))) { /* Overwrite the submitted node body since they don't have sufficient privileges. */ - $node->body = db_result(db_query("SELECT body FROM {node} WHERE nid = '%d'", $node->nid)); + $node->body = db_result(db_query('SELECT body FROM {node} WHERE nid = %d', $node->nid)); } } diff --git a/modules/poll.module b/modules/poll.module index 1a9a57121..c63fd6f6a 100644 --- a/modules/poll.module +++ b/modules/poll.module @@ -1,13 +1,13 @@ <?php // $Id$ -function poll_help($section = 'admin/help#poll') { - $output = ''; - +/** + * Implementation of hook_help(). + */ +function poll_help($section) { switch ($section) { - case 'admin/help#poll': - $output .= t(" + return t(" <p>Users with the correct <a href=\"%permissions\">permissions</a> can create and/or vote on polls.</p> <ul> <li>To create a poll a user needs the \"create polls\" permission.</li> @@ -16,18 +16,16 @@ function poll_help($section = 'admin/help#poll') { <li>To administer polls you need the \"administer nodes\" permission.</li> </ul> <p>Creating a poll is much like creating any other node. Click \"create poll\" in your user box. The title of the poll should be the question, then enter the answers and the \"base\" vote counts. You can also choose the time period over which the vote will run.</p><p>The <a href=\"%poll\">Poll</a> item in the navigation links will take you to a page where you can see all the current polls, vote on them (if you haven't already) and view the results.</p>", array("%permissions" => url("admin/user/permission"), "%poll" => url("poll"))); - break; case 'admin/system/modules#description': - $output = t("Enables your site to capture votes on different topics in the form of multiple choice questions."); - break; + return t("Enables your site to capture votes on different topics in the form of multiple choice questions."); case 'node/add#poll': - $output = t("A poll is a multiple-choice question which visitors can vote on."); - break; + return t("A poll is a multiple-choice question which visitors can vote on."); } - - return $output; } +/** + * Implementation of hook_access(). + */ function poll_access($op, $node) { if ($op == 'view') { return $node->status; @@ -38,6 +36,11 @@ function poll_access($op, $node) { } } +/** + * Implementation of hook_block(). + * + * Generates a block containing the latest poll. + */ function poll_block($op = 'list', $delta = 0) { if (user_access('access content')) { if ($op == 'list') { @@ -45,13 +48,13 @@ function poll_block($op = 'list', $delta = 0) { return $blocks; } else { - // Retrieve latest poll + // Retrieve the latest poll. $timestamp = db_result(db_query("SELECT MAX(created) FROM {node} WHERE type = 'poll' AND status = '1' AND moderate = '0'")); if ($timestamp) { $poll = node_load(array('type' => 'poll', 'created' => $timestamp, 'moderate' => 0, 'status' => 1)); if ($poll->nid) { - // Poll_view dumps the output into $poll->body + // poll_view() dumps the output into $poll->body. poll_view($poll, 1, 0, 1); } } @@ -62,19 +65,29 @@ function poll_block($op = 'list', $delta = 0) { } } +/** + * Implementation of hook_cron(). + * + * Closes polls that have exceeded their allowed runtime. + */ function poll_cron() { - // Close polls that have exceeded their allowed runtime $result = db_query("SELECT p.nid FROM {poll} p INNER JOIN {node} n ON p.nid=n.nid WHERE (n.created + p.runtime) < '". time() ."' AND p.active = '1' AND p.runtime != '0'"); while ($poll = db_fetch_object($result)) { db_query("UPDATE {poll} SET active='0' WHERE nid = %d", $poll->nid); } } +/** + * Implementation of hook_delete(). + */ function poll_delete($node) { db_query("DELETE FROM {poll} WHERE nid=%d", $node->nid); db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid); } +/** + * Implementation of hook_validate(). + */ function poll_validate(&$node) { if (isset($node->title)) { // Check for at least two options and validate amount of votes: @@ -99,6 +112,9 @@ function poll_validate(&$node) { return $error; } +/** + * Implementation of hook_form(). + */ function poll_form(&$node, &$error) { $admin = user_access('administer nodes'); @@ -110,7 +126,7 @@ function poll_form(&$node, &$error) { $node->choices = max(2, count($node->choice) ? count($node->choice) : 5); } - // User ticked 'need more choices' + // User ticked 'need more choices'. if ($node->morechoices) { $node->choices *= 2; } @@ -170,7 +186,7 @@ function poll_link($type, $node = 0, $main) { $links = array(); if ($type == 'system') { - menu('node/add/poll', t('poll'), user_access('create polls') ? 'node_page' : MENU_DENIED, 0); + menu('node/add/poll', t('poll'), user_access('create polls') ? MENU_FALLTHROUGH : MENU_DENIED, 0); menu('poll', t('polls'), user_access('access content') ? 'poll_page' : MENU_DENIED, 0, MENU_HIDE); } else if ($type == 'page' && user_access('access content')) { @@ -211,6 +227,9 @@ function poll_uid() { return $id; } +/** + * Implementation of hook_load(). + */ function poll_load($node) { // Load the appropriate choices into the $node object $poll = db_fetch_object(db_query("SELECT runtime, voters, active FROM {poll} WHERE nid = %d", $node->nid)); @@ -230,6 +249,9 @@ function poll_load($node) { return $poll; } +/** + * Implementation of hook_node_name(). + */ function poll_node_name($node) { return t("poll"); } @@ -246,12 +268,17 @@ function poll_page() { print theme('page', $output); } +/** + * Implementation of hook_perm(). + */ function poll_perm() { return array('create polls', 'vote on polls'); } +/** + * Creates a simple teaser that lists all the choices. + */ function poll_teaser($node) { - // Create a simple teaser that lists all the choices if (is_array($node->choice)) { foreach ($node->choice as $k => $choice) { $teaser .= '* '. $choice['chtext'] .'\n'; @@ -261,7 +288,7 @@ function poll_teaser($node) { } /** - * Display the vote form + * Generates the voting form for a poll. */ function poll_view_voting(&$node, $main, $page, $block) { $url = request_uri(); @@ -286,6 +313,9 @@ function poll_view_voting(&$node, $main, $page, $block) { return $output; } +/** + * Generates a graphical representation of the results of a poll. + */ function poll_view_results(&$node, $main, $page, $block) { // Display the results @@ -343,6 +373,13 @@ function poll_view_processvote(&$node) { } } +/** + * Implementation of hook_view(). + * + * @param $block + * An extra parameter that adapts the hook to display a block-ready + * rendering of the poll. + */ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { global $user; @@ -377,6 +414,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { } } +/** + * Implementation of hook_update(). + */ function poll_update($node) { db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid); diff --git a/modules/poll/poll.module b/modules/poll/poll.module index 1a9a57121..c63fd6f6a 100644 --- a/modules/poll/poll.module +++ b/modules/poll/poll.module @@ -1,13 +1,13 @@ <?php // $Id$ -function poll_help($section = 'admin/help#poll') { - $output = ''; - +/** + * Implementation of hook_help(). + */ +function poll_help($section) { switch ($section) { - case 'admin/help#poll': - $output .= t(" + return t(" <p>Users with the correct <a href=\"%permissions\">permissions</a> can create and/or vote on polls.</p> <ul> <li>To create a poll a user needs the \"create polls\" permission.</li> @@ -16,18 +16,16 @@ function poll_help($section = 'admin/help#poll') { <li>To administer polls you need the \"administer nodes\" permission.</li> </ul> <p>Creating a poll is much like creating any other node. Click \"create poll\" in your user box. The title of the poll should be the question, then enter the answers and the \"base\" vote counts. You can also choose the time period over which the vote will run.</p><p>The <a href=\"%poll\">Poll</a> item in the navigation links will take you to a page where you can see all the current polls, vote on them (if you haven't already) and view the results.</p>", array("%permissions" => url("admin/user/permission"), "%poll" => url("poll"))); - break; case 'admin/system/modules#description': - $output = t("Enables your site to capture votes on different topics in the form of multiple choice questions."); - break; + return t("Enables your site to capture votes on different topics in the form of multiple choice questions."); case 'node/add#poll': - $output = t("A poll is a multiple-choice question which visitors can vote on."); - break; + return t("A poll is a multiple-choice question which visitors can vote on."); } - - return $output; } +/** + * Implementation of hook_access(). + */ function poll_access($op, $node) { if ($op == 'view') { return $node->status; @@ -38,6 +36,11 @@ function poll_access($op, $node) { } } +/** + * Implementation of hook_block(). + * + * Generates a block containing the latest poll. + */ function poll_block($op = 'list', $delta = 0) { if (user_access('access content')) { if ($op == 'list') { @@ -45,13 +48,13 @@ function poll_block($op = 'list', $delta = 0) { return $blocks; } else { - // Retrieve latest poll + // Retrieve the latest poll. $timestamp = db_result(db_query("SELECT MAX(created) FROM {node} WHERE type = 'poll' AND status = '1' AND moderate = '0'")); if ($timestamp) { $poll = node_load(array('type' => 'poll', 'created' => $timestamp, 'moderate' => 0, 'status' => 1)); if ($poll->nid) { - // Poll_view dumps the output into $poll->body + // poll_view() dumps the output into $poll->body. poll_view($poll, 1, 0, 1); } } @@ -62,19 +65,29 @@ function poll_block($op = 'list', $delta = 0) { } } +/** + * Implementation of hook_cron(). + * + * Closes polls that have exceeded their allowed runtime. + */ function poll_cron() { - // Close polls that have exceeded their allowed runtime $result = db_query("SELECT p.nid FROM {poll} p INNER JOIN {node} n ON p.nid=n.nid WHERE (n.created + p.runtime) < '". time() ."' AND p.active = '1' AND p.runtime != '0'"); while ($poll = db_fetch_object($result)) { db_query("UPDATE {poll} SET active='0' WHERE nid = %d", $poll->nid); } } +/** + * Implementation of hook_delete(). + */ function poll_delete($node) { db_query("DELETE FROM {poll} WHERE nid=%d", $node->nid); db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid); } +/** + * Implementation of hook_validate(). + */ function poll_validate(&$node) { if (isset($node->title)) { // Check for at least two options and validate amount of votes: @@ -99,6 +112,9 @@ function poll_validate(&$node) { return $error; } +/** + * Implementation of hook_form(). + */ function poll_form(&$node, &$error) { $admin = user_access('administer nodes'); @@ -110,7 +126,7 @@ function poll_form(&$node, &$error) { $node->choices = max(2, count($node->choice) ? count($node->choice) : 5); } - // User ticked 'need more choices' + // User ticked 'need more choices'. if ($node->morechoices) { $node->choices *= 2; } @@ -170,7 +186,7 @@ function poll_link($type, $node = 0, $main) { $links = array(); if ($type == 'system') { - menu('node/add/poll', t('poll'), user_access('create polls') ? 'node_page' : MENU_DENIED, 0); + menu('node/add/poll', t('poll'), user_access('create polls') ? MENU_FALLTHROUGH : MENU_DENIED, 0); menu('poll', t('polls'), user_access('access content') ? 'poll_page' : MENU_DENIED, 0, MENU_HIDE); } else if ($type == 'page' && user_access('access content')) { @@ -211,6 +227,9 @@ function poll_uid() { return $id; } +/** + * Implementation of hook_load(). + */ function poll_load($node) { // Load the appropriate choices into the $node object $poll = db_fetch_object(db_query("SELECT runtime, voters, active FROM {poll} WHERE nid = %d", $node->nid)); @@ -230,6 +249,9 @@ function poll_load($node) { return $poll; } +/** + * Implementation of hook_node_name(). + */ function poll_node_name($node) { return t("poll"); } @@ -246,12 +268,17 @@ function poll_page() { print theme('page', $output); } +/** + * Implementation of hook_perm(). + */ function poll_perm() { return array('create polls', 'vote on polls'); } +/** + * Creates a simple teaser that lists all the choices. + */ function poll_teaser($node) { - // Create a simple teaser that lists all the choices if (is_array($node->choice)) { foreach ($node->choice as $k => $choice) { $teaser .= '* '. $choice['chtext'] .'\n'; @@ -261,7 +288,7 @@ function poll_teaser($node) { } /** - * Display the vote form + * Generates the voting form for a poll. */ function poll_view_voting(&$node, $main, $page, $block) { $url = request_uri(); @@ -286,6 +313,9 @@ function poll_view_voting(&$node, $main, $page, $block) { return $output; } +/** + * Generates a graphical representation of the results of a poll. + */ function poll_view_results(&$node, $main, $page, $block) { // Display the results @@ -343,6 +373,13 @@ function poll_view_processvote(&$node) { } } +/** + * Implementation of hook_view(). + * + * @param $block + * An extra parameter that adapts the hook to display a block-ready + * rendering of the poll. + */ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { global $user; @@ -377,6 +414,9 @@ function poll_view(&$node, $main = 0, $page = 0, $block = 0) { } } +/** + * Implementation of hook_update(). + */ function poll_update($node) { db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid); |