コード例 #1
0
ファイル: CommentController.php プロジェクト: blump/Comment
 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));
 }