aboutsummaryrefslogblamecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
blob: 93640dc68811a896b5b2d776c0443d3a327f5580 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                   


                                                                                       
                                                                                           
 








                                                                                                               

                                


                                                                      



                                   
                                                   

     




                                                                                                    
                      

         
                                                        
                                                                                                           


                                                         
                                                                                                            


                                                          
                                                                                                            


                                                     
                                                                                                        
          
 





                                                                           

     




                                    
<?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
{
    private $tokenStorage;
    private $repository;

    public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
    {
        $this->repository = $repository;
        $this->tokenStorage = $tokenStorage;
    }

    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
        ];
    }

    public function removeWww($url)
    {
        return preg_replace('/^www\./i', '', $url);
    }

    public function getGlobals()
    {
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;

        if (null === $user || !is_object($user)) {
            return [];
        }

        $unreadEntries = $this->repository->enableCache(
            $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery()
        );

        $starredEntries = $this->repository->enableCache(
            $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery()
        );

        $archivedEntries = $this->repository->enableCache(
            $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery()
        );

        $allEntries = $this->repository->enableCache(
            $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery()
        );

        return [
            'unreadEntries' => $unreadEntries->getSingleScalarResult(),
            'starredEntries' => $starredEntries->getSingleScalarResult(),
            'archivedEntries' => $archivedEntries->getSingleScalarResult(),
            'allEntries' => $allEntries->getSingleScalarResult(),
        ];
    }

    public function getName()
    {
        return 'wallabag_extension';
    }
}