forked from wallabag/wallabag
Added tags counter in sidebar (material theme)
This commit is contained in:
@ -2,18 +2,24 @@
|
||||
|
||||
namespace Wallabag\CoreBundle\Twig;
|
||||
|
||||
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 $repository;
|
||||
private $entryRepository;
|
||||
private $tagRepository;
|
||||
private $lifeTime;
|
||||
|
||||
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
|
||||
public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->entryRepository = $entryRepository;
|
||||
$this->tagRepository = $tagRepository;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->lifeTime = $lifeTime;
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
@ -27,6 +33,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
|
||||
new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
|
||||
);
|
||||
}
|
||||
|
||||
@ -52,19 +59,19 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
|
||||
|
||||
switch ($type) {
|
||||
case 'starred':
|
||||
$qb = $this->repository->getBuilderForStarredByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
|
||||
break;
|
||||
|
||||
case 'archive':
|
||||
$qb = $this->repository->getBuilderForArchiveByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
|
||||
break;
|
||||
|
||||
case 'unread':
|
||||
$qb = $this->repository->getBuilderForUnreadByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
|
||||
break;
|
||||
|
||||
case 'all':
|
||||
$qb = $this->repository->getBuilderForAllByUser($user->getId());
|
||||
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -78,13 +85,49 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
|
||||
->groupBy('e.id')
|
||||
->getQuery();
|
||||
|
||||
$data = $this->repository
|
||||
->enableCache($query)
|
||||
$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 [];
|
||||
}
|
||||
|
||||
$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
|
||||
*/
|
||||
public function enableCache(Query $query)
|
||||
{
|
||||
$query->useQueryCache(true);
|
||||
$query->useResultCache(true);
|
||||
$query->setResultCacheLifetime($this->lifeTime);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'wallabag_extension';
|
||||
|
||||
Reference in New Issue
Block a user