public function showAction() { $slug = $this->params('slug', null); if (!$slug) { return $this->redirect()->toRoute('home'); } $post = $this->postService->getPostBySlug($slug); if (!$post) { $this->flashMessenger()->addErrorMessage($this->getTranslation('POST_NOT_FOUND_SLUG', array($slug))); return $this->redirect()->toRoute('posts'); } $request = $this->getRequest(); $comment = new Comment(); $this->commentForm->bind($comment); if ($request->isPost()) { $data = $request->getPost(); $this->commentForm->setData($data); if ($this->commentForm->isValid()) { /** @var Comment $comment */ $comment = $this->commentForm->getData(); $this->commentService->addComment($comment, $post); $this->flashMessenger()->addSuccessMessage($this->getTranslation('FORM_SUCCESS_COMMENT')); return $this->redirect()->toRoute('posts/show', array('slug' => $post->getSlug())); } } return new ViewModel(array('post' => $post, 'form' => $this->commentForm)); }
public function showAction() { $slug = $this->params('slug'); $article = $this->getEntityManager()->getRepository('Blog\\Entity\\Article')->findOneBy(['slug' => $slug]); if (!$article) { throw new EntityNotFoundException('Entity Article not found'); } $comments = $this->getEntityManager()->getRepository('Blog\\Entity\\Comment')->getActiveByArticle($article->getId()); $comment = new Comment(); $commentForm = new CommentForm(); $commentForm->get('submit')->setAttribute('value', 'Ajouter un commentaire'); $request = $this->getRequest(); if ($request->isPost()) { $commentForm->setData($request->getPost()); if ($commentForm->isValid()) { $comment = $this->getHydrator()->hydrate($commentForm->getData(), $comment); //Persist and flush entity Comment $em = $this->getEntityManager(); $em->persist($comment); $em->flush(); $eventManager = $this->getEventManager(); $eventManager->trigger('comment.add', null, ['comment_id' => $comment->getId(), 'comment_name' => $comment->getName(), 'comment_email' => $comment->getEmail(), 'article_title' => $comment->getArticle()->getTitle(), 'article_id' => $comment->getArticle()->getId()]); //Envoie du mail $this->getServiceLocator()->get('mail')->sendMail($comment->getEmail(), $comment->getName(), $comment->getContent()); //Add flash message $this->flashMessenger()->addMessage('Votre commentaire a été ajouté, il est en attente de validation par l\'administrateur'); $eventManager = $this->getEventManager(); $eventManager->trigger('comment.add', null, compact($comment)); //Redirection return $this->redirect()->toRoute('article', ['slug' => $article->getSlug()]); } } return new ViewModel(['article' => $article, 'comments' => $comments, 'commentForm' => $commentForm]); }
/** * Saves a comment * * @param array $data * @return boolean **/ public function edit($data) { $this->form->setData($data); if ($this->form->isValid()) { $comment = $this->form->getData(); $this->em->persist($comment); $this->em->flush(); $this->addMessage('Updated comment', CommentService::MSG_NOTICE); return true; } $this->addMessage('Something went wrong', CommentService::MSG_ERROR); return false; }