X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FTwig%2FWallabagExtension.php;h=351172c460f2fc24ba76c37e85e76b14ac958c57;hb=f808b01692a835673f328d7221ba8c212caa9b61;hp=fb4c7412395f4affcb3b4b73f844ea06d498d4cc;hpb=d6de23a100221ae1afaa92a58af17a17d0c6614e;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index fb4c7412..351172c4 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -3,6 +3,7 @@ namespace Wallabag\CoreBundle\Twig; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Translation\TranslatorInterface; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; @@ -12,13 +13,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa private $entryRepository; private $tagRepository; private $lifeTime; + private $translator; - public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) + public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator) { $this->entryRepository = $entryRepository; $this->tagRepository = $tagRepository; $this->tokenStorage = $tokenStorage; $this->lifeTime = $lifeTime; + $this->translator = $translator; } public function getFilters() @@ -30,10 +33,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa public function getFunctions() { - return array( + return [ new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), - ); + new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), + ]; } public function removeWww($url) @@ -60,19 +64,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa 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)); } @@ -107,6 +107,43 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return $this->tagRepository->countAllTags($user->getId()); } + /** + * Display a single line about reading stats. + * + * @return string + */ + public function displayStats() + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return 0; + } + + $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) + ->select('e.id') + ->groupBy('e.id') + ->getQuery(); + + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($this->lifeTime); + + $nbArchives = count($query->getArrayResult()); + + $interval = $user->getCreatedAt()->diff(new \DateTime('now')); + $nbDays = (int) $interval->format('%a') ?: 1; + + // force setlocale for date translation + setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()))); + + return $this->translator->trans('footer.stats', [ + '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()), + '%nb_archives%' => $nbArchives, + '%per_day%' => round($nbArchives / $nbDays, 2), + ]); + } + public function getName() { return 'wallabag_extension';