1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace App\Twig;
use App\Entity\Comptoir;
use App\Entity\Flux;
use App\Entity\Groupe;
use App\Entity\Groupeprestataire;
use App\Entity\News;
use App\Entity\Prestataire;
use App\Entity\Rubrique;
use App\Entity\Siege;
use App\Entity\User;
use App\Entity\GlobalParameter;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
public $em;
public $security;
public $container;
public $paginator;
public $session;
public function __construct(ContainerInterface $container, Security $security, EntityManagerInterface $em, PaginatorInterface $paginator, SessionInterface $session)
{
$this->em = $em;
$this->security = $security;
$this->container = $container;
$this->paginator = $paginator;
$this->session = $session;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('showModalGroupChoice', array($this, 'showModalGroupChoice')),
new \Twig_SimpleFunction('getCurrentComptoir', array($this, 'getCurrentComptoir')),
new \Twig_SimpleFunction('getCurrentGroupe', array($this, 'getCurrentGroupe')),
new \Twig_SimpleFunction('getCurrentPrestataire', array($this, 'getCurrentPrestataire')),
new \Twig_SimpleFunction('isCotisationValid', array($this, 'isCotisationValid')),
new \Twig_SimpleFunction('getSiege', array($this, 'getSiege')),
new \Twig_SimpleFunction('isDevFixture', array($this, 'isDevFixture')),
new \Twig_SimpleFunction('getLastNews', array($this, 'getLastNews')),
new \Twig_SimpleFunction('getAllPrestataires', array($this, 'getAllPrestataires')),
new \Twig_SimpleFunction('getAllGroupePrestataires', array($this, 'getAllGroupePrestataires')),
new \Twig_SimpleFunction('getAllComptoirs', array($this, 'getAllComptoirs')),
new \Twig_SimpleFunction('getAllRubriques', array($this, 'getAllRubriques')),
new \Twig_SimpleFunction('getAllGroupes', array($this, 'getAllGroupes')),
new \Twig_SimpleFunction('getAllFlux', array($this, 'getAllFlux')),
new \Twig_SimpleFunction('mediaurl', array($this, 'mediaurl')),
new \Twig_SimpleFunction('getWordpressApiKey', array($this, 'getWordpressApiKey')),
new \Twig_SimpleFunction('parameter', function ($name) {
return $this->container->getParameter($name);
}),
new \Twig_SimpleFunction('isPayzenEnabled', array($this, 'isPayzenEnabled'))
];
}
public function isPayzenEnabled()
{
return $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::USE_PAYZEN) === 'true' ? true : false;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecode']),
new \Twig_SimpleFilter('safe_email', [$this, 'safeEmailFilter']),
);
}
public function jsonDecode($string)
{
return json_decode($string, true);
}
public function getWordpressApiKey()
{
$users = $this->em->getRepository(User::class)->findByRole('ROLE_API');
if (count($users) <= 0) {
// @TODO :erreur => crée un nouvel utilisateur API si non existant ?
return '';
}
return $users[0]->getApiKey();
}
public function showModalGroupChoice()
{
if ($this->security->getUser() != null) {
if (count($this->security->getUser()->getPossiblegroups()) > 1 && count($this->security->getUser()->getGroups()) == 0) {
return true;
}
}
return false;
}
public function getCurrentComptoir()
{
if (!$this->session->has('_comptoirgere')) {
return null;
}
return $this->em->getRepository(Comptoir::class)->findOneById($this->session->get('_comptoirgere')->getId());
}
public function getCurrentGroupe()
{
if (!$this->session->has('_groupegere')) {
return null;
}
return $this->em->getRepository(Groupe::class)->findOneById($this->session->get('_groupegere')->getId());
}
public function getCurrentPrestataire()
{
if (!$this->session->has('_prestagere')) {
return null;
}
return $this->em->getRepository(Prestataire::class)->findOneById($this->session->get('_prestagere')->getId());
}
public function isCotisationValid(): bool
{
if ($this->security->getUser() != null) {
if ($this->security->getUser()->isGranted('ROLE_ADHERENT') && $this->security->getUser()->getAdherent() != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($this->security->getUser()->getAdherent(), 'cotisation');
} elseif ($this->security->getUser()->isGranted('ROLE_PRESTATAIRE') && $this->session->get('_prestagere') != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'));
}
$cotisations = $query->getResult();
foreach ($cotisations as $cotisation) {
if ($cotisation->getCotisationInfos()->getFin() > new \DateTime("now")) {
return true;
}
}
}
return false;
}
public function isDevFixture(?string $username = null)
{
if ($username == null) {
$user = $this->em->getRepository(User::class)->findBy(array('username' => ['user_redacteur', 'user_tresorier', 'user_prestataire', 'user_contact']));
return (count($user) === 4);
}
return ($this->em->getRepository(User::class)->findOneByUsername($username) != null);
}
public function getSiege()
{
return $this->em->getRepository(Siege::class)->findOneById(1);
}
public function getLastNews($limit = 5)
{
return $this->em->getRepository(News::class)->findBy(array('enabled' => true), array('createdAt' => 'DESC'), $limit);
}
public function getAllPrestataires()
{
return $this->em->getRepository(Prestataire::class)->findBy(array('mlc' => false, 'enabled' => true), array('raison'=> 'ASC'));
}
public function getAllGroupePrestataires($type = '')
{
return $this->em->getRepository(Groupeprestataire::class)->findBy(array('type' => $type, 'enabled' => true));
}
public function getAllComptoirs()
{
return $this->em->getRepository(Comptoir::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
}
public function getAllRubriques()
{
return $this->em->getRepository(Rubrique::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
}
public function getAllFlux(User $user, Request $request, $parenttype = null)
{
$query = null;
if ($this->session->get('_prestagere') != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), $parenttype);
} elseif ($user->getAdherent() != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent(), $parenttype);
} elseif ($this->session->get('_comptoirgere') != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByComptoir($this->session->get('_comptoirgere'));
} elseif ($this->session->get('_groupegere') != null) {
$query = $this->em->getRepository(Flux::class)->getQueryByGroupe($this->session->get('_groupegere'));
}
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;
}
public function getAllGroupes()
{
return $this->em->getRepository(Groupe::class)->findBy(array('enabled' => true), array('name'=> 'ASC'));
}
public function mediaurl($media, $format)
{
$provider = $this->container->get($media->getProviderName());
return $provider->generatePublicUrl($media, $format);
}
public function getTests(): array
{
return [
new TwigTest('instanceof', [$this, 'instanceof']),
];
}
public function instanceof($var, $instance)
{
$reflexionClass = new \ReflectionClass($instance);
;
return $reflexionClass->isInstance($var);
}
/**
* 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('@', '@', $email);
}
}