comment.pages.inc

  1. drupal
    1. 6 modules/comment/comment.pages.inc
    2. 7 modules/comment/comment.pages.inc
    3. 8 core/modules/comment/comment.pages.inc

User page callbacks for the comment module.

Functions & methods

NameDescription
comment_approveMenu callback; publish specified comment.
comment_replyThis function is responsible for generating a comment reply form. There are several cases that have to be handled, including:

File

modules/comment/comment.pages.inc
View source
  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
  25. * The rendered parent node or comment plus the new comment form.
  26. */
  27. function comment_reply($node, $pid = NULL) {
  28. // Set the breadcrumb trail.
  29. drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/' . $node->nid)));
  30. $op = isset($_POST['op']) ? $_POST['op'] : '';
  31. $build = array();
  32. // The user is previewing a comment prior to submitting it.
  33. if ($op == t('Preview')) {
  34. if (user_access('post comments')) {
  35. $build['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) array('pid' => $pid, 'nid' => $node->nid));
  36. }
  37. else {
  38. drupal_set_message(t('You are not authorized to post comments.'), 'error');
  39. drupal_goto("node/$node->nid");
  40. }
  41. }
  42. else {
  43. // $pid indicates that this is a reply to a comment.
  44. if ($pid) {
  45. if (user_access('access comments')) {
  46. // Load the comment whose cid = $pid
  47. $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(
  48. ':cid' => $pid,
  49. ':status' => COMMENT_PUBLISHED,
  50. ))->fetchObject();
  51. if ($comment) {
  52. // If that comment exists, make sure that the current comment and the
  53. // parent comment both belong to the same parent node.
  54. if ($comment->nid != $node->nid) {
  55. // Attempting to reply to a comment not belonging to the current nid.
  56. drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
  57. drupal_goto("node/$node->nid");
  58. }
  59. // Display the parent comment
  60. $comment->node_type = 'comment_node_' . $node->type;
  61. field_attach_load('comment', array($comment->cid => $comment));
  62. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  63. $build['comment_parent'] = comment_view($comment, $node);
  64. }
  65. else {
  66. drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
  67. drupal_goto("node/$node->nid");
  68. }
  69. }
  70. else {
  71. drupal_set_message(t('You are not authorized to view comments.'), 'error');
  72. drupal_goto("node/$node->nid");
  73. }
  74. }
  75. // This is the case where the comment is in response to a node. Display the node.
  76. elseif (user_access('access content')) {
  77. $build['comment_node'] = node_view($node);
  78. }
  79. // Should we show the reply box?
  80. if ($node->comment != COMMENT_NODE_OPEN) {
  81. drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
  82. drupal_goto("node/$node->nid");
  83. }
  84. elseif (user_access('post comments')) {
  85. $edit = array('nid' => $node->nid, 'pid' => $pid);
  86. $build['comment_form'] = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);
  87. }
  88. else {
  89. drupal_set_message(t('You are not authorized to post comments.'), 'error');
  90. drupal_goto("node/$node->nid");
  91. }
  92. }
  93. return $build;
  94. }
  95. /**
  96. * Menu callback; publish specified comment.
  97. *
  98. * @param $cid
  99. * A comment identifier.
  100. */
  101. function comment_approve($cid) {
  102. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], "comment/$cid/approve")) {
  103. return MENU_ACCESS_DENIED;
  104. }
  105. if ($comment = comment_load($cid)) {
  106. $comment->status = COMMENT_PUBLISHED;
  107. comment_save($comment);
  108. drupal_set_message(t('Comment approved.'));
  109. drupal_goto('node/' . $comment->nid);
  110. }
  111. return MENU_NOT_FOUND;
  112. }
Login or register to post comments