X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FTwig%2FWallabagExtension.php;h=3780b13e7db5884abf3b9e156dff8db0e0b8c397;hb=32e95760f567e5849dd71aaba9aabb1ab56bcd0a;hp=f8328860b6489da92561dd2180cf8ebac0c181ac;hpb=24429857d8a431cd20077c5c0c3db81e2fedb1ac;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index f8328860..3780b13e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,18 +2,130 @@ namespace Wallabag\CoreBundle\Twig; -class WallabagExtension extends \Twig_Extension +use Doctrine\ORM\Query; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Wallabag\CoreBundle\Repository\EntryRepository; +use Wallabag\CoreBundle\Repository\TagRepository; + +class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { + private $tokenStorage; + private $entryRepository; + private $tagRepository; + private $lifeTime; + + public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) + { + $this->entryRepository = $entryRepository; + $this->tagRepository = $tagRepository; + $this->tokenStorage = $tokenStorage; + $this->lifeTime = $lifeTime; + } + public function getFilters() + { + return [ + new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), + ]; + } + + public function getFunctions() { return array( - new \Twig_SimpleFilter('removeWww', array($this, 'removeWww')), + new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), + new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), ); } public function removeWww($url) { - return preg_replace('/^www\./i', '',$url); + return preg_replace('/^www\./i', '', $url); + } + + /** + * Return number of entries depending of the type (unread, archive, starred or all). + * + * @param string $type Type of entries to count + * + * @return int + */ + public function countEntries($type) + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return 0; + } + + switch ($type) { + case 'starred': + $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); + break; + + case 'archive': + $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); + break; + + case 'unread': + $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); + break; + + case 'all': + $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); + break; + + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } + + // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) + // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function + $query = $qb + ->select('e.id') + ->groupBy('e.id') + ->getQuery(); + + $data = $this->enableCache($query) + ->getArrayResult(); + + return count($data); + } + + /** + * Return number of tags. + * + * @return int + */ + public function countTags() + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return 0; + } + + $qb = $this->tagRepository->findAllTags($user->getId()); + + $data = $this->enableCache($qb->getQuery()) + ->getArrayResult(); + + return count($data); + } + + /** + * Enable cache for a query. + * + * @param Query $query + * + * @return Query + */ + private function enableCache(Query $query) + { + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($this->lifeTime); + + return $query; } public function getName()