SubmitNewCommentController.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. class SubmitNewCommentController implements IController {
  3. private $user;
  4. private $form;
  5. private $userInputScrubber;
  6. private $stringURLLinker;
  7. private $database;
  8. private $header;
  9. private $notificationScraper;
  10. public function __construct() {
  11. $this->user = new User();
  12. $this->form = new Form();
  13. $this->userInputScrubber = new UserInputScrubber();
  14. $this->stringUrlLinker = new StringURLLinker();
  15. $this->database = new Database();
  16. $this->header = new Header();
  17. $this->notificationScraper = new NotificationScraper();
  18. }
  19. public function execute() {
  20. $this->user->populate();
  21. $userId = $this->user->getId();
  22. if ($userId) {
  23. $commentBody = $this->form->postCleanDefaulted('commentBody', '');
  24. $currentPostId = $this->form->getCleanDefaulted('id', 0);
  25. $currentTopicId = $this->form->getCleanDefaulted('t', 0);
  26. $parentCommentId = $this->form->postCleanDefaulted('parentComment', 0);
  27. $file = $this->form->file('attachment1');
  28. $commentBody = $this->userInputScrubber->scrubString($commentBody);
  29. if ($commentBody == "") {
  30. // ERROR: empty comment, what do
  31. return;
  32. }
  33. $commentBody = $this->stringUrlLinker->replaceUrlsWithLinks($commentBody);
  34. if (empty($currentTopicId)) {
  35. //ERROR: somehow they're posting in an undefined topic. No.
  36. return;
  37. }
  38. $parentCommentId = $this->userInputScrubber->scrubString($parentCommentId);
  39. $sql = sprintf("INSERT INTO comments (content, timestamp)
  40. VALUES ('%s', NOW())", $commentBody);
  41. $commentId = $this->database->write($sql);
  42. if ($commentId == 0) {
  43. //ERROR: something went wrong with saving the post, do something here
  44. return;
  45. }
  46. $fileUploadHandler = new FileUploadHandler();
  47. $fileUploadHandler->saveFileToComment($file, $commentId, $userId);
  48. $sql = sprintf("INSERT INTO comment_comment_associations (comment_id, parent_comment_id)
  49. VALUES ('%s', '%s')", $commentId, $parentCommentId);
  50. $this->database->write($sql);
  51. $sql = sprintf("INSERT INTO comment_user_associations (comment_id, user_id)
  52. VALUES ('%s', '%s')", $commentId, $userId);
  53. $this->database->write($sql);
  54. $sql = sprintf("INSERT INTO comment_post_associations (comment_id, post_id)
  55. VALUES ('%s', '%s')", $commentId, $currentPostId);
  56. $this->database->write($sql);
  57. $userTagData = $this->notificationScraper->getUserTagDataFromString($commentBody);
  58. foreach ($userTagData as $userId => $userTagString) {
  59. $sql = sprintf("INSERT INTO user_tags (user_id) VALUES ('%s')", $userId);
  60. $userTagId = $this->database->write($sql);
  61. $sql = sprintf("INSERT INTO post_tags (post_id, tag_id) VALUES ('%s', '%s')", $currentPostId, $userTagId);
  62. $this->database->write($sql);
  63. }
  64. $sql = "INSERT INTO post_last_updated (post_id, last_updated) VALUES (" . $currentPostId . ", NOW())";
  65. $this->database->write($sql);
  66. if($parentCommentId == 0) {
  67. //send notification to the post author
  68. $postModel = new PostModel();
  69. $parentPostData = $postModel->getPost($currentPostId);
  70. $sql = sprintf("INSERT INTO user_tags (user_id) VALUES ('%s')", $parentPostData['user_id']);
  71. $notificationId = $this->database->write($sql);
  72. $sql = sprintf("INSERT INTO comment_tags (tag_id, comment_id) VALUES ('%s', '%s')", $notificationId, $commentId);
  73. $this->database->write($sql);
  74. } else {
  75. //send notification to the parent comment author
  76. $commentModel = new CommentModel();
  77. $parentCommentData = $commentModel->getComment($parentCommentId);
  78. $sql = sprintf("INSERT INTO user_tags (user_id) VALUES ('%s')", $parentCommentData['commenter_user_id']);
  79. $notificationId = $this->database->write($sql);
  80. $sql = sprintf("INSERT INTO comment_tags (tag_id, comment_id) VALUES ('%s', '%s')", $notificationId, $commentId);
  81. $this->database->write($sql);
  82. }
  83. $this->header->redirect("/?a=post&id=" . $currentPostId . "&t=" . $currentTopicId);
  84. } else {
  85. $this->header->redirect("/");
  86. }
  87. }
  88. }