public function commentAction(Request $request) { /* @var $user User */ $user = $this->getUser(); if (!$user) { throw new UnauthorizedHttpException('You must be logged in to comment.'); } $decklist_id = filter_var($request->get('id'), FILTER_SANITIZE_NUMBER_INT); $decklist = $this->getDoctrine()->getRepository('AppBundle:Decklist')->find($decklist_id); $comment_text = trim($request->get('comment')); if ($decklist && !empty($comment_text)) { $comment_text = preg_replace('%(?<!\\()\\b(?:(?:https?|ftp)://)(?:((?:(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)(?:\\.(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)*(?:\\.[a-z\\x{00a1}-\\x{ffff}]{2,6}))(?::\\d+)?)(?:[^\\s]*)?%iu', '[$1]($0)', $comment_text); $mentionned_usernames = []; $matches = []; if (preg_match_all('/`@([\\w_]+)`/', $comment_text, $matches, PREG_PATTERN_ORDER)) { $mentionned_usernames = array_unique($matches[1]); } $comment_html = $this->get('texts')->markdown($comment_text); $now = new DateTime(); $comment = new Comment(); $comment->setText($comment_html); $comment->setDateCreation($now); $comment->setUser($user); $comment->setDecklist($decklist); $comment->setIsHidden(FALSE); $this->getDoctrine()->getManager()->persist($comment); $decklist->setDateUpdate($now); $decklist->setNbcomments($decklist->getNbcomments() + 1); $this->getDoctrine()->getManager()->flush(); // send emails $spool = []; if ($decklist->getUser()->getIsNotifAuthor()) { if (!isset($spool[$decklist->getUser()->getEmail()])) { $spool[$decklist->getUser()->getEmail()] = 'AppBundle:Emails:newcomment_author.html.twig'; } } foreach ($decklist->getComments() as $comment) { /* @var $comment Comment */ $commenter = $comment->getUser(); if ($commenter && $commenter->getIsNotifCommenter()) { if (!isset($spool[$commenter->getEmail()])) { $spool[$commenter->getEmail()] = 'AppBundle:Emails:newcomment_commenter.html.twig'; } } } foreach ($mentionned_usernames as $mentionned_username) { /* @var $mentionned_user User */ $mentionned_user = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(array('username' => $mentionned_username)); if ($mentionned_user && $mentionned_user->getIsNotifMention()) { if (!isset($spool[$mentionned_user->getEmail()])) { $spool[$mentionned_user->getEmail()] = 'AppBundle:Emails:newcomment_mentionned.html.twig'; } } } unset($spool[$user->getEmail()]); $email_data = array('username' => $user->getUsername(), 'decklist_name' => $decklist->getName(), 'url' => $this->generateUrl('decklist_detail', array('decklist_id' => $decklist->getId(), 'decklist_name' => $decklist->getNameCanonical()), UrlGeneratorInterface::ABSOLUTE_URL) . '#' . $comment->getId(), 'comment' => $comment_html, 'profile' => $this->generateUrl('user_profile_edit', [], UrlGeneratorInterface::ABSOLUTE_URL)); foreach ($spool as $email => $view) { $message = \Swift_Message::newInstance()->setSubject("[thronesdb] New comment")->setFrom(array("*****@*****.**" => $user->getUsername()))->setTo($email)->setBody($this->renderView($view, $email_data), 'text/html'); $this->get('mailer')->send($message); } } return $this->redirect($this->generateUrl('decklist_detail', array('decklist_id' => $decklist_id, 'decklist_name' => $decklist->getNameCanonical()))); }