Exemplo n.º 1
0
 /**
  * Creates an Comment object based on a DB row.
  *
  * @param array $row The DB row containing Comment data.
  * @return \MicroCMS\Domain\Comment
  */
 protected function buildDomainObject($row)
 {
     $comment = new Comment();
     $comment->setId($row['com_id']);
     $comment->setContent($row['com_content']);
     $comment->setAuthor($row['com_author']);
     if (array_key_exists('art_id', $row)) {
         // Find and set the associated article
         $articleId = $row['art_id'];
         $article = $this->articleDAO->find($articleId);
         $comment->setArticle($article);
     }
     return $comment;
 }
Exemplo n.º 2
0
 /**
  * Article details controller.
  *
  * @param integer $id Article id
  * @param Request $request Incoming request
  * @param Application $app Silex application
  */
 public function articleAction($id, Request $request, Application $app)
 {
     $article = $app['dao.article']->find($id);
     $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));
 }
Exemplo n.º 3
0
use MicroCMS\Form\Type\GroupType;
// Home page
$app->get('/', function () use($app) {
    $articles = $app['dao.article']->findAll();
    return $app['twig']->render('index.html.twig', array('articles' => $articles));
})->bind('home');
// Article details with comments
$app->match('/article/{id}', function ($id, Request $request) use($app) {
    $article = $app['dao.article']->find($id);
    $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));
})->bind('article');
// Login form
$app->get('/login', function (Request $request) use($app) {
    return $app['twig']->render('login.html.twig', array('error' => $app['security.last_error']($request), 'last_username' => $app['session']->get('_security.last_username')));
})->bind('login');