Added tagged post listing functionality.
This commit is contained in:
parent
d74d2e7ebf
commit
2e248a9b58
@ -4,9 +4,11 @@ namespace GergelyPolonkai\FrontBundle\Controller;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
|
||||
use GergelyPolonkai\FrontBundle\Entity\Post;
|
||||
use GergelyPolonkai\FrontBundle\Entity\Tag;
|
||||
|
||||
/**
|
||||
* Description of BlogController
|
||||
@ -17,6 +19,9 @@ use GergelyPolonkai\FrontBundle\Entity\Post;
|
||||
*/
|
||||
class BlogController extends Controller
|
||||
{
|
||||
// TODO: Make this a config parameter
|
||||
private $postsPerPage = 10;
|
||||
|
||||
/**
|
||||
* @Route("/", name="GergelyPolonkaiFrontBundle_blogListing")
|
||||
* @Route("/page/{cPage}", name="GergelyPolonkaiFrontBundle_blogListingPage", requirements={"cPage": "\d+"})
|
||||
@ -24,16 +29,14 @@ class BlogController extends Controller
|
||||
*/
|
||||
public function listAction($cPage = 1)
|
||||
{
|
||||
// TODO: Make this a config parameter
|
||||
$postsPerPage = 10;
|
||||
--$cPage;
|
||||
|
||||
$query = $this
|
||||
->getDoctrine()
|
||||
->getEntityManager()
|
||||
->createQuery("SELECT p FROM GergelyPolonkaiFrontBundle:Post p WHERE p.draft = FALSE ORDER BY p.createdAt DESC")
|
||||
->setFirstResult($cPage * $postsPerPage)
|
||||
->setMaxResults($postsPerPage);
|
||||
->setFirstResult($cPage * $this->postsPerPage)
|
||||
->setMaxResults($this->postsPerPage);
|
||||
|
||||
$paginator = new Paginator($query, $fetchJoinCollection = true);
|
||||
$tagManager = $this->get('fpn_tag.tag_manager');
|
||||
@ -42,13 +45,13 @@ class BlogController extends Controller
|
||||
}
|
||||
|
||||
$count = $paginator->count();
|
||||
$pageCount = ceil($count / $postsPerPage);
|
||||
$pageCount = ceil($count / $this->postsPerPage);
|
||||
|
||||
return array(
|
||||
'cpage' => $cPage,
|
||||
'count' => $pageCount,
|
||||
'posts' => $paginator,
|
||||
'perPage' => $postsPerPage,
|
||||
'perPage' => $this->postsPerPage,
|
||||
);
|
||||
}
|
||||
|
||||
@ -93,4 +96,43 @@ class BlogController extends Controller
|
||||
'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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use FPN\TagBundle\Entity\Tag as BaseTag;
|
||||
*
|
||||
* @author polesz
|
||||
*
|
||||
* @ORM\Entity
|
||||
* @ORM\Entity(repositoryClass="DoctrineExtensions\Taggable\Entity\TagRepository")
|
||||
* @ORM\Table(name="tags")
|
||||
*/
|
||||
class Tag extends BaseTag
|
||||
|
@ -26,6 +26,9 @@
|
||||
|
||||
{% block content %}
|
||||
{{ block('paginator') }}
|
||||
{% if posts|length == 0 %}
|
||||
No posts found.
|
||||
{% endif %}
|
||||
{% for post in posts %}
|
||||
{% include 'GergelyPolonkaiFrontBundle:Blog:postViewer.html.twig' with {'post': post, 'title_links': true} %}
|
||||
{% endfor %}
|
||||
|
@ -5,7 +5,7 @@
|
||||
{% if post.tags|length > 0 %}
|
||||
<p class="article-tags">Tags:
|
||||
{% for tag in post.tags %}
|
||||
{{ tag.name }}
|
||||
<a href="{{ path('GergelyPolonkaiFrontBundle_blogTagList', { name: tag.name }) }}">{{ tag.name }}</a>
|
||||
{% endfor %}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
Loading…
Reference in New Issue
Block a user