webmaster@1: '. t('The comment module allows visitors to comment on your posts, creating ad hoc discussion boards. Any content type may have its Default comment setting set to Read/Write to allow comments, or Disabled, to prevent comments. Comment display settings and other controls may also be customized for each content type (some display settings are customizable by individual users).', array('@content-type' => url('admin/content/types'))) .'
';
webmaster@1: $output .= ''. t('Comment permissions are assigned to user roles, and are used to determine whether anonymous users (or other roles) are allowed to comment on posts. If anonymous users are allowed to comment, their individual contact information may be retained in cookies stored on their local computer for use in later comment submissions. When a comment has no replies, it may be (optionally) edited by its author. The comment module uses the same input formats and HTML tags available when creating other forms of content.') .'
';
webmaster@1: $output .= ''. t('For more information, see the online handbook entry for Comment module.', array('@comment' => 'http://drupal.org/handbook/modules/comment/')) .'
';
webmaster@1: return $output;
webmaster@1: case 'admin/content/comment':
webmaster@1: return ''. t("Below is a list of the latest comments posted to your site. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'
';
webmaster@1: case 'admin/content/comment/approval':
webmaster@1: return ''. t("Below is a list of the comments posted to your site that need approval. To approve a comment, click on 'edit' and then change its 'moderation status' to Approved. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'
';
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_theme().
webmaster@1: */
webmaster@1: function comment_theme() {
webmaster@1: return array(
webmaster@1: 'comment_block' => array(
webmaster@1: 'arguments' => array(),
webmaster@1: ),
webmaster@1: 'comment_admin_overview' => array(
webmaster@1: 'arguments' => array('form' => NULL),
webmaster@1: ),
webmaster@1: 'comment_preview' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array(), 'visible' => 1),
webmaster@1: ),
webmaster@1: 'comment_view' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array(), 'visible' => 1),
webmaster@1: ),
webmaster@1: 'comment_controls' => array(
webmaster@1: 'arguments' => array('form' => NULL),
webmaster@1: ),
webmaster@1: 'comment' => array(
webmaster@1: 'template' => 'comment',
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL, 'links' => array()),
webmaster@1: ),
webmaster@1: 'comment_folded' => array(
webmaster@1: 'template' => 'comment-folded',
webmaster@1: 'arguments' => array('comment' => NULL),
webmaster@1: ),
webmaster@1: 'comment_flat_collapsed' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL),
webmaster@1: ),
webmaster@1: 'comment_flat_expanded' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL),
webmaster@1: ),
webmaster@1: 'comment_thread_collapsed' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL),
webmaster@1: ),
webmaster@1: 'comment_thread_expanded' => array(
webmaster@1: 'arguments' => array('comment' => NULL, 'node' => NULL),
webmaster@1: ),
webmaster@1: 'comment_post_forbidden' => array(
webmaster@1: 'arguments' => array('nid' => NULL),
webmaster@1: ),
webmaster@1: 'comment_wrapper' => array(
webmaster@1: 'template' => 'comment-wrapper',
webmaster@1: 'arguments' => array('content' => NULL, 'node' => NULL),
webmaster@1: ),
webmaster@1: 'comment_submitted' => array(
webmaster@1: 'arguments' => array('comment' => NULL),
webmaster@1: ),
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_menu().
webmaster@1: */
webmaster@1: function comment_menu() {
webmaster@1: $items['admin/content/comment'] = array(
webmaster@1: 'title' => 'Comments',
webmaster@1: 'description' => 'List and edit site comments and the comment moderation queue.',
webmaster@1: 'page callback' => 'comment_admin',
webmaster@1: 'access arguments' => array('administer comments'),
webmaster@1: 'file' => 'comment.admin.inc',
webmaster@1: );
webmaster@1:
webmaster@1: // Tabs:
webmaster@1: $items['admin/content/comment/new'] = array(
webmaster@1: 'title' => 'Published comments',
webmaster@1: 'type' => MENU_DEFAULT_LOCAL_TASK,
webmaster@1: 'weight' => -10,
webmaster@1: );
webmaster@1: $items['admin/content/comment/approval'] = array(
webmaster@1: 'title' => 'Approval queue',
webmaster@1: 'page arguments' => array('approval'),
webmaster@5: 'access arguments' => array('administer comments'),
webmaster@1: 'type' => MENU_LOCAL_TASK,
webmaster@1: 'file' => 'comment.admin.inc',
webmaster@1: );
webmaster@1:
webmaster@1: $items['comment/delete'] = array(
webmaster@1: 'title' => 'Delete comment',
webmaster@1: 'page callback' => 'comment_delete',
webmaster@1: 'access arguments' => array('administer comments'),
webmaster@1: 'type' => MENU_CALLBACK,
webmaster@1: 'file' => 'comment.admin.inc',
webmaster@1: );
webmaster@1:
webmaster@1: $items['comment/edit'] = array(
webmaster@1: 'title' => 'Edit comment',
webmaster@1: 'page callback' => 'comment_edit',
webmaster@1: 'access arguments' => array('post comments'),
webmaster@1: 'type' => MENU_CALLBACK,
webmaster@1: 'file' => 'comment.pages.inc',
webmaster@1: );
webmaster@1: $items['comment/reply/%node'] = array(
webmaster@1: 'title' => 'Reply to comment',
webmaster@1: 'page callback' => 'comment_reply',
webmaster@1: 'page arguments' => array(2),
webmaster@1: 'access callback' => 'node_access',
webmaster@1: 'access arguments' => array('view', 2),
webmaster@1: 'type' => MENU_CALLBACK,
webmaster@1: 'file' => 'comment.pages.inc',
webmaster@1: );
webmaster@1:
webmaster@1: return $items;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_node_type().
webmaster@1: */
webmaster@1: function comment_node_type($op, $info) {
webmaster@1: $settings = array(
webmaster@1: 'comment',
webmaster@1: 'comment_default_mode',
webmaster@1: 'comment_default_order',
webmaster@1: 'comment_default_per_page',
webmaster@1: 'comment_controls',
webmaster@1: 'comment_anonymous',
webmaster@1: 'comment_subject_field',
webmaster@1: 'comment_preview',
webmaster@1: 'comment_form_location',
webmaster@1: );
webmaster@1: switch ($op) {
webmaster@1: case 'delete':
webmaster@1: foreach ($settings as $setting) {
webmaster@1: variable_del($setting .'_'. $info->type);
webmaster@1: }
webmaster@1: break;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_perm().
webmaster@1: */
webmaster@1: function comment_perm() {
webmaster@1: return array('access comments', 'post comments', 'administer comments', 'post comments without approval');
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_block().
webmaster@1: *
webmaster@1: * Generates a block with the most recent comments.
webmaster@1: */
webmaster@1: function comment_block($op = 'list', $delta = 0) {
webmaster@1: if ($op == 'list') {
webmaster@1: $blocks[0]['info'] = t('Recent comments');
webmaster@1: return $blocks;
webmaster@1: }
webmaster@1: else if ($op == 'view' && user_access('access comments')) {
webmaster@1: $block['subject'] = t('Recent comments');
webmaster@1: $block['content'] = theme('comment_block');
webmaster@1: return $block;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Find a number of recent comments. This is done in two steps.
webmaster@1: * 1. Find the n (specified by $number) nodes that have the most recent
webmaster@1: * comments. This is done by querying node_comment_statistics which has
webmaster@1: * an index on last_comment_timestamp, and is thus a fast query.
webmaster@1: * 2. Loading the information from the comments table based on the nids found
webmaster@1: * in step 1.
webmaster@1: *
webmaster@1: * @param $number
webmaster@1: * (optional) The maximum number of comments to find.
webmaster@1: * @return
webmaster@1: * An array of comment objects each containing a nid,
webmaster@1: * subject, cid, and timestamp, or an empty array if there are no recent
webmaster@1: * comments visible to the current user.
webmaster@1: */
webmaster@1: function comment_get_recent($number = 10) {
webmaster@1: // Select the $number nodes (visible to the current user) with the most
webmaster@1: // recent comments. This is efficient due to the index on
webmaster@1: // last_comment_timestamp.
webmaster@1: $result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number);
webmaster@1:
webmaster@1: $nids = array();
webmaster@1: while ($row = db_fetch_object($result)) {
webmaster@1: $nids[] = $row->nid;
webmaster@1: }
webmaster@1:
webmaster@1: $comments = array();
webmaster@1: if (!empty($nids)) {
webmaster@1: // From among the comments on the nodes selected in the first query,
webmaster@1: // find the $number most recent comments.
webmaster@1: $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', COMMENT_PUBLISHED, 0, $number);
webmaster@1: while ($comment = db_fetch_object($result)) {
webmaster@1: $comments[] = $comment;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return $comments;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Calculate page number for first new comment.
webmaster@1: *
webmaster@1: * @param $num_comments
webmaster@1: * Number of comments.
webmaster@1: * @param $new_replies
webmaster@1: * Number of new replies.
webmaster@1: * @param $node
webmaster@1: * The first new comment node.
webmaster@1: * @return
webmaster@1: * "page=X" if the page number is greater than zero; empty string otherwise.
webmaster@1: */
webmaster@1: function comment_new_page_count($num_comments, $new_replies, $node) {
webmaster@1: $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
webmaster@1: $mode = _comment_get_display_setting('mode', $node);
webmaster@1: $order = _comment_get_display_setting('sort', $node);
webmaster@1: $pagenum = NULL;
webmaster@1: $flat = in_array($mode, array(COMMENT_MODE_FLAT_COLLAPSED, COMMENT_MODE_FLAT_EXPANDED));
webmaster@1: if ($num_comments <= $comments_per_page || ($flat && $order == COMMENT_ORDER_NEWEST_FIRST)) {
webmaster@1: // Only one page of comments or flat forum and newest first.
webmaster@1: // First new comment will always be on first page.
webmaster@1: $pageno = 0;
webmaster@1: }
webmaster@1: else {
webmaster@1: if ($flat) {
webmaster@1: // Flat comments and oldest first.
webmaster@1: $count = $num_comments - $new_replies;
webmaster@1: }
webmaster@1: else {
webmaster@1: // Threaded comments. See the documentation for comment_render().
webmaster@1: if ($order == COMMENT_ORDER_NEWEST_FIRST) {
webmaster@1: // Newest first: find the last thread with new comment
webmaster@1: $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY thread DESC LIMIT 1', $node->nid, $new_replies);
webmaster@1: $thread = db_result($result);
webmaster@1: $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND thread > '". $thread ."'", $node->nid);
webmaster@1: }
webmaster@1: else {
webmaster@1: // Oldest first: find the first thread with new comment
webmaster@1: $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
webmaster@1: $thread = substr(db_result($result), 0, -1);
webmaster@1: $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '". $thread ."'", $node->nid);
webmaster@1: }
webmaster@1: $count = db_result($result_count);
webmaster@1: }
webmaster@1: $pageno = $count / $comments_per_page;
webmaster@1: }
webmaster@1: if ($pageno >= 1) {
webmaster@1: $pagenum = "page=". intval($pageno);
webmaster@1: }
webmaster@1: return $pagenum;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Returns a formatted list of recent comments to be displayed in the comment block.
webmaster@1: *
webmaster@1: * @return
webmaster@1: * The comment list HTML.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_block() {
webmaster@1: $items = array();
webmaster@1: foreach (comment_get_recent() as $comment) {
webmaster@1: $items[] = l($comment->subject, 'node/'. $comment->nid, array('fragment' => 'comment-'. $comment->cid)) .'
'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
webmaster@1: }
webmaster@1: if ($items) {
webmaster@1: return theme('item_list', $items);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_link().
webmaster@1: */
webmaster@1: function comment_link($type, $node = NULL, $teaser = FALSE) {
webmaster@1: $links = array();
webmaster@1:
webmaster@1: if ($type == 'node' && $node->comment) {
webmaster@1:
webmaster@1: if ($teaser) {
webmaster@1: // Main page: display the number of comments that have been posted.
webmaster@1:
webmaster@1: if (user_access('access comments')) {
webmaster@1: $all = comment_num_all($node->nid);
webmaster@1:
webmaster@1: if ($all) {
webmaster@1: $links['comment_comments'] = array(
webmaster@1: 'title' => format_plural($all, '1 comment', '@count comments'),
webmaster@1: 'href' => "node/$node->nid",
webmaster@1: 'attributes' => array('title' => t('Jump to the first comment of this posting.')),
webmaster@1: 'fragment' => 'comments'
webmaster@1: );
webmaster@1:
webmaster@1: $new = comment_num_new($node->nid);
webmaster@1:
webmaster@1: if ($new) {
webmaster@1: $links['comment_new_comments'] = array(
webmaster@1: 'title' => format_plural($new, '1 new comment', '@count new comments'),
webmaster@1: 'href' => "node/$node->nid",
webmaster@1: 'query' => comment_new_page_count($all, $new, $node),
webmaster@1: 'attributes' => array('title' => t('Jump to the first new comment of this posting.')),
webmaster@1: 'fragment' => 'new'
webmaster@1: );
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: if ($node->comment == COMMENT_NODE_READ_WRITE) {
webmaster@1: if (user_access('post comments')) {
webmaster@1: $links['comment_add'] = array(
webmaster@1: 'title' => t('Add new comment'),
webmaster@1: 'href' => "comment/reply/$node->nid",
webmaster@1: 'attributes' => array('title' => t('Add a new comment to this page.')),
webmaster@1: 'fragment' => 'comment-form'
webmaster@1: );
webmaster@1: }
webmaster@1: else {
webmaster@1: $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: // Node page: add a "post comment" link if the user is allowed to
webmaster@1: // post comments, if this node is not read-only, and if the comment form isn't already shown
webmaster@1:
webmaster@1: if ($node->comment == COMMENT_NODE_READ_WRITE) {
webmaster@1: if (user_access('post comments')) {
webmaster@1: if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
webmaster@1: $links['comment_add'] = array(
webmaster@1: 'title' => t('Add new comment'),
webmaster@1: 'href' => "comment/reply/$node->nid",
webmaster@1: 'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
webmaster@1: 'fragment' => 'comment-form'
webmaster@1: );
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: if ($type == 'comment') {
webmaster@1: $links = comment_links($node, $teaser);
webmaster@1: }
webmaster@1: if (isset($links['comment_forbidden'])) {
webmaster@1: $links['comment_forbidden']['html'] = TRUE;
webmaster@1: }
webmaster@1:
webmaster@1: return $links;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_form_alter().
webmaster@1: */
webmaster@1: function comment_form_alter(&$form, $form_state, $form_id) {
webmaster@1: if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
webmaster@1: $form['comment'] = array(
webmaster@1: '#type' => 'fieldset',
webmaster@1: '#title' => t('Comment settings'),
webmaster@1: '#collapsible' => TRUE,
webmaster@1: '#collapsed' => TRUE,
webmaster@1: );
webmaster@1: $form['comment']['comment'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Default comment setting'),
webmaster@1: '#default_value' => variable_get('comment_'. $form['#node_type']->type, COMMENT_NODE_READ_WRITE),
webmaster@1: '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
webmaster@1: '#description' => t('Users with the administer comments permission will be able to override this setting.'),
webmaster@1: );
webmaster@1: $form['comment']['comment_default_mode'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Default display mode'),
webmaster@1: '#default_value' => variable_get('comment_default_mode_'. $form['#node_type']->type, COMMENT_MODE_THREADED_EXPANDED),
webmaster@1: '#options' => _comment_get_modes(),
webmaster@1: '#description' => t('The default view for comments. Expanded views display the body of the comment. Threaded views keep replies together.'),
webmaster@1: );
webmaster@1: $form['comment']['comment_default_order'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Default display order'),
webmaster@1: '#default_value' => variable_get('comment_default_order_'. $form['#node_type']->type, COMMENT_ORDER_NEWEST_FIRST),
webmaster@1: '#options' => _comment_get_orders(),
webmaster@1: '#description' => t('The default sorting for new users and anonymous users while viewing comments. These users may change their view using the comment control panel. For registered users, this change is remembered as a persistent user preference.'),
webmaster@1: );
webmaster@1: $form['comment']['comment_default_per_page'] = array(
webmaster@1: '#type' => 'select',
webmaster@1: '#title' => t('Default comments per page'),
webmaster@1: '#default_value' => variable_get('comment_default_per_page_'. $form['#node_type']->type, 50),
webmaster@1: '#options' => _comment_per_page(),
webmaster@1: '#description' => t('Default number of comments for each page: more comments are distributed in several pages.'),
webmaster@1: );
webmaster@1: $form['comment']['comment_controls'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Comment controls'),
webmaster@1: '#default_value' => variable_get('comment_controls_'. $form['#node_type']->type, COMMENT_CONTROLS_HIDDEN),
webmaster@1: '#options' => array(
webmaster@1: t('Display above the comments'),
webmaster@1: t('Display below the comments'),
webmaster@1: t('Display above and below the comments'),
webmaster@1: t('Do not display')),
webmaster@1: '#description' => t('Position of the comment controls box. The comment controls let the user change the default display mode and display order of comments.'),
webmaster@1: );
webmaster@1: $form['comment']['comment_anonymous'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Anonymous commenting'),
webmaster@1: '#default_value' => variable_get('comment_anonymous_'. $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
webmaster@1: '#options' => array(
webmaster@1: COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
webmaster@1: COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
webmaster@1: COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information')),
webmaster@1: '#description' => t('This option is enabled when anonymous users have permission to post comments on the permissions page.', array('@url' => url('admin/user/permissions', array('fragment' => 'module-comment')))),
webmaster@1: );
webmaster@1: if (!user_access('post comments', drupal_anonymous_user())) {
webmaster@1: $form['comment']['comment_anonymous']['#disabled'] = TRUE;
webmaster@1: }
webmaster@1: $form['comment']['comment_subject_field'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Comment subject field'),
webmaster@1: '#default_value' => variable_get('comment_subject_field_'. $form['#node_type']->type, 1),
webmaster@1: '#options' => array(t('Disabled'), t('Enabled')),
webmaster@1: '#description' => t('Can users provide a unique subject for their comments?'),
webmaster@1: );
webmaster@1: $form['comment']['comment_preview'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Preview comment'),
webmaster@1: '#default_value' => variable_get('comment_preview_'. $form['#node_type']->type, COMMENT_PREVIEW_REQUIRED),
webmaster@1: '#options' => array(t('Optional'), t('Required')),
webmaster@1: '#description' => t("Forces a user to look at their comment by clicking on a 'Preview' button before they can actually add the comment"),
webmaster@1: );
webmaster@1: $form['comment']['comment_form_location'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#title' => t('Location of comment submission form'),
webmaster@1: '#default_value' => variable_get('comment_form_location_'. $form['#node_type']->type, COMMENT_FORM_SEPARATE_PAGE),
webmaster@1: '#options' => array(t('Display on separate page'), t('Display below post or comments')),
webmaster@1: );
webmaster@1: }
webmaster@1: elseif (isset($form['type']) && isset($form['#node'])) {
webmaster@1: if ($form['type']['#value'] .'_node_form' == $form_id) {
webmaster@1: $node = $form['#node'];
webmaster@1: $form['comment_settings'] = array(
webmaster@1: '#type' => 'fieldset',
webmaster@1: '#access' => user_access('administer comments'),
webmaster@1: '#title' => t('Comment settings'),
webmaster@1: '#collapsible' => TRUE,
webmaster@1: '#collapsed' => TRUE,
webmaster@1: '#weight' => 30,
webmaster@1: );
webmaster@1: $form['comment_settings']['comment'] = array(
webmaster@1: '#type' => 'radios',
webmaster@1: '#parents' => array('comment'),
webmaster@1: '#default_value' => $node->comment,
webmaster@1: '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
webmaster@1: );
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_nodeapi().
webmaster@1: */
webmaster@1: function comment_nodeapi(&$node, $op, $arg = 0) {
webmaster@1: switch ($op) {
webmaster@1: case 'load':
webmaster@1: return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
webmaster@1: break;
webmaster@1:
webmaster@1: case 'prepare':
webmaster@1: if (!isset($node->comment)) {
webmaster@1: $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
webmaster@1: }
webmaster@1: break;
webmaster@1:
webmaster@1: case 'insert':
webmaster@1: db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->changed, $node->uid);
webmaster@1: break;
webmaster@1:
webmaster@1: case 'delete':
webmaster@1: db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
webmaster@1: db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
webmaster@1: break;
webmaster@1:
webmaster@1: case 'update index':
webmaster@1: $text = '';
webmaster@1: $comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED);
webmaster@1: while ($comment = db_fetch_object($comments)) {
webmaster@1: $text .= ''. check_plain($comment->subject) .'
'. check_markup($comment->comment, $comment->format, FALSE);
webmaster@1: }
webmaster@1: return $text;
webmaster@1:
webmaster@1: case 'search result':
webmaster@1: $comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
webmaster@1: return format_plural($comments, '1 comment', '@count comments');
webmaster@1:
webmaster@1: case 'rss item':
webmaster@1: if ($node->comment != COMMENT_NODE_DISABLED) {
webmaster@1: return array(array('key' => 'comments', 'value' => url('node/'. $node->nid, array('fragment' => 'comments', 'absolute' => TRUE))));
webmaster@1: }
webmaster@1: else {
webmaster@1: return array();
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_user().
webmaster@1: */
webmaster@1: function comment_user($type, $edit, &$user, $category = NULL) {
webmaster@1: if ($type == 'delete') {
webmaster@1: db_query('UPDATE {comments} SET uid = 0 WHERE uid = %d', $user->uid);
webmaster@1: db_query('UPDATE {node_comment_statistics} SET last_comment_uid = 0 WHERE last_comment_uid = %d', $user->uid);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * This is *not* a hook_access() implementation. This function is called
webmaster@1: * to determine whether the current user has access to a particular comment.
webmaster@1: *
webmaster@1: * Authenticated users can edit their comments as long they have not been
webmaster@1: * replied to. This prevents people from changing or revising their
webmaster@1: * statements based on the replies to their posts.
webmaster@1: *
webmaster@1: * @param $op
webmaster@1: * The operation that is to be performed on the comment. Only 'edit' is recognized now.
webmaster@1: * @param $comment
webmaster@1: * The comment object.
webmaster@1: * @return
webmaster@1: * TRUE if the current user has acces to the comment, FALSE otherwise.
webmaster@1: */
webmaster@1: function comment_access($op, $comment) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: if ($op == 'edit') {
webmaster@1: return ($user->uid && $user->uid == $comment->uid && comment_num_replies($comment->cid) == 0) || user_access('administer comments');
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * A simple helper function.
webmaster@1: *
webmaster@1: * @return
webmaster@1: * The 0th and the 1st path components joined by a slash.
webmaster@1: */
webmaster@1: function comment_node_url() {
webmaster@1: return arg(0) .'/'. arg(1);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Accepts a submission of new or changed comment content.
webmaster@1: *
webmaster@1: * @param $edit
webmaster@1: * A comment array.
webmaster@1: *
webmaster@1: * @return
webmaster@1: * If the comment is successfully saved the comment ID is returned. If the comment
webmaster@1: * is not saved, FALSE is returned.
webmaster@1: */
webmaster@1: function comment_save($edit) {
webmaster@1: global $user;
webmaster@1: if (user_access('post comments') && (user_access('administer comments') || node_comment_mode($edit['nid']) == COMMENT_NODE_READ_WRITE)) {
webmaster@1: if (!form_get_errors()) {
webmaster@1: $edit += array(
webmaster@1: 'mail' => '',
webmaster@1: 'homepage' => '',
webmaster@1: 'name' => '',
webmaster@1: 'status' => user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
webmaster@1: );
webmaster@1: if ($edit['cid']) {
webmaster@1: // Update the comment in the database.
webmaster@1: db_query("UPDATE {comments} SET status = %d, timestamp = %d, subject = '%s', comment = '%s', format = %d, uid = %d, name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d", $edit['status'], $edit['timestamp'], $edit['subject'], $edit['comment'], $edit['format'], $edit['uid'], $edit['name'], $edit['mail'], $edit['homepage'], $edit['cid']);
webmaster@1:
webmaster@1: // Allow modules to respond to the updating of a comment.
webmaster@1: comment_invoke_comment($edit, 'update');
webmaster@1:
webmaster@1: // Add an entry to the watchdog log.
webmaster@1: watchdog('content', 'Comment: updated %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
webmaster@1: }
webmaster@1: else {
webmaster@1: // Add the comment to database.
webmaster@1: // Here we are building the thread field. See the documentation for
webmaster@1: // comment_render().
webmaster@1: if ($edit['pid'] == 0) {
webmaster@1: // This is a comment with no parent comment (depth 0): we start
webmaster@1: // by retrieving the maximum thread level.
webmaster@1: $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid']));
webmaster@1:
webmaster@1: // Strip the "/" from the end of the thread.
webmaster@1: $max = rtrim($max, '/');
webmaster@1:
webmaster@1: // Finally, build the thread field for this new comment.
webmaster@1: $thread = int2vancode(vancode2int($max) + 1) .'/';
webmaster@1: }
webmaster@1: else {
webmaster@1: // This is comment with a parent comment: we increase
webmaster@1: // the part of the thread value at the proper depth.
webmaster@1:
webmaster@1: // Get the parent comment:
webmaster@1: $parent = _comment_load($edit['pid']);
webmaster@1:
webmaster@1: // Strip the "/" from the end of the parent thread.
webmaster@1: $parent->thread = (string) rtrim((string) $parent->thread, '/');
webmaster@1:
webmaster@1: // Get the max value in _this_ thread.
webmaster@1: $max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid']));
webmaster@1:
webmaster@1: if ($max == '') {
webmaster@1: // First child of this parent.
webmaster@1: $thread = $parent->thread .'.'. int2vancode(0) .'/';
webmaster@1: }
webmaster@1: else {
webmaster@1: // Strip the "/" at the end of the thread.
webmaster@1: $max = rtrim($max, '/');
webmaster@1:
webmaster@1: // We need to get the value at the correct depth.
webmaster@1: $parts = explode('.', $max);
webmaster@1: $parent_depth = count(explode('.', $parent->thread));
webmaster@1: $last = $parts[$parent_depth];
webmaster@1:
webmaster@1: // Finally, build the thread field for this new comment.
webmaster@1: $thread = $parent->thread .'.'. int2vancode(vancode2int($last) + 1) .'/';
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: $edit['timestamp'] = time();
webmaster@1:
webmaster@1: if ($edit['uid'] === $user->uid) { // '===' because we want to modify anonymous users too
webmaster@1: $edit['name'] = $user->name;
webmaster@1: }
webmaster@1:
webmaster@1: db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $edit['status'], $thread, $edit['name'], $edit['mail'], $edit['homepage']);
webmaster@1: $edit['cid'] = db_last_insert_id('comments', 'cid');
webmaster@1:
webmaster@1: // Tell the other modules a new comment has been submitted.
webmaster@1: comment_invoke_comment($edit, 'insert');
webmaster@1:
webmaster@1: // Add an entry to the watchdog log.
webmaster@1: watchdog('content', 'Comment: added %subject.', array('%subject' => $edit['subject']), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], array('fragment' => 'comment-'. $edit['cid'])));
webmaster@1: }
webmaster@1: _comment_update_node_statistics($edit['nid']);
webmaster@1:
webmaster@1: // Clear the cache so an anonymous user can see his comment being added.
webmaster@1: cache_clear_all();
webmaster@1:
webmaster@1: // Explain the approval queue if necessary, and then
webmaster@1: // redirect the user to the node he's commenting on.
webmaster@1: if ($edit['status'] == COMMENT_NOT_PUBLISHED) {
webmaster@1: drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
webmaster@1: }
webmaster@1: else {
webmaster@1: comment_invoke_comment($edit, 'publish');
webmaster@1: }
webmaster@1: return $edit['cid'];
webmaster@1: }
webmaster@1: else {
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
webmaster@1: drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject'])), 'error');
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Build command links for a comment (e.g.\ edit, reply, delete) with respect to the current user's access permissions.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment to which the links will be related.
webmaster@1: * @param $return
webmaster@1: * Not used.
webmaster@1: * @return
webmaster@1: * An associative array containing the links.
webmaster@1: */
webmaster@1: function comment_links($comment, $return = 1) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $links = array();
webmaster@1:
webmaster@1: // If we are viewing just this comment, we link back to the node.
webmaster@1: if ($return) {
webmaster@1: $links['comment_parent'] = array(
webmaster@1: 'title' => t('parent'),
webmaster@1: 'href' => comment_node_url(),
webmaster@1: 'fragment' => "comment-$comment->cid"
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
webmaster@1: if (user_access('administer comments') && user_access('post comments')) {
webmaster@1: $links['comment_delete'] = array(
webmaster@1: 'title' => t('delete'),
webmaster@1: 'href' => "comment/delete/$comment->cid"
webmaster@1: );
webmaster@1: $links['comment_edit'] = array(
webmaster@1: 'title' => t('edit'),
webmaster@1: 'href' => "comment/edit/$comment->cid"
webmaster@1: );
webmaster@1: $links['comment_reply'] = array(
webmaster@1: 'title' => t('reply'),
webmaster@1: 'href' => "comment/reply/$comment->nid/$comment->cid"
webmaster@1: );
webmaster@1: }
webmaster@1: else if (user_access('post comments')) {
webmaster@1: if (comment_access('edit', $comment)) {
webmaster@1: $links['comment_edit'] = array(
webmaster@1: 'title' => t('edit'),
webmaster@1: 'href' => "comment/edit/$comment->cid"
webmaster@1: );
webmaster@1: }
webmaster@1: $links['comment_reply'] = array(
webmaster@1: 'title' => t('reply'),
webmaster@1: 'href' => "comment/reply/$comment->nid/$comment->cid"
webmaster@1: );
webmaster@1: }
webmaster@1: else {
webmaster@1: $node = node_load($comment->nid);
webmaster@1: $links['comment_forbidden']['title'] = theme('comment_post_forbidden', $node);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return $links;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Renders comment(s).
webmaster@1: *
webmaster@1: * @param $node
webmaster@1: * The node which comment(s) needs rendering.
webmaster@1: * @param $cid
webmaster@1: * Optional, if given, only one comment is rendered.
webmaster@1: *
webmaster@1: * To display threaded comments in the correct order we keep a 'thread' field
webmaster@1: * and order by that value. This field keeps this data in
webmaster@1: * a way which is easy to update and convenient to use.
webmaster@1: *
webmaster@1: * A "thread" value starts at "1". If we add a child (A) to this comment,
webmaster@1: * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
webmaster@1: * brother of (A) will get "1.2". Next brother of the parent of (A) will get
webmaster@1: * "2" and so on.
webmaster@1: *
webmaster@1: * First of all note that the thread field stores the depth of the comment:
webmaster@1: * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
webmaster@1: *
webmaster@1: * Now to get the ordering right, consider this example:
webmaster@1: *
webmaster@1: * 1
webmaster@1: * 1.1
webmaster@1: * 1.1.1
webmaster@1: * 1.2
webmaster@1: * 2
webmaster@1: *
webmaster@1: * If we "ORDER BY thread ASC" we get the above result, and this is the
webmaster@1: * natural order sorted by time. However, if we "ORDER BY thread DESC"
webmaster@1: * we get:
webmaster@1: *
webmaster@1: * 2
webmaster@1: * 1.2
webmaster@1: * 1.1.1
webmaster@1: * 1.1
webmaster@1: * 1
webmaster@1: *
webmaster@1: * Clearly, this is not a natural way to see a thread, and users will get
webmaster@1: * confused. The natural order to show a thread by time desc would be:
webmaster@1: *
webmaster@1: * 2
webmaster@1: * 1
webmaster@1: * 1.2
webmaster@1: * 1.1
webmaster@1: * 1.1.1
webmaster@1: *
webmaster@1: * which is what we already did before the standard pager patch. To achieve
webmaster@1: * this we simply add a "/" at the end of each "thread" value. This way out
webmaster@1: * thread fields will look like depicted below:
webmaster@1: *
webmaster@1: * 1/
webmaster@1: * 1.1/
webmaster@1: * 1.1.1/
webmaster@1: * 1.2/
webmaster@1: * 2/
webmaster@1: *
webmaster@1: * we add "/" since this char is, in ASCII, higher than every number, so if
webmaster@1: * now we "ORDER BY thread DESC" we get the correct order. However this would
webmaster@1: * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
webmaster@1: * to consider the trailing "/" so we use a substring only.
webmaster@1: */
webmaster@1: function comment_render($node, $cid = 0) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $output = '';
webmaster@1:
webmaster@1: if (user_access('access comments')) {
webmaster@1: // Pre-process variables.
webmaster@1: $nid = $node->nid;
webmaster@1: if (empty($nid)) {
webmaster@1: $nid = 0;
webmaster@1: }
webmaster@1:
webmaster@1: $mode = _comment_get_display_setting('mode', $node);
webmaster@1: $order = _comment_get_display_setting('sort', $node);
webmaster@1: $comments_per_page = _comment_get_display_setting('comments_per_page', $node);
webmaster@1:
webmaster@1: if ($cid && is_numeric($cid)) {
webmaster@1: // Single comment view.
webmaster@1: $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
webmaster@1: $query_args = array($cid);
webmaster@1: if (!user_access('administer comments')) {
webmaster@1: $query .= ' AND c.status = %d';
webmaster@1: $query_args[] = COMMENT_PUBLISHED;
webmaster@1: }
webmaster@1:
webmaster@1: $query = db_rewrite_sql($query, 'c', 'cid');
webmaster@1: $result = db_query($query, $query_args);
webmaster@1:
webmaster@1: if ($comment = db_fetch_object($result)) {
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@1: $links = module_invoke_all('link', 'comment', $comment, 1);
webmaster@1: drupal_alter('link', $links, $node);
webmaster@1:
webmaster@1: $output .= theme('comment_view', $comment, $node, $links);
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: // Multiple comment view
webmaster@1: $query_count = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
webmaster@1: $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d';
webmaster@1:
webmaster@1: $query_args = array($nid);
webmaster@1: if (!user_access('administer comments')) {
webmaster@1: $query .= ' AND c.status = %d';
webmaster@1: $query_count .= ' AND status = %d';
webmaster@1: $query_args[] = COMMENT_PUBLISHED;
webmaster@1: }
webmaster@1:
webmaster@1: if ($order == COMMENT_ORDER_NEWEST_FIRST) {
webmaster@1: if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
webmaster@1: $query .= ' ORDER BY c.cid DESC';
webmaster@1: }
webmaster@1: else {
webmaster@1: $query .= ' ORDER BY c.thread DESC';
webmaster@1: }
webmaster@1: }
webmaster@1: else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
webmaster@1: if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
webmaster@1: $query .= ' ORDER BY c.cid';
webmaster@1: }
webmaster@1: else {
webmaster@1: // See comment above. Analysis reveals that this doesn't cost too
webmaster@1: // much. It scales much much better than having the whole comment
webmaster@1: // structure.
webmaster@1: $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
webmaster@1: }
webmaster@1: }
webmaster@1: $query = db_rewrite_sql($query, 'c', 'cid');
webmaster@1: $query_count = db_rewrite_sql($query_count, 'c', 'cid');
webmaster@1:
webmaster@1: // Start a form, for use with comment control.
webmaster@1: $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
webmaster@1:
webmaster@1: $divs = 0;
webmaster@1: $num_rows = FALSE;
webmaster@1: $comments = '';
webmaster@1: drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
webmaster@1: while ($comment = db_fetch_object($result)) {
webmaster@1: $comment = drupal_unpack($comment);
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@1: $comment->depth = count(explode('.', $comment->thread)) - 1;
webmaster@1:
webmaster@1: if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
webmaster@1: if ($comment->depth > $divs) {
webmaster@1: $divs++;
webmaster@1: $comments .= '';
webmaster@1: }
webmaster@1: else {
webmaster@1: while ($comment->depth < $divs) {
webmaster@1: $divs--;
webmaster@1: $comments .= '
';
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
webmaster@1: $comments .= theme('comment_flat_collapsed', $comment, $node);
webmaster@1: }
webmaster@1: else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
webmaster@1: $comments .= theme('comment_flat_expanded', $comment, $node);
webmaster@1: }
webmaster@1: else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
webmaster@1: $comments .= theme('comment_thread_collapsed', $comment, $node);
webmaster@1: }
webmaster@1: else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
webmaster@1: $comments .= theme('comment_thread_expanded', $comment, $node);
webmaster@1: }
webmaster@1:
webmaster@1: $num_rows = TRUE;
webmaster@1: }
webmaster@1: while ($divs-- > 0) {
webmaster@1: $comments .= '';
webmaster@1: }
webmaster@1:
webmaster@1: $comment_controls = variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN);
webmaster@1: if ($num_rows && ($comment_controls == COMMENT_CONTROLS_ABOVE || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
webmaster@1: $output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
webmaster@1: }
webmaster@1:
webmaster@1: $output .= $comments;
webmaster@1: $output .= theme('pager', NULL, $comments_per_page, 0);
webmaster@1:
webmaster@1: if ($num_rows && ($comment_controls == COMMENT_CONTROLS_BELOW || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
webmaster@1: $output .= drupal_get_form('comment_controls', $mode, $order, $comments_per_page);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: // If enabled, show new comment form if it's not already being displayed.
webmaster@1: $reply = arg(0) == 'comment' && arg(1) == 'reply';
webmaster@1: if (user_access('post comments') && node_comment_mode($nid) == COMMENT_NODE_READ_WRITE && (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_BELOW) && !$reply) {
webmaster@1: $output .= comment_form_box(array('nid' => $nid), t('Post new comment'));
webmaster@1: }
webmaster@1:
webmaster@1: $output = theme('comment_wrapper', $output, $node);
webmaster@1: }
webmaster@1:
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Comment operations. We offer different update operations depending on
webmaster@1: * which comment administration page we're on.
webmaster@1: *
webmaster@1: * @param $action
webmaster@1: * The comment administration page.
webmaster@1: * @return
webmaster@1: * An associative array containing the offered operations.
webmaster@1: */
webmaster@1: function comment_operations($action = NULL) {
webmaster@1: if ($action == 'publish') {
webmaster@1: $operations = array(
webmaster@1: 'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
webmaster@1: 'delete' => array(t('Delete the selected comments'), '')
webmaster@1: );
webmaster@1: }
webmaster@1: else if ($action == 'unpublish') {
webmaster@1: $operations = array(
webmaster@1: 'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
webmaster@1: 'delete' => array(t('Delete the selected comments'), '')
webmaster@1: );
webmaster@1: }
webmaster@1: else {
webmaster@1: $operations = array(
webmaster@1: 'publish' => array(t('Publish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_PUBLISHED .' WHERE cid = %d'),
webmaster@1: 'unpublish' => array(t('Unpublish the selected comments'), 'UPDATE {comments} SET status = '. COMMENT_NOT_PUBLISHED .' WHERE cid = %d'),
webmaster@1: 'delete' => array(t('Delete the selected comments'), '')
webmaster@1: );
webmaster@1: }
webmaster@1: return $operations;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Misc functions: helpers, privates, history
webmaster@1: */
webmaster@1:
webmaster@1: /**
webmaster@1: * Load the entire comment by cid.
webmaster@1: *
webmaster@1: * @param $cid
webmaster@1: * The identifying comment id.
webmaster@1: * @return
webmaster@1: * The comment object.
webmaster@1: */
webmaster@1: function _comment_load($cid) {
webmaster@1: return db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Get comment count for a node.
webmaster@1: *
webmaster@1: * @param $nid
webmaster@1: * The node id.
webmaster@1: * @return
webmaster@1: * The comment count.
webmaster@1: */
webmaster@1: function comment_num_all($nid) {
webmaster@1: static $cache;
webmaster@1:
webmaster@1: if (!isset($cache[$nid])) {
webmaster@1: $cache[$nid] = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $nid));
webmaster@1: }
webmaster@1: return $cache[$nid];
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Get replies count for a comment.
webmaster@1: *
webmaster@1: * @param $pid
webmaster@1: * The comment id.
webmaster@1: * @return
webmaster@1: * The replies count.
webmaster@1: */
webmaster@1: function comment_num_replies($pid) {
webmaster@1: static $cache;
webmaster@1:
webmaster@1: if (!isset($cache[$pid])) {
webmaster@1: $cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND status = %d', $pid, COMMENT_PUBLISHED));
webmaster@1: }
webmaster@1:
webmaster@1: return $cache[$pid];
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Get number of new comments for current user and specified node.
webmaster@1: *
webmaster@1: * @param $nid
webmaster@1: * node-id to count comments for
webmaster@1: * @param $timestamp
webmaster@1: * time to count from (defaults to time of last user access
webmaster@1: * to node)
webmaster@1: */
webmaster@1: function comment_num_new($nid, $timestamp = 0) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: if ($user->uid) {
webmaster@1: // Retrieve the timestamp at which the current user last viewed the
webmaster@1: // specified node.
webmaster@1: if (!$timestamp) {
webmaster@1: $timestamp = node_last_viewed($nid);
webmaster@1: }
webmaster@1: $timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
webmaster@1:
webmaster@1: // Use the timestamp to retrieve the number of new comments.
webmaster@1: $result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d', $nid, $timestamp, COMMENT_PUBLISHED));
webmaster@1:
webmaster@1: return $result;
webmaster@1: }
webmaster@1: else {
webmaster@1: return 0;
webmaster@1: }
webmaster@1:
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Validate comment data.
webmaster@1: *
webmaster@1: * @param $edit
webmaster@1: * An associative array containig the comment data.
webmaster@1: * @return
webmaster@1: * The original $edit.
webmaster@1: */
webmaster@1: function comment_validate($edit) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: // Invoke other validation handlers
webmaster@1: comment_invoke_comment($edit, 'validate');
webmaster@1:
webmaster@1: if (isset($edit['date'])) {
webmaster@1: // As of PHP 5.1.0, strtotime returns FALSE upon failure instead of -1.
webmaster@1: if (strtotime($edit['date']) <= 0) {
webmaster@1: form_set_error('date', t('You have to specify a valid date.'));
webmaster@1: }
webmaster@1: }
webmaster@1: if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
webmaster@1: form_set_error('author', t('You have to specify a valid author.'));
webmaster@1: }
webmaster@1:
webmaster@1: // Check validity of name, mail and homepage (if given)
webmaster@1: if (!$user->uid || isset($edit['is_anonymous'])) {
webmaster@1: $node = node_load($edit['nid']);
webmaster@1: if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
webmaster@1: if ($edit['name']) {
webmaster@1: $taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = '%s'", $edit['name']));
webmaster@1:
webmaster@1: if ($taken != 0) {
webmaster@1: form_set_error('name', t('The name you used belongs to a registered user.'));
webmaster@1: }
webmaster@1:
webmaster@1: }
webmaster@1: else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
webmaster@1: form_set_error('name', t('You have to leave your name.'));
webmaster@1: }
webmaster@1:
webmaster@1: if ($edit['mail']) {
webmaster@1: if (!valid_email_address($edit['mail'])) {
webmaster@1: form_set_error('mail', t('The e-mail address you specified is not valid.'));
webmaster@1: }
webmaster@1: }
webmaster@1: else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
webmaster@1: form_set_error('mail', t('You have to leave an e-mail address.'));
webmaster@1: }
webmaster@1:
webmaster@1: if ($edit['homepage']) {
webmaster@1: if (!valid_url($edit['homepage'], TRUE)) {
webmaster@1: 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 http://example.com/directory
.'));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: return $edit;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Generate the basic commenting form, for appending to a node or display on a separate page.
webmaster@1: *
webmaster@1: * @param $title
webmaster@1: * Not used.
webmaster@1: * @ingroup forms
webmaster@1: * @see comment_form_validate()
webmaster@1: * @see comment_form_submit()
webmaster@1: */
webmaster@1: function comment_form(&$form_state, $edit, $title = NULL) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $op = isset($_POST['op']) ? $_POST['op'] : '';
webmaster@1: $node = node_load($edit['nid']);
webmaster@1:
webmaster@1: if (!$user->uid && variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
webmaster@1: drupal_add_js(drupal_get_path('module', 'comment') .'/comment.js');
webmaster@1: }
webmaster@1: $edit += array('name' => '', 'mail' => '', 'homepage' => '');
webmaster@1: if ($user->uid) {
webmaster@1: if (!empty($edit['cid']) && user_access('administer comments')) {
webmaster@1: if (!empty($edit['author'])) {
webmaster@1: $author = $edit['author'];
webmaster@1: }
webmaster@1: elseif (!empty($edit['name'])) {
webmaster@1: $author = $edit['name'];
webmaster@1: }
webmaster@1: else {
webmaster@1: $author = $edit['registered_name'];
webmaster@1: }
webmaster@1:
webmaster@1: if (!empty($edit['status'])) {
webmaster@1: $status = $edit['status'];
webmaster@1: }
webmaster@1: else {
webmaster@1: $status = 0;
webmaster@1: }
webmaster@1:
webmaster@1: if (!empty($edit['date'])) {
webmaster@1: $date = $edit['date'];
webmaster@1: }
webmaster@1: else {
webmaster@1: $date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
webmaster@1: }
webmaster@1:
webmaster@1: $form['admin'] = array(
webmaster@1: '#type' => 'fieldset',
webmaster@1: '#title' => t('Administration'),
webmaster@1: '#collapsible' => TRUE,
webmaster@1: '#collapsed' => TRUE,
webmaster@1: '#weight' => -2,
webmaster@1: );
webmaster@1:
webmaster@1: if ($edit['registered_name'] != '') {
webmaster@1: // The comment is by a registered user
webmaster@1: $form['admin']['author'] = array(
webmaster@1: '#type' => 'textfield',
webmaster@1: '#title' => t('Authored by'),
webmaster@1: '#size' => 30,
webmaster@1: '#maxlength' => 60,
webmaster@1: '#autocomplete_path' => 'user/autocomplete',
webmaster@1: '#default_value' => $author,
webmaster@1: '#weight' => -1,
webmaster@1: );
webmaster@1: }
webmaster@1: else {
webmaster@1: // The comment is by an anonymous user
webmaster@1: $form['is_anonymous'] = array(
webmaster@1: '#type' => 'value',
webmaster@1: '#value' => TRUE,
webmaster@1: );
webmaster@1: $form['admin']['name'] = array(
webmaster@1: '#type' => 'textfield',
webmaster@1: '#title' => t('Authored by'),
webmaster@1: '#size' => 30,
webmaster@1: '#maxlength' => 60,
webmaster@1: '#default_value' => $author,
webmaster@1: '#weight' => -1,
webmaster@1: );
webmaster@1: $form['admin']['mail'] = array(
webmaster@1: '#type' => 'textfield',
webmaster@1: '#title' => t('E-mail'),
webmaster@1: '#maxlength' => 64,
webmaster@1: '#size' => 30,
webmaster@1: '#default_value' => $edit['mail'],
webmaster@1: '#description' => t('The content of this field is kept private and will not be shown publicly.'),
webmaster@1: );
webmaster@1:
webmaster@1: $form['admin']['homepage'] = array(
webmaster@1: '#type' => 'textfield',
webmaster@1: '#title' => t('Homepage'),
webmaster@1: '#maxlength' => 255,
webmaster@1: '#size' => 30,
webmaster@1: '#default_value' => $edit['homepage'],
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: $form['admin']['date'] = array('#type' => 'textfield', '#parents' => array('date'), '#title' => t('Authored on'), '#size' => 20, '#maxlength' => 25, '#default_value' => $date, '#weight' => -1);
webmaster@1:
webmaster@1: $form['admin']['status'] = array('#type' => 'radios', '#parents' => array('status'), '#title' => t('Status'), '#default_value' => $status, '#options' => array(t('Published'), t('Not published')), '#weight' => -1);
webmaster@1:
webmaster@1: }
webmaster@1: else {
webmaster@1: $form['_author'] = array('#type' => 'item', '#title' => t('Your name'), '#value' => theme('username', $user)
webmaster@1: );
webmaster@1: $form['author'] = array('#type' => 'value', '#value' => $user->name);
webmaster@1: }
webmaster@1: }
webmaster@1: else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
webmaster@1: $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous'))
webmaster@1: );
webmaster@1:
webmaster@1: $form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.')
webmaster@1: );
webmaster@1:
webmaster@1: $form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
webmaster@1: }
webmaster@1: else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
webmaster@1: $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')), '#required' => TRUE);
webmaster@1:
webmaster@1: $form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);
webmaster@1:
webmaster@1: $form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
webmaster@1: }
webmaster@1:
webmaster@1: if (variable_get('comment_subject_field_'. $node->type, 1) == 1) {
webmaster@1: $form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#maxlength' => 64, '#default_value' => !empty($edit['subject']) ? $edit['subject'] : '');
webmaster@1: }
webmaster@1:
webmaster@1: if (!empty($edit['comment'])) {
webmaster@1: $default = $edit['comment'];
webmaster@1: }
webmaster@1: else {
webmaster@1: $default = '';
webmaster@1: }
webmaster@1:
webmaster@1: $form['comment_filter']['comment'] = array(
webmaster@1: '#type' => 'textarea',
webmaster@1: '#title' => t('Comment'),
webmaster@1: '#rows' => 15,
webmaster@1: '#default_value' => $default,
webmaster@1: '#required' => TRUE,
webmaster@1: );
webmaster@1: if (!isset($edit['format'])) {
webmaster@1: $edit['format'] = FILTER_FORMAT_DEFAULT;
webmaster@1: }
webmaster@1: $form['comment_filter']['format'] = filter_form($edit['format']);
webmaster@1:
webmaster@1: $form['cid'] = array('#type' => 'value', '#value' => !empty($edit['cid']) ? $edit['cid'] : NULL);
webmaster@1: $form['pid'] = array('#type' => 'value', '#value' => !empty($edit['pid']) ? $edit['pid'] : NULL);
webmaster@1: $form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
webmaster@1: $form['uid'] = array('#type' => 'value', '#value' => !empty($edit['uid']) ? $edit['uid'] : NULL);
webmaster@1:
webmaster@1: // Only show save button if preview is optional or if we are in preview mode.
webmaster@1: // We show the save button in preview mode even if there are form errors so that
webmaster@1: // optional form elements (e.g., captcha) can be updated in preview mode.
webmaster@1: if (!form_get_errors() && ((variable_get('comment_preview_'. $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview')) || ($op == t('Save')))) {
webmaster@1: $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 19);
webmaster@1: }
webmaster@1:
webmaster@1: $form['preview'] = array('#type' => 'button', '#value' => t('Preview'), '#weight' => 20);
webmaster@1: $form['#token'] = 'comment'. $edit['nid'] . (isset($edit['pid']) ? $edit['pid'] : '');
webmaster@1:
webmaster@1: if ($op == t('Preview')) {
webmaster@1: $form['#after_build'] = array('comment_form_add_preview');
webmaster@1: }
webmaster@1:
webmaster@1: if (empty($edit['cid']) && empty($edit['pid'])) {
webmaster@1: $form['#action'] = url('comment/reply/'. $edit['nid']);
webmaster@1: }
webmaster@1:
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme the comment form box.
webmaster@1: *
webmaster@1: * @param $edit
webmaster@1: * The form structure.
webmaster@1: * @param $title
webmaster@1: * The form title.
webmaster@1: */
webmaster@1: function comment_form_box($edit, $title = NULL) {
webmaster@1: return theme('box', $title, drupal_get_form('comment_form', $edit, $title));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Form builder; Generate and validate a comment preview form.
webmaster@1: *
webmaster@1: * @ingroup forms
webmaster@1: */
webmaster@1: function comment_form_add_preview($form, &$form_state) {
webmaster@1: global $user;
webmaster@1: $edit = $form_state['values'];
webmaster@1: drupal_set_title(t('Preview comment'));
webmaster@1:
webmaster@1: $output = '';
webmaster@1: $node = node_load($edit['nid']);
webmaster@1:
webmaster@1: // Invoke full validation for the form, to protect against cross site
webmaster@1: // request forgeries (CSRF) and setting arbitrary values for fields such as
webmaster@1: // the input format. Preview the comment only when form validation does not
webmaster@1: // set any errors.
webmaster@1: drupal_validate_form($form['form_id']['#value'], $form, $form_state);
webmaster@1: if (!form_get_errors()) {
webmaster@1: _comment_form_submit($edit);
webmaster@1: $comment = (object)$edit;
webmaster@1:
webmaster@1: // Attach the user and time information.
webmaster@1: if (!empty($edit['author'])) {
webmaster@1: $account = user_load(array('name' => $edit['author']));
webmaster@1: }
webmaster@1: elseif ($user->uid && !isset($edit['is_anonymous'])) {
webmaster@1: $account = $user;
webmaster@1: }
webmaster@1: if (!empty($account)) {
webmaster@1: $comment->uid = $account->uid;
webmaster@1: $comment->name = check_plain($account->name);
webmaster@1: }
webmaster@1: elseif (empty($comment->name)) {
webmaster@1: $comment->name = variable_get('anonymous', t('Anonymous'));
webmaster@1: }
webmaster@1: $comment->timestamp = !empty($edit['timestamp']) ? $edit['timestamp'] : time();
webmaster@1: $output .= theme('comment_view', $comment, $node);
webmaster@1: }
webmaster@1: $form['comment_preview'] = array(
webmaster@1: '#value' => $output,
webmaster@1: '#weight' => -100,
webmaster@1: '#prefix' => '',
webmaster@1: '#suffix' => '
',
webmaster@1: );
webmaster@1:
webmaster@1: $output = '';
webmaster@1:
webmaster@1: if ($edit['pid']) {
webmaster@1: $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
webmaster@1: $comment = drupal_unpack($comment);
webmaster@1: $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
webmaster@1: $output .= theme('comment_view', $comment, $node);
webmaster@1: }
webmaster@1: else {
webmaster@1: $suffix = empty($form['#suffix']) ? '' : $form['#suffix'];
webmaster@1: $form['#suffix'] = $suffix . node_view($node);
webmaster@1: $edit['pid'] = 0;
webmaster@1: }
webmaster@1:
webmaster@1: $form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);
webmaster@1:
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Validate comment form submissions.
webmaster@1: */
webmaster@1: function comment_form_validate($form, &$form_state) {
webmaster@1: global $user;
webmaster@1: if ($user->uid === 0) {
webmaster@1: foreach (array('name', 'homepage', 'mail') as $field) {
webmaster@1: // Set cookie for 365 days.
webmaster@1: if (isset($form_state['values'][$field])) {
webmaster@1: setcookie('comment_info_'. $field, $form_state['values'][$field], time() + 31536000, '/');
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: comment_validate($form_state['values']);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Prepare a comment for submission.
webmaster@1: *
webmaster@1: * @param $comment_values
webmaster@1: * An associative array containing the comment data.
webmaster@1: */
webmaster@1: function _comment_form_submit(&$comment_values) {
webmaster@1: $comment_values += array('subject' => '');
webmaster@1: if (!isset($comment_values['date'])) {
webmaster@1: $comment_values['date'] = 'now';
webmaster@1: }
webmaster@1: $comment_values['timestamp'] = strtotime($comment_values['date']);
webmaster@1: if (isset($comment_values['author'])) {
webmaster@1: $account = user_load(array('name' => $comment_values['author']));
webmaster@1: $comment_values['uid'] = $account->uid;
webmaster@1: $comment_values['name'] = $comment_values['author'];
webmaster@1: }
webmaster@1: // Validate the comment's subject. If not specified, extract
webmaster@1: // one from the comment's body.
webmaster@1: if (trim($comment_values['subject']) == '') {
webmaster@1: // The body may be in any format, so we:
webmaster@1: // 1) Filter it into HTML
webmaster@1: // 2) Strip out all HTML tags
webmaster@1: // 3) Convert entities back to plain-text.
webmaster@1: // Note: format is checked by check_markup().
webmaster@1: $comment_values['subject'] = trim(truncate_utf8(decode_entities(strip_tags(check_markup($comment_values['comment'], $comment_values['format']))), 29, TRUE));
webmaster@1: // Edge cases where the comment body is populated only by HTML tags will
webmaster@1: // require a default subject.
webmaster@1: if ($comment_values['subject'] == '') {
webmaster@1: $comment_values['subject'] = t('(No subject)');
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment form submissions; prepare the comment, store it, and set a redirection target.
webmaster@1: */
webmaster@1: function comment_form_submit($form, &$form_state) {
webmaster@1: _comment_form_submit($form_state['values']);
webmaster@1: if ($cid = comment_save($form_state['values'])) {
webmaster@1: $node = node_load($form_state['values']['nid']);
webmaster@1: $page = comment_new_page_count($node->comment_count, 1, $node);
webmaster@1: $form_state['redirect'] = array('node/'. $node->nid, $page, "comment-$cid");
webmaster@1: return;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme a single comment block.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment object.
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @param $links
webmaster@1: * An associative array containing control links.
webmaster@1: * @param $visible
webmaster@1: * Switches between folded/unfolded view.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_view($comment, $node, $links = array(), $visible = TRUE) {
webmaster@1: static $first_new = TRUE;
webmaster@1:
webmaster@1: $output = '';
webmaster@1: $comment->new = node_mark($comment->nid, $comment->timestamp);
webmaster@1: if ($first_new && $comment->new != MARK_READ) {
webmaster@1: // Assign the anchor only for the first new comment. This avoids duplicate
webmaster@1: // id attributes on a page.
webmaster@1: $first_new = FALSE;
webmaster@1: $output .= "\n";
webmaster@1: }
webmaster@1:
webmaster@1: $output .= "\n";
webmaster@1:
webmaster@1: // Switch to folded/unfolded view of the comment
webmaster@1: if ($visible) {
webmaster@1: $comment->comment = check_markup($comment->comment, $comment->format, FALSE);
webmaster@1:
webmaster@1: // Comment API hook
webmaster@1: comment_invoke_comment($comment, 'view');
webmaster@1:
webmaster@1: $output .= theme('comment', $comment, $node, $links);
webmaster@1: }
webmaster@1: else {
webmaster@1: $output .= theme('comment_folded', $comment);
webmaster@1: }
webmaster@1:
webmaster@1: return $output;
webmaster@1: }
webmaster@1:
webmaster@1:
webmaster@1: /**
webmaster@1: * Build a comment control form.
webmaster@1: *
webmaster@1: * @param $mode
webmaster@1: * Comment display mode.
webmaster@1: * @param $order
webmaster@1: * Comment order mode.
webmaster@1: * @param $comments_per_page
webmaster@1: * Comments per page.
webmaster@1: * @ingroup forms
webmaster@1: */
webmaster@1: function comment_controls($mode = COMMENT_MODE_THREADED_EXPANDED, $order = COMMENT_ORDER_NEWEST_FIRST, $comments_per_page = 50) {
webmaster@1: $form['mode'] = array('#type' => 'select',
webmaster@1: '#default_value' => $mode,
webmaster@1: '#options' => _comment_get_modes(),
webmaster@1: '#weight' => 1,
webmaster@1: );
webmaster@1: $form['order'] = array(
webmaster@1: '#type' => 'select',
webmaster@1: '#default_value' => $order,
webmaster@1: '#options' => _comment_get_orders(),
webmaster@1: '#weight' => 2,
webmaster@1: );
webmaster@1: foreach (_comment_per_page() as $i) {
webmaster@1: $options[$i] = t('!a comments per page', array('!a' => $i));
webmaster@1: }
webmaster@1: $form['comments_per_page'] = array('#type' => 'select',
webmaster@1: '#default_value' => $comments_per_page,
webmaster@1: '#options' => $options,
webmaster@1: '#weight' => 3,
webmaster@1: );
webmaster@1:
webmaster@1: $form['submit'] = array('#type' => 'submit',
webmaster@1: '#value' => t('Save settings'),
webmaster@1: '#weight' => 20,
webmaster@1: );
webmaster@1:
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme comment controls box where the user can change the default display mode and display order of comments.
webmaster@1: *
webmaster@1: * @param $form
webmaster@1: * The form structure.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_controls($form) {
webmaster@1: $output = '';
webmaster@1: $output .= drupal_render($form);
webmaster@1: $output .= '
';
webmaster@1: $output .= ''. t('Select your preferred way to display the comments and click "Save settings" to activate your changes.') .'
';
webmaster@1: return theme('box', t('Comment viewing options'), $output);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment_controls form submissions.
webmaster@1: */
webmaster@1: function comment_controls_submit($form, &$form_state) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: $mode = $form_state['values']['mode'];
webmaster@1: $order = $form_state['values']['order'];
webmaster@1: $comments_per_page = $form_state['values']['comments_per_page'];
webmaster@1:
webmaster@1: if ($user->uid) {
webmaster@1: $account = user_save($user, array('mode' => $mode, 'sort' => $order, 'comments_per_page' => $comments_per_page));
webmaster@1: // Terminate if an error occured during user_save().
webmaster@1: if (!$account) {
webmaster@1: drupal_set_message(t("Error saving user account."), 'error');
webmaster@1: return;
webmaster@1: }
webmaster@1: $user = $account;
webmaster@1: }
webmaster@1: else {
webmaster@1: $_SESSION['comment_mode'] = $mode;
webmaster@1: $_SESSION['comment_sort'] = $order;
webmaster@1: $_SESSION['comment_comments_per_page'] = $comments_per_page;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process variables for comment.tpl.php.
webmaster@1: *
webmaster@1: * @see comment.tpl.php
webmaster@1: * @see theme_comment()
webmaster@1: */
webmaster@1: function template_preprocess_comment(&$variables) {
webmaster@1: $comment = $variables['comment'];
webmaster@1: $node = $variables['node'];
webmaster@1: $variables['author'] = theme('username', $comment);
webmaster@1: $variables['content'] = $comment->comment;
webmaster@1: $variables['date'] = format_date($comment->timestamp);
webmaster@1: $variables['links'] = isset($variables['links']) ? theme('links', $variables['links']) : '';
webmaster@1: $variables['new'] = $comment->new ? t('new') : '';
webmaster@1: $variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '';
webmaster@1: $variables['signature'] = $comment->signature;
webmaster@1: $variables['submitted'] = theme('comment_submitted', $comment);
webmaster@1: $variables['title'] = l($comment->subject, $_GET['q'], array('fragment' => "comment-$comment->cid"));
webmaster@1: $variables['template_files'][] = 'comment-'. $node->type;
webmaster@1: // set status to a string representation of comment->status.
webmaster@1: if (isset($comment->preview)) {
webmaster@1: $variables['status'] = 'comment-preview';
webmaster@1: }
webmaster@1: else {
webmaster@1: $variables['status'] = ($comment->status == COMMENT_NOT_PUBLISHED) ? 'comment-unpublished' : 'comment-published';
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process variables for comment-folded.tpl.php.
webmaster@1: *
webmaster@1: * @see comment-folded.tpl.php
webmaster@1: * @see theme_comment_folded()
webmaster@1: */
webmaster@1: function template_preprocess_comment_folded(&$variables) {
webmaster@1: $comment = $variables['comment'];
webmaster@1: $variables['author'] = theme('username', $comment);
webmaster@1: $variables['date'] = format_date($comment->timestamp);
webmaster@1: $variables['new'] = $comment->new ? t('new') : '';
webmaster@1: $variables['title'] = l($comment->subject, comment_node_url() .'/'. $comment->cid, array('fragment' => "comment-$comment->cid"));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme comment flat collapsed view.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment to be themed.
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_flat_collapsed($comment, $node) {
webmaster@1: return theme('comment_view', $comment, $node, '', 0);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme comment flat expanded view.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment to be themed.
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_flat_expanded($comment, $node) {
webmaster@1: return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme comment thread collapsed view.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment to be themed.
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_thread_collapsed($comment, $node) {
webmaster@1: return theme('comment_view', $comment, $node, '', 0);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme comment thread expanded view.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment to be themed.
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_thread_expanded($comment, $node) {
webmaster@1: return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme a "you can't post comments" notice.
webmaster@1: *
webmaster@1: * @param $node
webmaster@1: * The comment node.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_post_forbidden($node) {
webmaster@1: global $user;
webmaster@1: static $authenticated_post_comments;
webmaster@1:
webmaster@1: if (!$user->uid) {
webmaster@1: if (!isset($authenticated_post_comments)) {
webmaster@1: // We only output any link if we are certain, that users get permission
webmaster@1: // to post comments by logging in. We also locally cache this information.
webmaster@1: $authenticated_post_comments = array_key_exists(DRUPAL_AUTHENTICATED_RID, user_roles(TRUE, 'post comments') + user_roles(TRUE, 'post comments without approval'));
webmaster@1: }
webmaster@1:
webmaster@1: if ($authenticated_post_comments) {
webmaster@1: // We cannot use drupal_get_destination() because these links
webmaster@1: // sometimes appear on /node and taxonomy listing pages.
webmaster@1: if (variable_get('comment_form_location_'. $node->type, COMMENT_FORM_SEPARATE_PAGE) == COMMENT_FORM_SEPARATE_PAGE) {
webmaster@1: $destination = 'destination='. drupal_urlencode("comment/reply/$node->nid#comment-form");
webmaster@1: }
webmaster@1: else {
webmaster@1: $destination = 'destination='. drupal_urlencode("node/$node->nid#comment-form");
webmaster@1: }
webmaster@1:
webmaster@1: if (variable_get('user_register', 1)) {
webmaster@1: // Users can register themselves.
webmaster@1: return t('Login or register to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination))));
webmaster@1: }
webmaster@1: else {
webmaster@1: // Only admins can add new users, no public registration.
webmaster@1: return t('Login to post comments', array('@login' => url('user/login', array('query' => $destination))));
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process variables for comment-wrapper.tpl.php.
webmaster@1: *
webmaster@1: * @see comment-wrapper.tpl.php
webmaster@1: * @see theme_comment_wrapper()
webmaster@1: */
webmaster@1: function template_preprocess_comment_wrapper(&$variables) {
webmaster@1: // Provide contextual information.
webmaster@1: $variables['display_mode'] = _comment_get_display_setting('mode', $variables['node']);
webmaster@1: $variables['display_order'] = _comment_get_display_setting('sort', $variables['node']);
webmaster@1: $variables['comment_controls_state'] = variable_get('comment_controls_'. $variables['node']->type, COMMENT_CONTROLS_HIDDEN);
webmaster@1: $variables['template_files'][] = 'comment-wrapper-'. $variables['node']->type;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Theme a "Submitted by ..." notice.
webmaster@1: *
webmaster@1: * @param $comment
webmaster@1: * The comment.
webmaster@1: * @ingroup themeable
webmaster@1: */
webmaster@1: function theme_comment_submitted($comment) {
webmaster@1: return t('Submitted by !username on @datetime.',
webmaster@1: array(
webmaster@1: '!username' => theme('username', $comment),
webmaster@1: '@datetime' => format_date($comment->timestamp)
webmaster@1: ));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Return an array of viewing modes for comment listings.
webmaster@1: *
webmaster@1: * We can't use a global variable array because the locale system
webmaster@1: * is not initialized yet when the comment module is loaded.
webmaster@1: */
webmaster@1: function _comment_get_modes() {
webmaster@1: return array(
webmaster@1: COMMENT_MODE_FLAT_COLLAPSED => t('Flat list - collapsed'),
webmaster@1: COMMENT_MODE_FLAT_EXPANDED => t('Flat list - expanded'),
webmaster@1: COMMENT_MODE_THREADED_COLLAPSED => t('Threaded list - collapsed'),
webmaster@1: COMMENT_MODE_THREADED_EXPANDED => t('Threaded list - expanded')
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Return an array of viewing orders for comment listings.
webmaster@1: *
webmaster@1: * We can't use a global variable array because the locale system
webmaster@1: * is not initialized yet when the comment module is loaded.
webmaster@1: */
webmaster@1: function _comment_get_orders() {
webmaster@1: return array(
webmaster@1: COMMENT_ORDER_NEWEST_FIRST => t('Date - newest first'),
webmaster@1: COMMENT_ORDER_OLDEST_FIRST => t('Date - oldest first')
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Return an array of "comments per page" settings from which the user
webmaster@1: * can choose.
webmaster@1: */
webmaster@1: function _comment_per_page() {
webmaster@1: return drupal_map_assoc(array(10, 30, 50, 70, 90, 150, 200, 250, 300));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Return a current comment display setting
webmaster@1: *
webmaster@1: * @param $setting
webmaster@1: * can be one of these: 'mode', 'sort', 'comments_per_page'
webmaster@1: * @param $node
webmaster@1: * The comment node in question.
webmaster@1: */
webmaster@1: function _comment_get_display_setting($setting, $node) {
webmaster@1: global $user;
webmaster@1:
webmaster@1: if (isset($_GET[$setting])) {
webmaster@1: $value = $_GET[$setting];
webmaster@1: }
webmaster@1: else {
webmaster@1: // get the setting's site default
webmaster@1: switch ($setting) {
webmaster@1: case 'mode':
webmaster@1: $default = variable_get('comment_default_mode_'. $node->type, COMMENT_MODE_THREADED_EXPANDED);
webmaster@1: break;
webmaster@1: case 'sort':
webmaster@1: $default = variable_get('comment_default_order_'. $node->type, COMMENT_ORDER_NEWEST_FIRST);
webmaster@1: break;
webmaster@1: case 'comments_per_page':
webmaster@1: $default = variable_get('comment_default_per_page_'. $node->type, 50);
webmaster@1: }
webmaster@1: if (variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_HIDDEN) {
webmaster@1: // if comment controls are disabled use site default
webmaster@1: $value = $default;
webmaster@1: }
webmaster@1: else {
webmaster@1: // otherwise use the user's setting if set
webmaster@1: if (isset($user->$setting) && $user->$setting) {
webmaster@1: $value = $user->$setting;
webmaster@1: }
webmaster@1: else if (isset($_SESSION['comment_'. $setting]) && $_SESSION['comment_'. $setting]) {
webmaster@1: $value = $_SESSION['comment_'. $setting];
webmaster@1: }
webmaster@1: else {
webmaster@1: $value = $default;
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: return $value;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Updates the comment statistics for a given node. This should be called any
webmaster@1: * time a comment is added, deleted, or updated.
webmaster@1: *
webmaster@1: * The following fields are contained in the node_comment_statistics table.
webmaster@1: * - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
webmaster@1: * - last_comment_name: the name of the anonymous poster for the last comment
webmaster@1: * - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
webmaster@1: * - comment_count: the total number of approved/published comments on this node.
webmaster@1: */
webmaster@1: function _comment_update_node_statistics($nid) {
webmaster@1: $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = %d', $nid, COMMENT_PUBLISHED));
webmaster@1:
webmaster@1: // comments exist
webmaster@1: if ($count > 0) {
webmaster@1: $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
webmaster@1: db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
webmaster@1: }
webmaster@1:
webmaster@1: // no comments
webmaster@1: else {
webmaster@1: $node = db_fetch_object(db_query("SELECT uid, created FROM {node} WHERE nid = %d", $nid));
webmaster@1: db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Invoke a hook_comment() operation in all modules.
webmaster@1: *
webmaster@1: * @param &$comment
webmaster@1: * A comment object.
webmaster@1: * @param $op
webmaster@1: * A string containing the name of the comment operation.
webmaster@1: * @return
webmaster@1: * The returned value of the invoked hooks.
webmaster@1: */
webmaster@1: function comment_invoke_comment(&$comment, $op) {
webmaster@1: $return = array();
webmaster@1: foreach (module_implements('comment') as $name) {
webmaster@1: $function = $name .'_comment';
webmaster@1: $result = $function($comment, $op);
webmaster@1: if (isset($result) && is_array($result)) {
webmaster@1: $return = array_merge($return, $result);
webmaster@1: }
webmaster@1: else if (isset($result)) {
webmaster@1: $return[] = $result;
webmaster@1: }
webmaster@1: }
webmaster@1: return $return;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Generate vancode.
webmaster@1: *
webmaster@1: * Consists of a leading character indicating length, followed by N digits
webmaster@1: * with a numerical value in base 36. Vancodes can be sorted as strings
webmaster@1: * without messing up numerical order.
webmaster@1: *
webmaster@1: * It goes:
webmaster@1: * 00, 01, 02, ..., 0y, 0z,
webmaster@1: * 110, 111, ... , 1zy, 1zz,
webmaster@1: * 2100, 2101, ..., 2zzy, 2zzz,
webmaster@1: * 31000, 31001, ...
webmaster@1: */
webmaster@1: function int2vancode($i = 0) {
webmaster@1: $num = base_convert((int)$i, 10, 36);
webmaster@1: $length = strlen($num);
webmaster@1: return chr($length + ord('0') - 1) . $num;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Decode vancode back to an integer.
webmaster@1: */
webmaster@1: function vancode2int($c = '00') {
webmaster@1: return base_convert(substr($c, 1), 36, 10);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_hook_info().
webmaster@1: */
webmaster@1: function comment_hook_info() {
webmaster@1: return array(
webmaster@1: 'comment' => array(
webmaster@1: 'comment' => array(
webmaster@1: 'insert' => array(
webmaster@1: 'runs when' => t('After saving a new comment'),
webmaster@1: ),
webmaster@1: 'update' => array(
webmaster@1: 'runs when' => t('After saving an updated comment'),
webmaster@1: ),
webmaster@1: 'delete' => array(
webmaster@1: 'runs when' => t('After deleting a comment')
webmaster@1: ),
webmaster@1: 'view' => array(
webmaster@1: 'runs when' => t('When a comment is being viewed by an authenticated user')
webmaster@1: ),
webmaster@1: ),
webmaster@1: ),
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of hook_action_info().
webmaster@1: */
webmaster@1: function comment_action_info() {
webmaster@1: return array(
webmaster@1: 'comment_unpublish_action' => array(
webmaster@1: 'description' => t('Unpublish comment'),
webmaster@1: 'type' => 'comment',
webmaster@1: 'configurable' => FALSE,
webmaster@1: 'hooks' => array(
webmaster@1: 'comment' => array('insert', 'update'),
webmaster@1: )
webmaster@1: ),
webmaster@1: 'comment_unpublish_by_keyword_action' => array(
webmaster@1: 'description' => t('Unpublish comment containing keyword(s)'),
webmaster@1: 'type' => 'comment',
webmaster@1: 'configurable' => TRUE,
webmaster@1: 'hooks' => array(
webmaster@1: 'comment' => array('insert', 'update'),
webmaster@1: )
webmaster@1: )
webmaster@1: );
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Drupal action to unpublish a comment.
webmaster@1: *
webmaster@1: * @param $context
webmaster@1: * Keyed array. Must contain the id of the comment if $comment is not passed.
webmaster@1: * @param $comment
webmaster@1: * An optional comment object.
webmaster@1: */
webmaster@1: function comment_unpublish_action($comment, $context = array()) {
webmaster@1: if (isset($comment->cid)) {
webmaster@1: $cid = $comment->cid;
webmaster@1: $subject = $comment->subject;
webmaster@1: }
webmaster@1: else {
webmaster@1: $cid = $context['cid'];
webmaster@1: $subject = db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $cid));
webmaster@1: }
webmaster@1: db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_NOT_PUBLISHED, $cid);
webmaster@1: watchdog('action', 'Unpublished comment %subject.', array('%subject' => $subject));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Form builder; Prepare a form for blacklisted keywords.
webmaster@1: *
webmaster@1: * @ingroup forms
webmaster@1: */
webmaster@1: function comment_unpublish_by_keyword_action_form($context) {
webmaster@1: $form['keywords'] = array(
webmaster@1: '#title' => t('Keywords'),
webmaster@1: '#type' => 'textarea',
webmaster@1: '#description' => t('The comment will be unpublished if it contains any of the character sequences above. Use a comma-separated list of character sequences. Example: funny, bungee jumping, "Company, Inc.". Character sequences are case-sensitive.'),
webmaster@1: '#default_value' => isset($context['keywords']) ? drupal_implode_tags($context['keywords']) : '',
webmaster@1: );
webmaster@1: return $form;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Process comment_unpublish_by_keyword_action_form form submissions.
webmaster@1: */
webmaster@1: function comment_unpublish_by_keyword_action_submit($form, $form_state) {
webmaster@1: return array('keywords' => drupal_explode_tags($form_state['values']['keywords']));
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Implementation of a configurable Drupal action.
webmaster@1: * Unpublish a comment if it contains a certain string.
webmaster@1: *
webmaster@1: * @param $context
webmaster@1: * An array providing more information about the context of the call to this action.
webmaster@1: * Unused here since this action currently only supports the insert and update ops of
webmaster@1: * the comment hook, both of which provide a complete $comment object.
webmaster@1: * @param $comment
webmaster@1: * A comment object.
webmaster@1: */
webmaster@1: function comment_unpublish_by_keyword_action($comment, $context) {
webmaster@1: foreach ($context['keywords'] as $keyword) {
webmaster@1: if (strstr($comment->comment, $keyword) || strstr($comment->subject, $keyword)) {
webmaster@1: db_query('UPDATE {comments} SET status = %d WHERE cid = %d', COMMENT_NOT_PUBLISHED, $comment->cid);
webmaster@1: watchdog('action', 'Unpublished comment %subject.', array('%subject' => $comment->subject));
webmaster@1: break;
webmaster@1: }
webmaster@1: }
webmaster@1: }