3 namespace Wallabag\CoreBundle\Twig
;
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
6 use Wallabag\CoreBundle\Repository\EntryRepository
;
8 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
10 private $tokenStorage;
13 public function __construct(EntryRepository
$repository = null, TokenStorageInterface
$tokenStorage = null)
15 $this->repository
= $repository;
16 $this->tokenStorage
= $tokenStorage;
19 public function getFilters()
22 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
26 public function getFunctions()
29 new \
Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
33 public function removeWww($url)
35 return preg_replace('/^www\./i', '', $url);
39 * Return number of entries depending of the type (unread, archive, starred or all)
41 * @param string $type Type of entries to count
45 public function countEntries($type)
47 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
49 if (null === $user || !is_object($user)) {
55 $qb = $this->repository
->getBuilderForStarredByUser($user->getId());
59 $qb = $this->repository
->getBuilderForArchiveByUser($user->getId());
63 $qb = $this->repository
->getBuilderForUnreadByUser($user->getId());
67 $qb = $this->repository
->getBuilderForAllByUser($user->getId());
71 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
74 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
75 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
81 $data =$this->repository
88 public function getName()
90 return 'wallabag_extension';