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]); }
/** * Edits the content of a comment * * @param void * @return ViewModel * @throws AccessProhibitedException **/ public function editAction() { $this->_checkAcl('edit'); $comment = $this->_service->getOne($this->getRequest()->getQuery('id')); $form = new CommentForm(); $form->bind($comment); $form->remove('parentId'); $form->remove('captcha'); $form->get('comment')->setAttribute('id', 'commentEditor'); $form->get('comment')->setAttribute('class', 'ckeditor'); $form->get('submit')->setValue('Save'); $this->_service->setForm($form); if ($this->getRequest()->isPost()) { $this->_service->edit($this->getRequest()->getPost()); } return new ViewModel(array('form' => $form, 'messages' => $this->_service->getMessages(CommentService::MSG_NOTICE), 'errors' => $this->_service->getMessages(CommentService::MSG_ERROR))); }