Added tagged post listing functionality.

This commit is contained in:
Gergely POLONKAI 2012-10-06 13:25:43 +02:00
parent d74d2e7ebf
commit 2e248a9b58
4 changed files with 53 additions and 8 deletions

View File

@ -4,9 +4,11 @@ namespace GergelyPolonkai\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\ORM\Tools\Pagination\Paginator;
use GergelyPolonkai\FrontBundle\Entity\Post; use GergelyPolonkai\FrontBundle\Entity\Post;
use GergelyPolonkai\FrontBundle\Entity\Tag;
/** /**
* Description of BlogController * Description of BlogController
@ -17,6 +19,9 @@ use GergelyPolonkai\FrontBundle\Entity\Post;
*/ */
class BlogController extends Controller class BlogController extends Controller
{ {
// TODO: Make this a config parameter
private $postsPerPage = 10;
/** /**
* @Route("/", name="GergelyPolonkaiFrontBundle_blogListing") * @Route("/", name="GergelyPolonkaiFrontBundle_blogListing")
* @Route("/page/{cPage}", name="GergelyPolonkaiFrontBundle_blogListingPage", requirements={"cPage": "\d+"}) * @Route("/page/{cPage}", name="GergelyPolonkaiFrontBundle_blogListingPage", requirements={"cPage": "\d+"})
@ -24,16 +29,14 @@ class BlogController extends Controller
*/ */
public function listAction($cPage = 1) public function listAction($cPage = 1)
{ {
// TODO: Make this a config parameter
$postsPerPage = 10;
--$cPage; --$cPage;
$query = $this $query = $this
->getDoctrine() ->getDoctrine()
->getEntityManager() ->getEntityManager()
->createQuery("SELECT p FROM GergelyPolonkaiFrontBundle:Post p WHERE p.draft = FALSE ORDER BY p.createdAt DESC") ->createQuery("SELECT p FROM GergelyPolonkaiFrontBundle:Post p WHERE p.draft = FALSE ORDER BY p.createdAt DESC")
->setFirstResult($cPage * $postsPerPage) ->setFirstResult($cPage * $this->postsPerPage)
->setMaxResults($postsPerPage); ->setMaxResults($this->postsPerPage);
$paginator = new Paginator($query, $fetchJoinCollection = true); $paginator = new Paginator($query, $fetchJoinCollection = true);
$tagManager = $this->get('fpn_tag.tag_manager'); $tagManager = $this->get('fpn_tag.tag_manager');
@ -42,13 +45,13 @@ class BlogController extends Controller
} }
$count = $paginator->count(); $count = $paginator->count();
$pageCount = ceil($count / $postsPerPage); $pageCount = ceil($count / $this->postsPerPage);
return array( return array(
'cpage' => $cPage, 'cpage' => $cPage,
'count' => $pageCount, 'count' => $pageCount,
'posts' => $paginator, 'posts' => $paginator,
'perPage' => $postsPerPage, 'perPage' => $this->postsPerPage,
); );
} }
@ -93,4 +96,43 @@ class BlogController extends Controller
'posts' => $posts, 'posts' => $posts,
); );
} }
/**
* @Route("/tag/{name}/", name="GergelyPolonkaiFrontBundle_blogTagList")
* @Route("/tag/{name}/page/{cPage}", name="GergelyPolonkaiFrontBundle_blogListingPage", requirements={"cPage": "\d+"})
* @Template("GergelyPolonkaiFrontBundle:Blog:list.html.twig")
* @ParamConverter("tag")
*/
public function tagListAction(Tag $tag, $cPage = 1)
{
$tagRepository = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:Tag');
$ids = $tagRepository->getResourceIdsForTag('gergelypolonkaifront_post', $tag->getName());
--$cPage;
$query = $this
->getDoctrine()
->getEntityManager()
->createQuery("SELECT p FROM GergelyPolonkaiFrontBundle:Post p WHERE p.draft = FALSE AND p.id IN (:idList) ORDER BY p.createdAt DESC")
->setParameter('idList', $ids)
->setFirstResult($cPage * $this->postsPerPage)
->setMaxResults($this->postsPerPage);
$paginator = new Paginator($query, $fetchJoinCollection = true);
$tagManager = $this->get('fpn_tag.tag_manager');
foreach ($paginator as $post) {
$tagManager->loadTagging($post);
}
$count = $paginator->count();
$pageCount = ceil($count / $this->postsPerPage);
return array(
'cpage' => $cPage,
'count' => $pageCount,
'posts' => $paginator,
'perPage' => $this->postsPerPage,
);
}
} }

View File

@ -9,7 +9,7 @@ use FPN\TagBundle\Entity\Tag as BaseTag;
* *
* @author polesz * @author polesz
* *
* @ORM\Entity * @ORM\Entity(repositoryClass="DoctrineExtensions\Taggable\Entity\TagRepository")
* @ORM\Table(name="tags") * @ORM\Table(name="tags")
*/ */
class Tag extends BaseTag class Tag extends BaseTag

View File

@ -26,6 +26,9 @@
{% block content %} {% block content %}
{{ block('paginator') }} {{ block('paginator') }}
{% if posts|length == 0 %}
No posts found.
{% endif %}
{% for post in posts %} {% for post in posts %}
{% include 'GergelyPolonkaiFrontBundle:Blog:postViewer.html.twig' with {'post': post, 'title_links': true} %} {% include 'GergelyPolonkaiFrontBundle:Blog:postViewer.html.twig' with {'post': post, 'title_links': true} %}
{% endfor %} {% endfor %}

View File

@ -5,7 +5,7 @@
{% if post.tags|length > 0 %} {% if post.tags|length > 0 %}
<p class="article-tags">Tags: <p class="article-tags">Tags:
{% for tag in post.tags %} {% for tag in post.tags %}
{{ tag.name }} <a href="{{ path('GergelyPolonkaiFrontBundle_blogTagList', { name: tag.name }) }}">{{ tag.name }}</a>
{% endfor %} {% endfor %}
</p> </p>
{% endif %} {% endif %}