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; }
/** * 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))); }
/** * @see \Zend\ServiceManager\FactoryInterface::createService() * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return \Blog\Form\CommentForm */ public function createService(ServiceLocatorInterface $serviceLocator) { $oForm = new CommentForm('comment'); return $oForm->setInputFilter(new CommentInputFilter()); }
/** * To view a specific blog post * * @param void * @return ViewModel * @throws AccessProhibitedException **/ public function viewAction() { $this->_checkAcl('view'); $request = $this->getRequest(); $post = $this->_postService->getFromTitle($request->getQuery('title')); $form = new CommentForm(); $form->setReplyToList($post->getComments()); $viewVars = array(); if ($request->isPost()) { $service = new CommentService($this->_em); $service->setForm($form); $service->setPost($post); $service->save($request->getPost()); $viewVars['messages'] = $service->getMessages(CommentService::MSG_NOTICE); $viewVars['errors'] = $service->getMessages(CommentService::MSG_ERROR); } $viewVars['post'] = $post; $viewVars['form'] = $form; $viewVars['query'] = $request->getQuery(); return new ViewModel($viewVars); }