/** * listing of all the comments * @return array */ public function listAction() { $request = $this->getRequest(); $pageParam = $this->params('page'); $pageSession = new Container('pageComment'); $tabFiltreSession = new Container('tabFiltreCommentSession'); $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); // initialisation du tableau de filtre $tabFiltre['post'] = null; // $tabFiltre['category'] = null; $reset = $this->params('reset'); if (!empty($reset)) { $tabFiltreSession->filtre = $tabFiltre; $pageSession->page = 1; } // le numéro de page on récupère celui reçut en param si y'en a un sinon celui en session $numPage = $pageParam ? $pageParam : $pageSession->page; // créer le form de filtre $formManager = $this->serviceLocator->get('FormElementManager'); $form = $formManager->get('Admin\\Form\\Form\\FilterCommentForm'); $comment = new Comment(); if ($tabFiltreSession->filtre != null) { $comment->setPost($tabFiltreSession->filtre['post']); // $post->setCategory($tabFiltreSession->filtre['category']); } $form->bind($comment); if ($request->isPost() == false) { if (empty($tabFiltreSession->filtre)) { $comments = $em->getRepository('Blog\\Entity\\Comment')->getList($numPage, 20, $tabFiltre); } else { // on filtre avec la session $comments = $em->getRepository('Blog\\Entity\\Comment')->getList($numPage, 20, $tabFiltreSession->filtre); } } else { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $tabFiltre = array(); $tabFiltre['post'] = $data->getPost(); // $tabFiltre['category'] = $data->getCategory(); $comments = $em->getRepository('Blog\\Entity\\Comment')->getList($numPage, 20, $tabFiltre); $tabFiltreSession->filtre = $tabFiltre; } } // On écrase la variable de session if ($numPage) { $pageSession->page = $numPage; } return new ViewModel(array("comments" => $comments, "form" => $form)); }
public function showAction() { $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $idPost = $this->params('id'); $post = $em->getRepository('Blog\\Entity\\Post')->find($idPost); if ($this->zfcUserAuthentication()->hasIdentity()) { $form = new UserCommentForm($this->getRequest()->getBaseUrl() . '/post/captcha/'); } else { $form = new AnonymousCommentForm($this->getRequest()->getBaseUrl() . '/post/captcha/'); } $request = $this->getRequest(); if ($request->isPost()) { //set data post $form->setData($request->getPost()); if ($form->isValid()) { $auth = $this->getServiceLocator()->get('zfcuser_auth_service'); $user = $auth->getIdentity(); $data = $form->getData(); $comment = new Comment(); $comment->setPost($post); if (!$this->zfcUserAuthentication()->hasIdentity()) { $comment->setAnonymous($data['anonymous']); } else { $comment->setAnonymous($user->getUsername()); $comment->setAuthor($user); } $comment->setComment($data['comment']); $em->persist($comment); $em->flush(); $this->flashMessenger()->setNamespace('success')->addMessage('Votre commentaire a été ajouté'); return $this->redirect()->toRoute('show_post', array("id" => $post->getId())); } } // Le post passé en param est erroné on redirige vers l'accueil if ($post == null) { return $this->redirect()->toRoute('home'); } $comments = $em->getRepository('Blog\\Entity\\Comment')->getByPosts($post); return new ViewModel(array('post' => $post, 'form' => $form, 'comments' => $comments)); }
public function addComment(Comment $comment, Post $post) { $comment->setPost($post); $this->em->persist($comment); $this->em->flush($comment); }