1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-01 17:38:38 +00:00
wallabag/src/Wallabag/CoreBundle/Twig/WallabagExtension.php

93 lines
2.5 KiB
PHP
Raw Normal View History

2015-09-29 22:59:44 +02:00
<?php
namespace Wallabag\CoreBundle\Twig;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
2015-09-29 22:59:44 +02:00
{
private $tokenStorage;
private $repository;
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
{
$this->repository = $repository;
$this->tokenStorage = $tokenStorage;
}
2015-09-29 22:59:44 +02:00
public function getFilters()
{
return [
new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
];
2015-09-29 22:59:44 +02:00
}
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
);
}
2015-09-29 22:59:44 +02:00
public function removeWww($url)
{
2015-10-01 09:26:52 +02:00
return preg_replace('/^www\./i', '', $url);
2015-09-29 22:59:44 +02:00
}
/**
2016-09-03 19:26:23 +02:00
* Return number of entries depending of the type (unread, archive, starred or all).
*
2016-09-03 19:26:23 +02: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 [];
}
switch ($type) {
case 'starred':
$qb = $this->repository->getBuilderForStarredByUser($user->getId());
break;
2016-09-01 20:20:12 +02:00
case 'archive':
$qb = $this->repository->getBuilderForArchiveByUser($user->getId());
break;
2016-09-01 20:20:12 +02:00
case 'unread':
$qb = $this->repository->getBuilderForUnreadByUser($user->getId());
break;
2016-09-01 20:20:12 +02:00
case 'all':
$qb = $this->repository->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();
2016-09-03 19:26:23 +02:00
$data = $this->repository
->enableCache($query)
->getArrayResult();
return count($data);
}
2015-09-29 22:59:44 +02:00
public function getName()
{
return 'wallabag_extension';
}
}