| 1 | | <?php |
| 2 | | // $Id: comment.pages.inc,v 1.7 2008/10/12 04:30:05 webchick Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * User page callbacks for the comment module. |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Form builder; generate a comment editing form. |
| 11 | | * |
| 12 | | * @param $cid |
| 13 | | * ID of the comment to be edited. |
| 14 | | * @ingroup forms |
| 15 | | */ |
| 16 | 50 | function comment_edit($cid) { |
| 17 | 3 | global $user; |
| 18 | 3 | $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS
registered_name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid =
u.uid WHERE c.cid = %d', $cid)); |
| 19 | 3 | $comment = drupal_unpack($comment); |
| 20 | 3 | $comment->name = $comment->uid ? $comment->registered_name :
$comment->name; |
| 21 | | |
| 22 | 3 | if (comment_access('edit', $comment)) { |
| 23 | 3 | return comment_form_box((array)$comment); |
| 24 | 0 | } |
| 25 | | else { |
| 26 | 0 | drupal_access_denied(); |
| 27 | | } |
| 28 | 0 | } |
| 29 | | |
| 30 | | /** |
| 31 | | * This function is responsible for generating a comment reply form. |
| 32 | | * There are several cases that have to be handled, including: |
| 33 | | * - replies to comments |
| 34 | | * - replies to nodes |
| 35 | | * - attempts to reply to nodes that can no longer accept comments |
| 36 | | * - respecting access permissions ('access comments', 'post comments',
etc.) |
| 37 | | * |
| 38 | | * The node or comment that is being replied to must appear above the
comment |
| 39 | | * form to provide the user context while authoring the comment. |
| 40 | | * |
| 41 | | * @param $node |
| 42 | | * Every comment belongs to a node. This is that node. |
| 43 | | * |
| 44 | | * @param $pid |
| 45 | | * Some comments are replies to other comments. In those cases, $pid is
the parent |
| 46 | | * comment's cid. |
| 47 | | * |
| 48 | | * @return |
| 49 | | * The rendered parent node or comment plus the new comment form. |
| 50 | | */ |
| 51 | 50 | function comment_reply($node, $pid = NULL) { |
| 52 | | // Set the breadcrumb trail. |
| 53 | 46 | drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/' .
$node->nid))); |
| 54 | 46 | $op = isset($_POST['op']) ? $_POST['op'] : ''; |
| 55 | 46 | $output = ''; |
| 56 | | |
| 57 | 46 | if (user_access('access comments')) { |
| 58 | | // The user is previewing a comment prior to submitting it. |
| 59 | 45 | if ($op == t('Preview')) { |
| 60 | 12 | if (user_access('post comments')) { |
| 61 | 12 | $output .= comment_form_box(array('pid' => $pid, 'nid' =>
$node->nid), NULL); |
| 62 | 12 | } |
| 63 | | else { |
| 64 | 0 | drupal_set_message(t('You are not authorized to post comments.'),
'error'); |
| 65 | 0 | drupal_goto("node/$node->nid"); |
| 66 | | } |
| 67 | 12 | } |
| 68 | | else { |
| 69 | | // $pid indicates that this is a reply to a comment. |
| 70 | 33 | if ($pid) { |
| 71 | | // Load the comment whose cid = $pid |
| 72 | 4 | if ($comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name
AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER
JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $pid,
COMMENT_PUBLISHED))) { |
| 73 | | // If that comment exists, make sure that the current comment and
the |
| 74 | | // parent comment both belong to the same parent node. |
| 75 | 4 | if ($comment->nid != $node->nid) { |
| 76 | | // Attempting to reply to a comment not belonging to the
current nid. |
| 77 | 0 | drupal_set_message(t('The comment you are replying to does not
exist.'), 'error'); |
| 78 | 0 | drupal_goto("node/$node->nid"); |
| 79 | 0 | } |
| 80 | | // Display the parent comment |
| 81 | 4 | $comment = drupal_unpack($comment); |
| 82 | 4 | $comment->name = $comment->uid ? $comment->registered_name :
$comment->name; |
| 83 | 4 | $output .= theme('comment_view', $comment, $node); |
| 84 | 4 | } |
| 85 | | else { |
| 86 | 0 | drupal_set_message(t('The comment you are replying to does not
exist.'), 'error'); |
| 87 | 0 | drupal_goto("node/$node->nid"); |
| 88 | | } |
| 89 | 4 | } |
| 90 | | // This is the case where the comment is in response to a node.
Display the node. |
| 91 | 29 | elseif (user_access('access content')) { |
| 92 | 29 | $output .= node_view($node); |
| 93 | 29 | } |
| 94 | | |
| 95 | | // Should we show the reply box? |
| 96 | 33 | if (node_comment_mode($node->nid) != COMMENT_NODE_READ_WRITE) { |
| 97 | 2 | drupal_set_message(t("This discussion is closed: you can't post new
comments."), 'error'); |
| 98 | 2 | drupal_goto("node/$node->nid"); |
| 99 | 0 | } |
| 100 | 31 | elseif (user_access('post comments')) { |
| 101 | 31 | $output .= comment_form_box(array('pid' => $pid, 'nid' =>
$node->nid), t('Reply')); |
| 102 | 18 | } |
| 103 | | else { |
| 104 | 0 | drupal_set_message(t('You are not authorized to post comments.'),
'error'); |
| 105 | 0 | drupal_goto("node/$node->nid"); |
| 106 | | } |
| 107 | | } |
| 108 | 30 | } |
| 109 | | else { |
| 110 | 1 | drupal_set_message(t('You are not authorized to view comments.'),
'error'); |
| 111 | 1 | drupal_goto("node/$node->nid"); |
| 112 | | } |
| 113 | | |
| 114 | 30 | return $output; |
| 115 | 0 | } |
| 116 | | |
| 117 | | /** |
| 118 | | * Publish specified comment. |
| 119 | | * |
| 120 | | * @param $cid ID of comment to be published. |
| 121 | | */ |
| 122 | 50 | function comment_approve($cid) { |
| 123 | | // Load the comment whose cid = $cid |
| 124 | 1 | if ($comment = comment_load($cid)) { |
| 125 | 1 | $operations = comment_operations('publish'); |
| 126 | 1 | db_query($operations['publish'][1], $cid); |
| 127 | | |
| 128 | 1 | drupal_set_message(t('Comment approved.')); |
| 129 | 1 | drupal_goto("node/$comment->nid"); |
| 130 | 0 | } |
| 131 | | else { |
| 132 | 0 | drupal_set_message(t('The comment you are approving does not exist.'),
'error'); |
| 133 | 0 | drupal_goto(); |
| 134 | | } |
| 135 | 0 | } |
| 136 | 50 | |