1. /**
  2. * Function that handles the ajax response when user clicks a reply button.
  3. */
  4. function modern_comments_reply($node, $pid = NULL) {
  5. if ($node === FALSE) {
  6. return MENU_NOT_FOUND;
  7. }
  8. if (!user_access('post comments') || !user_access('access comments')) {
  9. return MENU_ACCESS_DENIED;
  10. }
  11. // If there is a pid this is a reply to a comment.
  12. if ($pid) {
  13. // Make sure the comment is valid and published.
  14. if (!($comments = comment_load_multiple(array($pid), array('status' => COMMENT_PUBLISHED)))) {
  15. return MENU_NOT_FOUND;
  16. }
  17. $comment = $comments[$pid];
  18. // Make sure the comment belongs to this node.
  19. if ($comment->nid != $node->nid) {
  20. return MENU_NOT_FOUND;
  21. }
  22. }
  23. // Build form.
  24. $obj = (object) array('nid' => $node->nid);
  25. if($pid) $obj->pid = $pid;
  26. $form_build = drupal_get_form("comment_node_{$node->type}_form", $obj);
  27. $form = drupal_render($form_build);
  28. $commands[] = ajax_command_remove("#comments form.comment-form");
  29. if ($pid) {
  30. $commands[] = ajax_command_append("#comment-id-{$comment->cid}", $form);
  31. }
  32. else {
  33. $commands[] = ajax_command_after("#comment-add-section", $form);
  34. $commands[] = ajax_command_invoke(NULL, 'scrolltoel', array('#comment-add-section'));
  35. }
  36. // This is to remove the "Your comment has been posted" status message that
  37. // will appear upon refresh. This seems dirty but it means we don't have to
  38. // rewrite the whole comment_form_submit(). Please chime in if you think this
  39. // is dumb.
  40. modern_comments_remove_status($_SESSION);
  41. return array('#type' => 'ajax', '#commands' => $commands);
  42. }