Skip to content
This repository was archived by the owner on Oct 2, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 80 additions & 34 deletions Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,97 @@

namespace PSS\Bundle\BlogBundle\Controller;


# Symfony/Doctrine internal
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;


# Specific


# Domain objects


# Entities
use PSS\Bundle\BlogBundle\Entity\Post;
use PSS\Bundle\BlogBundle\Entity\Term;



/**
* Base Controller
*
* Basic views available here, to override you can
* use the PostManager service. The idea of this controller
* is that you can extend as a Base, and alter the returned array()
* that each method returns using parent::methodName();
*
* Cookbook in PSSBlogBundle/Resources/doc/recipe-extending-blog-controller.md
*
* @Route("/blog")
*/
class BlogController extends Controller
{
/**
* @Route("/blog", name="blog_index")
* Generate a comment form and process
*/
public function indexAction()
protected function comment(Request $request, Post $post)
{
$entityManager = $this->get('doctrine.orm.entity_manager');

$query = $entityManager
->getRepository('PSS\Bundle\BlogBundle\Entity\Post')
->getPublishedPostsQuery();

$paginator = $this->createPaginator($query);

return $this->render('PSSBlogBundle:Blog:index.html.twig', array('paginator' => $paginator));
# try {
# $cmtMgr = $this->get('pss.blogbundle.manager.comment');
# $entity = $cmtMgr->entityFactory($request); // Setting the "always required" stuff
}

/**
* @Route("/tag/{tag}", name="blog_posts_by_tag")
* @Route("/{year}/{month}/{slug}", name="blog_show")
* @Template()
*/
public function postsByTagAction($tag)
public function showAction(Post $post, Request $request)
{
$entityManager = $this->get('doctrine.orm.entity_manager');

$query = $entityManager
->getRepository('PSS\Bundle\BlogBundle\Entity\Post')
->getPublishedPostsByTagQuery($tag);

$paginator = $this->createPaginator($query);
return array(
'post' => $post->onlyIfPublished(),
'comment_form' => $this->comment($request,$post)
);
}

if ($paginator->getTotalItemCount() == 0) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Page Not Found');
}
/**
* @Route("/", name="blog_index")
* @Template()
*/
public function indexAction()
{
$pm = $this->getPostManager();
$paginator = $this->createPaginator($pm->getRepository()->getPublishedPostsQuery());

return $this->render('PSSBlogBundle:Blog:postsByTag.html.twig', array('paginator' => $paginator));
return array(
'paginator' => $paginator
);
}

/**
* @Route("/{slug}", name="blog_show")
* @Route("/tag/{slug}", name="blog_posts_by_tag")
* @Template()
*/
public function showAction($slug)
public function postsByTagAction(Term $term)
{
$entityManager = $this->get('doctrine.orm.entity_manager');
$pm = $this->getPostManager();
$repository = $pm->getRepository();
$query = $repository
->getPublishedByTerm($term);

$paginator = $this->createPaginator($query->getQuery());

try {
$post = $entityManager
->getRepository('PSS\Bundle\BlogBundle\Entity\Post')
->findPublishedPostOrPage($slug);
} catch (\Doctrine\ORM\NoResultException $exception) {
if ($paginator->getTotalItemCount() == 0) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Page Not Found');
}

return $this->render('PSSBlogBundle:Blog:show.html.twig', array('post' => $post));
return array(
'paginator' => $paginator,
'term' => $term
);
}

public function recentPostsAction($max)
Expand Down Expand Up @@ -86,11 +120,23 @@ public function tagCloudAction()
}

/**
* Access to Post Manager service
*
* @return \PSS\Bundle\BlogBundle\Manager\PostManager
*/
protected function getPostManager()
{
return $this->get('pss.blog.manager.post');
}

/**
* Create a paginator from a Query
*
* @param \Doctrine\ORM\Query $query
*
* @return \Knp\Component\Pager\Pagination\PaginationInterface
*/
private function createPaginator(\Doctrine\ORM\Query $query)
protected function createPaginator(\Doctrine\ORM\Query $query)
{
$paginator = $this->get('knp_paginator');
$request = $this->get('request');
Expand Down
Loading