2002-02-16 13:38:00 +00:00
<?php
2001-10-20 18:57:09 +00:00
// $Id$
2000-12-14 14:13:37 +00:00
2004-07-10 07:58:47 +00:00
/**
* @file
2004-08-21 06:42:38 +00:00
* Enables users to comment on published content.
2004-07-10 07:58:47 +00:00
*
* When enabled, the Drupal comment module creates a discussion
* board for each Drupal node. Users can post comments to discuss
* a forum topic, weblog post, story, collaborative book page, etc.
*/
2006-12-12 06:14:21 +00:00
/**
* Comment is published.
2005-05-05 20:12:49 +00:00
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_PUBLISHED', 0);
2006-12-12 06:14:21 +00:00
/**
* Comment is awaiting approval.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_NOT_PUBLISHED', 1);
/**
2006-12-12 06:14:21 +00:00
* Comments are displayed in a flat list - collapsed.
2005-11-13 09:05:38 +00:00
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_MODE_FLAT_COLLAPSED', 1);
2006-12-12 06:14:21 +00:00
/**
* Comments are displayed in a flat list - expanded.
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_MODE_FLAT_EXPANDED', 2);
2006-12-12 06:14:21 +00:00
/**
* Comments are displayed as a threaded list - collapsed.
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_MODE_THREADED_COLLAPSED', 3);
2006-12-12 06:14:21 +00:00
/**
* Comments are displayed as a threaded list - expanded.
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_MODE_THREADED_EXPANDED', 4);
2005-11-13 09:05:38 +00:00
/**
2006-12-12 06:14:21 +00:00
* Comments are ordered by date - newest first.
2005-11-13 09:05:38 +00:00
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_ORDER_NEWEST_FIRST', 1);
2006-12-12 06:14:21 +00:00
/**
* Comments are ordered by date - oldest first.
*/
2006-04-27 22:03:20 +00:00
define('COMMENT_ORDER_OLDEST_FIRST', 2);
2005-11-13 09:05:38 +00:00
/**
2006-12-12 06:14:21 +00:00
* Comment controls should be shown above the comment list.
2005-11-13 09:05:38 +00:00
*/
define('COMMENT_CONTROLS_ABOVE', 0);
2006-12-12 06:14:21 +00:00
/**
* Comment controls should be shown below the comment list.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_CONTROLS_BELOW', 1);
2006-12-12 06:14:21 +00:00
/**
* Comment controls should be shown both above and below the comment list.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_CONTROLS_ABOVE_BELOW', 2);
2006-12-12 06:14:21 +00:00
/**
* Comment controls are hidden.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_CONTROLS_HIDDEN', 3);
/**
2006-12-12 06:14:21 +00:00
* Anonymous posters may not enter their contact information.
2005-11-13 09:05:38 +00:00
*/
define('COMMENT_ANONYMOUS_MAYNOT_CONTACT', 0);
2006-12-12 06:14:21 +00:00
/**
* Anonymous posters may leave their contact information.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_ANONYMOUS_MAY_CONTACT', 1);
2006-12-12 06:14:21 +00:00
/**
* Anonymous posters must leave their contact information.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_ANONYMOUS_MUST_CONTACT', 2);
/**
2006-12-12 06:14:21 +00:00
* Comment form should be displayed on a separate page.
2005-11-13 09:05:38 +00:00
*/
define('COMMENT_FORM_SEPARATE_PAGE', 0);
2006-12-12 06:14:21 +00:00
/**
* Comment form should be shown below post or list of comments.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_FORM_BELOW', 1);
/**
2006-12-12 06:14:21 +00:00
* Comments for this node are disabled.
2005-11-13 09:05:38 +00:00
*/
define('COMMENT_NODE_DISABLED', 0);
2006-12-12 06:14:21 +00:00
/**
* Comments for this node are locked.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_NODE_READ_ONLY', 1);
2006-12-12 06:14:21 +00:00
/**
* Comments are enabled on this node.
*/
2005-11-13 09:05:38 +00:00
define('COMMENT_NODE_READ_WRITE', 2);
2005-05-05 20:12:49 +00:00
2005-11-15 07:29:47 +00:00
/**
2006-12-12 06:14:21 +00:00
* Comment preview is optional.
2005-11-15 07:29:47 +00:00
*/
define('COMMENT_PREVIEW_OPTIONAL', 0);
2006-12-12 06:14:21 +00:00
/**
* Comment preview is required.
*/
2005-11-15 07:29:47 +00:00
define('COMMENT_PREVIEW_REQUIRED', 1);
2004-06-04 18:50:29 +00:00
/**
* Implementation of hook_help().
*/
2005-11-01 10:17:34 +00:00
function comment_help($section) {
2003-08-05 18:33:39 +00:00
switch ($section) {
2003-10-09 18:53:22 +00:00
case 'admin/help#comment':
2006-04-11 11:33:15 +00:00
$output = '<p>'. t('The comment module creates a discussion board for each post. Users can post comments to discuss a forum topic, weblog post, story, collaborative book page, etc. The ability to comment is an important part of involving members in a community dialogue.') .'</p>';
2006-05-07 00:08:36 +00:00
$output .= '<p>'. t('An administrator can give comment permissions to user groups, and users can (optionally) edit their last comment, assuming no others have been posted since. Attached to each comment board is a control panel for customizing the way that comments are displayed. Users can control the chronological ordering of posts (newest or oldest first) and the number of posts to display on each page. Comments behave like other user submissions. Filters, smileys and HTML that work in nodes will also work with comments. The comment module provides specific features to inform site members when new comments have been posted.') .'</p>';
2006-08-18 12:17:00 +00:00
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@comment">Comment page</a>.', array('@comment' => 'http://drupal.org/handbook/modules/comment/')) .'</p>';
2005-11-01 10:17:34 +00:00
return $output;
2006-07-31 11:25:55 +00:00
case 'admin/content/comment':
case 'admin/content/comment/new':
2006-11-26 23:10:29 +00:00
return '<p>'. t("Below is a list of the latest comments posted to your site. Click on a subject to see the comment, the author's name to edit the author's user information , 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/content/comment/approval':
2006-11-26 23:10:29 +00:00
return '<p>'. t("Below is a list of the comments posted to your site that need approval. To approve a comment, click on 'edit' and then change its 'moderation status' to Approved. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/content/comment/settings':
2006-11-26 23:10:29 +00:00
return '<p>'. t("Comments can be attached to any node, and their settings are below. The display comes in two types: a 'flat list' where everything is flush to the left side, and comments come in chronological order, and a 'threaded list' where replies to other comments are placed immediately below and slightly indented, forming an outline. They also come in two styles: 'expanded', where you see both the title and the contents, and 'collapsed' where you only see the title. Preview comment forces a user to look at their comment by clicking on a 'Preview' button before they can actually add the comment.") .'</p>';
2004-06-04 18:50:29 +00:00
}
2002-02-22 19:44:22 +00:00
}
2007-04-06 13:27:23 +00:00
/**
* Implementation of hook_theme()
*/
function comment_theme() {
return array(
'comment_block' => array(
'arguments' => array(),
),
'comment_admin_overview' => array(
'arguments' => array('form' => NULL),
),
'comment_preview' => array(
'arguments' => array('comment' => NULL, 'links' => array(), 'visible' => 1),
),
'comment_view' => array(
'arguments' => array('comment' => NULL, 'links' => array(), 'visible' => 1),
),
'comment_controls' => array(
'arguments' => array('form' => NULL),
),
'comment' => array(
'arguments' => array('comment' => NULL, 'links' => array()),
2007-04-27 07:42:54 +00:00
'file' => 'comment.tpl.php',
2007-04-06 13:27:23 +00:00
),
'comment_folded' => array(
'arguments' => array('comment' => NULL),
),
'comment_flat_collapsed' => array(
'arguments' => array('comment' => NULL),
),
'comment_flat_expanded' => array(
'arguments' => array('comment' => NULL),
),
'comment_thread_collapsed' => array(
'arguments' => array('comment' => NULL),
),
'comment_thread_expanded' => array(
'arguments' => array('comment' => NULL),
),
'comment_post_forbidden' => array(
'arguments' => array('nid' => NULL),
),
'comment_wrapper' => array(
'arguments' => array('content' => NULL),
),
);
}
2007-01-24 14:48:36 +00:00
function _comment_view_access($node, $cid) {
return $node && $cid;
}
2004-07-08 19:58:10 +00:00
/**
* Implementation of hook_menu().
*/
2007-01-24 14:48:36 +00:00
function comment_menu() {
$items['admin/content/comment'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Comments',
'description' => 'List and edit site comments and the comment moderation queue.',
2007-01-24 14:48:36 +00:00
'page callback' => 'comment_admin',
'access arguments' => array('administer comments'),
);
2004-07-08 19:58:10 +00:00
2007-01-24 14:48:36 +00:00
// Tabs:
$items['admin/content/comment/list'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'List',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
2004-09-16 07:17:56 +00:00
2007-01-24 14:48:36 +00:00
// Subtabs:
$items['admin/content/comment/list/new'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Published comments',
2007-01-24 14:48:36 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/content/comment/list/approval'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Approval queue',
2007-01-24 14:48:36 +00:00
'page arguments' => array('approval'),
'type' => MENU_LOCAL_TASK,
);
$items['admin/content/comment/settings'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Settings',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('comment_admin_settings'),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items['comment/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete comment',
2007-01-24 14:48:36 +00:00
'page callback' => 'comment_delete',
'access arguments' => array('administer comments'),
'type' => MENU_CALLBACK,
);
$items['comment/edit'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit comment',
2007-01-24 14:48:36 +00:00
'page callback' => 'comment_edit',
'access arguments' => array('post comments'),
'type' => MENU_CALLBACK,
);
2007-02-11 09:30:51 +00:00
$items['comment/reply/%node'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Reply to comment',
2007-01-24 14:48:36 +00:00
'page callback' => 'comment_reply',
2007-03-06 16:14:04 +00:00
'page arguments' => array(2),
2007-01-24 14:48:36 +00:00
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'type' => MENU_CALLBACK,
);
2007-02-11 09:30:51 +00:00
$items['node/%node/%'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'View',
2007-01-24 14:48:36 +00:00
'page callback' => 'node_page_view',
'page arguments' => array(1, 2),
'access callback' => '_comment_view_access',
'access arguments' => array(1, 2),
'type' => MENU_CALLBACK,
);
2004-07-08 19:58:10 +00:00
return $items;
}
/**
* Implementation of hook_perm().
*/
function comment_perm() {
2005-10-07 06:11:12 +00:00
return array('access comments', 'post comments', 'administer comments', 'post comments without approval');
2004-07-08 19:58:10 +00:00
}
/**
* Implementation of hook_block().
*
* Generates a block with the most recent comments.
*/
function comment_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Recent comments');
return $blocks;
}
2004-10-31 07:34:47 +00:00
else if ($op == 'view' && user_access('access comments')) {
2004-07-08 19:58:10 +00:00
$block['subject'] = t('Recent comments');
2006-01-17 18:02:23 +00:00
$block['content'] = theme('comment_block');
2004-07-08 19:58:10 +00:00
return $block;
}
}
2006-12-19 09:02:28 +00:00
/**
* Find a number of recent comments. This is done in two steps.
* 1. Find the n (specified by $number) nodes that have the most recent
* comments. This is done by querying node_comment_statistics which has
* an index on last_comment_timestamp, and is thus a fast query.
* 2. Loading the information from the comments table based on the nids found
* in step 1.
*
* @param $number (optional) The maximum number of comments to find.
* @return $comments An array of comment objects each containing a nid,
* subject, cid, and timstamp, or an empty array if there are no recent
* comments visible to the current user.
*/
function comment_get_recent($number = 10) {
// Select the $number nodes (visible to the current user) with the most
// recent comments. This is efficient due to the index on
// last_comment_timestamp.
2007-03-06 16:15:19 +00:00
$result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number);
2006-12-19 09:02:28 +00:00
$nids = array();
while ($row = db_fetch_object($result)) {
$nids[] = $row->nid;
}
$comments = array();
if (!empty($nids)) {
// From among the comments on the nodes selected in the first query,
// find the $number most recent comments.
$result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, $number);
while ($comment = db_fetch_object($result)) {
$comments[] = $comment;
}
}
return $comments;
}
/**
* Returns a formatted list of recent comments to be displayed in the comment
* block.
*
* @ingroup themeable
*/
2006-01-17 18:02:23 +00:00
function theme_comment_block() {
$items = array();
2006-12-19 09:02:28 +00:00
foreach (comment_get_recent() as $comment) {
2007-02-15 11:40:19 +00:00
$items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
2006-01-17 18:02:23 +00:00
}
2006-12-01 16:50:36 +00:00
if ($items) {
return theme('item_list', $items);
}
2006-01-17 18:02:23 +00:00
}
2004-07-08 19:58:10 +00:00
/**
* Implementation of hook_link().
*/
2006-07-19 07:15:35 +00:00
function comment_link($type, $node = NULL, $teaser = FALSE) {
2004-07-08 19:58:10 +00:00
$links = array();
if ($type == 'node' && $node->comment) {
2006-07-19 07:15:35 +00:00
if ($teaser) {
2004-07-08 19:58:10 +00:00
// Main page: display the number of comments that have been posted.
if (user_access('access comments')) {
$all = comment_num_all($node->nid);
if ($all) {
2006-05-18 14:58:57 +00:00
$links['comment_comments'] = array(
2006-08-18 12:17:00 +00:00
'title' => format_plural($all, '1 comment', '@count comments'),
2006-07-04 08:59:05 +00:00
'href' => "node/$node->nid",
'attributes' => array('title' => t('Jump to the first comment of this posting.')),
2006-07-19 06:19:17 +00:00
'fragment' => 'comments'
2006-05-18 14:58:57 +00:00
);
2007-01-03 11:32:38 +00:00
$new = comment_num_new($node->nid);
2004-07-08 19:58:10 +00:00
if ($new) {
2006-05-18 14:58:57 +00:00
$links['comment_new_comments'] = array(
2006-08-18 12:17:00 +00:00
'title' => format_plural($new, '1 new comment', '@count new comments'),
2006-07-04 08:59:05 +00:00
'href' => "node/$node->nid",
'attributes' => array('title' => t('Jump to the first new comment of this posting.')),
'fragment' => 'new'
2006-05-18 14:58:57 +00:00
);
2004-07-08 19:58:10 +00:00
}
}
else {
2005-11-13 09:05:38 +00:00
if ($node->comment == COMMENT_NODE_READ_WRITE) {
2004-07-08 19:58:10 +00:00
if (user_access('post comments')) {
2006-05-18 14:58:57 +00:00
$links['comment_add'] = array(
2006-11-10 08:37:20 +00:00
'title' => t('Add new comment'),
2006-07-04 08:59:05 +00:00
'href' => "comment/reply/$node->nid",
'attributes' => array('title' => t('Add a new comment to this page.')),
2007-01-03 11:04:50 +00:00
'fragment' => 'comment-form'
2006-05-18 14:58:57 +00:00
);
2004-07-08 19:58:10 +00:00
}
else {
2006-10-18 18:00:40 +00:00
$links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node->nid);
2004-07-08 19:58:10 +00:00
}
}
}
}
}
else {
// Node page: add a "post comment" link if the user is allowed to
// post comments, if this node is not read-only, and if the comment form isn't already shown
2006-01-26 08:27:58 +00:00
if ($node->comment == COMMENT_NODE_READ_WRITE) {
2004-07-08 19:58:10 +00:00
if (user_access('post comments')) {
2006-01-26 08:27:58 +00:00
if (variable_get('comment_form_location', COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
2006-05-18 14:58:57 +00:00
$links['comment_add'] = array(
2006-10-22 08:28:47 +00:00
'title' => t('Add new comment'),
2006-07-04 08:59:05 +00:00
'href' => "comment/reply/$node->nid",
'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
2007-01-03 11:04:50 +00:00
'fragment' => 'comment-form'
2006-05-18 14:58:57 +00:00
);
2006-01-26 08:27:58 +00:00
}
2004-07-08 19:58:10 +00:00
}
else {
2006-10-18 18:00:40 +00:00
$links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node->nid);
2004-07-08 19:58:10 +00:00
}
}
}
}
if ($type == 'comment') {
2006-07-19 07:15:35 +00:00
$links = comment_links($node, $teaser);
2004-07-08 19:58:10 +00:00
}
2006-10-18 18:00:40 +00:00
if (isset($links['comment_forbidden'])) {
$links['comment_forbidden']['html'] = TRUE;
}
2004-07-08 19:58:10 +00:00
return $links;
}
2007-05-28 06:08:47 +00:00
function comment_form_alter(&$form, $form_state, $form_id) {
2006-08-06 23:00:42 +00:00
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['workflow']['comment'] = array(
'#type' => 'radios',
'#title' => t('Default comment setting'),
2006-11-26 02:43:48 +00:00
'#default_value' => variable_get('comment_'. $form['#node_type']->type, COMMENT_NODE_READ_WRITE),
2006-08-06 23:00:42 +00:00
'#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
'#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
);
}
2007-03-27 05:13:55 +00:00
elseif (isset($form['type']) && isset($form['#node'])) {
2006-01-05 14:35:55 +00:00
if ($form['type']['#value'] .'_node_form' == $form_id) {
2005-12-05 09:11:33 +00:00
$node = $form['#node'];
2006-08-22 11:13:04 +00:00
$form['comment_settings'] = array(
'#type' => 'fieldset',
'#access' => user_access('administer comments'),
'#title' => t('Comment settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 30,
);
$form['comment_settings']['comment'] = array(
'#type' => 'radios',
'#parents' => array('comment'),
'#default_value' => $node->comment,
'#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
);
2005-12-05 09:11:33 +00:00
}
2005-11-25 10:11:59 +00:00
}
}
2004-07-08 19:58:10 +00:00
/**
* Implementation of hook_nodeapi().
2004-09-14 05:48:02 +00:00
*
2004-07-08 19:58:10 +00:00
*/
function comment_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
2004-09-14 05:48:02 +00:00
case 'load':
2005-02-11 19:01:53 +00:00
return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
2006-01-05 14:35:55 +00:00
break;
case 'prepare':
if (!isset($node->comment)) {
2005-11-13 09:05:38 +00:00
$node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
2004-07-08 19:58:10 +00:00
}
break;
2005-01-24 21:20:16 +00:00
2006-08-31 21:58:36 +00:00
case 'insert':
db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
break;
case 'delete':
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
break;
2004-12-31 09:30:12 +00:00
case 'update index':
$text = '';
2005-05-05 20:12:49 +00:00
$comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
2004-12-31 09:30:12 +00:00
while ($comment = db_fetch_object($comments)) {
2005-07-29 21:06:33 +00:00
$text .= '<h2>'. check_plain($comment->subject) .'</h2>'. check_markup($comment->comment, $comment->format, FALSE);
2004-12-31 09:30:12 +00:00
}
return $text;
2005-01-24 21:20:16 +00:00
2004-12-31 09:30:12 +00:00
case 'search result':
$comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
2006-08-18 12:17:00 +00:00
return format_plural($comments, '1 comment', '@count comments');
2005-09-19 19:13:35 +00:00
2005-10-20 08:56:34 +00:00
case 'rss item':
2006-07-13 12:29:15 +00:00
if ($node->comment != COMMENT_NODE_DISABLED) {
2007-02-15 11:40:19 +00:00
return array(array('key' => 'comments', 'value' => url('node/'. $node->nid, array('fragment' => 'comments', 'absolute' => TRUE))));
2006-07-13 12:29:15 +00:00
}
else {
return array();
}
2004-07-08 19:58:10 +00:00
}
}
/**
* Implementation of hook_user().
*/
function comment_user($type, $edit, &$user, $category = NULL) {
2007-03-30 07:45:20 +00:00
if ($type == 'delete') {
2006-03-26 19:31:00 +00:00
db_query('UPDATE {comments} SET uid = 0 WHERE uid = %d', $user->uid);
db_query('UPDATE {node_comment_statistics} SET last_comment_uid = 0 WHERE last_comment_uid = %d', $user->uid);
}
2004-07-08 19:58:10 +00:00
}
2004-06-04 18:50:29 +00:00
/**
2004-06-18 15:04:37 +00:00
* Menu callback; presents the comment settings page.
2004-06-04 18:50:29 +00:00
*/
2006-07-10 19:27:52 +00:00
function comment_admin_settings() {
2005-11-21 08:32:31 +00:00
$form['viewing_options'] = array(
'#type' => 'fieldset',
2005-12-07 05:35:02 +00:00
'#title' => t('Viewing options'),
2005-11-21 08:32:31 +00:00
'#collapsible' => TRUE,
);
2004-06-18 15:04:37 +00:00
2005-11-13 09:05:38 +00:00
$form['viewing_options']['comment_default_mode'] = array(
'#type' => 'radios',
'#title' => t('Default display mode'),
'#default_value' => variable_get('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED),
'#options' => _comment_get_modes(),
2005-11-21 08:32:31 +00:00
'#description' => t('The default view for comments. Expanded views display the body of the comment. Threaded views keep replies together.'),
2005-11-13 09:05:38 +00:00
);
2005-10-07 06:11:12 +00:00
2005-11-13 09:05:38 +00:00
$form['viewing_options']['comment_default_order'] = array(
'#type' => 'radios',
'#title' => t('Default display order'),
2006-01-05 10:53:47 +00:00
'#default_value' => variable_get('comment_default_order', COMMENT_ORDER_NEWEST_FIRST),
2005-11-13 09:05:38 +00:00
'#options' => _comment_get_orders(),
2005-11-21 08:32:31 +00:00
'#description' => t('The default sorting for new users and anonymous users while viewing comments. These users may change their view using the comment control panel. For registered users, this change is remembered as a persistent user preference.'),
2005-11-13 09:05:38 +00:00
);
2002-01-03 22:02:23 +00:00
2005-10-07 06:11:12 +00:00
$form['viewing_options']['comment_default_per_page'] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'select',
'#title' => t('Default comments per page'),
'#default_value' => variable_get('comment_default_per_page', 50),
'#options' => _comment_per_page(),
2005-11-21 08:32:31 +00:00
'#description' => t('Default number of comments for each page: more comments are distributed in several pages.'),
2005-10-07 06:11:12 +00:00
);
2005-11-13 09:05:38 +00:00
$form['viewing_options']['comment_controls'] = array(
'#type' => 'radios',
'#title' => t('Comment controls'),
'#default_value' => variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN),
'#options' => array(
t('Display above the comments'),
t('Display below the comments'),
t('Display above and below the comments'),
t('Do not display')),
2006-05-07 00:08:36 +00:00
'#description' => t('Position of the comment controls box. The comment controls let the user change the default display mode and display order of comments.'),
2005-11-13 09:05:38 +00:00
);
2005-10-07 06:11:12 +00:00
2005-11-21 08:32:31 +00:00
$form['posting_settings'] = array(
'#type' => 'fieldset',
2005-12-07 05:35:02 +00:00
'#title' => t('Posting settings'),
2005-11-21 08:32:31 +00:00
'#collapsible' => TRUE,
);
2005-10-07 06:11:12 +00:00
2005-11-13 09:05:38 +00:00
$form['posting_settings']['comment_anonymous'] = array(
'#type' => 'radios',
2006-01-20 09:33:03 +00:00
'#title' => t('Anonymous commenting'),
2005-11-13 09:05:38 +00:00
'#default_value' => variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT),
'#options' => array(
COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information')),
2007-02-15 11:40:19 +00:00
'#description' => t('This option is enabled when anonymous users have permission to post comments on the <a href="@url">permissions page</a>.', array('@url' => url('admin/user/access', array('fragment' => 'module-comment')))),
2005-11-13 09:05:38 +00:00
);
2006-01-20 09:33:03 +00:00
if (!user_access('post comments', user_load(array('uid' => 0)))) {
2006-08-27 12:54:01 +00:00
$form['posting_settings']['comment_anonymous']['#disabled'] = TRUE;
2006-01-20 09:33:03 +00:00
}
2005-10-07 06:11:12 +00:00
$form['posting_settings']['comment_subject_field'] = array(
2005-10-11 19:44:35 +00:00
'#type' => 'radios',
'#title' => t('Comment subject field'),
'#default_value' => variable_get('comment_subject_field', 1),
'#options' => array(t('Disabled'), t('Enabled')),
2005-11-21 08:32:31 +00:00
'#description' => t('Can users provide a unique subject for their comments?'),
2005-10-07 06:11:12 +00:00
);
2005-11-21 08:32:31 +00:00
$form['posting_settings']['comment_preview'] = array(
'#type' => 'radios',
'#title' => t('Preview comment'),
'#default_value' => variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED),
'#options' => array(t('Optional'), t('Required')),
);
2005-10-07 06:11:12 +00:00
2005-11-13 09:05:38 +00:00
$form['posting_settings']['comment_form_location'] = array(
'#type' => 'radios',
'#title' => t('Location of comment submission form'),
'#default_value' => variable_get('comment_form_location', COMMENT_FORM_SEPARATE_PAGE),
2005-11-21 08:32:31 +00:00
'#options' => array(t('Display on separate page'), t('Display below post or comments')),
2005-11-13 09:05:38 +00:00
);
2005-10-07 06:11:12 +00:00
2006-08-18 18:58:47 +00:00
return system_settings_form($form);
2001-12-31 13:02:53 +00:00
}
2004-06-04 18:50:29 +00:00
/**
* This is *not* a hook_access() implementation. This function is called
* to determine whether the current user has access to a particular comment.
*
* Authenticated users can edit their comments as long they have not been
* replied to. This prevents people from changing or revising their
2006-05-20 08:14:09 +00:00
* statements based on the replies to their posts.
2004-06-04 18:50:29 +00:00
*/
2001-12-30 16:16:38 +00:00
function comment_access($op, $comment) {
2001-12-08 11:37:07 +00:00
global $user;
2004-06-04 18:50:29 +00:00
if ($op == 'edit') {
2005-07-19 17:51:59 +00:00
return ($user->uid && $user->uid == $comment->uid && comment_num_replies($comment->cid) == 0) || user_access('administer comments');
2001-12-08 11:37:07 +00:00
}
}
2004-06-04 18:50:29 +00:00
2004-04-18 15:17:10 +00:00
function comment_node_url() {
2004-07-10 07:58:47 +00:00
return arg(0) .'/'. arg(1);
2003-09-15 15:58:20 +00:00
}
2001-12-08 11:37:07 +00:00
2001-12-30 16:16:38 +00:00
function comment_edit($cid) {
global $user;
2004-12-29 18:41:52 +00:00
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $cid));
2004-01-13 19:25:37 +00:00
$comment = drupal_unpack($comment);
2004-10-15 11:13:11 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2004-06-04 18:50:29 +00:00
if (comment_access('edit', $comment)) {
2006-08-18 18:58:47 +00:00
return comment_form_box((array)$comment);
2004-12-15 21:19:42 +00:00
}
else {
drupal_access_denied();
2001-12-30 16:16:38 +00:00
}
}
2006-12-08 16:16:30 +00:00
/**
* This function is responsible for generating a comment reply form.
* There are several cases that have to be handled, including:
* - replies to comments
* - replies to nodes
* - attempts to reply to nodes that can no longer accept comments
* - respecting access permissions ('access comments', 'post comments', etc.)
*
* The node or comment that is being replied to must appear above the comment
* form to provide the user context while authoring the comment.
*
2007-03-06 16:14:04 +00:00
* @param $node
* Every comment belongs to a node. This is that node.
2006-12-08 16:16:30 +00:00
* @param $pid
* Some comments are replies to other comments. In those cases, $pid is the parent
* comment's cid.
*
* @return $output
* The rendered parent node or comment plus the new comment form.
*/
2007-03-06 16:14:04 +00:00
function comment_reply($node, $pid = NULL) {
2006-12-08 16:16:30 +00:00
// Set the breadcrumb trail.
2007-03-06 16:14:04 +00:00
menu_set_location(array(array('path' => "node/$node->nid", 'title' => $node->title), array('path' => "comment/reply/$node->nid")));
2003-02-15 11:39:56 +00:00
2005-11-21 08:32:31 +00:00
$op = isset($_POST['op']) ? $_POST['op'] : '';
2002-04-14 20:46:41 +00:00
2005-11-21 08:32:31 +00:00
$output = '';
2002-09-15 13:00:12 +00:00
2004-07-10 07:58:47 +00:00
if (user_access('access comments')) {
2006-12-08 16:16:30 +00:00
// The user is previewing a comment prior to submitting it.
2005-11-21 08:32:31 +00:00
if ($op == t('Preview comment')) {
if (user_access('post comments')) {
2007-03-06 16:14:04 +00:00
$output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), NULL);
2005-11-21 08:32:31 +00:00
}
else {
drupal_set_message(t('You are not authorized to post comments.'), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2005-11-21 08:32:31 +00:00
}
2002-04-14 20:46:41 +00:00
}
else {
2006-12-08 16:16:30 +00:00
// $pid indicates that this is a reply to a comment.
2005-11-21 08:32:31 +00:00
if ($pid) {
2006-12-08 16:16:30 +00:00
// load the comment whose cid = $pid
2007-03-30 07:45:20 +00:00
if ($comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $pid, COMMENT_PUBLISHED))) {
2006-12-08 16:16:30 +00:00
// If that comment exists, make sure that the current comment and the parent comment both
// belong to the same parent node.
2007-03-06 16:14:04 +00:00
if ($comment->nid != $node->nid) {
2006-04-04 07:07:45 +00:00
// Attempting to reply to a comment not belonging to the current nid.
drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2006-04-04 07:07:45 +00:00
}
2006-12-08 16:16:30 +00:00
// Display the parent comment
2006-03-04 18:01:54 +00:00
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output .= theme('comment_view', $comment);
}
else {
drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2006-03-04 18:01:54 +00:00
}
2005-11-21 08:32:31 +00:00
}
2006-12-08 16:16:30 +00:00
// This is the case where the comment is in response to a node. Display the node.
2005-11-21 08:32:31 +00:00
else if (user_access('access content')) {
$output .= node_view($node);
}
2006-12-08 16:16:30 +00:00
// Should we show the reply box?
2007-03-06 16:14:04 +00:00
if (node_comment_mode($node->nid) != COMMENT_NODE_READ_WRITE) {
2005-11-21 08:32:31 +00:00
drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2005-11-21 08:32:31 +00:00
}
else if (user_access('post comments')) {
2007-03-06 16:14:04 +00:00
$output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), t('Reply'));
2005-11-21 08:32:31 +00:00
}
else {
drupal_set_message(t('You are not authorized to post comments.'), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2005-11-21 08:32:31 +00:00
}
2002-04-14 20:46:41 +00:00
}
2002-04-22 09:05:36 +00:00
}
else {
2005-07-19 17:51:59 +00:00
drupal_set_message(t('You are not authorized to view comments.'), 'error');
2007-03-06 16:14:04 +00:00
drupal_goto("node/$node->nid");
2001-12-08 11:37:07 +00:00
}
2003-12-09 20:15:18 +00:00
2005-04-24 16:34:36 +00:00
return $output;
2001-12-08 11:37:07 +00:00
}
2005-07-19 17:51:59 +00:00
/**
* Accepts a submission of new or changed comment content.
*
* @param $edit
* A comment array.
*
* @return
2006-05-07 00:08:36 +00:00
* If the comment is successfully saved the comment ID is returned. If the comment
2005-07-19 17:51:59 +00:00
* is not saved, FALSE is returned.
*/
function comment_save($edit) {
2002-09-15 13:00:12 +00:00
global $user;
2006-02-26 19:52:09 +00:00
if (user_access('post comments') && (user_access('administer comments') || node_comment_mode($edit['nid']) == COMMENT_NODE_READ_WRITE)) {
2004-07-04 16:50:02 +00:00
if (!form_get_errors()) {
2004-12-02 20:24:53 +00:00
if ($edit['cid']) {
2005-07-19 17:51:59 +00:00
// Update the comment in the database.
2006-02-09 09:09:03 +00:00
db_query("UPDATE {comments} SET status = %d, timestamp = %d, subject = '%s', comment = '%s', format = %d, uid = %d, name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d", $edit['status'], $edit['timestamp'], $edit['subject'], $edit['comment'], $edit['format'], $edit['uid'], $edit['name'], $edit['mail'], $edit['homepage'], $edit['cid']);
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
_comment_update_node_statistics($edit['nid']);
2004-06-04 18:50:29 +00:00
// Allow modules to respond to the updating of a comment.
2005-10-07 06:11:12 +00:00
comment_invoke_comment($edit, 'update');
2005-01-09 10:06:26 +00:00
// Add an entry to the watchdog log.
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: updated %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
2001-12-30 16:16:38 +00:00
}
else {
2006-12-25 22:39:08 +00:00
// Check for duplicate comments. Note that we have to use the
// validated/filtered data to perform such check.
$duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND nid = %d AND subject = '%s' AND comment = '%s'", $edit['pid'], $edit['nid'], $edit['subject'], $edit['comment']), 0);
if ($duplicate != 0) {
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: duplicate %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
2006-12-25 22:39:08 +00:00
}
2004-06-04 18:50:29 +00:00
// Add the comment to database.
2005-05-05 20:12:49 +00:00
$status = user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
2004-06-04 18:50:29 +00:00
$roles = variable_get('comment_roles', array());
2004-05-10 20:34:25 +00:00
$score = 0;
foreach (array_intersect(array_keys($roles), array_keys($user->roles)) as $rid) {
$score = max($roles[$rid], $score);
}
2002-09-15 13:00:12 +00:00
$users = serialize(array(0 => $score));
2006-11-26 22:35:20 +00:00
// Here we are building the thread field. See the documentation for
// comment_render().
2004-06-04 18:50:29 +00:00
if ($edit['pid'] == 0) {
2004-12-02 20:24:53 +00:00
// This is a comment with no parent comment (depth 0): we start
// by retrieving the maximum thread level.
2004-06-04 18:50:29 +00:00
$max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
// Strip the "/" from the end of the thread.
$max = rtrim($max, '/');
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
// Finally, build the thread field for this new comment.
2006-02-09 08:33:36 +00:00
$thread = int2vancode(vancode2int($max) + 1) .'/';
2003-08-12 18:32:54 +00:00
}
else {
2004-12-02 20:24:53 +00:00
// This is comment with a parent comment: we increase
// the part of the thread value at the proper depth.
2003-08-12 18:32:54 +00:00
// Get the parent comment:
2005-12-07 05:35:02 +00:00
$parent = _comment_load($edit['pid']);
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
// Strip the "/" from the end of the parent thread.
2004-12-02 20:24:53 +00:00
$parent->thread = (string) rtrim((string) $parent->thread, '/');
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
// Get the max value in _this_ thread.
2004-05-10 20:34:25 +00:00
$max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid']));
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
if ($max == '') {
// First child of this parent.
2006-02-09 08:33:36 +00:00
$thread = $parent->thread .'.'. int2vancode(0) .'/';
2003-08-12 18:32:54 +00:00
}
else {
2004-06-04 18:50:29 +00:00
// Strip the "/" at the end of the thread.
$max = rtrim($max, '/');
2003-08-12 18:32:54 +00:00
2004-06-04 18:50:29 +00:00
// We need to get the value at the correct depth.
$parts = explode('.', $max);
$parent_depth = count(explode('.', $parent->thread));
2003-08-12 18:32:54 +00:00
$last = $parts[$parent_depth];
2004-06-04 18:50:29 +00:00
// Finally, build the thread field for this new comment.
2006-02-09 08:33:36 +00:00
$thread = $parent->thread .'.'. int2vancode(vancode2int($last) + 1) .'/';
2003-08-12 18:32:54 +00:00
}
}
2004-12-02 20:24:53 +00:00
$edit['cid'] = db_next_id('{comments}_cid');
2004-09-14 05:48:02 +00:00
$edit['timestamp'] = time();
2006-10-09 06:59:01 +00:00
if ($edit['uid'] === $user->uid) { // '===' because we want to modify anonymous users too
2004-09-14 05:48:02 +00:00
$edit['name'] = $user->name;
}
2007-01-31 15:49:26 +00:00
$edit += array('mail' => '', 'homepage' => '');
2007-05-25 15:04:42 +00:00
db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $status, $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']);
2004-09-14 05:48:02 +00:00
_comment_update_node_statistics($edit['nid']);
2002-09-15 13:00:12 +00:00
2004-06-04 18:50:29 +00:00
// Tell the other modules a new comment has been submitted.
2005-10-07 06:11:12 +00:00
comment_invoke_comment($edit, 'insert');
2004-05-31 12:46:03 +00:00
2004-06-04 18:50:29 +00:00
// Add an entry to the watchdog log.
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: added %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
2001-12-30 16:16:38 +00:00
}
2001-12-22 14:12:18 +00:00
2004-06-04 18:50:29 +00:00
// Clear the cache so an anonymous user can see his comment being added.
2002-11-17 06:42:52 +00:00
cache_clear_all();
2004-05-31 12:46:03 +00:00
2004-12-07 16:55:38 +00:00
// Explain the approval queue if necessary, and then
2004-07-13 20:51:29 +00:00
// redirect the user to the node he's commenting on.
2005-05-05 20:12:49 +00:00
if ($status == COMMENT_NOT_PUBLISHED) {
2004-07-13 20:51:29 +00:00
drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
2004-05-31 12:46:03 +00:00
}
2005-07-19 17:51:59 +00:00
return $edit['cid'];
2004-05-31 12:46:03 +00:00
}
else {
2005-07-19 17:51:59 +00:00
return FALSE;
2001-12-08 11:37:07 +00:00
}
}
2002-09-15 13:00:12 +00:00
else {
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed node %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed node %subject.', array('%subject' => $edit['subject'])), 'error');
2005-07-19 17:51:59 +00:00
return FALSE;
2001-12-08 11:37:07 +00:00
}
}
function comment_links($comment, $return = 1) {
2003-02-15 11:39:56 +00:00
global $user;
2001-12-08 11:37:07 +00:00
2001-12-27 20:35:40 +00:00
$links = array();
2002-12-02 19:14:41 +00:00
2004-06-04 18:50:29 +00:00
// If we are viewing just this comment, we link back to the node.
2001-12-08 11:37:07 +00:00
if ($return) {
2006-05-18 14:58:57 +00:00
$links['comment_parent'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('parent'),
'href' => comment_node_url(),
'fragment' => "comment-$comment->cid"
2006-05-18 14:58:57 +00:00
);
2001-12-08 11:37:07 +00:00
}
2001-12-27 20:35:40 +00:00
2005-11-15 07:29:47 +00:00
if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
2005-12-19 10:12:52 +00:00
if (user_access('administer comments') && user_access('post comments')) {
2006-05-18 14:58:57 +00:00
$links['comment_delete'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('delete'),
'href' => "comment/delete/$comment->cid"
2006-05-18 14:58:57 +00:00
);
$links['comment_edit'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('edit'),
'href' => "comment/edit/$comment->cid"
2006-05-18 14:58:57 +00:00
);
$links['comment_reply'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('reply'),
'href' => "comment/reply/$comment->nid/$comment->cid"
2006-05-18 14:58:57 +00:00
);
2003-12-24 13:13:07 +00:00
}
2004-06-04 18:50:29 +00:00
else if (user_access('post comments')) {
if (comment_access('edit', $comment)) {
2006-05-18 14:58:57 +00:00
$links['comment_edit'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('edit'),
'href' => "comment/edit/$comment->cid"
2006-05-18 14:58:57 +00:00
);
2002-09-15 13:00:12 +00:00
}
2006-05-18 14:58:57 +00:00
$links['comment_reply'] = array(
2006-07-04 08:59:05 +00:00
'title' => t('reply'),
'href' => "comment/reply/$comment->nid/$comment->cid"
2006-05-18 14:58:57 +00:00
);
2001-12-30 16:16:38 +00:00
}
else {
2006-10-18 18:00:40 +00:00
$links['comment_forbidden']['title'] = theme('comment_post_forbidden', $comment->nid);
2001-12-30 16:16:38 +00:00
}
2001-12-08 11:37:07 +00:00
}
2002-09-15 13:00:12 +00:00
2004-02-15 16:28:33 +00:00
return $links;
2001-12-08 11:37:07 +00:00
}
2006-11-26 22:35:20 +00:00
/**
* Renders comment(s).
*
* @param $node
* The node which comment(s) needs rendering.
* @param $cid
* Optional, if given, only one comment is rendered.
*
* To display threaded comments in the correct order we keep a 'thread' field
* and order by that value. This field keeps this data in
* a way which is easy to update and convenient to use.
*
* A "thread" value starts at "1". If we add a child (A) to this comment,
* we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
* brother of (A) will get "1.2". Next brother of the parent of (A) will get
* "2" and so on.
*
* First of all note that the thread field stores the depth of the comment:
* depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
*
* Now to get the ordering right, consider this example:
*
* 1
* 1.1
* 1.1.1
* 1.2
* 2
*
* If we "ORDER BY thread ASC" we get the above result, and this is the
* natural order sorted by time. However, if we "ORDER BY thread DESC"
* we get:
*
* 2
* 1.2
* 1.1.1
* 1.1
* 1
*
* Clearly, this is not a natural way to see a thread, and users will get
* confused. The natural order to show a thread by time desc would be:
*
* 2
* 1
* 1.2
* 1.1
* 1.1.1
*
* which is what we already did before the standard pager patch. To achieve
* this we simply add a "/" at the end of each "thread" value. This way out
* thread fields will look like depicted below:
*
* 1/
* 1.1/
* 1.1.1/
* 1.2/
* 2/
*
* we add "/" since this char is, in ASCII, higher than every number, so if
* now we "ORDER BY thread DESC" we get the correct order. However this would
* spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
* to consider the trailing "/" so we use a substring only.
*/
2002-09-15 13:00:12 +00:00
function comment_render($node, $cid = 0) {
2003-05-30 11:13:53 +00:00
global $user;
2004-06-04 18:50:29 +00:00
$output = '';
2001-12-08 11:37:07 +00:00
2004-06-04 18:50:29 +00:00
if (user_access('access comments')) {
// Pre-process variables.
2002-09-15 13:00:12 +00:00
$nid = $node->nid;
2001-12-30 16:16:38 +00:00
if (empty($nid)) {
$nid = 0;
2001-12-08 11:37:07 +00:00
}
2006-04-27 22:03:20 +00:00
$mode = _comment_get_display_setting('mode');
$order = _comment_get_display_setting('sort');
$comments_per_page = _comment_get_display_setting('comments_per_page');
2002-09-15 13:00:12 +00:00
2002-05-16 09:32:01 +00:00
if ($cid) {
2004-06-04 18:50:29 +00:00
// Single comment view.
2007-03-30 07:45:20 +00:00
$query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.score, c.users, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
2006-04-15 04:07:18 +00:00
$query_args = array($cid);
if (!user_access('administer comments')) {
$query .= ' AND c.status = %d';
$query_args[] = COMMENT_PUBLISHED;
}
2006-12-08 15:32:10 +00:00
2006-04-15 04:07:18 +00:00
$result = db_query($query, $query_args);
2002-09-15 13:00:12 +00:00
2001-12-08 11:37:07 +00:00
if ($comment = db_fetch_object($result)) {
2004-10-15 11:13:11 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2006-05-18 14:58:57 +00:00
$links = module_invoke_all('link', 'comment', $comment, 1);
2007-03-26 00:35:59 +00:00
drupal_alter('link', $links, $node);
2006-05-18 14:58:57 +00:00
$output .= theme('comment_view', $comment, $links);
2001-12-08 11:37:07 +00:00
}
2002-05-05 19:02:02 +00:00
}
2001-12-08 11:37:07 +00:00
else {
2004-06-04 18:50:29 +00:00
// Multiple comment view
2006-04-15 04:07:18 +00:00
$query_count = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
2007-03-30 07:45:20 +00:00
$query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.score, c.users, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
2006-04-15 04:07:18 +00:00
$query_args = array($nid);
if (!user_access('administer comments')) {
$query .= ' AND c.status = %d';
$query_count .= ' AND status = %d';
$query_args[] = COMMENT_PUBLISHED;
}
2003-08-12 18:32:54 +00:00
2005-11-13 09:05:38 +00:00
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
2004-06-04 18:50:29 +00:00
$query .= ' ORDER BY c.timestamp DESC';
2003-08-12 18:32:54 +00:00
}
else {
2004-06-04 18:50:29 +00:00
$query .= ' ORDER BY c.thread DESC';
2003-08-12 18:32:54 +00:00
}
2002-05-05 19:02:02 +00:00
}
2005-11-13 09:05:38 +00:00
else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
2004-06-04 18:50:29 +00:00
$query .= ' ORDER BY c.timestamp';
2003-08-12 18:32:54 +00:00
}
else {
/*
2006-05-07 00:08:36 +00:00
** See comment above. Analysis learns that this doesn't cost
** too much. It scales much much better than having the whole
2003-08-12 18:32:54 +00:00
** comment structure.
*/
2004-06-04 18:50:29 +00:00
$query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
2003-08-12 18:32:54 +00:00
}
2002-09-15 13:00:12 +00:00
}
2005-10-07 06:11:12 +00:00
// Start a form, for use with comment control.
2006-04-15 04:07:18 +00:00
$result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
2005-11-13 09:05:38 +00:00
if (db_num_rows($result) && (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE || variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE_BELOW)) {
2006-08-18 18:58:47 +00:00
$output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
2001-12-08 11:37:07 +00:00
}
2001-12-27 20:35:40 +00:00
2006-11-12 00:11:15 +00:00
$divs = 0;
2006-10-11 13:17:23 +00:00
$last_depth = 0;
2007-01-23 19:11:18 +00:00
drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
2003-08-12 18:32:54 +00:00
while ($comment = db_fetch_object($result)) {
2004-01-13 19:25:37 +00:00
$comment = drupal_unpack($comment);
2004-10-15 11:13:11 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2004-06-04 18:50:29 +00:00
$comment->depth = count(explode('.', $comment->thread)) - 1;
2002-09-15 13:00:12 +00:00
2006-12-15 01:33:00 +00:00
if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
if ($comment->depth > $last_depth) {
$divs++;
$output .= '<div class="indented">';
$last_depth++;
}
else {
while ($comment->depth < $last_depth) {
$divs--;
$output .= '</div>';
$last_depth--;
}
2006-12-12 09:20:17 +00:00
}
2006-10-11 13:17:23 +00:00
}
2005-11-13 09:05:38 +00:00
if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
2005-10-07 06:11:12 +00:00
$output .= theme('comment_flat_collapsed', $comment);
2002-05-05 19:02:02 +00:00
}
2005-11-13 09:05:38 +00:00
else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
2005-10-07 06:11:12 +00:00
$output .= theme('comment_flat_expanded', $comment);
2001-12-08 11:37:07 +00:00
}
2005-11-13 09:05:38 +00:00
else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
2005-11-27 11:07:02 +00:00
$output .= theme('comment_thread_collapsed', $comment);
2002-05-05 19:02:02 +00:00
}
2005-11-13 09:05:38 +00:00
else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
2005-11-27 11:07:02 +00:00
$output .= theme('comment_thread_expanded', $comment);
2003-08-12 18:32:54 +00:00
}
2004-07-08 19:58:10 +00:00
}
2006-10-11 13:17:23 +00:00
for ($i = 0; $i < $divs; $i++) {
$output .= '</div>';
}
2006-04-27 22:03:20 +00:00
$output .= theme('pager', NULL, $comments_per_page, 0);
2004-06-18 15:04:37 +00:00
2005-11-13 09:05:38 +00:00
if (db_num_rows($result) && (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_BELOW || variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_ABOVE_BELOW)) {
2006-08-18 18:58:47 +00:00
$output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
2004-07-08 19:58:10 +00:00
}
}
// If enabled, show new comment form.
2005-11-13 09:05:38 +00:00
if (user_access('post comments') && node_comment_mode($nid) == COMMENT_NODE_READ_WRITE && (variable_get('comment_form_location', COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW)) {
2006-08-18 18:58:47 +00:00
$output .= comment_form_box(array('nid' => $nid), t('Post new comment'));
2004-07-08 19:58:10 +00:00
}
2006-07-19 06:19:17 +00:00
$output = theme('comment_wrapper', $output);
2004-07-08 19:58:10 +00:00
}
2006-07-19 06:19:17 +00:00
2004-07-08 19:58:10 +00:00
return $output;
2004-06-18 15:04:37 +00:00
}
2004-07-10 18:11:18 +00:00
/**
* Menu callback; delete a comment.
*/
2006-10-17 08:45:51 +00:00
function comment_delete($cid = NULL) {
2004-05-18 18:41:46 +00:00
$comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid));
2004-10-15 11:13:11 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2003-12-24 13:13:07 +00:00
2004-07-10 07:58:47 +00:00
$output = '';
2003-12-24 13:13:07 +00:00
2007-05-14 13:43:38 +00:00
if (is_object($comment) && is_numeric($comment->cid)) {
$output = drupal_get_form('comment_confirm_delete', $comment);
2001-12-30 16:16:38 +00:00
}
else {
2004-08-16 18:02:48 +00:00
drupal_set_message(t('The comment no longer exists.'));
2001-12-30 16:16:38 +00:00
}
2004-07-10 07:58:47 +00:00
2005-04-24 16:34:36 +00:00
return $output;
2001-08-03 18:39:17 +00:00
}
2007-05-14 13:43:38 +00:00
function comment_confirm_delete($comment) {
$form = array();
$form['#comment'] = $comment;
2006-08-18 18:58:47 +00:00
return confirm_form(
2007-05-14 13:43:38 +00:00
$form,
t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
'node/'. $comment->nid,
2006-08-18 18:58:47 +00:00
t('Any replies to this comment will be lost. This action cannot be undone.'),
t('Delete'),
2007-05-14 13:43:38 +00:00
t('Cancel'),
'comment_confirm_delete');
}
2007-05-28 06:08:47 +00:00
function comment_confirm_delete_submit($form, &$form_state, $form_values) {
2007-05-14 13:43:38 +00:00
drupal_set_message(t('The comment and all its replies have been deleted.'));
$comment = $form['#comment'];
// Delete comment and its replies.
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
// Clear the cache so an anonymous user sees that his comment was deleted.
cache_clear_all();
$form_state['redirect'] = "node/$comment->nid";
return;
2006-08-18 18:58:47 +00:00
}
2005-12-07 05:35:02 +00:00
/**
2006-05-07 00:08:36 +00:00
* Comment operations. We offer different update operations depending on
2005-12-07 05:35:02 +00:00
* which comment administration page we're on.
*/
function comment_operations($action = NULL) {
if ($action == 'publish') {
$operations = array(
2005-12-08 09:11:32 +00:00
'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
2005-12-07 05:35:02 +00:00
'delete' => array(t('Delete the selected comments'), '')
);
}
else if ($action == 'unpublish') {
$operations = array(
2005-12-08 09:11:32 +00:00
'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
2005-12-07 05:35:02 +00:00
'delete' => array(t('Delete the selected comments'), '')
);
}
else {
$operations = array(
2005-12-08 09:11:32 +00:00
'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
2005-12-07 05:35:02 +00:00
'delete' => array(t('Delete the selected comments'), '')
);
}
return $operations;
}
2004-07-10 18:11:18 +00:00
/**
* Menu callback; present an administrative comment listing.
*/
2006-09-17 19:09:34 +00:00
function comment_admin($type = 'new') {
2006-08-29 09:12:03 +00:00
$edit = $_POST;
2005-12-07 05:35:02 +00:00
2007-01-31 15:49:26 +00:00
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && $edit['comments']) {
2006-09-17 19:09:34 +00:00
return drupal_get_form('comment_multiple_delete_confirm');
}
else {
return drupal_get_form('comment_admin_overview', $type, arg(4));
2005-12-07 05:35:02 +00:00
}
2006-09-17 19:09:34 +00:00
}
2005-12-07 05:35:02 +00:00
2006-09-17 19:09:34 +00:00
function comment_admin_overview($type = 'new', $arg) {
2005-12-07 05:35:02 +00:00
// build an 'Update options' form
$form['options'] = array(
'#type' => 'fieldset', '#title' => t('Update options'),
'#prefix' => '<div class="container-inline">', '#suffix' => '</div>'
2003-08-21 15:47:50 +00:00
);
2005-12-07 05:35:02 +00:00
$options = array();
2006-09-17 19:09:34 +00:00
foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
2005-12-07 05:35:02 +00:00
$options[$key] = $value[0];
}
$form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'publish');
$form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
2003-08-21 15:47:50 +00:00
2005-12-07 05:35:02 +00:00
// load the comments that we want to display
2005-05-05 20:12:49 +00:00
$status = ($type == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
2006-01-08 00:29:37 +00:00
$form['header'] = array('#type' => 'value', '#value' => array(
2006-11-21 08:16:39 +00:00
theme('table_select_header_cell'),
2006-01-08 00:29:37 +00:00
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Author'), 'field' => 'name'),
array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
array('data' => t('Operations'))
));
$result = pager_query('SELECT c.subject, c.nid, c.cid, c.comment, c.timestamp, c.status, c.name, c.homepage, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.status = %d'. tablesort_sql($form['header']['#value']), 50, 0, NULL, $status);
2002-09-15 13:00:12 +00:00
2005-12-07 05:35:02 +00:00
// build a table listing the appropriate comments
$destination = drupal_get_destination();
2002-09-15 13:00:12 +00:00
while ($comment = db_fetch_object($result)) {
2005-12-07 05:35:02 +00:00
$comments[$comment->cid] = '';
2006-02-27 14:54:24 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2007-02-15 11:40:19 +00:00
$form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('title' => truncate_utf8($comment->comment, 128), 'fragment' => 'comment-'. $comment->cid)));
2005-12-07 05:35:02 +00:00
$form['username'][$comment->cid] = array('#value' => theme('username', $comment));
$form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
2007-02-15 11:40:19 +00:00
$form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array('query' => $destination)));
2005-12-07 05:35:02 +00:00
}
2007-01-31 15:49:26 +00:00
$form['comments'] = array('#type' => 'checkboxes', '#options' => isset($comments) ? $comments: array());
2006-04-13 08:25:27 +00:00
$form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
2006-08-18 18:58:47 +00:00
return $form;
2005-12-07 05:35:02 +00:00
}
/**
* We can't execute any 'Update options' if no comments were selected.
*/
2007-05-28 06:08:47 +00:00
function comment_admin_overview_validate($form, &$form_state, $form_values) {
2006-09-08 16:33:02 +00:00
$form_values['comments'] = array_diff($form_values['comments'], array(0));
if (count($form_values['comments']) == 0) {
2005-12-07 05:35:02 +00:00
form_set_error('', t('Please select one or more comments to perform the update on.'));
2006-07-31 11:25:55 +00:00
drupal_goto('admin/content/comment');
2005-12-07 05:35:02 +00:00
}
}
/**
* Execute the chosen 'Update option' on the selected comments, such as
* publishing, unpublishing or deleting.
*/
2007-05-28 06:08:47 +00:00
function comment_admin_overview_submit($form, &$form_state, $form_values) {
2005-12-07 05:35:02 +00:00
$operations = comment_operations();
2006-09-08 16:33:02 +00:00
if ($operations[$form_values['operation']][1]) {
2005-12-07 05:35:02 +00:00
// extract the appropriate database query operation
2006-09-08 16:33:02 +00:00
$query = $operations[$form_values['operation']][1];
foreach ($form_values['comments'] as $cid => $value) {
2005-12-07 05:35:02 +00:00
if ($value) {
// perform the update action, then refresh node statistics
db_query($query, $cid);
$comment = _comment_load($cid);
_comment_update_node_statistics($comment->nid);
2006-03-06 20:52:55 +00:00
// Allow modules to respond to the updating of a comment.
2006-09-08 16:33:02 +00:00
comment_invoke_comment($comment, $form_values['operation']);
2006-03-06 20:52:55 +00:00
// Add an entry to the watchdog log.
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)));
2005-12-07 05:35:02 +00:00
}
}
cache_clear_all();
drupal_set_message(t('The update has been performed.'));
2006-07-31 11:25:55 +00:00
drupal_goto('admin/content/comment');
2002-09-15 13:00:12 +00:00
}
2005-12-07 05:35:02 +00:00
}
2002-09-15 13:00:12 +00:00
2005-12-07 05:35:02 +00:00
function theme_comment_admin_overview($form) {
2006-08-10 15:42:33 +00:00
$output = drupal_render($form['options']);
2005-12-14 20:10:45 +00:00
if (isset($form['subject']) && is_array($form['subject'])) {
2005-12-07 05:35:02 +00:00
foreach (element_children($form['subject']) as $key) {
$row = array();
2006-08-10 15:42:33 +00:00
$row[] = drupal_render($form['comments'][$key]);
$row[] = drupal_render($form['subject'][$key]);
$row[] = drupal_render($form['username'][$key]);
$row[] = drupal_render($form['timestamp'][$key]);
$row[] = drupal_render($form['operations'][$key]);
2005-12-07 05:35:02 +00:00
$rows[] = $row;
}
}
else {
2004-11-15 11:16:39 +00:00
$rows[] = array(array('data' => t('No comments available.'), 'colspan' => '6'));
2002-09-15 13:00:12 +00:00
}
2006-01-08 00:29:37 +00:00
$output .= theme('table', $form['header']['#value'], $rows);
2005-12-07 05:35:02 +00:00
if ($form['pager']['#value']) {
2006-08-10 15:42:33 +00:00
$output .= drupal_render($form['pager']);
2005-12-07 05:35:02 +00:00
}
2006-08-10 15:42:33 +00:00
$output .= drupal_render($form);
2005-12-07 05:35:02 +00:00
2005-09-27 18:10:19 +00:00
return $output;
2002-09-15 13:00:12 +00:00
}
2005-12-07 05:35:02 +00:00
/**
* List the selected comments and verify that the admin really wants to delete
* them.
*/
function comment_multiple_delete_confirm() {
2006-08-29 09:12:03 +00:00
$edit = $_POST;
2005-12-07 05:35:02 +00:00
$form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
// array_filter() returns only elements with actual values
2006-07-02 02:59:21 +00:00
$comment_counter = 0;
2005-12-07 05:35:02 +00:00
foreach (array_filter($edit['comments']) as $cid => $value) {
2006-07-02 02:59:21 +00:00
$comment = _comment_load($cid);
2006-07-10 18:00:37 +00:00
if (is_object($comment) && is_numeric($comment->cid)) {
2006-07-02 02:59:21 +00:00
$subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid));
$form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) .'</li>');
$comment_counter++;
}
2005-12-07 05:35:02 +00:00
}
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
2006-07-02 02:59:21 +00:00
if (!$comment_counter) {
drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.'));
2006-07-31 11:25:55 +00:00
drupal_goto('admin/content/comment');
2006-07-02 02:59:21 +00:00
}
else {
2006-08-18 18:58:47 +00:00
return confirm_form($form,
2006-07-02 02:59:21 +00:00
t('Are you sure you want to delete these comments and all their children?'),
2006-07-31 11:25:55 +00:00
'admin/content/comment', t('This action cannot be undone.'),
2006-07-02 02:59:21 +00:00
t('Delete comments'), t('Cancel'));
}
2005-12-07 05:35:02 +00:00
}
/**
* Perform the actual comment deletion.
*/
2007-05-28 06:08:47 +00:00
function comment_multiple_delete_confirm_submit($form, &$form_state, $form_values) {
2006-09-17 19:09:34 +00:00
if ($form_values['confirm']) {
foreach ($form_values['comments'] as $cid => $value) {
2005-12-07 05:35:02 +00:00
$comment = _comment_load($cid);
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
}
2006-08-10 05:47:37 +00:00
cache_clear_all();
2005-12-07 05:35:02 +00:00
drupal_set_message(t('The comments have been deleted.'));
}
2006-07-31 11:25:55 +00:00
drupal_goto('admin/content/comment');
2005-12-07 05:35:02 +00:00
}
2004-07-08 19:58:10 +00:00
/**
- Patch #12232 by Steven/UnConed: search module improvements.
1) Clean up the text analyser: make it handle UTF-8 and all sorts of characters. The word splitter now does intelligent splitting into words and supports all Unicode characters. It has smart handling of acronyms, URLs, dates, ...
2) It now indexes the filtered output, which means it can take advantage of HTML tags. Meaningful tags (headers, strong, em, ...) are analysed and used to boost certain words scores. This has the side-effect of allowing the indexing of PHP nodes.
3) Link analyser for node links. The HTML analyser also checks for links. If they point to a node on the current site (handles path aliases) then the link's words are counted as part of the target node. This helps bring out commonly linked FAQs and answers to the top of the results.
4) Index comments along with the node. This means that the search can make a difference between a single node/comment about 'X' and a whole thread about 'X'. It also makes the search results much shorter and more relevant (before this patch, comments were even shown first).
5) We now keep track of total counts as well as a per item count for a word. This allows us to divide the word score by the total before adding up the scores for different words, and automatically makes noisewords have less influence than rare words. This dramatically improves the relevancy of multiword searches. This also makes the disadvantage of now using OR searching instead of AND searching less problematic.
6) Includes support for text preprocessors through a hook. This is required to index Chinese and Japanese, because these languages do not use spaces between words. An external utility can be used to split these into words through a simple wrapper module. Other uses could be spell checking (although it would have no UI).
7) Indexing is now regulated: only a certain amount of items will be indexed per cron run. This prevents PHP from running out of memory or timing out. This also makes the reindexing required for this patch automatic. I also added an index coverage estimate to the search admin screen.
8) Code cleanup! Moved all the search stuff from common.inc into search.module, rewired some hooks and simplified the functions used. The search form and results now also use valid XHTML and form_ functions. The search admin was moved from search/configure to admin/search for consistency.
9) Improved search output: we also show much more info per item: date, author, node type, amount of comments and a cool dynamic excerpt à la Google. The search form is now much more simpler and the help is only displayed as tips when no search results are found.
10) By moving all search logic to SQL, I was able to add a pager to the search results. This improves usability and performance dramatically.
2004-10-31 03:03:27 +00:00
*** misc functions: helpers, privates, history
2004-07-08 19:58:10 +00:00
**/
2005-12-07 05:35:02 +00:00
/**
* Load the entire comment by cid.
*/
function _comment_load($cid) {
return db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
}
2004-07-08 19:58:10 +00:00
function comment_num_all($nid) {
static $cache;
if (!isset($cache[$nid])) {
2004-09-14 05:48:02 +00:00
$cache[$nid] = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $nid));
2004-07-08 19:58:10 +00:00
}
return $cache[$nid];
}
function comment_num_replies($pid) {
static $cache;
if (!isset($cache[$pid])) {
2005-05-05 20:12:49 +00:00
$cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND status = %d', $pid, COMMENT_PUBLISHED));
2004-07-08 19:58:10 +00:00
}
return $cache[$pid];
}
/**
* get number of new comments for current user and specified node
*
* @param $nid node-id to count comments for
* @param $timestamp time to count from (defaults to time of last user access
* to node)
*/
function comment_num_new($nid, $timestamp = 0) {
global $user;
if ($user->uid) {
// Retrieve the timestamp at which the current user last viewed the
// specified node.
if (!$timestamp) {
$timestamp = node_last_viewed($nid);
}
2005-03-12 08:52:29 +00:00
$timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
2002-09-15 13:00:12 +00:00
2004-07-08 19:58:10 +00:00
// Use the timestamp to retrieve the number of new comments.
2005-05-05 20:12:49 +00:00
$result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d', $nid, $timestamp, COMMENT_PUBLISHED));
2002-09-15 13:00:12 +00:00
2004-07-08 19:58:10 +00:00
return $result;
2002-09-15 13:00:12 +00:00
}
else {
2004-07-08 19:58:10 +00:00
return 0;
2002-09-15 13:00:12 +00:00
}
}
2006-01-18 15:09:39 +00:00
function comment_validate($edit) {
global $user;
// Invoke other validation handlers
comment_invoke_comment($edit, 'validate');
2006-01-26 08:48:57 +00:00
if (isset($edit['date'])) {
// As of PHP 5.1.0, strtotime returns FALSE upon failure instead of -1.
if (strtotime($edit['date']) <= 0) {
form_set_error('date', t('You have to specify a valid date.'));
}
2006-01-18 15:09:39 +00:00
}
if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
form_set_error('author', t('You have to specify a valid author.'));
}
// Check validity of name, mail and homepage (if given)
if (!$user->uid || isset($edit['is_anonymous'])) {
if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
if ($edit['name']) {
$taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = '%s'", $edit['name']), 0);
if ($taken != 0) {
form_set_error('name', t('The name you used belongs to a registered user.'));
}
}
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
form_set_error('name', t('You have to leave your name.'));
}
if ($edit['mail']) {
if (!valid_email_address($edit['mail'])) {
form_set_error('mail', t('The e-mail address you specified is not valid.'));
}
}
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
form_set_error('mail', t('You have to leave an e-mail address.'));
}
if ($edit['homepage']) {
if (!valid_url($edit['homepage'], TRUE)) {
2006-05-07 00:08:36 +00:00
form_set_error('homepage', t('The URL of your homepage is not valid. Remember that it must be fully qualified, i.e. of the form <code>http://example.com/directory</code>.'));
2006-01-18 15:09:39 +00:00
}
}
}
}
return $edit;
}
2002-09-15 13:00:12 +00:00
/*
2005-10-07 06:11:12 +00:00
** Generate the basic commenting form, for appending to a node or display on a separate page.
** This is rendered by theme_comment_form.
2002-09-15 13:00:12 +00:00
*/
2005-10-07 06:11:12 +00:00
function comment_form($edit, $title = NULL) {
2004-01-03 12:32:21 +00:00
global $user;
2005-11-21 08:32:31 +00:00
$op = isset($_POST['op']) ? $_POST['op'] : '';
2007-05-17 20:57:19 +00:00
if (!$user->uid && variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
drupal_add_js(drupal_get_path('module', 'comment') . '/comment.js');
}
2004-05-18 18:41:46 +00:00
if ($user->uid) {
2007-01-31 15:49:26 +00:00
if (!empty($edit['cid']) && user_access('administer comments')) {
2007-03-06 16:14:04 +00:00
if (!empty($edit['author'])) {
2005-07-19 17:51:59 +00:00
$author = $edit['author'];
}
2007-03-06 16:14:04 +00:00
elseif (!empty($edit['name'])) {
2005-07-19 17:51:59 +00:00
$author = $edit['name'];
}
else {
$author = $edit['registered_name'];
}
2007-03-06 16:14:04 +00:00
if (!empty($edit['status'])) {
2005-10-07 06:11:12 +00:00
$status = $edit['status'];
}
else {
$status = 0;
}
2007-03-06 16:14:04 +00:00
if (!empty($edit['date'])) {
2005-10-07 06:11:12 +00:00
$date = $edit['date'];
}
else {
$date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
}
2006-01-18 15:09:39 +00:00
$form['admin'] = array(
'#type' => 'fieldset',
'#title' => t('Administration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -2,
);
if ($edit['registered_name'] != '') {
// The comment is by a registered user
$form['admin']['author'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#size' => 30,
'#maxlength' => 60,
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => $author,
'#weight' => -1,
);
}
else {
// The comment is by an anonymous user
$form['is_anonymous'] = array(
'#type' => 'value',
'#value' => TRUE,
);
$form['admin']['name'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#size' => 30,
'#maxlength' => 60,
'#default_value' => $author,
'#weight' => -1,
);
$form['admin']['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#default_value' => $edit['mail'],
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
);
2005-10-07 06:11:12 +00:00
2006-01-18 15:09:39 +00:00
$form['admin']['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
'#default_value' => $edit['homepage'],
);
}
2005-10-07 06:11:12 +00:00
2005-10-11 19:44:35 +00:00
$form['admin']['date'] = array('#type' => 'textfield', '#parents' => array('date'), '#title' => t('Authored on'), '#size' => 20, '#maxlength' => 25, '#default_value' => $date, '#weight' => -1);
2005-10-07 06:11:12 +00:00
2005-10-11 19:44:35 +00:00
$form['admin']['status'] = array('#type' => 'radios', '#parents' => array('status'), '#title' => t('Status'), '#default_value' => $status, '#options' => array(t('Published'), t('Not published')), '#weight' => -1);
2005-10-07 06:11:12 +00:00
2005-07-19 17:51:59 +00:00
}
else {
2006-01-18 15:09:39 +00:00
$form['_author'] = array('#type' => 'item', '#title' => t('Your name'), '#value' => theme('username', $user)
2005-10-07 06:11:12 +00:00
);
2006-01-18 15:09:39 +00:00
$form['author'] = array('#type' => 'value', '#value' => $user->name);
2005-07-19 17:51:59 +00:00
}
2004-05-18 18:41:46 +00:00
}
2005-11-13 09:05:38 +00:00
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
2006-11-24 09:01:57 +00:00
$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous'))
2005-10-07 06:11:12 +00:00
);
2005-10-11 19:44:35 +00:00
$form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.')
2005-10-07 06:11:12 +00:00
);
2005-10-11 19:44:35 +00:00
$form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
2004-05-18 18:41:46 +00:00
}
2005-11-13 09:05:38 +00:00
else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
2006-11-24 09:01:57 +00:00
$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')), '#required' => TRUE);
2005-10-07 06:11:12 +00:00
2007-01-23 19:11:18 +00:00
$form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);
2005-10-07 06:11:12 +00:00
2005-10-11 19:44:35 +00:00
$form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
2004-07-08 15:24:30 +00:00
}
2004-05-18 18:41:46 +00:00
2005-10-07 06:11:12 +00:00
if (variable_get('comment_subject_field', 1) == 1) {
2007-01-31 15:49:26 +00:00
$form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#maxlength' => 64, '#default_value' => !empty($edit['subject']) ? $edit['subject'] : '');
2004-05-18 18:41:46 +00:00
}
2004-01-03 12:32:21 +00:00
2007-03-27 05:13:55 +00:00
if (!empty($edit['comment'])) {
$default = $edit['comment'];
}
else {
$default = '';
}
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#required' => TRUE,
);
2006-09-05 02:36:02 +00:00
if (!isset($edit['format'])) {
$edit['format'] = FILTER_FORMAT_DEFAULT;
}
- Patch #45530 by Morbus: filter_form shouldn't default to #weight 0
When a form element doesn't specify a #weight, it is assumed internally as #weight 0. However, to ensure that our form elements display visually *as they were defined in the array* we, in form_builder, count the number of elements, divide by 1000, and set that as the weight:
# Assign a decimal placeholder weight to preserve original array order
if (!isset($form[$key]['#weight'])) {
$form[$key]['#weight'] = $count/1000;
}
The above code will set the #weights of elements that have not defined a weight to something like 0 (first element in array definition), 0.001, 0.002, and so on. However, anytime a form element *explicitly* defines a #weight of 0, that #weight is kept at exactly 0, which would cause that form element to appear BEFORE the elements that didn't have a #weight defined (and thus received a #weight such as 0.002).
Consider the following pseudo example:
$form['game_title'] = array(
'#type' => 'textfield',
...
);
$form['game_description'] = array(
'#type' => 'textarea',
...
);
$form['game_format'] = filter_form(variable_get('game_format', NULL));
return $form;
Here, we're not definiing weights on our two textfields. We then add an filter_form. The second parameter of the filter_form is $weight, which defaults to 0. After this $form hits form_builder, we have weights 0 (game_title), 0.001 (game_description), and 0 (filter_form) respectively. This is then sorted by weight, which causes filter_form (the third element in the array) to appear BEFORE game_description (0 is lighter than 0.001).
The short lesson is: explicitly defining #weight 0 for a form element is probably a bad idea. This patch changes the default #weight of filter_form to NULL, instead of 0, and also removes any other explicit setting of #weight to 0 in core.
2006-01-20 09:04:34 +00:00
$form['comment_filter']['format'] = filter_form($edit['format']);
2004-01-03 12:32:21 +00:00
2007-01-31 15:49:26 +00:00
$form['cid'] = array('#type' => 'value', '#value' => !empty($edit['cid']) ? $edit['cid'] : NULL);
2005-11-21 08:32:31 +00:00
$form['pid'] = array('#type' => 'value', '#value' => $edit['pid']);
$form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
2007-01-31 15:49:26 +00:00
$form['uid'] = array('#type' => 'value', '#value' => !empty($edit['uid']) ? $edit['uid'] : NULL);
2004-01-03 12:32:21 +00:00
2006-01-18 15:09:39 +00:00
$form['preview'] = array('#type' => 'button', '#value' => t('Preview comment'), '#weight' => 19);
2007-01-23 19:11:18 +00:00
$form['#token'] = 'comment'. $edit['nid'] . $edit['pid'];
2004-05-31 12:46:03 +00:00
2004-12-23 06:47:24 +00:00
// Only show post button if preview is optional or if we are in preview mode.
// We show the post button in preview mode even if there are form errors so that
// optional form elements (e.g., captcha) can be updated in preview mode.
2006-01-31 15:26:39 +00:00
if (!form_get_errors() && ((variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview comment')) || ($op == t('Post comment')))) {
2005-10-11 19:44:35 +00:00
$form['submit'] = array('#type' => 'submit', '#value' => t('Post comment'), '#weight' => 20);
2005-10-07 06:11:12 +00:00
}
2005-11-21 08:32:31 +00:00
if ($op == t('Preview comment')) {
2006-04-20 07:11:37 +00:00
$form['#after_build'] = array('comment_form_add_preview');
2005-11-21 08:32:31 +00:00
}
2007-01-31 15:49:26 +00:00
if (!empty($_REQUEST['destination'])) {
2005-10-11 19:44:35 +00:00
$form['#attributes']['destination'] = $_REQUEST['destination'];
2004-01-03 12:32:21 +00:00
}
2005-11-21 08:32:31 +00:00
if (empty($edit['cid']) && empty($edit['pid'])) {
$form['#action'] = url('comment/reply/'. $edit['nid']);
}
2004-01-03 12:32:21 +00:00
2005-10-07 06:11:12 +00:00
// Graft in extra form additions
$form = array_merge($form, comment_invoke_comment($form, 'form'));
2006-08-18 18:58:47 +00:00
return $form;
}
2005-10-07 06:11:12 +00:00
2006-08-18 18:58:47 +00:00
function comment_form_box($edit, $title = NULL) {
return theme('box', $title, drupal_get_form('comment_form', $edit, $title));
2005-10-07 06:11:12 +00:00
}
2007-05-14 13:43:38 +00:00
function comment_form_add_preview($form, $edit, &$form_state) {
2005-11-21 08:32:31 +00:00
global $user;
2006-01-18 15:09:39 +00:00
drupal_set_title(t('Preview comment'));
2005-11-21 08:32:31 +00:00
$output = '';
2007-03-01 19:53:04 +00:00
// Invoke full validation for the form, to protect against cross site
// request forgeries (CSRF) and setting arbitrary values for fields such as
// the input format. Preview the comment only when form validation does not
// set any errors.
2007-05-14 13:43:38 +00:00
drupal_validate_form($form['form_id']['#value'], $form, $form_state);
2005-11-21 08:32:31 +00:00
if (!form_get_errors()) {
2007-03-01 19:53:04 +00:00
$comment = (object)_comment_form_submit($edit);
// Attach the user and time information.
if ($edit['author']) {
$account = user_load(array('name' => $edit['author']));
}
elseif ($user->uid && !isset($edit['is_anonymous'])) {
$account = $user;
}
if ($account) {
$comment->uid = $account->uid;
$comment->name = check_plain($account->name);
2007-03-30 07:45:20 +00:00
}
else {
$comment->name = variable_get('anonymous', t('Anonymous'));
2007-03-01 19:53:04 +00:00
}
2007-03-06 16:14:04 +00:00
$comment->timestamp = !empty($edit['timestamp']) ? $edit['timestamp'] : time();
2005-11-21 08:32:31 +00:00
$output .= theme('comment_view', $comment);
}
2006-04-15 04:07:18 +00:00
$form['comment_preview'] = array(
'#value' => $output,
'#weight' => -100,
'#prefix' => '<div class="preview">',
'#suffix' => '</div>',
);
2005-11-21 08:32:31 +00:00
$output = '';
if ($edit['pid']) {
2007-03-30 07:45:20 +00:00
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
2005-11-21 08:32:31 +00:00
$comment = drupal_unpack($comment);
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$output .= theme('comment_view', $comment);
}
else {
2006-07-02 20:24:17 +00:00
$form['#suffix'] = node_view(node_load($edit['nid']));
2005-11-21 08:32:31 +00:00
$edit['pid'] = 0;
}
$form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);
return $form;
}
2007-05-28 06:08:47 +00:00
function comment_form_validate($form, &$form_state, $form_values) {
2007-05-29 06:27:37 +00:00
global $user;
if ($user->uid === 0) {
foreach (array('name', 'homepage', 'mail') as $field) {
//set cookie for 365 days
setcookie('comment_info_'. $field, $form_values[$field], time() + 31536000);
}
2007-05-17 20:57:19 +00:00
}
2005-11-21 08:32:31 +00:00
comment_validate($form_values);
}
2007-05-28 06:08:47 +00:00
function _comment_form_submit(&$form_values) {
2006-01-18 15:09:39 +00:00
if (!isset($form_values['date'])) {
$form_values['date'] = 'now';
}
$form_values['timestamp'] = strtotime($form_values['date']);
if (isset($form_values['author'])) {
$account = user_load(array('name' => $form_values['author']));
$form_values['uid'] = $account->uid;
$form_values['name'] = $form_values['author'];
}
2006-05-07 00:08:36 +00:00
// Validate the comment's subject. If not specified, extract
2006-01-18 15:09:39 +00:00
// one from the comment's body.
if (trim($form_values['subject']) == '') {
// The body may be in any format, so we:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
2006-12-12 06:06:41 +00:00
// Note: format is checked by check_markup().
$form_values['subject'] = trim(truncate_utf8(decode_entities(strip_tags(check_markup($form_values['comment'], $form_values['format']))), 29, TRUE));
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
if ($form_values['subject'] == '') {
$form_values['subject'] = t('(No subject)');
}
2005-11-21 08:32:31 +00:00
}
2006-01-18 15:09:39 +00:00
}
2007-05-28 06:08:47 +00:00
function comment_form_submit($form, &$form_state, $form_values) {
_comment_form_submit($form_state['values']);
2006-01-18 15:09:39 +00:00
if ($cid = comment_save($form_values)) {
2007-05-14 13:43:38 +00:00
$form_state['redirect'] = array('node/'. $form_values['nid'], NULL, "comment-$cid");
return;
2005-11-21 08:32:31 +00:00
}
}
2005-10-07 06:11:12 +00:00
/*
** Renderer or visualization functions this can be optionally
** overridden by themes.
*/
2005-11-21 08:32:31 +00:00
function theme_comment_preview($comment, $links = array(), $visible = 1) {
2005-05-22 21:14:59 +00:00
$output = '<div class="preview">';
$output .= theme('comment_view', $comment, $links, $visible);
$output .= '</div>';
return $output;
};
2005-11-21 08:32:31 +00:00
function theme_comment_view($comment, $links = array(), $visible = 1) {
2006-12-30 21:20:38 +00:00
static $first_new = TRUE;
2004-05-20 10:23:52 +00:00
2004-06-04 18:50:29 +00:00
$output = '';
2006-12-30 21:20:38 +00:00
$comment->new = node_mark($comment->nid, $comment->timestamp);
if ($first_new && $comment->new != MARK_READ) {
// Assign the anchor only for the first new comment. This avoids duplicate
// id attributes on a page.
$first_new = FALSE;
2003-11-15 10:03:31 +00:00
$output .= "<a id=\"new\"></a>\n";
}
2004-02-07 10:57:27 +00:00
$output .= "<a id=\"comment-$comment->cid\"></a>\n";
2003-11-15 10:03:31 +00:00
2004-06-04 18:50:29 +00:00
// Switch to folded/unfolded view of the comment
2003-11-15 10:03:31 +00:00
if ($visible) {
2005-07-29 21:06:33 +00:00
$comment->comment = check_markup($comment->comment, $comment->format, FALSE);
2005-10-07 06:11:12 +00:00
// Comment API hook
comment_invoke_comment($comment, 'view');
2004-05-31 12:46:03 +00:00
$output .= theme('comment', $comment, $links);
2003-11-15 10:03:31 +00:00
}
else {
2004-06-04 18:50:29 +00:00
$output .= theme('comment_folded', $comment);
2003-11-15 10:03:31 +00:00
}
2004-05-20 10:23:52 +00:00
2003-11-15 10:03:31 +00:00
return $output;
}
2005-11-22 15:48:46 +00:00
function comment_controls($mode = COMMENT_MODE_THREADED_EXPANDED, $order = COMMENT_ORDER_NEWEST_FIRST, $comments_per_page = 50) {
$form['mode'] = array('#type' => 'select',
'#default_value' => $mode,
'#options' => _comment_get_modes(),
'#weight' => 1,
);
$form['order'] = array(
'#type' => 'select',
'#default_value' => $order,
'#options' => _comment_get_orders(),
'#weight' => 2,
);
2005-10-07 06:11:12 +00:00
foreach (_comment_per_page() as $i) {
2006-11-27 02:14:33 +00:00
$options[$i] = t('!a comments per page', array('!a' => $i));
2005-10-07 06:11:12 +00:00
}
2005-11-22 15:48:46 +00:00
$form['comments_per_page'] = array('#type' => 'select',
'#default_value' => $comments_per_page,
'#options' => $options,
'#weight' => 3,
);
2002-09-15 13:00:12 +00:00
2005-11-22 15:48:46 +00:00
$form['submit'] = array('#type' => 'submit',
'#value' => t('Save settings'),
'#weight' => 20,
);
2002-09-15 13:00:12 +00:00
2006-08-18 18:58:47 +00:00
return $form;
2002-09-15 13:00:12 +00:00
}
2005-10-07 06:11:12 +00:00
function theme_comment_controls($form) {
$output .= '<div class="container-inline">';
2006-08-10 15:42:33 +00:00
$output .= drupal_render($form);
2005-10-07 06:11:12 +00:00
$output .= '</div>';
2005-11-22 15:48:46 +00:00
$output .= '<div class="description">'. t('Select your preferred way to display the comments and click "Save settings" to activate your changes.') .'</div>';
2005-10-07 06:11:12 +00:00
return theme('box', t('Comment viewing options'), $output);
2002-09-15 13:00:12 +00:00
}
2007-05-28 06:08:47 +00:00
function comment_controls_submit($form, &$form_state, $form_values) {
2005-11-22 15:48:46 +00:00
global $user;
$mode = $form_values['mode'];
$order = $form_values['order'];
$comments_per_page = $form_values['comments_per_page'];
if ($user->uid) {
$user = user_save($user, array('mode' => $mode, 'sort' => $order, 'comments_per_page' => $comments_per_page));
}
else {
$_SESSION['comment_mode'] = $mode;
$_SESSION['comment_sort'] = $order;
$_SESSION['comment_comments_per_page'] = $comments_per_page;
}
}
2007-04-27 07:42:54 +00:00
/**
* Prepare values for comment.tpl.php
*/
function template_preprocess_comment(&$variables) {
$comment = $variables['comment'];
$variables['author'] = theme('username', $comment);
$variables['comment'] = $comment;
$variables['content'] = $comment->comment;
$variables['date'] = format_date($comment->timestamp);
$variables['links'] = isset($variables['links']) ? theme('links', $variables['links']) : '';
$variables['new'] = $comment->new ? t('new') : '';
$variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '';
$variables['signature'] = $comment->signature;
$variables['submitted'] = t('Submitted by !a on @b.',
array('!a' => theme('username', $comment),
'@b' => format_date($comment->timestamp)));
$variables['title'] = l($comment->subject, $_GET['q'], array('fragment' => "comment-$comment->cid"));
2002-09-15 13:00:12 +00:00
}
2003-11-09 23:27:22 +00:00
function theme_comment_folded($comment) {
2004-03-12 08:09:09 +00:00
$output = "<div class=\"comment-folded\">\n";
2007-04-13 08:56:59 +00:00
$output .= ' <span class="subject">'. l($comment->subject, comment_node_url() .'/'. $comment->cid, array('fragment' => "comment-$comment->cid")) .' '. theme('mark', $comment->new) .'</span> ';
2005-08-01 05:14:05 +00:00
$output .= '<span class="credit">'. t('by') .' '. theme('username', $comment) ."</span>\n";
2004-03-12 08:09:09 +00:00
$output .= "</div>\n";
return $output;
2002-09-15 13:00:12 +00:00
}
2005-10-07 06:11:12 +00:00
function theme_comment_flat_collapsed($comment) {
return theme('comment_view', $comment, '', 0);
2002-09-15 13:00:12 +00:00
}
2005-10-07 06:11:12 +00:00
function theme_comment_flat_expanded($comment) {
2005-10-20 09:27:36 +00:00
return theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 0));
2002-09-15 13:00:12 +00:00
}
2005-11-27 11:07:02 +00:00
function theme_comment_thread_collapsed($comment) {
2005-10-07 06:11:12 +00:00
$output .= theme('comment_view', $comment, '', 0);
2003-11-15 10:03:31 +00:00
return $output;
2002-09-15 13:00:12 +00:00
}
2005-11-27 11:07:02 +00:00
function theme_comment_thread_expanded($comment) {
2004-06-04 18:50:29 +00:00
$output = '';
2005-10-20 09:27:36 +00:00
$output .= theme('comment_view', $comment, module_invoke_all('link', 'comment', $comment, 0));
2003-11-15 10:03:31 +00:00
return $output;
2002-09-15 13:00:12 +00:00
}
2006-01-26 08:27:58 +00:00
function theme_comment_post_forbidden($nid) {
2002-09-15 13:00:12 +00:00
global $user;
if ($user->uid) {
2003-12-24 13:13:07 +00:00
return t("you can't post comments");
2002-09-15 13:00:12 +00:00
}
else {
2006-01-26 08:27:58 +00:00
// we cannot use drupal_get_destination() because these links sometimes appear on /node and taxo listing pages
if (variable_get('comment_form_location', COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
2007-03-12 13:08:02 +00:00
$destination = "destination=". drupal_urlencode("comment/reply/$nid#comment-form");
2006-01-26 08:27:58 +00:00
}
else {
2007-03-12 13:08:02 +00:00
$destination = "destination=". drupal_urlencode("node/$nid#comment-form");
2006-01-26 08:27:58 +00:00
}
2004-07-14 05:44:36 +00:00
if (variable_get('user_register', 1)) {
2007-02-15 11:40:19 +00:00
return t('<a href="@login">Login</a> or <a href="@register">register</a> to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination))));
2004-07-14 05:44:36 +00:00
}
else {
2007-02-15 11:40:19 +00:00
return t('<a href="@login">Login</a> to post comments', array('@login' => url('user/login', array('query' => $destination))));
2004-07-14 05:44:36 +00:00
}
2002-09-15 13:00:12 +00:00
}
}
2006-07-19 06:19:17 +00:00
/**
* Allow themable wrapping of all comments.
*/
function theme_comment_wrapper($content) {
2007-01-23 19:11:18 +00:00
return '<div id="comments">'. $content .'</div>';
2006-07-19 06:19:17 +00:00
}
2004-07-08 19:58:10 +00:00
function _comment_delete_thread($comment) {
2006-07-10 18:00:37 +00:00
if (!is_object($comment) || !is_numeric($comment->cid)) {
2007-04-24 18:23:48 +00:00
watchdog('content', 'Can not delete non-existent comment.', WATCHDOG_WARNING);
2006-07-02 02:59:21 +00:00
return;
}
2004-07-08 19:58:10 +00:00
// Delete the comment:
db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid);
2007-04-24 18:23:48 +00:00
watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject));
2002-03-05 20:15:17 +00:00
2005-10-07 06:11:12 +00:00
comment_invoke_comment($comment, 'delete');
2004-09-14 05:48:02 +00:00
2005-10-07 06:11:12 +00:00
// Delete the comment's replies
2004-09-14 05:48:02 +00:00
$result = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE pid = %d', $comment->cid);
2004-07-08 19:58:10 +00:00
while ($comment = db_fetch_object($result)) {
2004-10-15 11:13:11 +00:00
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
2004-07-08 19:58:10 +00:00
_comment_delete_thread($comment);
2003-02-16 14:57:35 +00:00
}
}
2004-06-04 18:50:29 +00:00
/**
* Return an array of viewing modes for comment listings.
*
* We can't use a global variable array because the locale system
* is not initialized yet when the comment module is loaded.
*/
2003-10-03 15:14:15 +00:00
function _comment_get_modes() {
2005-11-13 09:05:38 +00:00
return array(
COMMENT_MODE_FLAT_COLLAPSED => t('Flat list - collapsed'),
COMMENT_MODE_FLAT_EXPANDED => t('Flat list - expanded'),
COMMENT_MODE_THREADED_COLLAPSED => t('Threaded list - collapsed'),
COMMENT_MODE_THREADED_EXPANDED => t('Threaded list - expanded')
);
2003-10-03 15:14:15 +00:00
}
2004-06-04 18:50:29 +00:00
/**
* Return an array of viewing orders for comment listings.
*
* We can't use a global variable array because the locale system
* is not initialized yet when the comment module is loaded.
*/
2003-10-03 15:14:15 +00:00
function _comment_get_orders() {
2005-11-13 09:05:38 +00:00
return array(
COMMENT_ORDER_NEWEST_FIRST => t('Date - newest first'),
COMMENT_ORDER_OLDEST_FIRST => t('Date - oldest first')
);
2003-10-03 15:14:15 +00:00
}
2004-06-04 18:50:29 +00:00
/**
* Return an array of "comments per page" settings from which the user
* can choose.
*/
2004-01-17 10:32:52 +00:00
function _comment_per_page() {
2005-09-29 08:04:51 +00:00
return drupal_map_assoc(array(10, 30, 50, 70, 90, 150, 200, 250, 300));
2004-01-17 10:32:52 +00:00
}
2006-04-27 22:03:20 +00:00
/**
* Return a current comment display setting
*
* $setting can be one of these: 'mode', 'sort', 'comments_per_page'
*/
function _comment_get_display_setting($setting) {
global $user;
2006-05-07 00:08:36 +00:00
2006-09-09 00:23:11 +00:00
if (isset($_GET[$setting])) {
2006-04-27 22:03:20 +00:00
$value = $_GET[$setting];
}
else {
// get the setting's site default
switch ($setting) {
case 'mode':
$default = variable_get('comment_default_mode', COMMENT_MODE_THREADED_EXPANDED);
break;
case 'sort':
$default = variable_get('comment_default_order', COMMENT_ORDER_NEWEST_FIRST);
break;
case 'comments_per_page':
$default = variable_get('comment_default_per_page', '50');
}
if (variable_get('comment_controls', COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_HIDDEN) {
// if comment controls are disabled use site default
$value = $default;
}
else {
// otherwise use the user's setting if set
if ($user->$setting) {
$value = $user->$setting;
}
else if ($_SESSION['comment_'. $setting]) {
$value = $_SESSION['comment_'. $setting];
}
else {
$value = $default;
}
}
}
return $value;
}
2004-09-14 05:48:02 +00:00
/**
2006-05-07 00:08:36 +00:00
* Updates the comment statistics for a given node. This should be called any
2004-09-14 05:48:02 +00:00
* time a comment is added, deleted, or updated.
*
* The following fields are contained in the node_comment_statistics table.
* - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
* - last_comment_name: the name of the anonymous poster for the last comment
* - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
2004-10-17 19:09:09 +00:00
* - comment_count: the total number of approved/published comments on this node.
2004-09-14 05:48:02 +00:00
*/
function _comment_update_node_statistics($nid) {
2005-05-05 20:12:49 +00:00
$count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = %d', $nid, COMMENT_PUBLISHED));
2004-09-14 05:48:02 +00:00
2005-02-11 19:01:53 +00:00
// comments exist
if ($count > 0) {
2005-05-05 20:12:49 +00:00
$last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
2006-01-22 07:26:33 +00:00
db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
2005-02-11 19:01:53 +00:00
}
// no comments
else {
2006-02-01 14:04:50 +00:00
$node = db_fetch_object(db_query("SELECT uid, created FROM {node} WHERE nid = %d", $nid));
db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
2005-02-11 19:01:53 +00:00
}
}
2005-08-25 21:14:17 +00:00
2005-10-07 06:11:12 +00:00
/**
* Invoke a hook_comment() operation in all modules.
*
* @param &$comment
* A comment object.
* @param $op
* A string containing the name of the comment operation.
* @return
* The returned value of the invoked hooks.
*/
function comment_invoke_comment(&$comment, $op) {
$return = array();
foreach (module_implements('comment') as $name) {
$function = $name .'_comment';
$result = $function($comment, $op);
2005-12-14 20:10:45 +00:00
if (isset($result) && is_array($result)) {
2005-10-07 06:11:12 +00:00
$return = array_merge($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
2006-02-09 08:33:36 +00:00
2007-04-06 04:39:51 +00:00
/**
* Generate vancode.
*
* Consists of a leading character indicating length, followed by N digits
* with a numerical value in base 36. Vancodes can be sorted as strings
* without messing up numerical order.
*
* It goes:
* 00, 01, 02, ..., 0y, 0z,
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*/
function int2vancode($i = 0) {
$num = base_convert((int)$i, 10, 36);
$length = strlen($num);
return chr($length + ord('0') - 1) . $num;
}
2006-02-09 08:33:36 +00:00
/**
* Decode vancode back to an integer.
*/
function vancode2int($c = '00') {
return base_convert(substr($c, 1), 36, 10);
}