Exemple #1
0
 public function showAction()
 {
     $formComment = new CommentForm();
     // On récupère l'objet Request
     $request = $this->getRequest();
     // On vérifie si le formulaire a été posté
     if ($request->isPost()) {
         // On instancie notre modèle Post
         $comment = new Comment();
         // Et on passe l'InputFilter de Post au formulaire
         $formComment->setInputFilter($comment->getInputFilter());
         $formComment->setData($request->getPost());
         // Si le formulaire est valide
         if ($formComment->isValid()) {
             // On prend les données du formulaire qui sont converti pour correspondre à notre modèle Post
             $comment->exchangeArray($formComment->getData());
             var_dump($formComment->getData());
             $this->getServiceLocator()->get('Zend\\Log')->info("Un commentaire ajouté !");
             $this->sendMail($formComment->getData());
             // On enregistre ces données dans la table Post
             //                $this->getServiceLocator()->get('Application\Service\CommentService')->saveComment($comment);
             // Puis on redirige sur la page d'accueil.
             return $this->redirect()->toUrl('/');
         }
         // Si le formulaire n'est pas valide, on reste sur la page et les erreurs apparaissent
     }
     $post = $this->getServiceLocator()->get('Application\\Service\\PostService')->getById($this->params('id'));
     $post->category = $this->getServiceLocator()->get('Application\\Service\\CategoryService')->getById($post->category_id);
     $post->author = $this->getServiceLocator()->get('Application\\Service\\UserService')->getById($post->author);
     $post->comments = $this->getServiceLocator()->get('Application\\Service\\CommentService')->getByPostId($post->post_id);
     if (is_array($post->tags)) {
         $post->tags = $this->getServiceLocator()->get('Application\\Service\\TagService')->getByArrayId($post->tags);
     }
     $formComment->setData(array("post_id" => $post->post_id));
     return new ViewModel(array('post' => $post, 'form' => $formComment, 'post_id' => $this->params('id'), 'flashMessages' => $this->flashMessenger()->getMessages()));
 }
 public function addAction()
 {
     $formComment = new CommentForm();
     // On récupère l'objet Request
     $request = $this->getRequest();
     // On vérifie si le formulaire a été posté
     if ($request->isPost()) {
         // On instancie notre modèle Comment
         $comment = new Comment();
         // Et on passe l'InputFilter de Comment au formulaire
         $formComment->setInputFilter($comment->getInputFilter());
         $formComment->setData($request->getPost());
         // Si le formulaire est valide
         if ($formComment->isValid()) {
             // On prend les données du formulaire qui sont converti pour correspondre à notre modèle Comment
             $comment->exchangeArray($formComment->getData());
             $destinataire = $formComment->getData()['email'];
             $this->sendMail($formComment->getData()['email']);
             // On enregistre ces données dans la table Comment
             $this->getServiceLocator()->get('Application\\Service\\CommentService')->saveComment($comment);
             $this->getServiceLocator()->get('Zend\\Log')->info("Un commentaire a été ajoutée");
             $this->flashMessenger()->addMessage(array('success' => "Votre commentaire a été ajoutée"));
             // Puis on redirige sur la page d'accueil.
             return $this->redirect()->toUrl($this->getRequest()->getServer('HTTP_REFERER'));
         } else {
             // Si le formulaire n'est pas valide, on reste sur la page et les erreurs apparaissent
             foreach ($formComment->getMessages() as $messageId => $messages) {
                 foreach ($messages as $message) {
                     $this->getServiceLocator()->get('Zend\\Log')->err("Validation failure '{$messageId}': {$message}");
                     $this->flashMessenger()->addMessage(array('error' => "Validation failure '{$messageId}': {$message}"));
                 }
             }
         }
     }
     return new ViewModel(array('form' => $formComment, 'flashMessages' => $this->flashMessenger()->getMessages()));
 }
 public function addAction()
 {
     $request = $this->getRequest();
     $this->layout('layout/ajax_layout.phtml');
     if ($request->isPost()) {
         $c = new Comment();
         $c->setCommentAuthor($request->getPost('comment_author'));
         $c->setCommentAuthorIp($_SERVER['REMOTE_ADDR']);
         $c->setCommentContent($request->getPost('comment_content'));
         $c->setCommentDate(new \DateTime('now'));
         $c->setCommentTitle("");
         $this->getEntityManager()->persist($c);
         $this->getEntityManager()->flush();
         $this->flashMessenger()->addInfoMessage("Comentário postado com sucesso!");
         return $this->redirect()->toRoute('home');
     }
 }
Exemple #4
0
 public function addComment(Comment $comment)
 {
     $comment->setAlbum($this);
     $comments = $this->getComments();
     $comments[] = $comment;
     $this->setComments($comments);
     return $this;
 }