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

namespace App\Twig;

5
use App\Entity\Comptoir;
6
use App\Entity\Flux;
7
use App\Entity\Groupe;
Julien Jorry committed
8
use App\Entity\News;
9
use App\Entity\Prestataire;
10
use App\Entity\Rubrique;
11 12 13
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
14 15 16
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
17
use Twig\TwigTest;
18 19 20

class AppExtension extends AbstractExtension
{
21
    public $container;
22
    public $paginator;
23

24
    public function __construct($container, $paginator)
25 26
    {
        $this->container = $container;
27
        $this->paginator = $paginator;
28 29 30 31 32
    }

    public function getFunctions()
    {
        return [
Julien Jorry committed
33
            new \Twig_SimpleFunction('getLastNews', array($this, 'getLastNews')),
34 35
            new \Twig_SimpleFunction('getAllPrestataires', array($this, 'getAllPrestataires')),
            new \Twig_SimpleFunction('getAllComptoirs', array($this, 'getAllComptoirs')),
36 37
            new \Twig_SimpleFunction('getAllRubriques', array($this, 'getAllRubriques')),
            new \Twig_SimpleFunction('getAllGroupes', array($this, 'getAllGroupes')),
38
            new \Twig_SimpleFunction('getAllFlux', array($this, 'getAllFlux')),
39
            new \Twig_SimpleFunction('mediaurl', array($this, 'mediaurl')),
Julien Jorry committed
40
            new \Twig_SimpleFunction('parameter', function ($name) {
41 42 43 44
                return $this->container->getParameter($name);
            })
        ];
    }
45

Julien Jorry committed
46 47 48 49 50
    public function getLastNews($limit = 5)
    {
        return $this->container->get('doctrine')->getRepository(News::class)->findBy(array('enabled' => true), array('createdAt' => 'DESC'), $limit);
    }

51 52 53 54 55 56 57 58 59 60
    public function getAllPrestataires()
    {
        return $this->container->get('doctrine')->getRepository(Prestataire::class)->findBy(array('enabled' => true));
    }

    public function getAllComptoirs()
    {
        return $this->container->get('doctrine')->getRepository(Comptoir::class)->findBy(array('enabled' => true));
    }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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']),
        ];
    }

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    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;
    }

97 98 99 100 101
    public function getAllGroupes()
    {
        return $this->container->get('doctrine')->getRepository(Groupe::class)->findBy(array('enabled' => true));
    }

102 103 104 105 106 107
    public function mediaurl($media, $format)
    {
        $provider = $this->container->get($media->getProviderName());
        return $provider->generatePublicUrl($media, $format);
    }

108
    public function getTests(): array
109 110
    {
        return [
111
            new TwigTest('instanceof', [$this, 'instanceof']),
112 113 114
        ];
    }

115
    public function instanceof($var, $instance)
116
    {
117 118
        $reflexionClass = new \ReflectionClass($instance);
        return $reflexionClass->isInstance($var);
119
    }
120 121 122 123 124 125 126 127 128 129 130 131

    /**
     * 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   = '';
Julien Jorry committed
132 133 134 135 136
        for ($i=0, $len = strlen($str); $i < $len; $i++) {
            $j = mt_rand(0, 1);
            if ($j === 0) {
                $email .= '&#'.ord($str[$i]).';';
            } else if ($j === 1) {
137 138 139
                $email .= $str[$i];
            }
        }
Julien Jorry committed
140
        return str_replace('@', '&#64;', $email);
141
    }
142
}