Ejemplo n.º 1
0
 public function articleAction($id, Request $request, Application $app)
 {
     $article = $app['dao.article']->find($id);
     $articlepanier = new ArticlePanier();
     $articlepanier->setArticle($article->getId());
     $articleForm = $app['form.factory']->create(new ArticlePanierType(), $articlepanier, ['action' => $app['url_generator']->generate('add_article_to_basket')]);
     $articleForm->handleRequest($request);
     $articleFormView = $articleForm->createView();
     $commentFormView = null;
     if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
         // A user is fully authenticated : he can add comments
         $comment = new Comment();
         $comment->setArticle($article);
         $user = $app['user'];
         $comment->setAuthor($user);
         $commentForm = $app['form.factory']->create(new CommentType(), $comment);
         $commentForm->handleRequest($request);
         if ($commentForm->isSubmitted() && $commentForm->isValid()) {
             $app['dao.comment']->save($comment);
             $app['session']->getFlashBag()->add('success', 'Your comment was succesfully added.');
         }
         $commentFormView = $commentForm->createView();
     }
     $comments = $app['dao.comment']->findAllByArticle($id);
     return $app['twig']->render('article.html.twig', array('article' => $article, 'comments' => $comments, 'commentForm' => $commentFormView, 'articleForm' => $articleFormView));
 }
Ejemplo n.º 2
0
 public function save(Comment $comment)
 {
     $commentData = array('art_id' => $comment->getArticle()->getId(), 'user_id' => $comment->getAuthor()->getId(), 'com_content' => $comment->getContent());
     if ($comment->getId()) {
         // The comment has already been saved : update it
         $this->getDb()->update('comment', $commentData, array('com_id' => $comment->getId()));
     } else {
         // The comment has never been saved : insert it
         $this->getDb()->insert('comment', $commentData);
         // Get the id of the newly created comment and set it on the entity.
         $id = $this->getDb()->lastInsertId();
         $comment->setId($id);
     }
 }