|
1 <?php |
|
2 |
|
3 /** |
|
4 * @file |
|
5 * User page callbacks for the comment module. |
|
6 */ |
|
7 |
|
8 /** |
|
9 * This function is responsible for generating a comment reply form. |
|
10 * There are several cases that have to be handled, including: |
|
11 * - replies to comments |
|
12 * - replies to nodes |
|
13 * - attempts to reply to nodes that can no longer accept comments |
|
14 * - respecting access permissions ('access comments', 'post comments', etc.) |
|
15 * |
|
16 * The node or comment that is being replied to must appear above the comment |
|
17 * form to provide the user context while authoring the comment. |
|
18 * |
|
19 * @param $node |
|
20 * Every comment belongs to a node. This is that node. |
|
21 * |
|
22 * @param $pid |
|
23 * Some comments are replies to other comments. In those cases, $pid is the parent |
|
24 * comment's cid. |
|
25 * |
|
26 * @return array |
|
27 * An associative array containing: |
|
28 * - An array for rendering the node or parent comment. |
|
29 * - comment_node: If the comment is a reply to the node. |
|
30 * - comment_parent: If the comment is a reply to another comment. |
|
31 * - comment_form: The comment form as a renderable array. |
|
32 */ |
|
33 function comment_reply($node, $pid = NULL) { |
|
34 // Set the breadcrumb trail. |
|
35 drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/' . $node->nid))); |
|
36 $op = isset($_POST['op']) ? $_POST['op'] : ''; |
|
37 $build = array(); |
|
38 |
|
39 // The user is previewing a comment prior to submitting it. |
|
40 if ($op == t('Preview')) { |
|
41 if (user_access('post comments')) { |
|
42 $build['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) array('pid' => $pid, 'nid' => $node->nid)); |
|
43 } |
|
44 else { |
|
45 drupal_set_message(t('You are not authorized to post comments.'), 'error'); |
|
46 drupal_goto("node/$node->nid"); |
|
47 } |
|
48 } |
|
49 else { |
|
50 // $pid indicates that this is a reply to a comment. |
|
51 if ($pid) { |
|
52 if (user_access('access comments')) { |
|
53 // Load the comment whose cid = $pid |
|
54 $comment = db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid AND c.status = :status', array( |
|
55 ':cid' => $pid, |
|
56 ':status' => COMMENT_PUBLISHED, |
|
57 ))->fetchObject(); |
|
58 if ($comment) { |
|
59 // If that comment exists, make sure that the current comment and the |
|
60 // parent comment both belong to the same parent node. |
|
61 if ($comment->nid != $node->nid) { |
|
62 // Attempting to reply to a comment not belonging to the current nid. |
|
63 drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); |
|
64 drupal_goto("node/$node->nid"); |
|
65 } |
|
66 // Display the parent comment |
|
67 $comment->node_type = 'comment_node_' . $node->type; |
|
68 field_attach_load('comment', array($comment->cid => $comment)); |
|
69 $comment->name = $comment->uid ? $comment->registered_name : $comment->name; |
|
70 $build['comment_parent'] = comment_view($comment, $node); |
|
71 } |
|
72 else { |
|
73 drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); |
|
74 drupal_goto("node/$node->nid"); |
|
75 } |
|
76 } |
|
77 else { |
|
78 drupal_set_message(t('You are not authorized to view comments.'), 'error'); |
|
79 drupal_goto("node/$node->nid"); |
|
80 } |
|
81 } |
|
82 // This is the case where the comment is in response to a node. Display the node. |
|
83 elseif (user_access('access content')) { |
|
84 $build['comment_node'] = node_view($node); |
|
85 } |
|
86 |
|
87 // Should we show the reply box? |
|
88 if ($node->comment != COMMENT_NODE_OPEN) { |
|
89 drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error'); |
|
90 drupal_goto("node/$node->nid"); |
|
91 } |
|
92 elseif (user_access('post comments')) { |
|
93 $edit = array('nid' => $node->nid, 'pid' => $pid); |
|
94 $build['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) $edit); |
|
95 } |
|
96 else { |
|
97 drupal_set_message(t('You are not authorized to post comments.'), 'error'); |
|
98 drupal_goto("node/$node->nid"); |
|
99 } |
|
100 } |
|
101 |
|
102 return $build; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Menu callback; publish specified comment. |
|
107 * |
|
108 * @param $cid |
|
109 * A comment identifier. |
|
110 */ |
|
111 function comment_approve($cid) { |
|
112 if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], "comment/$cid/approve")) { |
|
113 return MENU_ACCESS_DENIED; |
|
114 } |
|
115 if ($comment = comment_load($cid)) { |
|
116 $comment->status = COMMENT_PUBLISHED; |
|
117 comment_save($comment); |
|
118 |
|
119 drupal_set_message(t('Comment approved.')); |
|
120 drupal_goto('node/' . $comment->nid); |
|
121 } |
|
122 return MENU_NOT_FOUND; |
|
123 } |