wallabag/src/Wallabag/CoreBundle/Twig/WallabagExtension.php

164 lines
5.1 KiB
PHP
Raw Normal View History

2015-09-29 20:59:44 +00:00
<?php
namespace Wallabag\CoreBundle\Twig;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2017-07-01 07:52:38 +00:00
use Symfony\Component\Translation\TranslatorInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;
use Wallabag\CoreBundle\Repository\TagRepository;
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
2015-09-29 20:59:44 +00:00
{
private $tokenStorage;
private $entryRepository;
private $tagRepository;
private $lifeTime;
2016-10-01 13:58:26 +00:00
private $translator;
2016-10-01 13:58:26 +00:00
public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator)
{
$this->entryRepository = $entryRepository;
$this->tagRepository = $tagRepository;
$this->tokenStorage = $tokenStorage;
$this->lifeTime = $lifeTime;
2016-10-01 13:58:26 +00:00
$this->translator = $translator;
}
2015-09-29 20:59:44 +00:00
public function getFilters()
{
return [
new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
new \Twig_SimpleFilter('removeScheme', [$this, 'removeScheme']),
new \Twig_SimpleFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
];
2015-09-29 20:59:44 +00:00
}
public function getFunctions()
{
2017-07-01 07:52:38 +00:00
return [
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
2016-10-01 13:58:26 +00:00
new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
2017-07-01 07:52:38 +00:00
];
}
2015-09-29 20:59:44 +00:00
public function removeWww($url)
{
2015-10-01 07:26:52 +00:00
return preg_replace('/^www\./i', '', $url);
2015-09-29 20:59:44 +00:00
}
public function removeScheme($url)
{
return preg_replace('#^https?://#i', '', $url);
}
public function removeSchemeAndWww($url)
{
2019-04-25 12:12:56 +00:00
return $this->removeWww($this->removeScheme($url));
}
/**
2016-09-03 17:26:23 +00:00
* Return number of entries depending of the type (unread, archive, starred or all).
*
2016-09-03 17:26:23 +00:00
* @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();
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime($this->lifeTime);
return \count($query->getArrayResult());
}
/**
* 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;
}
2016-09-25 10:23:44 +00:00
return $this->tagRepository->countAllTags($user->getId());
}
2016-10-01 13:58:26 +00:00
/**
* 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)) {
2016-10-01 13:58:26 +00:00
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());
2016-10-01 13:58:26 +00:00
$interval = $user->getCreatedAt()->diff(new \DateTime('now'));
$nbDays = (int) $interval->format('%a') ?: 1;
// force setlocale for date translation
2017-07-01 07:52:38 +00:00
setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
2016-10-01 13:58:26 +00:00
return $this->translator->trans('footer.stats', [
'%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
2016-10-01 13:58:26 +00:00
'%nb_archives%' => $nbArchives,
'%per_day%' => round($nbArchives / $nbDays, 2),
]);
}
2015-09-29 20:59:44 +00:00
public function getName()
{
return 'wallabag_extension';
}
}