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 editAction() { $id = (int) $this->params()->fromRoute('id', 0); // id that we editing, defaults to zero $form = new CommentForm(); // form used for the edit $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $albumDao = $em->getRepository('Application\\Entity\\Album'); $comment = $em->getRepository('Application\\Entity\\Comment')->find($id); // if we do not have an entry for the comment, i.e id not found or not defined // send them to add if (!$comment) { return $this->redirect()->toRoute('admin/comment', array('action' => 'add')); } // retarded but robust way to do this $albumTitles = array(); foreach ($albumDao->findAll() as $v) { $albumTitles[$v->id] = "{$v->title} ({$v->artist})"; } // setup form // (validation, data and button) $form->setInputFilter($comment->getInputFilter())->setData($comment->toArray())->get('submit')->setAttribute('value', 'Edit'); $form->get('album_id')->setValue((int) (!isset($comment->album)) ? null : $comment->album->id)->setAttributes(array('options' => $albumTitles)); // process a submission if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); // set the form with the submitted values // is valid? if ($form->isValid()) { $albumId = (int) $form->get('album_id')->getValue(); $comment->setOptions($form->getData()); // set the data $comment->id = $id; $album = $albumDao->find($albumId); $comment->setOptions($form->getData()); // set the data if ($album) { $em->persist($album); // set data $em->persist($comment); // set data $album->addComment($comment); $em->flush(); // save $this->flashMessenger()->addMessage(array('alert-success' => 'Comment Updated!')); } else { $this->flashMessenger()->addMessage(array('alert-error' => 'Error! Album not found!')); } // Redirect to list of comments return $this->redirect()->toRoute('admin/comment'); } else { //$this->flashMessenger()->addMessage(array('alert-error'=>'Form error')); } } return array('id' => $id, 'form' => $form); }