comment.pages.inc 4.5 KB

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