Commit a158eca1 authored by Dries's avatar Dries

- Patch #7577 by JonBob: code improvements to the book, forum, page and poll module.

parent 3de9d33e
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -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 -&gt; moderate -&gt; post -&gt; 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));
}
}
......
......@@ -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 -&gt; moderate -&gt; post -&gt; 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));
}
}
......
<?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);
......
<?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) {
}
}