/** * Notify shop managers of a new comment. * @param CommentCreateEvent $event */ public function notifyAdminOfNewComment(CommentCreateEvent $event) { $config = \Comment\Comment::getConfig(); if (!$config["notify_admin_new_comment"]) { return; } $comment = $event->getComment(); if ($comment === null) { return; } // get the default shop locale $shopLang = LangQuery::create()->findOneByByDefault(true); if ($shopLang !== null) { $shopLocale = $shopLang->getLocale(); } else { $shopLocale = null; } $getCommentRefEvent = new CommentReferenceGetterEvent($comment->getRef(), $comment->getRefId(), $shopLocale); $event->getDispatcher()->dispatch(CommentEvents::COMMENT_REFERENCE_GETTER, $getCommentRefEvent); $this->mailer->sendEmailToShopManagers('new_comment_notification_admin', ['comment_id' => $comment->getId(), 'ref_title' => $getCommentRefEvent->getTitle(), 'ref_type_title' => $getCommentRefEvent->getTypeTitle()]); }
public function createAction() { // only ajax $this->checkXmlHttpRequest(); $responseData = []; /** @var CommentDefinitionEvent $definition */ $definition = null; try { $params = $this->getRequest()->get('admin_add_comment'); $definition = $this->getDefinition($params['ref'], $params['ref_id']); } catch (InvalidDefinitionException $ex) { if ($ex->isSilent()) { // Comment not authorized on this resource $this->accessDenied(); } else { // The customer does not have minimum requirement to post comment $responseData = ["success" => false, "messages" => [$ex->getMessage()]]; return $this->jsonResponse(json_encode($responseData)); } } $customer = $definition->getCustomer(); $validationGroups = ['Default']; if (null === $customer) { $validationGroups[] = 'anonymous'; } if (!$definition->hasRating()) { $validationGroups[] = 'rating'; } $commentForm = $this->createForm('comment.add.form', 'form', [], ['validation_groups' => $validationGroups]); try { $form = $this->validateForm($commentForm); $event = new CommentCreateEvent(); $event->bindForm($form); $event->setVerified($definition->isVerified()); if (null !== $customer) { $event->setCustomerId($customer->getId()); } if (!$definition->getConfig()['moderate']) { $event->setStatus(\Comment\Model\Comment::ACCEPTED); } else { $event->setStatus(\Comment\Model\Comment::PENDING); } $event->setLocale($this->getRequest()->getLocale()); $this->dispatch(CommentEvents::COMMENT_CREATE, $event); if (null !== $event->getComment()) { $responseData = ["success" => true, "messages" => [$this->getTranslator()->trans("Thank you for submitting your comment.", [], Comment::MESSAGE_DOMAIN)]]; if ($definition->getConfig()['moderate']) { $responseData['messages'][] = $this->getTranslator()->trans("Your comment will be put online once verified.", [], Comment::MESSAGE_DOMAIN); } } else { $responseData = ["success" => false, "messages" => [$this->getTranslator()->trans("Sorry, an unknown error occurred. Please try again.", [], Comment::MESSAGE_DOMAIN)]]; } } catch (Exception $ex) { $responseData = ["success" => false, "messages" => [$ex->getMessage()]]; } return $this->jsonResponse(json_encode($responseData)); }