comment.pages.inc

  1. 7.x drupal-7.x/modules/comment/comment.pages.inc
  2. 6.x drupal-6.x/modules/comment/comment.pages.inc

User page callbacks for the comment module.

File

drupal-6.x/modules/comment/comment.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the comment module.
  5. */
  6. /**
  7. * Form builder; generate a comment editing form.
  8. *
  9. * @param $cid
  10. * ID of the comment to be edited.
  11. * @ingroup forms
  12. */
  13. function comment_edit($cid) {
  14. global $user;
  15. $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));
  16. $comment = drupal_unpack($comment);
  17. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  18. if (comment_access('edit', $comment)) {
  19. return comment_form_box((array)$comment);
  20. }
  21. else {
  22. drupal_access_denied();
  23. }
  24. }
  25. /**
  26. * This function is responsible for generating a comment reply form.
  27. * There are several cases that have to be handled, including:
  28. * - replies to comments
  29. * - replies to nodes
  30. * - attempts to reply to nodes that can no longer accept comments
  31. * - respecting access permissions ('access comments', 'post comments', etc.)
  32. *
  33. * The node or comment that is being replied to must appear above the comment
  34. * form to provide the user context while authoring the comment.
  35. *
  36. * @param $node
  37. * Every comment belongs to a node. This is that node.
  38. *
  39. * @param $pid
  40. * Some comments are replies to other comments. In those cases, $pid is the parent
  41. * comment's cid.
  42. *
  43. * @return
  44. * The rendered parent node or comment plus the new comment form.
  45. */
  46. function comment_reply($node, $pid = NULL) {
  47. // Set the breadcrumb trail.
  48. drupal_set_breadcrumb(array(l(t('Home'), NULL), l($node->title, 'node/'. $node->nid)));
  49. $op = isset($_POST['op']) ? $_POST['op'] : '';
  50. $output = '';
  51. if (user_access('access comments')) {
  52. // The user is previewing a comment prior to submitting it.
  53. if ($op == t('Preview')) {
  54. if (user_access('post comments')) {
  55. $output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), NULL);
  56. }
  57. else {
  58. drupal_set_message(t('You are not authorized to post comments.'), 'error');
  59. drupal_goto("node/$node->nid");
  60. }
  61. }
  62. else {
  63. // $pid indicates that this is a reply to a comment.
  64. if ($pid) {
  65. // load the comment whose cid = $pid
  66. if ($comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.signature_format, 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))) {
  67. // If that comment exists, make sure that the current comment and the parent comment both
  68. // belong to the same parent node.
  69. if ($comment->nid != $node->nid) {
  70. // Attempting to reply to a comment not belonging to the current nid.
  71. drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
  72. drupal_goto("node/$node->nid");
  73. }
  74. // Display the parent comment
  75. $comment = drupal_unpack($comment);
  76. $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
  77. $output .= theme('comment_view', $comment, $node);
  78. }
  79. else {
  80. drupal_set_message(t('The comment you are replying to does not exist.'), 'error');
  81. drupal_goto("node/$node->nid");
  82. }
  83. }
  84. // This is the case where the comment is in response to a node. Display the node.
  85. else if (user_access('access content')) {
  86. $output .= node_view($node);
  87. }
  88. // Should we show the reply box?
  89. if (node_comment_mode($node->nid) != COMMENT_NODE_READ_WRITE) {
  90. drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error');
  91. drupal_goto("node/$node->nid");
  92. }
  93. else if (user_access('post comments')) {
  94. $output .= comment_form_box(array('pid' => $pid, 'nid' => $node->nid), t('Reply'));
  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. else {
  103. drupal_set_message(t('You are not authorized to view comments.'), 'error');
  104. drupal_goto("node/$node->nid");
  105. }
  106. return $output;
  107. }