AppExtension.php 5.39 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;
8
use App\Entity\Groupeprestataire;
Julien Jorry committed
9
use App\Entity\News;
10
use App\Entity\Prestataire;
11
use App\Entity\Rubrique;
Julien Jorry committed
12
use App\Entity\Siege;
13 14 15
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface;
16 17 18
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
19
use Twig\TwigTest;
20 21 22

class AppExtension extends AbstractExtension
{
23
    public $container;
24
    public $paginator;
25

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

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

Julien Jorry committed
50 51 52 53 54
    public function getSiege()
    {
        return $this->container->get('doctrine')->getRepository(Siege::class)->findOneById(1);
    }

Julien Jorry committed
55 56 57 58 59
    public function getLastNews($limit = 5)
    {
        return $this->container->get('doctrine')->getRepository(News::class)->findBy(array('enabled' => true), array('createdAt' => 'DESC'), $limit);
    }

60 61
    public function getAllPrestataires()
    {
62
        return $this->container->get('doctrine')->getRepository(Prestataire::class)->findBy(array('enabled' => true), array('raison'=> 'ASC'));
63 64
    }

65 66 67 68 69
    public function getAllGroupePrestataires($type = '')
    {
        return $this->container->get('doctrine')->getRepository(Groupeprestataire::class)->findBy(array('type' => $type, 'enabled' => true));
    }

70 71
    public function getAllComptoirs()
    {
72
        return $this->container->get('doctrine')->getRepository(Comptoir::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
73 74
    }

75 76
    public function getAllRubriques()
    {
77
        return $this->container->get('doctrine')->getRepository(Rubrique::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
78
    }
79

80 81 82 83 84 85 86 87 88 89 90 91
    /**
     * Return a list of all filters.
     *
     * @return array
     */
    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('safe_email', [$this, 'safeEmailFilter']),
        ];
    }

92 93 94 95 96 97 98
    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());
Julien Jorry committed
99 100 101 102
        } else if ($user->getComptoirsgere() != null) {
            $query = $this->container->get('doctrine')->getRepository(Flux::class)->getQueryByComptoir($user->getComptoirsgere());
        } else if ($user->getGroupesgere() != null) {
            $query = $this->container->get('doctrine')->getRepository(Flux::class)->getQueryByGroupe($user->getGroupesgere());
103 104 105 106 107 108 109 110 111 112 113 114 115
        }
        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;
    }

116 117
    public function getAllGroupes()
    {
118
        return $this->container->get('doctrine')->getRepository(Groupe::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
119 120
    }

121 122 123 124 125 126
    public function mediaurl($media, $format)
    {
        $provider = $this->container->get($media->getProviderName());
        return $provider->generatePublicUrl($media, $format);
    }

127
    public function getTests(): array
128 129
    {
        return [
130
            new TwigTest('instanceof', [$this, 'instanceof']),
131 132 133
        ];
    }

134
    public function instanceof($var, $instance)
135
    {
136 137
        $reflexionClass = new \ReflectionClass($instance);
        return $reflexionClass->isInstance($var);
138
    }
139 140 141 142 143 144 145 146 147 148 149 150

    /**
     * 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
151 152 153 154 155
        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) {
156 157 158
                $email .= $str[$i];
            }
        }
Julien Jorry committed
159
        return str_replace('@', '&#64;', $email);
160
    }
161
}