AppExtension.php 3.26 KB
Newer Older
1 2 3 4
<?php

namespace App\Twig;

5
use App\Entity\Flux;
6 7
use App\Entity\Groupe;
use App\Entity\Rubrique;
8 9 10
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
11 12 13
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
14
use Twig\TwigTest;
15 16 17

class AppExtension extends AbstractExtension
{
18
    public $container;
19
    public $paginator;
20

21
    public function __construct($container, $paginator)
22 23
    {
        $this->container = $container;
24
        $this->paginator = $paginator;
25 26 27 28 29
    }

    public function getFunctions()
    {
        return [
30 31
            new \Twig_SimpleFunction('getAllRubriques', array($this, 'getAllRubriques')),
            new \Twig_SimpleFunction('getAllGroupes', array($this, 'getAllGroupes')),
32
            new \Twig_SimpleFunction('getAllFlux', array($this, 'getAllFlux')),
33 34 35 36 37 38
            new \Twig_SimpleFunction('parameter', function($name)
            {
                return $this->container->getParameter($name);
            })
        ];
    }
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    public function getAllRubriques()
    {
        return $this->container->get('doctrine')->getRepository(Rubrique::class)->findBy(array('enabled' => true));
    }
    /**
     * Return a list of all filters.
     *
     * @return array
     */
    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('safe_email', [$this, 'safeEmailFilter']),
        ];
    }

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    public function getAllFlux(User $user, Request $request)
    {
        $query = null;
        if ($user->getPrestataire() != null) {
            $query = $this->container->get('doctrine')->getRepository(Flux::class)->getQueryByPrestataire($user->getPrestataire());
        } else if ($user->getAdherent() != null) {
            $query = $this->container->get('doctrine')->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent());
        }
        if ($query != null) {
            $pagination = $this->paginator->paginate(
                $query, /* query NOT result */
                $request->query->getInt('page', 1)/*page number*/,
                10/*limit per page*/
            );

            return $pagination;
        }
        return null;
    }

76 77 78 79 80
    public function getAllGroupes()
    {
        return $this->container->get('doctrine')->getRepository(Groupe::class)->findBy(array('enabled' => true));
    }

81
    public function getTests(): array
82 83
    {
        return [
84
            new TwigTest('instanceof', [$this, 'instanceof']),
85 86 87
        ];
    }

88
    public function instanceof($var, $instance)
89
    {
90 91
        $reflexionClass = new \ReflectionClass($instance);
        return $reflexionClass->isInstance($var);
92
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

    /**
     * Protects email address.
     * (inspired by : https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Twig/TwigExtension.php )
     *
     * @param  string $str
     *
     * @return string
     */
    public function safeEmailFilter($str)
    {
        $email   = '';
        for ( $i = 0, $len = strlen( $str ); $i < $len; $i++ ) {
            $j = mt_rand( 0, 1);
            if ( $j === 0 ) {
                $email .= '&#' . ord( $str[$i] ) . ';';
            } elseif ( $j === 1 ) {
                $email .= $str[$i];
            }
        }
        return str_replace( '@', '&#64;', $email );
    }
115
}