2015-09-29 20:59:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Twig;
|
|
|
|
|
2016-04-21 20:30:50 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
|
|
|
|
2016-04-21 20:35:42 +00:00
|
|
|
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
|
2015-09-29 20:59:44 +00:00
|
|
|
{
|
2016-04-21 20:30:50 +00:00
|
|
|
private $tokenStorage;
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
|
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->tokenStorage = $tokenStorage;
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:59:44 +00:00
|
|
|
public function getFilters()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
return [
|
|
|
|
new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
|
|
|
|
];
|
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
|
|
|
}
|
|
|
|
|
2016-04-21 20:30:50 +00:00
|
|
|
public function getGlobals()
|
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
|
|
|
if (null === $user || !is_object($user)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:20:12 +00:00
|
|
|
$unreadEntries = $this->repository->enableCache(
|
|
|
|
$this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
|
|
|
|
);
|
|
|
|
|
|
|
|
$starredEntries = $this->repository->enableCache(
|
|
|
|
$this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
|
|
|
|
);
|
|
|
|
|
|
|
|
$archivedEntries = $this->repository->enableCache(
|
|
|
|
$this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
|
|
|
|
);
|
|
|
|
|
|
|
|
$allEntries = $this->repository->enableCache(
|
|
|
|
$this->repository->getBuilderForAllByUser($user->getId())->getQuery()
|
|
|
|
);
|
2016-04-21 20:30:50 +00:00
|
|
|
|
|
|
|
return array(
|
2016-09-01 18:20:12 +00:00
|
|
|
'unreadEntries' => count($unreadEntries->getResult()),
|
|
|
|
'starredEntries' => count($starredEntries->getResult()),
|
|
|
|
'archivedEntries' => count($archivedEntries->getResult()),
|
|
|
|
'allEntries' => count($allEntries->getResult()),
|
2016-04-21 20:30:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:59:44 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'wallabag_extension';
|
|
|
|
}
|
|
|
|
}
|