namespace Wallabag\CoreBundle\Controller;
+use Pagerfanta\Adapter\ArrayAdapter;
+use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Form\Type\NewTagType;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class TagController extends Controller
{
$entry->removeTag($tag);
$em = $this->getDoctrine()->getManager();
$em->flush();
- if (count($tag->getEntries()) == 0) {
+
+ // remove orphan tag in case no entries are associated to it
+ if (count($tag->getEntries()) === 0) {
$em->remove($tag);
+ $em->flush();
}
- $em->flush();
$redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
{
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
- ->findTags($this->getUser()->getId());
-
- return $this->render(
- 'WallabagCoreBundle:Tag:tags.html.twig',
- [
- 'tags' => $tags,
- ]
- );
+ ->findAllTags($this->getUser()->getId());
+
+ $flatTags = [];
+
+ foreach ($tags as $key => $tag) {
+ $nbEntries = $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag['id']);
+
+ $flatTags[] = [
+ 'id' => $tag['id'],
+ 'label' => $tag['label'],
+ 'slug' => $tag['slug'],
+ 'nbEntries' => $nbEntries,
+ ];
+ }
+
+ return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
+ 'tags' => $flatTags,
+ ]);
+ }
+
+ /**
+ * @param Tag $tag
+ * @param int $page
+ *
+ * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
+ * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
+ *
+ * @return \Symfony\Component\HttpFoundation\Response
+ */
+ public function showEntriesForTagAction(Tag $tag, $page, Request $request)
+ {
+ $entriesByTag = $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->findAllByTagId($this->getUser()->getId(), $tag->getId());
+
+ $pagerAdapter = new ArrayAdapter($entriesByTag);
+
+ $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')
+ ->prepare($pagerAdapter, $page);
+
+ try {
+ $entries->setCurrentPage($page);
+ } catch (OutOfRangeCurrentPageException $e) {
+ if ($page > 1) {
+ return $this->redirect($this->generateUrl($request->get('_route'), [
+ 'slug' => $tag->getSlug(),
+ 'page' => $entries->getNbPages(),
+ ]), 302);
+ }
+ }
+
+ return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
+ 'form' => null,
+ 'entries' => $entries,
+ 'currentPage' => $page,
+ ]);
}
}