2002-02-16 13:38:00 +00:00
<?php
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
*
2012-01-03 04:36:15 +00:00
* When enabled, the Comment module creates a discussion board for each Drupal
* node. Users can post comments to discuss a forum topic, story, collaborative
* book page, etc.
2004-07-10 07:58:47 +00:00
*/
2006-12-12 06:14:21 +00:00
/**
2008-08-02 20:29:43 +00:00
* Comment is awaiting approval.
2005-05-05 20:12:49 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_NOT_PUBLISHED = 0;
2006-12-12 06:14:21 +00:00
/**
2008-08-02 20:29:43 +00:00
* Comment is published.
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_PUBLISHED = 1;
2005-11-13 09:05:38 +00:00
2006-12-12 06:14:21 +00:00
/**
* Comments are displayed in a flat list - expanded.
*/
2011-11-29 09:56:53 +00:00
const COMMENT_MODE_FLAT = 0;
2006-12-12 06:14:21 +00:00
/**
* Comments are displayed as a threaded list - expanded.
*/
2011-11-29 09:56:53 +00:00
const COMMENT_MODE_THREADED = 1;
2005-11-13 09:05:38 +00:00
/**
2008-05-14 13:12:41 +00:00
* Anonymous posters cannot enter their contact information.
2005-11-13 09:05:38 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
2006-12-12 06:14:21 +00:00
/**
* Anonymous posters may leave their contact information.
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MAY_CONTACT = 1;
2006-12-12 06:14:21 +00:00
/**
2008-05-14 13:12:41 +00:00
* Anonymous posters are required to leave their contact information.
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
2005-11-13 09:05:38 +00:00
/**
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
*/
2011-11-29 09:56:53 +00:00
const COMMENT_FORM_SEPARATE_PAGE = 0;
2006-12-12 06:14:21 +00:00
/**
* Comment form should be shown below post or list of comments.
*/
2011-11-29 09:56:53 +00:00
const COMMENT_FORM_BELOW = 1;
2005-11-13 09:05:38 +00:00
/**
2009-03-17 12:41:54 +00:00
* Comments for this node are hidden.
2005-11-13 09:05:38 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_NODE_HIDDEN = 0;
2006-12-12 06:14:21 +00:00
/**
2009-03-17 12:41:54 +00:00
* Comments for this node are closed.
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_NODE_CLOSED = 1;
2006-12-12 06:14:21 +00:00
/**
2009-03-17 12:41:54 +00:00
* Comments for this node are open.
2006-12-12 06:14:21 +00:00
*/
2011-11-29 09:56:53 +00:00
const COMMENT_NODE_OPEN = 2;
2005-05-05 20:12:49 +00:00
2004-06-04 18:50:29 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-06-04 18:50:29 +00:00
*/
2007-06-30 19:46:58 +00:00
function comment_help($path, $arg) {
switch ($path) {
2003-10-09 18:53:22 +00:00
case 'admin/help#comment':
2009-11-26 06:44:50 +00:00
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the online handbook entry for <a href="@comment">Comment module</a>.', array('@comment' => 'http://drupal.org/handbook/modules/comment/')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Default and custom settings') . '</dt>';
2010-01-13 14:25:59 +00:00
$output .= '<dd>' . t("Each <a href='@content-type'>content type</a> can have its own default comment settings configured as: <em>Open</em> to allow new comments, <em>Hidden</em> to hide existing comments and prevent new comments, or <em>Closed</em> to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('@content-type' => url('admin/structure/types'))) . '</dd>';
2010-10-05 06:17:29 +00:00
$output .= '<dt>' . t('Comment approval') . '</dt>';
$output .= '<dd>' . t("Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href='@comment-approval'>Unapproved comments</a> queue, until a user who has permission to <em>Administer comments</em> publishes or deletes them. Published comments can be bulk managed on the <a href='@admin-comment'>Published comments</a> administration page.", array('@comment-approval' => url('admin/content/comment/approval'), '@admin-comment' => url('admin/content/comment'))) . '</dd>';
2009-11-26 06:44:50 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2007-07-05 08:48:58 +00:00
}
2002-02-22 19:44:22 +00:00
}
2009-08-25 21:53:48 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_entity_info().
2009-08-25 21:53:48 +00:00
*/
function comment_entity_info() {
2011-09-16 18:58:27 +00:00
$return = array(
2009-08-25 21:53:48 +00:00
'comment' => array(
'label' => t('Comment'),
'base table' => 'comment',
2010-02-11 15:52:13 +00:00
'uri callback' => 'comment_uri',
2009-08-25 21:53:48 +00:00
'fieldable' => TRUE,
2011-12-05 12:12:15 +00:00
'controller class' => 'CommentStorageController',
'entity class' => 'Comment',
2010-03-27 05:52:50 +00:00
'entity keys' => array(
2009-08-25 21:53:48 +00:00
'id' => 'cid',
'bundle' => 'node_type',
2010-09-11 06:03:12 +00:00
'label' => 'subject',
2009-08-25 21:53:48 +00:00
),
'bundles' => array(),
2009-12-26 16:50:09 +00:00
'view modes' => array(
'full' => array(
'label' => t('Full comment'),
2010-05-23 19:10:23 +00:00
'custom settings' => FALSE,
2009-12-26 16:50:09 +00:00
),
),
2009-08-25 21:53:48 +00:00
'static cache' => FALSE,
),
);
foreach (node_type_get_names() as $type => $name) {
$return['comment']['bundles']['comment_node_' . $type] = array(
2010-03-03 07:57:27 +00:00
'label' => t('@node_type comment', array('@node_type' => $name)),
2011-09-28 06:39:49 +00:00
// Provide the node type/bundle name for other modules, so it does not
// have to be extracted manually from the bundle name.
'node bundle' => $type,
2010-01-30 04:14:17 +00:00
'admin' => array(
// Place the Field UI paths for comments one level below the
// corresponding paths for nodes, so that they appear in the same set
// of local tasks. Note that the paths use a different placeholder name
// and thus a different menu loader callback, so that Field UI page
// callbacks get a comment bundle name from the node type in the URL.
2010-03-26 17:14:46 +00:00
// See comment_node_type_load() and comment_menu_alter().
2010-01-30 04:14:17 +00:00
'path' => 'admin/structure/types/manage/%comment_node_type/comment',
'bundle argument' => 4,
'real path' => 'admin/structure/types/manage/' . str_replace('_', '-', $type) . '/comment',
'access arguments' => array('administer content types'),
),
2009-08-25 21:53:48 +00:00
);
}
return $return;
}
2010-01-30 04:14:17 +00:00
/**
2012-01-03 04:36:15 +00:00
* Loads the comment bundle name corresponding a given content type.
2010-01-30 04:14:17 +00:00
*
2012-01-03 04:36:15 +00:00
* This function is used as a menu loader callback in comment_menu().
*
* @param $name
* The URL-formatted machine name of the node type whose comment fields are
* to be edited. 'URL-formatted' means that underscores are replaced by
* hyphens.
*
* @return
* The comment bundle name corresponding to the node type.
*
* @see comment_menu_alter()
2010-01-30 04:14:17 +00:00
*/
function comment_node_type_load($name) {
if ($type = node_type_get_type(strtr($name, array('-' => '_')))) {
return 'comment_node_' . $type->type;
}
}
2010-01-13 06:26:49 +00:00
/**
2010-02-11 15:52:13 +00:00
* Entity uri callback.
2010-01-13 06:26:49 +00:00
*/
2010-02-11 15:52:13 +00:00
function comment_uri($comment) {
return array(
'path' => 'comment/' . $comment->cid,
'options' => array('fragment' => 'comment-' . $comment->cid),
);
2010-01-13 06:26:49 +00:00
}
2010-01-30 04:14:17 +00:00
/**
* Implements hook_field_extra_fields().
*/
function comment_field_extra_fields() {
$return = array();
foreach (node_type_get_types() as $type) {
if (variable_get('comment_subject_field_' . $type->type, 1) == 1) {
$return['comment']['comment_node_' . $type->type] = array(
2010-05-23 19:10:23 +00:00
'form' => array(
'author' => array(
'label' => t('Author'),
'description' => t('Author textfield'),
'weight' => -2,
),
2010-08-10 00:55:38 +00:00
'subject' => array(
2010-05-23 19:10:23 +00:00
'label' => t('Subject'),
'description' => t('Subject textfield'),
'weight' => -1,
),
2010-01-30 04:14:17 +00:00
),
);
}
}
return $return;
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function comment_theme() {
return array(
'comment_block' => array(
2009-10-23 22:24:19 +00:00
'variables' => array(),
2007-04-06 13:27:23 +00:00
),
'comment_preview' => array(
2009-10-23 22:24:19 +00:00
'variables' => array('comment' => NULL),
2007-04-06 13:27:23 +00:00
),
'comment' => array(
2007-08-26 07:46:11 +00:00
'template' => 'comment',
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2007-04-06 13:27:23 +00:00
),
'comment_post_forbidden' => array(
2009-10-23 22:24:19 +00:00
'variables' => array('node' => NULL),
2007-04-06 13:27:23 +00:00
),
'comment_wrapper' => array(
2007-08-26 07:46:11 +00:00
'template' => 'comment-wrapper',
2009-10-23 22:24:19 +00:00
'render element' => 'content',
2007-04-06 13:27:23 +00:00
),
);
}
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-07-08 19:58:10 +00:00
*/
2007-01-24 14:48:36 +00:00
function comment_menu() {
2009-07-30 19:24:21 +00:00
$items['admin/content/comment'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Comments',
2009-01-14 21:48:24 +00:00
'description' => 'List and edit site comments and the comment approval queue.',
2007-01-24 14:48:36 +00:00
'page callback' => 'comment_admin',
'access arguments' => array('administer comments'),
2010-10-01 15:24:18 +00:00
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
2009-08-24 00:14:23 +00:00
'file' => 'comment.admin.inc',
2007-01-24 14:48:36 +00:00
);
2008-05-14 13:12:41 +00:00
// Tabs begin here.
2009-07-30 19:24:21 +00:00
$items['admin/content/comment/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,
);
2009-07-30 19:24:21 +00:00
$items['admin/content/comment/approval'] = array(
2009-10-09 07:19:43 +00:00
'title' => 'Unapproved comments',
'title callback' => 'comment_count_unpublished',
2007-01-24 14:48:36 +00:00
'page arguments' => array('approval'),
2008-04-23 20:01:56 +00:00
'access arguments' => array('administer comments'),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
);
2010-02-22 15:38:52 +00:00
$items['comment/%'] = array(
2009-10-16 20:40:05 +00:00
'title' => 'Comment permalink',
'page callback' => 'comment_permalink',
'page arguments' => array(1),
'access arguments' => array('access comments'),
2007-01-24 14:48:36 +00:00
);
2010-02-22 15:38:52 +00:00
$items['comment/%/view'] = array(
2009-10-16 20:40:05 +00:00
'title' => 'View comment',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
2010-02-22 15:38:52 +00:00
// Every other comment path uses %, but this one loads the comment directly,
// so we don't end up loading it twice (in the page and access callback).
2009-10-16 20:40:05 +00:00
$items['comment/%comment/edit'] = array(
'title' => 'Edit',
2010-02-22 15:38:52 +00:00
'page callback' => 'comment_edit_page',
'page arguments' => array(1),
2009-07-28 10:09:25 +00:00
'access callback' => 'comment_access',
2009-10-16 20:40:05 +00:00
'access arguments' => array('edit', 1),
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
2010-02-22 15:38:52 +00:00
$items['comment/%/approve'] = array(
2009-10-16 20:40:05 +00:00
'title' => 'Approve',
'page callback' => 'comment_approve',
'page arguments' => array(1),
'access arguments' => array('administer comments'),
'file' => 'comment.pages.inc',
'weight' => 1,
);
2010-02-22 15:38:52 +00:00
$items['comment/%/delete'] = array(
2009-10-16 20:40:05 +00:00
'title' => 'Delete',
2010-02-22 15:38:52 +00:00
'page callback' => 'comment_confirm_delete_page',
'page arguments' => array(1),
2009-10-16 20:40:05 +00:00
'access arguments' => array('administer comments'),
'type' => MENU_LOCAL_TASK,
'file' => 'comment.admin.inc',
'weight' => 2,
2007-01-24 14:48:36 +00:00
);
2007-02-11 09:30:51 +00:00
$items['comment/reply/%node'] = array(
2009-06-08 05:10:37 +00:00
'title' => 'Add new 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),
2009-08-24 00:14:23 +00:00
'file' => 'comment.pages.inc',
2007-01-24 14:48:36 +00:00
);
2004-07-08 19:58:10 +00:00
return $items;
}
2010-03-28 11:08:30 +00:00
/**
* Implements hook_menu_alter().
*/
function comment_menu_alter(&$items) {
// Add comments to the description for admin/content.
2010-10-04 14:54:10 +00:00
$items['admin/content']['description'] = 'Administer content and comments.';
2010-03-28 11:08:30 +00:00
// Adjust the Field UI tabs on admin/structure/types/manage/[node-type].
// See comment_entity_info().
$items['admin/structure/types/manage/%comment_node_type/comment/fields']['title'] = 'Comment fields';
$items['admin/structure/types/manage/%comment_node_type/comment/fields']['weight'] = 3;
$items['admin/structure/types/manage/%comment_node_type/comment/display']['title'] = 'Comment display';
$items['admin/structure/types/manage/%comment_node_type/comment/display']['weight'] = 4;
}
2009-10-09 07:19:43 +00:00
/**
* Returns a menu title which includes the number of unapproved comments.
*/
function comment_count_unpublished() {
$count = db_query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array(
':status' => COMMENT_NOT_PUBLISHED,
))->fetchField();
return t('Unapproved comments (@count)', array('@count' => $count));
}
2009-07-31 19:44:21 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_type_insert().
2010-12-18 01:50:16 +00:00
*
2012-01-03 04:36:15 +00:00
* Creates a comment body field for a node type created while the Comment module
* is enabled. For node types created before the Comment module is enabled,
2010-12-18 01:50:16 +00:00
* hook_modules_enabled() serves to create the body fields.
*
* @see comment_modules_enabled()
2009-07-31 19:44:21 +00:00
*/
2009-08-11 15:50:56 +00:00
function comment_node_type_insert($info) {
2010-12-18 01:50:16 +00:00
_comment_body_field_create($info);
2009-08-11 15:50:56 +00:00
}
2009-07-31 19:44:21 +00:00
2009-08-11 15:50:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_type_update().
2009-08-11 15:50:56 +00:00
*/
function comment_node_type_update($info) {
if (!empty($info->old_type) && $info->type != $info->old_type) {
2009-10-15 12:44:36 +00:00
field_attach_rename_bundle('comment', 'comment_node_' . $info->old_type, 'comment_node_' . $info->type);
2009-08-11 15:50:56 +00:00
}
}
2009-07-31 19:44:21 +00:00
2009-08-11 15:50:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_type_delete().
2009-08-11 15:50:56 +00:00
*/
function comment_node_type_delete($info) {
2009-10-15 12:44:36 +00:00
field_attach_delete_bundle('comment', 'comment_node_' . $info->type);
2009-08-11 15:50:56 +00:00
$settings = array(
'comment',
'comment_default_mode',
'comment_default_per_page',
'comment_anonymous',
'comment_subject_field',
'comment_preview',
'comment_form_location',
);
foreach ($settings as $setting) {
variable_del($setting . '_' . $info->type);
2007-10-07 19:25:57 +00:00
}
}
2010-01-07 05:23:52 +00:00
/**
2010-12-18 01:50:16 +00:00
* Creates a comment_body field instance for a given node type.
2012-01-03 04:36:15 +00:00
*
* @param $info
* An object representing the content type. The only property that is
* currently used is $info->type, which is the machine name of the content
* type for which the body field (instance) is to be created.
2010-01-07 05:23:52 +00:00
*/
2010-12-18 01:50:16 +00:00
function _comment_body_field_create($info) {
// Create the field if needed.
if (!field_read_field('comment_body', array('include_inactive' => TRUE))) {
$field = array(
'field_name' => 'comment_body',
'type' => 'text_long',
'entity_types' => array('comment'),
);
field_create_field($field);
}
// Create the instance if needed.
if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) {
field_attach_create_bundle('comment', 'comment_node_' . $info->type);
// Attaches the body field by default.
$instance = array(
'field_name' => 'comment_body',
'label' => 'Comment',
'entity_type' => 'comment',
'bundle' => 'comment_node_' . $info->type,
'settings' => array('text_processing' => 1),
'required' => TRUE,
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
),
2010-01-07 05:23:52 +00:00
),
2010-12-18 01:50:16 +00:00
);
field_create_instance($instance);
}
2010-01-07 05:23:52 +00:00
}
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2004-07-08 19:58:10 +00:00
*/
2009-07-05 18:00:11 +00:00
function comment_permission() {
2008-02-20 13:46:43 +00:00
return array(
2008-10-09 15:15:55 +00:00
'administer comments' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Administer comments and comment settings'),
2008-10-09 15:15:55 +00:00
),
'access comments' => array(
2009-12-01 13:14:43 +00:00
'title' => t('View comments'),
2008-10-09 15:15:55 +00:00
),
'post comments' => array(
2010-10-05 06:17:29 +00:00
'title' => t('Post comments'),
2008-10-09 15:15:55 +00:00
),
2010-10-05 06:17:29 +00:00
'skip comment approval' => array(
'title' => t('Skip comment approval'),
2008-10-09 15:15:55 +00:00
),
2010-01-03 22:30:26 +00:00
'edit own comments' => array(
'title' => t('Edit own comments'),
),
2008-02-20 13:46:43 +00:00
);
2004-07-08 19:58:10 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_info().
2004-07-08 19:58:10 +00:00
*/
2009-08-29 05:46:04 +00:00
function comment_block_info() {
2008-12-16 23:57:33 +00:00
$blocks['recent']['info'] = t('Recent comments');
2010-11-06 23:24:33 +00:00
$blocks['recent']['properties']['administrative'] = TRUE;
2008-05-14 13:12:41 +00:00
2008-12-16 23:57:33 +00:00
return $blocks;
}
2008-02-21 19:38:07 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_configure().
2008-12-16 23:57:33 +00:00
*/
function comment_block_configure($delta = '') {
$form['comment_block_count'] = array(
'#type' => 'select',
'#title' => t('Number of recent comments'),
'#default_value' => variable_get('comment_block_count', 10),
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
);
2008-05-14 13:12:41 +00:00
2008-12-16 23:57:33 +00:00
return $form;
}
2008-02-21 19:38:07 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_save().
2008-12-16 23:57:33 +00:00
*/
function comment_block_save($delta = '', $edit = array()) {
2010-05-06 05:59:31 +00:00
variable_set('comment_block_count', (int) $edit['comment_block_count']);
2008-12-16 23:57:33 +00:00
}
2008-02-21 19:38:07 +00:00
2008-12-16 23:57:33 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_block_view().
2008-12-16 23:57:33 +00:00
*
* Generates a block with the most recent comments.
*/
function comment_block_view($delta = '') {
if (user_access('access comments')) {
$block['subject'] = t('Recent comments');
$block['content'] = theme('comment_block');
2008-05-14 13:12:41 +00:00
2008-12-16 23:57:33 +00:00
return $block;
2004-07-08 19:58:10 +00:00
}
}
2009-06-19 13:03:53 +00:00
/**
* Redirects comment links to the correct page depending on comment settings.
*
* Since comments are paged there is no way to guarantee which page a comment
* appears on. Comment paging and threading settings may be changed at any time.
* With threaded comments, an individual comment may move between pages as
* comments can be added either before or after it in the overall discussion.
* Therefore we use a central routing function for comment links, which
* calculates the page number based on current comment settings and returns
* the full comment view with the pager set dynamically.
*
2010-02-22 15:38:52 +00:00
* @param $cid
* A comment identifier.
2012-01-03 04:36:15 +00:00
*
2009-06-19 13:03:53 +00:00
* @return
* The comment listing set to the page on which the comment appears.
*/
2010-02-22 15:38:52 +00:00
function comment_permalink($cid) {
if (($comment = comment_load($cid)) && ($node = node_load($comment->nid))) {
2009-06-19 13:03:53 +00:00
// Find the current display page for this comment.
$page = comment_get_display_page($comment->cid, $node->type);
// Set $_GET['q'] and $_GET['page'] ourselves so that the node callback
// behaves as it would when visiting the page directly.
$_GET['q'] = 'node/' . $node->nid;
$_GET['page'] = $page;
// Return the node view, this will show the correct comment in context.
2009-10-15 14:07:30 +00:00
return menu_execute_active_handler('node/' . $node->nid, FALSE);
2009-06-19 13:03:53 +00:00
}
drupal_not_found();
}
2006-12-19 09:02:28 +00:00
/**
2012-01-03 04:36:15 +00:00
* Finds the most recent comments that are available to the current user.
2008-05-14 13:12:41 +00:00
*
* @param integer $number
2009-11-12 06:46:44 +00:00
* (optional) The maximum number of comments to find. Defaults to 10.
2011-05-14 17:30:10 +00:00
*
2007-12-16 21:01:45 +00:00
* @return
2009-11-12 06:46:44 +00:00
* An array of comment objects or an empty array if there are no recent
* comments visible to the current user.
2006-12-19 09:02:28 +00:00
*/
function comment_get_recent($number = 10) {
2009-11-12 06:46:44 +00:00
$query = db_select('comment', 'c');
$query->innerJoin('node', 'n', 'n.nid = c.nid');
$query->addTag('node_access');
$comments = $query
->fields('c')
->condition('c.status', COMMENT_PUBLISHED)
->condition('n.status', NODE_PUBLISHED)
2011-05-14 17:30:10 +00:00
->orderBy('c.created', 'DESC')
2011-06-19 00:19:46 +00:00
// Additionally order by cid to ensure that comments with the same timestamp
// are returned in the exact order posted.
->orderBy('c.cid', 'DESC')
2009-11-12 06:46:44 +00:00
->range(0, $number)
->execute()
->fetchAll();
return $comments ? $comments : array();
2006-12-19 09:02:28 +00:00
}
2007-09-02 14:56:18 +00:00
/**
2012-01-03 04:36:15 +00:00
* Calculates the page number for the first new comment.
2007-12-16 21:01:45 +00:00
*
* @param $num_comments
* Number of comments.
* @param $new_replies
* Number of new replies.
* @param $node
* The first new comment node.
2012-01-03 04:36:15 +00:00
*
2007-12-16 21:01:45 +00:00
* @return
* "page=X" if the page number is greater than zero; empty string otherwise.
2007-09-02 14:56:18 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_new_page_count($num_comments, $new_replies, $node) {
2009-10-09 02:26:15 +00:00
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
$comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
2007-09-02 14:56:18 +00:00
$pagenum = NULL;
2009-07-01 12:06:22 +00:00
$flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE;
2008-07-03 17:57:03 +00:00
if ($num_comments <= $comments_per_page) {
// Only one page of comments.
2007-09-02 14:56:18 +00:00
$pageno = 0;
2007-09-04 21:10:45 +00:00
}
2008-07-03 17:57:03 +00:00
elseif ($flat) {
// Flat comments.
$count = $num_comments - $new_replies;
2011-09-16 18:58:27 +00:00
$pageno = $count / $comments_per_page;
2008-07-03 17:57:03 +00:00
}
2007-09-02 14:56:18 +00:00
else {
2009-12-11 17:09:00 +00:00
// Threaded comments: we build a query with a subquery to find the first
// thread with a new comment.
// 1. Find all the threads with a new comment.
$unread_threads_query = db_select('comment')
->fields('comment', array('thread'))
->condition('nid', $node->nid)
->condition('status', COMMENT_PUBLISHED)
2010-02-17 05:31:31 +00:00
->orderBy('created', 'DESC')
->orderBy('cid', 'DESC')
2009-12-11 17:09:00 +00:00
->range(0, $new_replies);
// 2. Find the first thread.
$first_thread = db_select($unread_threads_query, 'thread')
->fields('thread', array('thread'))
->orderBy('SUBSTRING(thread, 1, (LENGTH(thread) - 1))')
->range(0, 1)
->execute()
->fetchField();
// Remove the final '/'.
$first_thread = substr($first_thread, 0, -1);
// Find the number of the first comment of the first unread thread.
$count = db_query('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
':status' => COMMENT_PUBLISHED,
2008-11-16 19:41:14 +00:00
':nid' => $node->nid,
2009-12-11 17:09:00 +00:00
':thread' => $first_thread,
2009-05-21 10:34:55 +00:00
))->fetchField();
2009-12-11 17:09:00 +00:00
2011-09-16 18:58:27 +00:00
$pageno = $count / $comments_per_page;
2007-09-02 14:56:18 +00:00
}
2008-05-14 13:12:41 +00:00
2007-09-02 14:56:18 +00:00
if ($pageno >= 1) {
2009-09-29 15:31:17 +00:00
$pagenum = array('page' => intval($pageno));
2007-09-02 14:56:18 +00:00
}
2008-05-14 13:12:41 +00:00
2007-09-02 14:56:18 +00:00
return $pagenum;
}
2006-12-19 09:02:28 +00:00
/**
2012-01-03 04:36:15 +00:00
* Returns HTML for a list of recent comments.
2006-12-19 09:02:28 +00:00
*
* @ingroup themeable
*/
2006-01-17 18:02:23 +00:00
function theme_comment_block() {
$items = array();
2008-02-21 19:38:07 +00:00
$number = variable_get('comment_block_count', 10);
foreach (comment_get_recent($number) as $comment) {
2010-09-13 05:52:18 +00:00
$items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . ' <span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . '</span>';
2006-01-17 18:02:23 +00:00
}
2008-05-14 13:12:41 +00:00
2006-12-01 16:50:36 +00:00
if ($items) {
2009-10-09 01:00:08 +00:00
return theme('item_list', array('items' => $items));
2006-12-01 16:50:36 +00:00
}
2010-05-10 20:12:21 +00:00
else {
return t('No comments available.');
}
2006-01-17 18:02:23 +00:00
}
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_view().
2004-07-08 19:58:10 +00:00
*/
2009-12-26 16:50:09 +00:00
function comment_node_view($node, $view_mode) {
2004-07-08 19:58:10 +00:00
$links = array();
2011-01-02 23:54:05 +00:00
if ($node->comment != COMMENT_NODE_HIDDEN) {
2009-12-26 16:50:09 +00:00
if ($view_mode == 'rss') {
2011-01-02 23:54:05 +00:00
// Add a comments RSS element which is a URL to the comments of this node.
$node->rss_elements[] = array(
'key' => 'comments',
'value' => url('node/' . $node->nid, array('fragment' => 'comments', 'absolute' => TRUE))
);
2009-05-03 10:11:35 +00:00
}
2009-12-26 16:50:09 +00:00
elseif ($view_mode == 'teaser') {
2010-03-01 11:18:31 +00:00
// Teaser view: display the number of comments that have been posted,
// or a link to add new comments if the user has permission, the node
// is open to new comments, and there currently are none.
2004-07-08 19:58:10 +00:00
if (user_access('access comments')) {
2008-12-16 22:05:51 +00:00
if (!empty($node->comment_count)) {
2010-05-05 15:49:04 +00:00
$links['comment-comments'] = array(
2008-06-18 16:06:42 +00:00
'title' => format_plural($node->comment_count, '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.')),
2009-06-01 22:01:56 +00:00
'fragment' => 'comments',
'html' => TRUE,
2006-05-18 14:58:57 +00:00
);
2011-01-02 23:54:05 +00:00
// Show a link to the first new comment.
if ($new = comment_num_new($node->nid)) {
2010-05-05 15:49:04 +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",
2008-06-18 16:06:42 +00:00
'query' => comment_new_page_count($node->comment_count, $new, $node),
2006-07-04 08:59:05 +00:00
'attributes' => array('title' => t('Jump to the first new comment of this posting.')),
2009-06-01 22:01:56 +00:00
'fragment' => 'new',
'html' => TRUE,
2006-05-18 14:58:57 +00:00
);
2004-07-08 19:58:10 +00:00
}
}
2011-01-02 23:54:05 +00:00
}
if ($node->comment == COMMENT_NODE_OPEN) {
if (user_access('post comments')) {
$links['comment-add'] = array(
'title' => t('Add new comment'),
'href' => "comment/reply/$node->nid",
'attributes' => array('title' => t('Add a new comment to this page.')),
'fragment' => 'comment-form',
);
}
2004-07-08 19:58:10 +00:00
else {
2011-09-07 10:49:13 +00:00
$links['comment-forbidden'] = array(
2011-01-02 23:54:05 +00:00
'title' => theme('comment_post_forbidden', array('node' => $node)),
'html' => TRUE,
);
2004-07-08 19:58:10 +00:00
}
}
}
2010-12-04 01:47:00 +00:00
elseif ($view_mode != 'search_index' && $view_mode != 'search_result') {
2010-03-01 11:18:31 +00:00
// Node in other view modes: add a "post comment" link if the user is
// allowed to post comments and if this node is allowing new comments.
// But we don't want this link if we're building the node for search
2010-12-04 01:47:00 +00:00
// indexing or constructing a search result excerpt.
2009-03-17 12:41:54 +00:00
if ($node->comment == COMMENT_NODE_OPEN) {
2011-01-02 23:54:05 +00:00
$comment_form_location = variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW);
2004-07-08 19:58:10 +00:00
if (user_access('post comments')) {
2011-01-02 23:54:05 +00:00
// Show the "post comment" link if the form is on another page, or
// if there are existing comments that the link will skip past.
if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) {
$links['comment-add'] = array(
'title' => t('Add new comment'),
'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
'href' => "node/$node->nid",
'fragment' => 'comment-form',
);
if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) {
$links['comment-add']['href'] = "comment/reply/$node->nid";
}
2006-01-26 08:27:58 +00:00
}
2004-07-08 19:58:10 +00:00
}
else {
2011-09-07 10:49:13 +00:00
$links['comment-forbidden'] = array(
2011-01-02 23:54:05 +00:00
'title' => theme('comment_post_forbidden', array('node' => $node)),
'html' => TRUE,
);
2004-07-08 19:58:10 +00:00
}
}
}
2009-01-26 14:08:44 +00:00
2010-11-14 21:04:45 +00:00
$node->content['links']['comment'] = array(
'#theme' => 'links__node__comment',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
2009-01-26 14:08:44 +00:00
2009-07-28 10:09:25 +00:00
// Only append comments when we are building a node on its own node detail
// page. We compare $node and $page_node to ensure that comments are not
// appended to other nodes shown on the page, for example a node_reference
2009-12-26 16:50:09 +00:00
// displayed in 'full' view mode within another node.
2011-01-02 23:54:05 +00:00
if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
2009-07-28 10:09:25 +00:00
$node->content['comments'] = comment_node_page_additions($node);
2008-12-31 12:02:24 +00:00
}
2006-10-18 18:00:40 +00:00
}
2004-07-08 19:58:10 +00:00
}
2009-07-28 10:09:25 +00:00
/**
2012-01-03 04:36:15 +00:00
* Builds the comment-related elements for node detail pages.
2009-07-28 10:09:25 +00:00
*
* @param $node
2012-01-03 04:36:15 +00:00
* The node object for which to build the comment-related elements.
*
* @return
* A renderable array representing the comment-related page elements for the
* node.
2009-07-28 10:09:25 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_node_page_additions($node) {
2009-07-28 10:09:25 +00:00
$additions = array();
// Only attempt to render comments if the node has visible comments.
// Unpublished comments are not included in $node->comment_count, so show
// comments unconditionally if the user is an administrator.
2011-01-02 23:54:05 +00:00
if (($node->comment_count && user_access('access comments')) || user_access('administer comments')) {
2009-10-09 02:26:15 +00:00
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
$comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
if ($cids = comment_get_thread($node, $mode, $comments_per_page)) {
2009-07-28 10:09:25 +00:00
$comments = comment_load_multiple($cids);
comment_prepare_thread($comments);
2009-12-21 13:47:32 +00:00
$build = comment_view_multiple($comments, $node);
2009-07-28 10:09:25 +00:00
$build['pager']['#theme'] = 'pager';
$additions['comments'] = $build;
}
}
// Append comment form if needed.
if (user_access('post comments') && $node->comment == COMMENT_NODE_OPEN && (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW)) {
2011-12-05 12:12:15 +00:00
$comment = entity_create('comment', array('nid' => $node->nid));
$additions['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", $comment);
2009-07-28 10:09:25 +00:00
}
if ($additions) {
$additions += array(
2010-09-09 23:01:48 +00:00
'#theme' => 'comment_wrapper__node_' . $node->type,
2009-07-28 10:09:25 +00:00
'#node' => $node,
'comments' => array(),
'comment_form' => array(),
);
}
return $additions;
}
/**
2012-01-03 04:36:15 +00:00
* Retrieves comments for a thread.
2009-07-28 10:09:25 +00:00
*
* @param $node
* The node whose comment(s) needs rendering.
2009-10-09 02:26:15 +00:00
* @param $mode
* The comment display mode; COMMENT_MODE_FLAT or COMMENT_MODE_THREADED.
* @param $comments_per_page
* The amount of comments to display per page.
2009-07-28 10:09:25 +00:00
*
2012-01-03 04:36:15 +00:00
* @return
* An array of the IDs of the comment to be displayed.
*
2009-07-28 10:09:25 +00:00
* 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, the
* thread fields will look like this:
*
* 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.
*/
2009-11-08 10:02:41 +00:00
function comment_get_thread($node, $mode, $comments_per_page) {
2009-07-28 10:09:25 +00:00
$query = db_select('comment', 'c')->extend('PagerDefault');
$query->addField('c', 'cid');
$query
->condition('c.nid', $node->nid)
->addTag('node_access')
2009-11-28 16:41:19 +00:00
->addTag('comment_filter')
->addMetaData('node', $node)
2009-07-28 10:09:25 +00:00
->limit($comments_per_page);
$count_query = db_select('comment', 'c');
$count_query->addExpression('COUNT(*)');
$count_query
->condition('c.nid', $node->nid)
2009-11-28 16:41:19 +00:00
->addTag('node_access')
->addTag('comment_filter')
->addMetaData('node', $node);
2009-07-28 10:09:25 +00:00
if (!user_access('administer comments')) {
$query->condition('c.status', COMMENT_PUBLISHED);
$count_query->condition('c.status', COMMENT_PUBLISHED);
}
if ($mode === COMMENT_MODE_FLAT) {
$query->orderBy('c.cid', 'ASC');
}
else {
// See comment above. Analysis reveals that this doesn't cost too
// much. It scales much much better than having the whole comment
// structure.
2010-12-01 00:19:18 +00:00
$query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
$query->orderBy('torder', 'ASC');
2009-07-28 10:09:25 +00:00
}
$query->setCountQuery($count_query);
$cids = $query->execute()->fetchCol();
return $cids;
}
/**
2012-01-03 04:36:15 +00:00
* Calculates the indentation level of each comment in a comment thread.
*
* This function loops over an array representing a comment thread. For each
* comment, the function calculates the indentation level and saves it in the
* 'divs' property of the comment object.
2009-07-28 10:09:25 +00:00
*
* @param array $comments
2012-01-03 04:36:15 +00:00
* An array of comment objects, keyed by comment ID.
2009-07-28 10:09:25 +00:00
*/
function comment_prepare_thread(&$comments) {
// A flag stating if we are still searching for first new comment on the thread.
$first_new = TRUE;
// A counter that helps track how indented we are.
$divs = 0;
foreach ($comments as $key => $comment) {
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;
$comment->first_new = TRUE;
}
// The $divs element instructs #prefix whether to add an indent div or
// close existing divs (a negative value).
$comment->depth = count(explode('.', $comment->thread)) - 1;
if ($comment->depth > $divs) {
$comment->divs = 1;
$divs++;
}
else {
$comment->divs = $comment->depth - $divs;
while ($comment->depth < $divs) {
$divs--;
}
}
$comments[$key] = $comment;
}
// The final comment must close up some hanging divs
$comments[$key]->divs_final = $divs;
}
/**
2012-01-03 04:36:15 +00:00
* Generates an array for rendering a comment.
2009-07-28 10:09:25 +00:00
*
* @param $comment
2012-01-03 04:36:15 +00:00
* The comment object.
2009-09-11 13:37:52 +00:00
* @param $node
* The node the comment is attached to.
2009-12-26 16:50:09 +00:00
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
2010-10-03 01:15:34 +00:00
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2009-07-28 10:09:25 +00:00
*
* @return
* An array as expected by drupal_render().
*/
2010-10-03 01:15:34 +00:00
function comment_view($comment, $node, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
2009-09-10 12:33:46 +00:00
// Populate $comment->content with a render() array.
2010-10-03 01:15:34 +00:00
comment_build_content($comment, $node, $view_mode, $langcode);
2009-07-28 10:09:25 +00:00
$build = $comment->content;
2009-09-10 12:33:46 +00:00
// We don't need duplicate rendering info in comment->content.
unset($comment->content);
2009-07-28 10:09:25 +00:00
$build += array(
2010-09-09 23:01:48 +00:00
'#theme' => 'comment__node_' . $node->type,
2009-07-28 10:09:25 +00:00
'#comment' => $comment,
2009-09-11 13:37:52 +00:00
'#node' => $node,
2009-12-26 16:50:09 +00:00
'#view_mode' => $view_mode,
2010-10-03 01:15:34 +00:00
'#language' => $langcode,
2009-07-28 10:09:25 +00:00
);
2010-07-29 00:50:56 +00:00
if (empty($comment->in_preview)) {
$prefix = '';
$is_threaded = isset($comment->divs) && variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED) == COMMENT_MODE_THREADED;
2009-07-28 10:09:25 +00:00
2010-07-29 00:50:56 +00:00
// Add 'new' anchor if needed.
if (!empty($comment->first_new)) {
$prefix .= "<a id=\"new\"></a>\n";
}
2009-07-28 10:09:25 +00:00
2010-07-29 00:50:56 +00:00
// Add indentation div or close open divs as needed.
if ($is_threaded) {
$prefix .= $comment->divs <= 0 ? str_repeat('</div>', abs($comment->divs)) : "\n" . '<div class="indented">';
}
2009-07-28 10:09:25 +00:00
2010-07-29 00:50:56 +00:00
// Add anchor for each comment.
$prefix .= "<a id=\"comment-$comment->cid\"></a>\n";
$build['#prefix'] = $prefix;
2009-07-28 10:09:25 +00:00
2010-07-29 00:50:56 +00:00
// Close all open divs.
if ($is_threaded && !empty($comment->divs_final)) {
$build['#suffix'] = str_repeat('</div>', $comment->divs_final);
}
2009-07-28 10:09:25 +00:00
}
2009-11-07 13:35:21 +00:00
// Allow modules to modify the structured comment.
2010-10-23 15:30:34 +00:00
$type = 'comment';
drupal_alter(array('comment_view', 'entity_view'), $build, $type);
2009-11-07 13:35:21 +00:00
2009-07-28 10:09:25 +00:00
return $build;
}
/**
* Builds a structured array representing the comment's content.
*
2012-01-03 04:36:15 +00:00
* The content built for the comment (field values, comments, file attachments
* or other comment components) will vary depending on the $view_mode parameter.
2009-07-28 10:09:25 +00:00
*
* @param $comment
* A comment object.
2009-09-11 13:37:52 +00:00
* @param $node
* The node the comment is attached to.
2009-12-26 16:50:09 +00:00
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
2010-10-03 01:15:34 +00:00
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
2009-07-28 10:09:25 +00:00
*/
2010-10-03 01:15:34 +00:00
function comment_build_content($comment, $node, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
2009-11-02 23:20:28 +00:00
// Remove previously built content, if exists.
$comment->content = array();
2009-07-28 10:09:25 +00:00
2009-11-07 13:35:21 +00:00
// Build fields content.
2011-05-13 14:29:20 +00:00
field_attach_prepare_view('comment', array($comment->cid => $comment), $view_mode, $langcode);
entity_prepare_view('comment', array($comment->cid => $comment), $langcode);
2010-10-03 01:15:34 +00:00
$comment->content += field_attach_view('comment', $comment, $view_mode, $langcode);
2009-07-31 19:44:21 +00:00
2010-11-14 21:04:45 +00:00
$comment->content['links'] = array(
'#theme' => 'links__comment',
'#pre_render' => array('drupal_pre_render_links'),
'#attributes' => array('class' => array('links', 'inline')),
);
2009-07-28 10:09:25 +00:00
if (empty($comment->in_preview)) {
$comment->content['links']['comment'] = array(
2010-11-14 21:04:45 +00:00
'#theme' => 'links__comment__comment',
2009-09-11 13:37:52 +00:00
'#links' => comment_links($comment, $node),
2009-08-22 14:34:23 +00:00
'#attributes' => array('class' => array('links', 'inline')),
2009-07-28 10:09:25 +00:00
);
}
// Allow modules to make their own additions to the comment.
2010-10-03 01:15:34 +00:00
module_invoke_all('comment_view', $comment, $view_mode, $langcode);
2010-10-23 15:30:34 +00:00
module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
2009-07-28 10:09:25 +00:00
}
2009-08-08 22:52:59 +00:00
/**
2012-01-03 04:36:15 +00:00
* Adds reply, edit, delete, etc. links, depending on user permissions.
2009-08-08 22:52:59 +00:00
*
* @param $comment
* The comment object.
2009-09-11 13:37:52 +00:00
* @param $node
* The node the comment is attached to.
2012-01-03 04:36:15 +00:00
*
2009-08-08 22:52:59 +00:00
* @return
* A structured array of links.
*/
2009-11-08 10:02:41 +00:00
function comment_links($comment, $node) {
2009-08-08 22:52:59 +00:00
$links = array();
if ($node->comment == COMMENT_NODE_OPEN) {
if (user_access('administer comments') && user_access('post comments')) {
2010-05-05 15:49:04 +00:00
$links['comment-delete'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('delete'),
2009-10-16 20:40:05 +00:00
'href' => "comment/$comment->cid/delete",
2009-08-08 22:52:59 +00:00
'html' => TRUE,
);
2010-05-05 15:49:04 +00:00
$links['comment-edit'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('edit'),
2009-10-16 20:40:05 +00:00
'href' => "comment/$comment->cid/edit",
2009-08-08 22:52:59 +00:00
'html' => TRUE,
);
2010-05-05 15:49:04 +00:00
$links['comment-reply'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('reply'),
'href' => "comment/reply/$comment->nid/$comment->cid",
'html' => TRUE,
);
if ($comment->status == COMMENT_NOT_PUBLISHED) {
2010-05-05 15:49:04 +00:00
$links['comment-approve'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('approve'),
2009-10-16 20:40:05 +00:00
'href' => "comment/$comment->cid/approve",
2009-08-08 22:52:59 +00:00
'html' => TRUE,
2010-03-28 07:00:30 +00:00
'query' => array('token' => drupal_get_token("comment/$comment->cid/approve")),
2009-08-08 22:52:59 +00:00
);
}
}
elseif (user_access('post comments')) {
if (comment_access('edit', $comment)) {
2010-05-05 15:49:04 +00:00
$links['comment-edit'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('edit'),
2009-10-16 20:40:05 +00:00
'href' => "comment/$comment->cid/edit",
2009-08-08 22:52:59 +00:00
'html' => TRUE,
);
}
2010-05-05 15:49:04 +00:00
$links['comment-reply'] = array(
2009-08-08 22:52:59 +00:00
'title' => t('reply'),
'href' => "comment/reply/$comment->nid/$comment->cid",
'html' => TRUE,
);
}
else {
2011-09-07 10:49:13 +00:00
$links['comment-forbidden']['title'] = theme('comment_post_forbidden', array('node' => $node));
$links['comment-forbidden']['html'] = TRUE;
2009-08-08 22:52:59 +00:00
}
}
return $links;
}
2009-07-28 10:09:25 +00:00
/**
2012-01-03 04:36:15 +00:00
* Constructs render array from an array of loaded comments.
2009-07-28 10:09:25 +00:00
*
* @param $comments
* An array of comments as returned by comment_load_multiple().
2009-09-11 13:37:52 +00:00
* @param $node
* The node the comments are attached to.
2009-12-26 16:50:09 +00:00
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
2009-07-28 10:09:25 +00:00
* @param $weight
* An integer representing the weight of the first comment in the list.
2010-10-03 01:15:34 +00:00
* @param $langcode
* A string indicating the language field values are to be shown in. If no
* language is provided the current content language is used.
*
2009-07-28 10:09:25 +00:00
* @return
* An array in the format expected by drupal_render().
2012-01-03 04:36:15 +00:00
*
* @see drupal_render()
2009-07-28 10:09:25 +00:00
*/
2010-10-03 01:15:34 +00:00
function comment_view_multiple($comments, $node, $view_mode = 'full', $weight = 0, $langcode = NULL) {
2011-05-13 14:29:20 +00:00
field_attach_prepare_view('comment', $comments, $view_mode, $langcode);
entity_prepare_view('comment', $comments, $langcode);
2009-10-16 03:21:23 +00:00
2009-07-28 10:09:25 +00:00
$build = array(
'#sorted' => TRUE,
);
foreach ($comments as $comment) {
2010-10-03 01:15:34 +00:00
$build[$comment->cid] = comment_view($comment, $node, $view_mode, $langcode);
2009-07-28 10:09:25 +00:00
$build[$comment->cid]['#weight'] = $weight;
$weight++;
}
return $build;
}
2007-12-16 21:01:45 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_form_FORM_ID_alter().
2007-12-16 21:01:45 +00:00
*/
2009-01-22 03:11:54 +00:00
function comment_form_node_type_form_alter(&$form, $form_state) {
2009-12-24 14:00:03 +00:00
if (isset($form['type'])) {
2007-10-07 19:25:57 +00:00
$form['comment'] = array(
'#type' => 'fieldset',
'#title' => t('Comment settings'),
'#collapsible' => TRUE,
2007-10-12 14:10:18 +00:00
'#collapsed' => TRUE,
2009-08-21 00:21:48 +00:00
'#group' => 'additional_settings',
2010-11-05 19:47:20 +00:00
'#attributes' => array(
'class' => array('comment-node-type-settings-form'),
),
2009-09-05 15:05:05 +00:00
'#attached' => array(
'js' => array(drupal_get_path('module', 'comment') . '/comment-node-form.js'),
),
2007-10-07 19:25:57 +00:00
);
2010-11-23 06:02:06 +00:00
// Unlike coment_form_node_form_alter(), all of these settings are applied
// as defaults to all new nodes. Therefore, it would be wrong to use #states
// to hide the other settings based on the primary comment setting.
2010-07-30 02:50:37 +00:00
$form['comment']['comment'] = array(
'#type' => 'select',
'#title' => t('Default comment setting for new content'),
'#default_value' => variable_get('comment_' . $form['#node_type']->type, COMMENT_NODE_OPEN),
2010-11-23 06:02:06 +00:00
'#options' => array(
COMMENT_NODE_OPEN => t('Open'),
COMMENT_NODE_CLOSED => t('Closed'),
COMMENT_NODE_HIDDEN => t('Hidden'),
),
2010-07-30 02:50:37 +00:00
);
2007-10-07 19:25:57 +00:00
$form['comment']['comment_default_mode'] = array(
2009-07-01 12:06:22 +00:00
'#type' => 'checkbox',
'#title' => t('Threading'),
'#default_value' => variable_get('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED),
'#description' => t('Show comment replies in a threaded list.'),
2007-10-07 19:25:57 +00:00
);
$form['comment']['comment_default_per_page'] = array(
'#type' => 'select',
2008-06-20 16:45:27 +00:00
'#title' => t('Comments per page'),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
2007-10-07 19:25:57 +00:00
'#options' => _comment_per_page(),
2009-07-01 12:06:22 +00:00
);
2007-10-07 19:25:57 +00:00
$form['comment']['comment_anonymous'] = array(
2009-07-01 12:06:22 +00:00
'#type' => 'select',
2007-10-07 19:25:57 +00:00
'#title' => t('Anonymous commenting'),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get('comment_anonymous_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
2007-10-07 19:25:57 +00:00
'#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'),
2010-11-23 06:02:06 +00:00
COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
),
'#access' => user_access('post comments', drupal_anonymous_user()),
2007-10-07 19:25:57 +00:00
);
$form['comment']['comment_subject_field'] = array(
2009-07-01 12:06:22 +00:00
'#type' => 'checkbox',
'#title' => t('Allow comment title'),
2008-04-14 17:48:46 +00:00
'#default_value' => variable_get('comment_subject_field_' . $form['#node_type']->type, 1),
2007-10-07 19:25:57 +00:00
);
$form['comment']['comment_form_location'] = array(
2009-07-01 12:06:22 +00:00
'#type' => 'checkbox',
'#title' => t('Show reply form on the same page as comments'),
'#default_value' => variable_get('comment_form_location_' . $form['#node_type']->type, COMMENT_FORM_BELOW),
);
$form['comment']['comment_preview'] = array(
2009-10-16 13:20:16 +00:00
'#type' => 'radios',
'#title' => t('Preview comment'),
'#default_value' => variable_get('comment_preview_' . $form['#node_type']->type, DRUPAL_OPTIONAL),
'#options' => array(
DRUPAL_DISABLED => t('Disabled'),
DRUPAL_OPTIONAL => t('Optional'),
DRUPAL_REQUIRED => t('Required'),
),
2007-10-07 19:25:57 +00:00
);
2006-08-06 23:00:42 +00:00
}
2009-01-22 03:11:54 +00:00
}
/**
2010-09-09 23:01:48 +00:00
* Implements hook_form_BASE_FORM_ID_alter().
2009-01-22 03:11:54 +00:00
*/
2010-09-09 23:01:48 +00:00
function comment_form_node_form_alter(&$form, $form_state) {
$node = $form['#node'];
$form['comment_settings'] = array(
'#type' => 'fieldset',
'#access' => user_access('administer comments'),
'#title' => t('Comment settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
2010-11-05 19:47:20 +00:00
'#attributes' => array(
'class' => array('comment-node-settings-form'),
),
2010-09-09 23:01:48 +00:00
'#attached' => array(
'js' => array(drupal_get_path('module', 'comment') . '/comment-node-form.js'),
),
'#weight' => 30,
);
$comment_count = isset($node->nid) ? db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() : 0;
$comment_settings = ($node->comment == COMMENT_NODE_HIDDEN && empty($comment_count)) ? COMMENT_NODE_CLOSED : $node->comment;
$form['comment_settings']['comment'] = array(
'#type' => 'radios',
2010-10-20 01:31:07 +00:00
'#title' => t('Comments'),
'#title_display' => 'invisible',
2010-09-09 23:01:48 +00:00
'#parents' => array('comment'),
'#default_value' => $comment_settings,
'#options' => array(
COMMENT_NODE_OPEN => t('Open'),
COMMENT_NODE_CLOSED => t('Closed'),
COMMENT_NODE_HIDDEN => t('Hidden'),
),
COMMENT_NODE_OPEN => array(
'#description' => t('Users with the "Post comments" permission can post comments.'),
),
COMMENT_NODE_CLOSED => array(
'#description' => t('Users cannot post comments, but existing comments will be displayed.'),
),
COMMENT_NODE_HIDDEN => array(
'#description' => t('Comments are hidden from view.'),
),
);
// If the node doesn't have any comments, the "hidden" option makes no
// sense, so don't even bother presenting it to the user.
if (empty($comment_count)) {
2012-01-10 03:06:42 +00:00
$form['comment_settings']['comment'][COMMENT_NODE_HIDDEN]['#access'] = FALSE;
// Also adjust the description of the "closed" option.
2010-09-09 23:01:48 +00:00
$form['comment_settings']['comment'][COMMENT_NODE_CLOSED]['#description'] = t('Users cannot post comments.');
2005-11-25 10:11:59 +00:00
}
}
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_load().
2004-07-08 19:58:10 +00:00
*/
2009-03-08 04:25:07 +00:00
function comment_node_load($nodes, $types) {
2008-12-05 22:18:46 +00:00
$comments_enabled = array();
// Check if comments are enabled for each node. If comments are disabled,
// assign values without hitting the database.
foreach ($nodes as $node) {
// Store whether comments are enabled for this node.
2009-03-17 12:41:54 +00:00
if ($node->comment != COMMENT_NODE_HIDDEN) {
2008-12-05 22:18:46 +00:00
$comments_enabled[] = $node->nid;
}
else {
2009-11-12 06:46:44 +00:00
$node->cid = 0;
2008-12-05 22:18:46 +00:00
$node->last_comment_timestamp = $node->created;
$node->last_comment_name = '';
2010-11-06 11:15:10 +00:00
$node->last_comment_uid = $node->uid;
2008-12-05 22:18:46 +00:00
$node->comment_count = 0;
}
}
// For nodes with comments enabled, fetch information from the database.
if (!empty($comments_enabled)) {
2010-11-06 11:15:10 +00:00
$result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled));
2008-12-05 22:18:46 +00:00
foreach ($result as $record) {
2009-11-12 06:46:44 +00:00
$nodes[$record->nid]->cid = $record->cid;
2008-12-05 22:18:46 +00:00
$nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp;
$nodes[$record->nid]->last_comment_name = $record->last_comment_name;
2010-11-06 11:15:10 +00:00
$nodes[$record->nid]->last_comment_uid = $record->last_comment_uid;
2008-12-05 22:18:46 +00:00
$nodes[$record->nid]->comment_count = $record->comment_count;
}
2008-10-06 12:55:56 +00:00
}
}
2006-01-05 14:35:55 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_prepare().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_node_prepare($node) {
2008-10-06 12:55:56 +00:00
if (!isset($node->comment)) {
2009-03-17 12:41:54 +00:00
$node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
2008-10-06 12:55:56 +00:00
}
}
2005-01-24 21:20:16 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_insert().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_node_insert($node) {
2010-04-16 13:56:45 +00:00
// Allow bulk updates and inserts to temporarily disable the
// maintenance of the {node_comment_statistics} table.
if (variable_get('comment_maintain_node_statistics', TRUE)) {
db_insert('node_comment_statistics')
->fields(array(
'nid' => $node->nid,
'cid' => 0,
'last_comment_timestamp' => $node->changed,
'last_comment_name' => NULL,
'last_comment_uid' => $node->uid,
'comment_count' => 0,
))
->execute();
}
2008-10-06 12:55:56 +00:00
}
2006-08-31 21:58:36 +00:00
2008-10-06 12:55:56 +00:00
/**
2011-12-17 12:43:37 +00:00
* Implements hook_node_predelete().
2008-10-06 12:55:56 +00:00
*/
2011-12-17 12:43:37 +00:00
function comment_node_predelete($node) {
2009-07-31 19:44:21 +00:00
$cids = db_query('SELECT cid FROM {comment} WHERE nid = :nid', array(':nid' => $node->nid))->fetchCol();
comment_delete_multiple($cids);
2008-11-11 21:44:01 +00:00
db_delete('node_comment_statistics')
->condition('nid', $node->nid)
->execute();
2008-10-06 12:55:56 +00:00
}
2006-08-31 21:58:36 +00:00
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_update_index().
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_node_update_index($node) {
2010-06-08 06:34:36 +00:00
$index_comments = &drupal_static(__FUNCTION__);
if ($index_comments === NULL) {
// Find and save roles that can 'access comments' or 'search content'.
$perms = array('access comments' => array(), 'search content' => array());
$result = db_query("SELECT rid, permission FROM {role_permission} WHERE permission IN ('access comments', 'search content')");
foreach ($result as $record) {
$perms[$record->permission][$record->rid] = $record->rid;
}
// Prevent indexing of comments if there are any roles that can search but
// not view comments.
$index_comments = TRUE;
foreach ($perms['search content'] as $rid) {
if (!isset($perms['access comments'][$rid]) && ($rid <= DRUPAL_AUTHENTICATED_RID || !isset($perms['access comments'][DRUPAL_AUTHENTICATED_RID]))) {
$index_comments = FALSE;
break;
}
}
}
if ($index_comments) {
$mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
$comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
if ($node->comment && $cids = comment_get_thread($node, $mode, $comments_per_page)) {
$comments = comment_load_multiple($cids);
comment_prepare_thread($comments);
$build = comment_view_multiple($comments, $node);
return drupal_render($build);
}
2008-10-06 12:55:56 +00:00
}
2010-06-08 06:34:36 +00:00
return '';
2008-10-06 12:55:56 +00:00
}
2005-01-24 21:20:16 +00:00
2008-12-31 12:02:24 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_update_index().
2008-12-31 12:02:24 +00:00
*/
function comment_update_index() {
// Store the maximum possible comments per thread (used for ranking by reply count)
variable_set('node_cron_comments_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}')->fetchField()));
}
2008-10-06 12:55:56 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_search_result().
2010-04-26 14:26:46 +00:00
*
* Formats a comment count string and returns it, for display with search
* results.
2008-10-06 12:55:56 +00:00
*/
2009-11-08 10:02:41 +00:00
function comment_node_search_result($node) {
2010-05-27 12:29:39 +00:00
// Do not make a string if comments are hidden.
2010-06-08 06:34:36 +00:00
if (user_access('access comments') && $node->comment != COMMENT_NODE_HIDDEN) {
2009-05-25 15:43:38 +00:00
$comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();
2010-04-26 14:26:46 +00:00
// Do not make a string if comments are closed and there are currently
// zero comments.
if ($node->comment != COMMENT_NODE_CLOSED || $comments > 0) {
2011-06-27 06:11:44 +00:00
return array('comment' => format_plural($comments, '1 comment', '@count comments'));
2010-04-26 14:26:46 +00:00
}
2009-05-25 15:43:38 +00:00
}
2008-10-06 12:55:56 +00:00
}
2005-09-19 19:13:35 +00:00
2004-07-08 19:58:10 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_user_cancel().
2004-07-08 19:58:10 +00:00
*/
2009-01-22 12:46:07 +00:00
function comment_user_cancel($edit, $account, $method) {
2009-01-08 08:42:13 +00:00
switch ($method) {
case 'user_cancel_block_unpublish':
2010-06-02 06:20:11 +00:00
$comments = comment_load_multiple(array(), array('uid' => $account->uid));
foreach ($comments as $comment) {
$comment->status = 0;
comment_save($comment);
}
2009-01-08 08:42:13 +00:00
break;
case 'user_cancel_reassign':
2010-06-02 06:20:11 +00:00
$comments = comment_load_multiple(array(), array('uid' => $account->uid));
foreach ($comments as $comment) {
$comment->uid = 0;
comment_save($comment);
}
2009-01-08 08:42:13 +00:00
break;
}
2004-07-08 19:58:10 +00:00
}
2010-03-12 15:56:30 +00:00
/**
2011-12-17 12:43:37 +00:00
* Implements hook_user_predelete().
2010-03-12 15:56:30 +00:00
*/
2011-12-17 12:43:37 +00:00
function comment_user_predelete($account) {
2010-03-12 15:56:30 +00:00
$cids = db_query('SELECT c.cid FROM {comment} c WHERE uid = :uid', array(':uid' => $account->uid))->fetchCol();
comment_delete_multiple($cids);
}
2004-06-04 18:50:29 +00:00
/**
2011-01-13 00:43:21 +00:00
* Determines whether the current user has access to a particular comment.
2004-06-04 18:50:29 +00:00
*
* Authenticated users can edit their comments as long they have not been
2011-01-13 00:43:21 +00:00
* replied to. This prevents people from changing or revising their statements
* based on the replies to their posts.
2007-12-16 21:01:45 +00:00
*
* @param $op
2011-01-13 00:43:21 +00:00
* The operation that is to be performed on the comment. Only 'edit' is
* recognized now.
2007-12-16 21:01:45 +00:00
* @param $comment
* The comment object.
2012-01-03 04:36:15 +00:00
*
2007-12-16 21:01:45 +00:00
* @return
* TRUE if the current user has acces to the comment, FALSE otherwise.
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') {
2010-08-20 01:21:14 +00:00
return ($user->uid && $user->uid == $comment->uid && $comment->status == COMMENT_PUBLISHED && user_access('edit own comments')) || user_access('administer comments');
2001-12-08 11:37:07 +00:00
}
}
2004-06-04 18:50:29 +00:00
2005-07-19 17:51:59 +00:00
/**
* Accepts a submission of new or changed comment content.
*
2009-06-22 15:35:54 +00:00
* @param $comment
2009-07-01 20:39:20 +00:00
* A comment object.
2005-07-19 17:51:59 +00:00
*/
2009-07-01 20:39:20 +00:00
function comment_save($comment) {
2011-12-05 12:12:15 +00:00
$comment->save();
2001-12-08 11:37:07 +00:00
}
2009-07-31 19:44:21 +00:00
/**
2012-01-03 04:36:15 +00:00
* Deletes a comment and all its replies.
2009-07-31 19:44:21 +00:00
*
* @param $cid
2012-01-03 04:36:15 +00:00
* The ID of the comment to delete.
2009-07-31 19:44:21 +00:00
*/
function comment_delete($cid) {
comment_delete_multiple(array($cid));
}
/**
2012-01-03 04:36:15 +00:00
* Deletes comments and all their replies.
2009-07-31 19:44:21 +00:00
*
* @param $cids
2012-01-03 04:36:15 +00:00
* The IDs of the comments to delete.
2011-12-17 12:43:37 +00:00
*
* @see hook_comment_predelete()
* @see hook_comment_delete()
2009-07-31 19:44:21 +00:00
*/
function comment_delete_multiple($cids) {
2011-12-05 12:12:15 +00:00
entity_delete_multiple('comment', $cids);
2009-07-31 19:44:21 +00:00
}
2004-07-08 19:58:10 +00:00
/**
2012-01-03 04:36:15 +00:00
* Loads comments from the database.
2009-07-10 05:50:08 +00:00
*
* @param $cids
* An array of comment IDs.
* @param $conditions
2010-11-20 09:43:43 +00:00
* (deprecated) An associative array of conditions on the {comments}
* table, where the keys are the database fields and the values are the
* values those fields must have. Instead, it is preferable to use
* EntityFieldQuery to retrieve a list of entity IDs loadable by
* this function.
2011-09-27 15:11:16 +00:00
* @param $reset
* Whether to reset the internal static entity cache. Note that the static
* cache is disabled in comment_entity_info() by default.
2010-11-20 09:43:43 +00:00
*
2009-07-10 05:50:08 +00:00
* @return
2010-11-20 09:43:43 +00:00
* An array of comment objects, indexed by comment ID.
*
2012-01-03 04:36:15 +00:00
* @todo Remove $conditions in Drupal 8.
*
2010-11-20 09:43:43 +00:00
* @see entity_load()
* @see EntityFieldQuery
2007-12-08 14:06:23 +00:00
*/
2011-09-27 15:11:16 +00:00
function comment_load_multiple($cids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('comment', $cids, $conditions, $reset);
2009-07-10 05:50:08 +00:00
}
2004-07-08 19:58:10 +00:00
2005-12-07 05:35:02 +00:00
/**
2012-01-03 04:36:15 +00:00
* Loads the entire comment by comment ID.
2007-12-16 21:01:45 +00:00
*
* @param $cid
2012-01-03 04:36:15 +00:00
* The ID of the comment to be loaded.
2011-09-27 15:11:16 +00:00
* @param $reset
* Whether to reset the internal static entity cache. Note that the static
* cache is disabled in comment_entity_info() by default.
*
2007-12-16 21:01:45 +00:00
* @return
* The comment object.
2005-12-07 05:35:02 +00:00
*/
2011-09-27 15:11:16 +00:00
function comment_load($cid, $reset = FALSE) {
$comment = comment_load_multiple(array($cid), array(), $reset);
2010-02-09 12:29:39 +00:00
return $comment ? $comment[$cid] : FALSE;
2005-12-07 05:35:02 +00:00
}
2004-07-08 19:58:10 +00:00
/**
2012-01-03 04:36:15 +00:00
* Gets the number of new comments for the current user and the specified node.
2004-07-08 19:58:10 +00:00
*
2007-12-16 21:01:45 +00:00
* @param $nid
2012-01-03 04:36:15 +00:00
* Node ID to count comments for.
2007-12-16 21:01:45 +00:00
* @param $timestamp
2008-05-14 13:12:41 +00:00
* Time to count from (defaults to time of last user access
* to node).
2012-01-03 04:36:15 +00:00
*
* @return
* The number of new comments or FALSE if the user is not logged in.
2004-07-08 19:58:10 +00:00
*/
function comment_num_new($nid, $timestamp = 0) {
global $user;
if ($user->uid) {
2008-05-14 13:12:41 +00:00
// Retrieve the timestamp at which the current user last viewed this node.
2004-07-08 19:58:10 +00:00
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.
2011-09-16 18:58:27 +00:00
return db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND created > :timestamp AND status = :status', array(
2008-11-11 21:44:01 +00:00
':nid' => $nid,
2009-11-21 17:53:17 +00:00
':timestamp' => $timestamp,
2009-05-21 10:34:55 +00:00
':status' => COMMENT_PUBLISHED,
))->fetchField();
2002-09-15 13:00:12 +00:00
}
else {
2008-06-18 03:36:24 +00:00
return FALSE;
2002-09-15 13:00:12 +00:00
}
}
2009-06-19 13:03:53 +00:00
/**
2012-01-03 04:36:15 +00:00
* Gets the display ordinal for a comment, starting from 0.
2009-06-19 13:03:53 +00:00
*
* Count the number of comments which appear before the comment we want to
* display, taking into account display settings and threading.
*
* @param $cid
* The comment ID.
* @param $node_type
* The node type of the comment's parent.
2012-01-03 04:36:15 +00:00
*
2009-06-19 13:03:53 +00:00
* @return
* The display ordinal for the comment.
2012-01-03 04:36:15 +00:00
*
2009-06-19 13:03:53 +00:00
* @see comment_get_display_page()
*/
function comment_get_display_ordinal($cid, $node_type) {
// Count how many comments (c1) are before $cid (c2) in display order. This is
// the 0-based display ordinal.
$query = db_select('comment', 'c1');
$query->innerJoin('comment', 'c2', 'c2.nid = c1.nid');
$query->addExpression('COUNT(*)', 'count');
$query->condition('c2.cid', $cid);
if (!user_access('administer comments')) {
$query->condition('c1.status', COMMENT_PUBLISHED);
}
2009-07-01 12:06:22 +00:00
$mode = variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED);
2009-06-19 13:03:53 +00:00
2009-07-01 12:06:22 +00:00
if ($mode == COMMENT_MODE_FLAT) {
2009-06-19 13:03:53 +00:00
// For flat comments, cid is used for ordering comments due to
// unpredicatable behavior with timestamp, so we make the same assumption
// here.
2009-06-30 09:57:57 +00:00
$query->condition('c1.cid', $cid, '<');
2009-06-19 13:03:53 +00:00
}
else {
// For threaded comments, the c.thread column is used for ordering. We can
2011-10-01 19:32:36 +00:00
// use the sorting code for comparison, but must remove the trailing slash.
2010-03-26 17:14:46 +00:00
// See comment_view_multiple().
2009-06-19 13:03:53 +00:00
$query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) -1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) -1))');
}
return $query->execute()->fetchField();
}
/**
2012-01-03 04:36:15 +00:00
* Returns the page number for a comment.
2009-06-19 13:03:53 +00:00
*
* Finds the correct page number for a comment taking into account display
* and paging settings.
*
* @param $cid
* The comment ID.
* @param $node_type
* The node type the comment is attached to.
2012-01-03 04:36:15 +00:00
*
2009-06-19 13:03:53 +00:00
* @return
* The page number.
*/
function comment_get_display_page($cid, $node_type) {
$ordinal = comment_get_display_ordinal($cid, $node_type);
$comments_per_page = variable_get('comment_default_per_page_' . $node_type, 50);
return floor($ordinal / $comments_per_page);
}
2010-02-22 15:38:52 +00:00
/**
2012-01-03 04:36:15 +00:00
* Page callback: Displays the comment editing form.
*
* Path: comment/%comment/edit
*
* @param $comment
* The comment object representing the comment to be edited.
*
* @see comment_menu()
2010-02-22 15:38:52 +00:00
*/
function comment_edit_page($comment) {
drupal_set_title(t('Edit comment %comment', array('%comment' => $comment->subject)), PASS_THROUGH);
2010-09-09 23:01:48 +00:00
$node = node_load($comment->nid);
return drupal_get_form("comment_node_{$node->type}_form", $comment);
}
/**
* Implements hook_forms().
*/
function comment_forms() {
$forms = array();
foreach (node_type_get_types() as $type) {
$forms["comment_node_{$type->type}_form"]['callback'] = 'comment_form';
}
return $forms;
2010-02-22 15:38:52 +00:00
}
2007-10-31 17:50:47 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form constructor for the basic commenting form.
2007-10-31 17:50:47 +00:00
*
2008-01-08 10:35:43 +00:00
* @see comment_form_validate()
* @see comment_form_submit()
2012-01-03 04:36:15 +00:00
* @see comment_form_build_preview()
2010-03-26 17:14:46 +00:00
* @ingroup forms
2007-10-31 17:50:47 +00:00
*/
2009-09-18 00:12:48 +00:00
function comment_form($form, &$form_state, $comment) {
2009-11-18 19:37:21 +00:00
global $user;
2009-07-21 21:48:24 +00:00
2010-06-17 13:44:45 +00:00
// During initial form build, add the comment entity to the form state for
// use during form building and processing. During a rebuild, use what is in
// the form state.
if (!isset($form_state['comment'])) {
$form_state['comment'] = $comment;
}
else {
$comment = $form_state['comment'];
}
2009-08-01 05:58:18 +00:00
$node = node_load($comment->nid);
2009-11-18 19:37:21 +00:00
$form['#node'] = $node;
2005-11-21 08:32:31 +00:00
2010-09-09 23:01:48 +00:00
// Use #comment-form as unique jump target, regardless of node type.
$form['#id'] = drupal_html_id('comment_form');
$form['#theme'] = array('comment_form__node_' . $node->type, 'comment_form');
2009-11-28 09:18:17 +00:00
$anonymous_contact = variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT);
$is_admin = (!empty($comment->cid) && user_access('administer comments'));
if (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
2010-11-30 17:16:37 +00:00
$form['#attached']['library'][] = array('system', 'jquery.cookie');
2009-10-16 15:54:33 +00:00
$form['#attributes']['class'][] = 'user-info-from-cookie';
2007-05-17 20:57:19 +00:00
}
2009-07-21 21:48:24 +00:00
2009-11-28 09:18:17 +00:00
// If not replying to a comment, use our dedicated page callback for new
// comments on nodes.
if (empty($comment->cid) && empty($comment->pid)) {
$form['#action'] = url('comment/reply/' . $comment->nid);
}
2009-07-21 21:48:24 +00:00
if (isset($form_state['comment_preview'])) {
$form += $form_state['comment_preview'];
}
2009-11-28 09:18:17 +00:00
// Display author information in a fieldset for comment moderators.
if ($is_admin) {
$form['author'] = array(
'#type' => 'fieldset',
'#title' => t('Administration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -2,
);
}
2010-04-22 10:12:25 +00:00
else {
// Sets the author form elements above the subject.
$form['author'] = array(
'#weight' => -2,
);
}
2010-01-07 05:23:52 +00:00
2009-11-28 09:18:17 +00:00
// Prepare default values for form elements.
if ($is_admin) {
2010-04-22 10:12:25 +00:00
$author = (!$comment->uid && $comment->name ? $comment->name : $comment->registered_name);
2009-11-28 09:18:17 +00:00
$status = (isset($comment->status) ? $comment->status : COMMENT_NOT_PUBLISHED);
2010-12-28 18:19:23 +00:00
$date = (!empty($comment->date) ? $comment->date : format_date($comment->created, 'custom', 'Y-m-d H:i O'));
2009-11-28 09:18:17 +00:00
}
else {
if ($user->uid) {
$author = $user->name;
2005-07-19 17:51:59 +00:00
}
else {
2010-12-09 01:57:56 +00:00
$author = ($comment->name ? $comment->name : '');
2005-07-19 17:51:59 +00:00
}
2010-10-05 06:17:29 +00:00
$status = (user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED);
2009-11-28 09:18:17 +00:00
$date = '';
2004-05-18 18:41:46 +00:00
}
2009-11-28 09:18:17 +00:00
// Add the author name field depending on the current user.
if ($is_admin) {
$form['author']['name'] = array(
2008-05-14 13:12:41 +00:00
'#type' => 'textfield',
2009-11-28 09:18:17 +00:00
'#title' => t('Authored by'),
'#default_value' => $author,
2008-05-14 13:12:41 +00:00
'#maxlength' => 60,
'#size' => 30,
2011-05-14 02:16:34 +00:00
'#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
'#autocomplete_path' => 'user/autocomplete',
2005-10-07 06:11:12 +00:00
);
2009-11-28 09:18:17 +00:00
}
elseif ($user->uid) {
$form['author']['_author'] = array(
'#type' => 'item',
'#title' => t('Your name'),
'#markup' => theme('username', array('account' => $user)),
2008-05-14 13:12:41 +00:00
);
2009-11-28 09:18:17 +00:00
$form['author']['name'] = array(
'#type' => 'value',
'#value' => $author,
2005-10-07 06:11:12 +00:00
);
2004-05-18 18:41:46 +00:00
}
2009-11-28 09:18:17 +00:00
else {
$form['author']['name'] = array(
2008-05-14 13:12:41 +00:00
'#type' => 'textfield',
'#title' => t('Your name'),
2009-11-28 09:18:17 +00:00
'#default_value' => $author,
2010-08-22 10:01:06 +00:00
'#required' => (!$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT),
2008-05-14 13:12:41 +00:00
'#maxlength' => 60,
'#size' => 30,
);
2004-07-08 15:24:30 +00:00
}
2004-05-18 18:41:46 +00:00
2009-11-28 09:18:17 +00:00
// Add author e-mail and homepage fields depending on the current user.
$form['author']['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#default_value' => $comment->mail,
2010-08-22 10:01:06 +00:00
'#required' => (!$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT),
2009-11-28 09:18:17 +00:00
'#maxlength' => 64,
'#size' => 30,
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
'#access' => $is_admin || (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
);
$form['author']['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#default_value' => $comment->homepage,
'#maxlength' => 255,
'#size' => 30,
'#access' => $is_admin || (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
);
// Add administrative comment publishing options.
$form['author']['date'] = array(
'#type' => 'textfield',
'#title' => t('Authored on'),
'#default_value' => $date,
'#maxlength' => 25,
'#size' => 20,
'#access' => $is_admin,
);
$form['author']['status'] = array(
'#type' => 'radios',
'#title' => t('Status'),
'#default_value' => $status,
'#options' => array(
COMMENT_PUBLISHED => t('Published'),
COMMENT_NOT_PUBLISHED => t('Not published'),
),
'#access' => $is_admin,
);
2007-03-27 05:13:55 +00:00
2009-11-28 09:18:17 +00:00
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 64,
'#default_value' => $comment->subject,
'#access' => variable_get('comment_subject_field_' . $node->type, 1) == 1,
2010-01-07 05:23:52 +00:00
'#weight' => -1,
2007-03-27 05:13:55 +00:00
);
2004-01-03 12:32:21 +00:00
2009-11-28 09:18:17 +00:00
// Used for conditional validation of author fields.
$form['is_anonymous'] = array(
2009-07-31 19:44:21 +00:00
'#type' => 'value',
2009-11-28 09:18:17 +00:00
'#value' => ($comment->cid ? !$comment->uid : !$user->uid),
2009-07-31 19:44:21 +00:00
);
2004-01-03 12:32:21 +00:00
2009-11-28 09:18:17 +00:00
// Add internal comment properties.
foreach (array('cid', 'pid', 'nid', 'language', 'uid') as $key) {
$form[$key] = array('#type' => 'value', '#value' => $comment->$key);
}
$form['node_type'] = array('#type' => 'value', '#value' => 'comment_node_' . $node->type);
2008-06-20 16:52:55 +00:00
// Only show the save button if comment previews are optional or if we are
2010-07-24 17:48:16 +00:00
// already previewing the submission.
2010-04-24 14:49:14 +00:00
$form['actions'] = array('#type' => 'actions');
2010-01-03 21:01:04 +00:00
$form['actions']['submit'] = array(
2009-10-16 13:20:16 +00:00
'#type' => 'submit',
'#value' => t('Save'),
2010-07-24 17:48:16 +00:00
'#access' => ($comment->cid && user_access('administer comments')) || variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || isset($form_state['comment_preview']),
2009-10-16 13:20:16 +00:00
'#weight' => 19,
);
2010-01-03 21:01:04 +00:00
$form['actions']['preview'] = array(
2009-07-21 21:48:24 +00:00
'#type' => 'submit',
2008-05-14 13:12:41 +00:00
'#value' => t('Preview'),
2009-10-16 13:20:16 +00:00
'#access' => (variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED),
2008-05-14 13:12:41 +00:00
'#weight' => 20,
2009-07-21 21:48:24 +00:00
'#submit' => array('comment_form_build_preview'),
2008-05-14 13:12:41 +00:00
);
2004-01-03 12:32:21 +00:00
2009-11-28 09:18:17 +00:00
// Attach fields.
2009-07-31 19:44:21 +00:00
$comment->node_type = 'comment_node_' . $node->type;
field_attach_form('comment', $comment, $form, $form_state);
2006-08-18 18:58:47 +00:00
return $form;
}
2005-10-07 06:11:12 +00:00
2007-12-16 21:01:45 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form submission handler for the 'preview' button in comment_form().
2007-12-16 21:01:45 +00:00
*/
2009-07-21 21:48:24 +00:00
function comment_form_build_preview($form, &$form_state) {
2010-11-20 06:49:47 +00:00
$comment = comment_form_submit_build_comment($form, $form_state);
2009-07-21 21:48:24 +00:00
$form_state['comment_preview'] = comment_preview($comment);
2010-06-17 13:44:45 +00:00
$form_state['rebuild'] = TRUE;
2009-07-21 21:48:24 +00:00
}
/**
2012-01-03 04:36:15 +00:00
* Generates a comment preview.
*
* @see comment_form_build_preview()
2009-07-21 21:48:24 +00:00
*/
function comment_preview($comment) {
2005-11-21 08:32:31 +00:00
global $user;
2009-07-21 21:48:24 +00:00
2008-10-13 00:33:05 +00:00
drupal_set_title(t('Preview comment'), PASS_THROUGH);
2005-11-21 08:32:31 +00:00
2009-07-21 21:48:24 +00:00
$node = node_load($comment->nid);
2005-11-21 08:32:31 +00:00
if (!form_get_errors()) {
2010-01-07 05:23:52 +00:00
$comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format'];
2007-03-01 19:53:04 +00:00
// Attach the user and time information.
2009-11-28 09:18:17 +00:00
if (!empty($comment->name)) {
$account = user_load_by_name($comment->name);
2007-03-01 19:53:04 +00:00
}
2009-11-28 09:18:17 +00:00
elseif ($user->uid && empty($comment->is_anonymous)) {
2007-03-01 19:53:04 +00:00
$account = $user;
}
2008-05-14 13:12:41 +00:00
2009-11-28 09:18:17 +00:00
if (!empty($account->uid)) {
2007-03-01 19:53:04 +00:00
$comment->uid = $account->uid;
$comment->name = check_plain($account->name);
2007-03-30 07:45:20 +00:00
}
2007-11-05 14:25:58 +00:00
elseif (empty($comment->name)) {
2007-03-30 07:45:20 +00:00
$comment->name = variable_get('anonymous', t('Anonymous'));
2007-03-01 19:53:04 +00:00
}
2008-05-14 13:12:41 +00:00
2009-10-10 13:37:11 +00:00
$comment->created = !empty($comment->created) ? $comment->created : REQUEST_TIME;
$comment->changed = REQUEST_TIME;
2009-07-28 10:09:25 +00:00
$comment->in_preview = TRUE;
2009-12-21 13:47:32 +00:00
$comment_build = comment_view($comment, $node);
2010-07-29 00:50:56 +00:00
$comment_build['#weight'] = -100;
2008-05-14 13:12:41 +00:00
2009-07-28 10:09:25 +00:00
$form['comment_preview'] = $comment_build;
}
2005-11-21 08:32:31 +00:00
2009-07-21 21:48:24 +00:00
if ($comment->pid) {
2009-07-31 19:44:21 +00:00
$build = array();
if ($comments = comment_load_multiple(array($comment->pid), array('status' => COMMENT_PUBLISHED))) {
$parent_comment = $comments[$comment->pid];
2009-12-21 13:47:32 +00:00
$build = comment_view($parent_comment, $node);
2009-07-31 19:44:21 +00:00
}
2005-11-21 08:32:31 +00:00
}
else {
2009-12-21 13:47:32 +00:00
$build = node_view($node);
2005-11-21 08:32:31 +00:00
}
2009-07-28 10:09:25 +00:00
$form['comment_output_below'] = $build;
$form['comment_output_below']['#weight'] = 100;
2005-11-21 08:32:31 +00:00
return $form;
}
2007-12-16 21:01:45 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form validation handler for comment_form().
*
* @see comment_form_submit()
2007-12-16 21:01:45 +00:00
*/
2007-06-04 07:22:23 +00:00
function comment_form_validate($form, &$form_state) {
2007-05-29 06:27:37 +00:00
global $user;
2010-06-17 13:44:45 +00:00
entity_form_field_validate('comment', $form, $form_state);
2009-07-31 19:44:21 +00:00
2009-11-28 09:18:17 +00:00
if (!empty($form_state['values']['cid'])) {
2011-05-14 02:16:34 +00:00
// Verify the name in case it is being changed from being anonymous.
$account = user_load_by_name($form_state['values']['name']);
$form_state['values']['uid'] = $account ? $account->uid : 0;
2009-11-28 09:18:17 +00:00
if ($form_state['values']['date'] && strtotime($form_state['values']['date']) === FALSE) {
2009-06-22 15:35:54 +00:00
form_set_error('date', t('You have to specify a valid date.'));
}
2011-05-14 02:16:34 +00:00
if ($form_state['values']['name'] && !$form_state['values']['is_anonymous'] && !$account) {
2009-11-28 09:18:17 +00:00
form_set_error('name', t('You have to specify a valid author.'));
}
2009-06-22 15:35:54 +00:00
}
2011-05-14 02:16:34 +00:00
elseif ($form_state['values']['is_anonymous']) {
// Validate anonymous comment author fields (if given). If the (original)
// author of this comment was an anonymous user, verify that no registered
// user with this name exists.
2010-08-22 10:01:06 +00:00
if ($form_state['values']['name']) {
$query = db_select('users', 'u');
$query->addField('u', 'uid', 'uid');
$taken = $query
->condition('name', db_like($form_state['values']['name']), 'LIKE')
->countQuery()
->execute()
->fetchField();
if ($taken) {
form_set_error('name', t('The name you used belongs to a registered user.'));
2009-06-22 15:35:54 +00:00
}
}
}
2010-08-22 10:01:06 +00:00
if ($form_state['values']['mail'] && !valid_email_address($form_state['values']['mail'])) {
form_set_error('mail', t('The e-mail address you specified is not valid.'));
}
if ($form_state['values']['homepage'] && !valid_url($form_state['values']['homepage'], TRUE)) {
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>.'));
}
2005-11-21 08:32:31 +00:00
}
2007-12-16 21:01:45 +00:00
/**
* Prepare a comment for submission.
*/
2009-07-21 21:48:24 +00:00
function comment_submit($comment) {
2010-06-17 13:44:45 +00:00
if (empty($comment->date)) {
$comment->date = 'now';
2006-01-18 15:09:39 +00:00
}
2010-06-17 13:44:45 +00:00
$comment->created = strtotime($comment->date);
$comment->changed = REQUEST_TIME;
2009-10-10 13:37:11 +00:00
2010-12-09 01:57:56 +00:00
// If the comment was posted by a registered user, assign the author's ID.
// @todo Too fragile. Should be prepared and stored in comment_form() already.
2010-08-22 10:01:06 +00:00
if (!$comment->is_anonymous && !empty($comment->name) && ($account = user_load_by_name($comment->name))) {
2010-06-17 13:44:45 +00:00
$comment->uid = $account->uid;
2006-01-18 15:09:39 +00:00
}
2010-12-09 01:57:56 +00:00
// If the comment was posted by an anonymous user and no author name was
// required, use "Anonymous" by default.
if ($comment->is_anonymous && (!isset($comment->name) || $comment->name === '')) {
$comment->name = variable_get('anonymous', t('Anonymous'));
}
2008-05-14 13:12:41 +00:00
// Validate the comment's subject. If not specified, extract from comment body.
2010-06-17 13:44:45 +00:00
if (trim($comment->subject) == '') {
2008-05-14 13:12:41 +00:00
// The body may be in any format, so:
2006-01-18 15:09:39 +00:00
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
2011-06-30 06:51:49 +00:00
$comment_body = $comment->comment_body[LANGUAGE_NONE][0];
if (isset($comment_body['format'])) {
$comment_text = check_markup($comment_body['value'], $comment_body['format']);
}
else {
$comment_text = check_plain($comment_body['value']);
}
$comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE);
2006-12-12 06:06:41 +00:00
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
2010-06-17 13:44:45 +00:00
if ($comment->subject == '') {
$comment->subject = t('(No subject)');
2006-12-12 06:06:41 +00:00
}
2005-11-21 08:32:31 +00:00
}
2010-06-17 13:44:45 +00:00
return $comment;
2009-07-21 21:48:24 +00:00
}
/**
2012-01-03 04:36:15 +00:00
* Updates the comment entity by processing the submission's values.
2010-06-17 13:44:45 +00:00
*
2010-11-20 06:49:47 +00:00
* This is the default builder function for the comment form. It is called
2010-06-17 13:44:45 +00:00
* during the "Save" and "Preview" submit handlers to retrieve the entity to
* save or preview. This function can also be called by a "Next" button of a
* wizard to update the form state's entity with the current step's values
* before proceeding to the next step.
*
* @see comment_form()
2012-01-03 04:36:15 +00:00
* @see comment_form_preview()
* @see comment_form_submit()
2009-07-21 21:48:24 +00:00
*/
function comment_form_submit_build_comment($form, &$form_state) {
2010-06-17 13:44:45 +00:00
$comment = $form_state['comment'];
entity_form_submit_build_entity('comment', $comment, $form, $form_state);
comment_submit($comment);
2009-07-21 21:48:24 +00:00
return $comment;
2006-01-18 15:09:39 +00:00
}
2007-12-16 21:01:45 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form submission handler for comment_form().
*
* @see comment_form_validate()
* @see comment_form_submit_build_comment()
2007-12-16 21:01:45 +00:00
*/
2007-06-04 07:22:23 +00:00
function comment_form_submit($form, &$form_state) {
2009-07-21 21:48:24 +00:00
$node = node_load($form_state['values']['nid']);
2010-11-20 06:49:47 +00:00
$comment = comment_form_submit_build_comment($form, $form_state);
2009-06-22 15:35:54 +00:00
if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {
2009-10-09 15:39:12 +00:00
// Save the anonymous user information to a cookie for reuse.
2011-04-10 20:42:16 +00:00
if (user_is_anonymous()) {
2010-04-07 16:35:03 +00:00
user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage'))));
2009-10-09 15:39:12 +00:00
}
2009-06-22 15:35:54 +00:00
comment_save($comment);
2009-12-01 16:03:35 +00:00
$form_state['values']['cid'] = $comment->cid;
2010-03-24 10:18:40 +00:00
// Add an entry to the watchdog log.
2011-07-04 16:58:33 +00:00
watchdog('content', 'Comment posted: %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)));
2010-03-24 10:18:40 +00:00
2009-06-22 15:35:54 +00:00
// Explain the approval queue if necessary.
2009-07-01 20:39:20 +00:00
if ($comment->status == COMMENT_NOT_PUBLISHED) {
2009-06-22 15:35:54 +00:00
if (!user_access('administer comments')) {
drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
}
}
else {
drupal_set_message(t('Your comment has been posted.'));
}
2009-09-22 07:36:57 +00:00
$query = array();
// Find the current display page for this comment.
$page = comment_get_display_page($comment->cid, $node->type);
if ($page > 0) {
$query['page'] = $page;
}
// Redirect to the newly posted comment.
2009-10-15 16:18:46 +00:00
$redirect = array('node/' . $node->nid, array('query' => $query, 'fragment' => 'comment-' . $comment->cid));
2009-06-22 15:35:54 +00:00
}
else {
2011-07-04 16:58:33 +00:00
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject), WATCHDOG_WARNING);
2009-07-21 21:48:24 +00:00
drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject)), 'error');
2009-09-22 07:36:57 +00:00
// Redirect the user to the node they are commenting on.
$redirect = 'node/' . $node->nid;
2005-11-21 08:32:31 +00:00
}
2009-06-22 15:35:54 +00:00
$form_state['redirect'] = $redirect;
2010-03-31 11:49:51 +00:00
// Clear the block and page caches so that anonymous users see the comment
// they have posted.
cache_clear_all();
2005-11-21 08:32:31 +00:00
}
2012-01-10 03:22:24 +00:00
/**
* Implements hook_preprocess_block().
*/
function comment_preprocess_block(&$variables) {
if ($variables['block']->module == 'comment') {
$variables['attributes_array']['role'] = 'navigation';
}
}
2007-04-27 07:42:54 +00:00
/**
2012-01-03 04:36:15 +00:00
* Preprocesses variables for comment.tpl.php.
2007-07-20 08:51:13 +00:00
*
* @see comment.tpl.php
2007-04-27 07:42:54 +00:00
*/
function template_preprocess_comment(&$variables) {
2009-07-28 10:09:25 +00:00
$comment = $variables['elements']['#comment'];
2009-09-11 13:37:52 +00:00
$node = $variables['elements']['#node'];
2011-12-05 12:06:35 +00:00
$variables['comment'] = $comment;
$variables['node'] = $node;
$variables['author'] = theme('username', array('account' => $comment));
$variables['created'] = format_date($comment->created);
$variables['changed'] = format_date($comment->changed);
$variables['new'] = !empty($comment->new) ? t('new') : '';
$variables['user_picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', array('account' => $comment)) : '';
2007-04-27 07:42:54 +00:00
$variables['signature'] = $comment->signature;
2010-04-06 19:49:03 +00:00
$uri = entity_uri('comment', $comment);
2010-09-23 19:47:31 +00:00
$uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark'));
2011-12-05 12:06:35 +00:00
$variables['title'] = l($comment->subject, $uri['path'], $uri['options']);
2010-09-23 19:47:31 +00:00
$variables['permalink'] = l(t('Permalink'), $uri['path'], $uri['options']);
2010-12-01 00:18:15 +00:00
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['created']));
2010-01-10 00:41:28 +00:00
// Preprocess fields.
field_attach_preprocess('comment', $comment, $variables['elements'], $variables);
2009-09-10 12:33:46 +00:00
// Helpful $content variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
2009-10-09 01:00:08 +00:00
2008-05-14 13:12:41 +00:00
// Set status to a string representation of comment->status.
2009-07-28 10:09:25 +00:00
if (isset($comment->in_preview)) {
2011-09-16 18:58:27 +00:00
$variables['status'] = 'comment-preview';
2008-01-04 19:24:24 +00:00
}
else {
2011-09-16 18:58:27 +00:00
$variables['status'] = ($comment->status == COMMENT_NOT_PUBLISHED) ? 'comment-unpublished' : 'comment-published';
2008-01-04 19:24:24 +00:00
}
2009-05-28 16:44:07 +00:00
// Gather comment classes.
2011-09-28 03:49:07 +00:00
if ($comment->uid == 0) {
2009-05-28 16:44:07 +00:00
$variables['classes_array'][] = 'comment-by-anonymous';
}
else {
// Published class is not needed. It is either 'comment-preview' or 'comment-unpublished'.
if ($variables['status'] != 'comment-published') {
$variables['classes_array'][] = $variables['status'];
}
if ($comment->uid === $variables['node']->uid) {
$variables['classes_array'][] = 'comment-by-node-author';
}
if ($comment->uid === $variables['user']->uid) {
$variables['classes_array'][] = 'comment-by-viewer';
}
2009-07-28 10:09:25 +00:00
if ($variables['new']) {
2009-05-28 16:44:07 +00:00
$variables['classes_array'][] = 'comment-new';
}
}
2002-09-15 13:00:12 +00:00
}
2007-12-06 09:58:34 +00:00
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a "you can't post comments" notice.
2007-12-06 09:58:34 +00:00
*
2009-10-09 01:00:08 +00:00
* @param $variables
* An associative array containing:
* - node: The comment node.
*
2007-12-06 09:58:34 +00:00
* @ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_comment_post_forbidden($variables) {
$node = $variables['node'];
2002-09-15 13:00:12 +00:00
global $user;
2008-02-06 19:38:28 +00:00
2009-11-02 01:00:42 +00:00
// Since this is expensive to compute, we cache it so that a page with many
// comments only has to query the database once for all the links.
$authenticated_post_comments = &drupal_static(__FUNCTION__, NULL);
2008-01-04 11:34:19 +00:00
if (!$user->uid) {
2009-11-02 01:00:42 +00:00
if (!isset($authenticated_post_comments)) {
// We only output a link if we are certain that users will get permission
// to post comments by logging in.
2011-01-02 23:54:05 +00:00
$comment_roles = user_roles(TRUE, 'post comments');
2010-10-28 02:27:09 +00:00
$authenticated_post_comments = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]);
2009-11-02 01:00:42 +00:00
}
2008-02-06 19:38:28 +00:00
2008-01-04 11:34:19 +00:00
if ($authenticated_post_comments) {
// We cannot use drupal_get_destination() because these links
// sometimes appear on /node and taxonomy listing pages.
2009-07-01 12:06:22 +00:00
if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_SEPARATE_PAGE) {
2009-09-29 15:31:17 +00:00
$destination = array('destination' => "comment/reply/$node->nid#comment-form");
2008-01-04 11:34:19 +00:00
}
else {
2009-09-29 15:31:17 +00:00
$destination = array('destination' => "node/$node->nid#comment-form");
2008-01-04 11:34:19 +00:00
}
2006-01-26 08:27:58 +00:00
2010-05-27 12:29:39 +00:00
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
2008-01-04 15:11:59 +00:00
// Users can register themselves.
2010-01-11 16:25:16 +00:00
return t('<a href="@login">Log in</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))));
2008-01-04 11:34:19 +00:00
}
else {
// Only admins can add new users, no public registration.
2010-01-11 16:25:16 +00:00
return t('<a href="@login">Log in</a> to post comments', array('@login' => url('user/login', array('query' => $destination))));
2008-01-04 11:34:19 +00:00
}
2004-07-14 05:44:36 +00:00
}
2002-09-15 13:00:12 +00:00
}
}
2006-07-19 06:19:17 +00:00
/**
2012-01-03 04:36:15 +00:00
* Preprocesses variables for comment-wrapper.tpl.php.
2007-07-20 08:51:13 +00:00
*
* @see comment-wrapper.tpl.php
* @see theme_comment_wrapper()
2006-07-19 06:19:17 +00:00
*/
2007-07-20 08:51:13 +00:00
function template_preprocess_comment_wrapper(&$variables) {
// Provide contextual information.
2009-07-28 10:09:25 +00:00
$variables['node'] = $variables['content']['#node'];
2009-10-09 02:26:15 +00:00
$variables['display_mode'] = variable_get('comment_default_mode_' . $variables['node']->type, COMMENT_MODE_THREADED);
2010-11-15 00:49:46 +00:00
// The comment form is optional and may not exist.
$variables['content'] += array('comment_form' => array());
2006-07-19 06:19:17 +00:00
}
2004-06-04 18:50:29 +00:00
/**
2012-01-03 04:36:15 +00:00
* Returns an array of viewing modes for comment listings.
2004-06-04 18:50:29 +00:00
*
* We can't use a global variable array because the locale system
2012-01-03 04:36:15 +00:00
* is not initialized yet when the Comment module is loaded.
2004-06-04 18:50:29 +00:00
*/
2003-10-03 15:14:15 +00:00
function _comment_get_modes() {
2005-11-13 09:05:38 +00:00
return array(
2009-07-01 12:06:22 +00:00
COMMENT_MODE_FLAT => t('Flat list'),
COMMENT_MODE_THREADED => t('Threaded list')
2005-11-13 09:05:38 +00:00
);
2003-10-03 15:14:15 +00:00
}
2004-06-04 18:50:29 +00:00
/**
2012-01-03 04:36:15 +00:00
* Returns an array of "comments per page" values that users can select from.
2004-06-04 18:50:29 +00:00
*/
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
}
2007-04-06 04:39:51 +00:00
/**
2012-01-03 04:36:15 +00:00
* Generates a sorting code.
2007-04-06 04:39:51 +00:00
*
* Consists of a leading character indicating length, followed by N digits
2011-10-01 19:32:36 +00:00
* with a numerical value in base 36 (alphadecimal). These codes can be sorted
* as strings without altering numerical order.
2007-04-06 04:39:51 +00:00
*
* It goes:
* 00, 01, 02, ..., 0y, 0z,
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*/
2011-10-01 19:32:36 +00:00
function comment_int_to_alphadecimal($i = 0) {
2010-05-06 05:59:31 +00:00
$num = base_convert((int) $i, 10, 36);
2007-04-06 04:39:51 +00:00
$length = strlen($num);
2008-05-14 13:12:41 +00:00
2007-04-06 04:39:51 +00:00
return chr($length + ord('0') - 1) . $num;
}
2006-02-09 08:33:36 +00:00
/**
2012-01-03 04:36:15 +00:00
* Decodes a sorting code back to an integer.
2011-10-01 19:32:36 +00:00
*
* @see comment_int_to_alphadecimal()
2006-02-09 08:33:36 +00:00
*/
2011-10-01 19:32:36 +00:00
function comment_alphadecimal_to_int($c = '00') {
2006-02-09 08:33:36 +00:00
return base_convert(substr($c, 1), 36, 10);
}
2007-06-29 18:06:51 +00:00
2011-10-01 19:32:36 +00:00
/**
2012-01-03 04:36:15 +00:00
* Increments a sorting code to the next value.
2011-10-01 19:32:36 +00:00
*
* @see comment_int_to_alphadecimal()
*/
function comment_increment_alphadecimal($c = '00') {
return comment_int_to_alphadecimal(comment_alphadecimal_to_int($c) + 1);
}
2007-06-29 18:06:51 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_action_info().
2007-06-29 18:06:51 +00:00
*/
function comment_action_info() {
return array(
2009-10-10 17:29:17 +00:00
'comment_publish_action' => array(
'label' => t('Publish comment'),
'type' => 'comment',
'configurable' => FALSE,
2009-11-06 03:59:06 +00:00
'behavior' => array('changes_property'),
'triggers' => array('comment_presave', 'comment_insert', 'comment_update'),
2009-10-10 17:29:17 +00:00
),
2007-06-29 18:06:51 +00:00
'comment_unpublish_action' => array(
2009-09-19 11:07:37 +00:00
'label' => t('Unpublish comment'),
2007-06-29 18:06:51 +00:00
'type' => 'comment',
'configurable' => FALSE,
2009-11-06 03:59:06 +00:00
'behavior' => array('changes_property'),
'triggers' => array('comment_presave', 'comment_insert', 'comment_update'),
2007-06-29 18:06:51 +00:00
),
'comment_unpublish_by_keyword_action' => array(
2009-09-19 11:07:37 +00:00
'label' => t('Unpublish comment containing keyword(s)'),
2007-06-29 18:06:51 +00:00
'type' => 'comment',
'configurable' => TRUE,
2009-11-06 03:59:06 +00:00
'behavior' => array('changes_property'),
'triggers' => array('comment_presave', 'comment_insert', 'comment_update'),
),
'comment_save_action' => array(
'label' => t('Save comment'),
'type' => 'comment',
'configurable' => FALSE,
2009-09-19 11:07:37 +00:00
'triggers' => array('comment_insert', 'comment_update'),
2009-10-10 17:29:17 +00:00
),
2007-06-29 18:06:51 +00:00
);
}
/**
2010-02-26 16:55:18 +00:00
* Publishes a comment.
2007-06-29 18:06:51 +00:00
*
2009-10-10 17:29:17 +00:00
* @param $comment
* An optional comment object.
2010-02-26 16:55:18 +00:00
* @param array $context
* Array with components:
* - 'cid': Comment ID. Required if $comment is not given.
2009-10-10 17:29:17 +00:00
*
* @ingroup actions
*/
function comment_publish_action($comment, $context = array()) {
2010-12-04 01:51:09 +00:00
if (isset($comment->subject)) {
2009-10-10 17:29:17 +00:00
$subject = $comment->subject;
2009-11-06 03:59:06 +00:00
$comment->status = COMMENT_PUBLISHED;
2009-10-10 17:29:17 +00:00
}
else {
$cid = $context['cid'];
2010-12-04 01:51:09 +00:00
$subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
2009-11-06 03:59:06 +00:00
db_update('comment')
->fields(array('status' => COMMENT_PUBLISHED))
->condition('cid', $cid)
->execute();
2009-10-10 17:29:17 +00:00
}
watchdog('action', 'Published comment %subject.', array('%subject' => $subject));
}
/**
2010-02-26 16:55:18 +00:00
* Unpublishes a comment.
2009-10-10 17:29:17 +00:00
*
2007-06-29 18:06:51 +00:00
* @param $comment
* An optional comment object.
2010-02-26 16:55:18 +00:00
* @param array $context
* Array with components:
* - 'cid': Comment ID. Required if $comment is not given.
2009-10-10 17:29:17 +00:00
*
* @ingroup actions
2007-06-29 18:06:51 +00:00
*/
function comment_unpublish_action($comment, $context = array()) {
2010-12-04 01:51:09 +00:00
if (isset($comment->subject)) {
2007-06-29 18:06:51 +00:00
$subject = $comment->subject;
2009-11-06 03:59:06 +00:00
$comment->status = COMMENT_NOT_PUBLISHED;
2007-06-29 18:06:51 +00:00
}
else {
$cid = $context['cid'];
2010-12-04 01:51:09 +00:00
$subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
2009-11-06 03:59:06 +00:00
db_update('comment')
->fields(array('status' => COMMENT_NOT_PUBLISHED))
->condition('cid', $cid)
->execute();
2007-06-29 18:06:51 +00:00
}
watchdog('action', 'Unpublished comment %subject.', array('%subject' => $subject));
}
2009-10-10 17:29:17 +00:00
/**
2010-02-26 16:55:18 +00:00
* Unpublishes a comment if it contains certain keywords.
2009-10-10 17:29:17 +00:00
*
* @param $comment
2010-02-26 16:55:18 +00:00
* Comment object to modify.
* @param array $context
* Array with components:
* - 'keywords': Keywords to look for. If the comment contains at least one
* of the keywords, it is unpublished.
2009-10-10 17:29:17 +00:00
*
* @ingroup actions
* @see comment_unpublish_by_keyword_action_form()
* @see comment_unpublish_by_keyword_action_submit()
*/
function comment_unpublish_by_keyword_action($comment, $context) {
foreach ($context['keywords'] as $keyword) {
2010-01-07 05:23:52 +00:00
$text = drupal_render($comment);
if (strpos($text, $keyword) !== FALSE) {
2009-11-06 03:59:06 +00:00
$comment->status = COMMENT_NOT_PUBLISHED;
2009-10-10 17:29:17 +00:00
watchdog('action', 'Unpublished comment %subject.', array('%subject' => $comment->subject));
break;
}
}
}
2007-12-16 21:01:45 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form constructor for the blacklisted keywords form.
2007-12-16 21:01:45 +00:00
*
* @ingroup forms
2009-10-10 17:29:17 +00:00
* @see comment_unpublish_by_keyword_action()
* @see comment_unpublish_by_keyword_action_submit()
2007-12-16 21:01:45 +00:00
*/
2007-06-29 18:06:51 +00:00
function comment_unpublish_by_keyword_action_form($context) {
$form['keywords'] = array(
'#title' => t('Keywords'),
'#type' => 'textarea',
2009-02-06 16:25:09 +00:00
'#description' => t('The comment will be unpublished if it contains any of the phrases above. Use a case-sensitive, comma-separated list of phrases. Example: funny, bungee jumping, "Company, Inc."'),
2007-06-29 18:06:51 +00:00
'#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '',
);
2008-05-14 13:12:41 +00:00
2007-06-29 18:06:51 +00:00
return $form;
}
2007-12-16 21:01:45 +00:00
/**
2012-01-03 04:36:15 +00:00
* Form submission handler for comment_unpublish_by_keyword_action_form().
2009-10-10 17:29:17 +00:00
*
* @see comment_unpublish_by_keyword_action()
2007-12-16 21:01:45 +00:00
*/
2007-06-29 18:06:51 +00:00
function comment_unpublish_by_keyword_action_submit($form, $form_state) {
return array('keywords' => drupal_explode_tags($form_state['values']['keywords']));
}
2009-11-06 03:59:06 +00:00
/**
2010-02-26 16:55:18 +00:00
* Saves a comment.
*
* @ingroup actions
2009-11-06 03:59:06 +00:00
*/
function comment_save_action($comment) {
comment_save($comment);
2010-03-31 11:49:51 +00:00
cache_clear_all();
2009-11-06 03:59:06 +00:00
watchdog('action', 'Saved comment %title', array('%title' => $comment->subject));
}
2008-05-14 11:10:54 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_ranking().
2008-05-14 11:10:54 +00:00
*/
function comment_ranking() {
return array(
'comments' => array(
'title' => t('Number of comments'),
2009-08-29 10:46:41 +00:00
'join' => array(
'type' => 'LEFT',
'table' => 'node_comment_statistics',
'alias' => 'node_comment_statistics',
'on' => 'node_comment_statistics.nid = i.sid',
),
2008-05-14 11:10:54 +00:00
// Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
2009-08-29 10:46:41 +00:00
'score' => '2.0 - 2.0 / (1.0 + node_comment_statistics.comment_count * CAST(:scale AS DECIMAL))',
'arguments' => array(':scale' => variable_get('node_cron_comments_scale', 0)),
2008-05-14 11:10:54 +00:00
),
);
2009-01-02 21:17:35 +00:00
}
2009-06-27 10:13:28 +00:00
2009-10-19 18:28:16 +00:00
/**
2009-10-20 17:33:43 +00:00
* Implements hook_rdf_mapping().
2009-10-19 18:28:16 +00:00
*/
function comment_rdf_mapping() {
return array(
array(
'type' => 'comment',
'bundle' => RDF_DEFAULT_BUNDLE,
'mapping' => array(
2010-02-07 09:11:28 +00:00
'rdftype' => array('sioc:Post', 'sioct:Comment'),
2009-10-20 17:33:43 +00:00
'title' => array(
2009-10-19 18:28:16 +00:00
'predicates' => array('dc:title'),
),
'created' => array(
'predicates' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
2010-01-05 18:56:49 +00:00
'changed' => array(
'predicates' => array('dc:modified'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
2010-02-07 09:11:28 +00:00
'comment_body' => array(
2009-10-19 18:28:16 +00:00
'predicates' => array('content:encoded'),
),
2009-10-20 17:33:43 +00:00
'pid' => array(
2009-10-19 18:28:16 +00:00
'predicates' => array('sioc:reply_of'),
2010-01-05 18:56:49 +00:00
'type' => 'rel',
2009-10-19 18:28:16 +00:00
),
2009-10-20 17:33:43 +00:00
'uid' => array(
2009-10-19 18:28:16 +00:00
'predicates' => array('sioc:has_creator'),
2010-01-05 18:56:49 +00:00
'type' => 'rel',
2009-10-19 18:28:16 +00:00
),
2009-10-20 17:33:43 +00:00
'name' => array(
2009-10-19 18:28:16 +00:00
'predicates' => array('foaf:name'),
),
),
),
);
}
2010-08-23 14:53:50 +00:00
/**
* Implements hook_file_download_access().
*/
function comment_file_download_access($field, $entity_type, $entity) {
if ($entity_type == 'comment') {
2011-10-22 04:32:15 +00:00
if (user_access('access comments') && $entity->status == COMMENT_PUBLISHED || user_access('administer comments')) {
$node = node_load($entity->nid);
return node_access('view', $node);
}
return FALSE;
2010-08-23 14:53:50 +00:00
}
}