1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
8
|
|
|
|
9
|
|
|
class MenuExtension extends \Twig_Extension |
10
|
|
|
{ |
11
|
|
|
protected $em; |
12
|
|
|
protected $urlgenerator; |
13
|
|
|
protected $user; |
14
|
|
|
|
15
|
26 |
|
public function __construct(ObjectManager $em, UrlGeneratorInterface $urlgenerator, TokenStorageInterface $user, $rootpath) |
16
|
|
|
{ |
17
|
26 |
|
$this->em = $em; |
|
|
|
|
18
|
26 |
|
$this->urlgenerator = $urlgenerator; |
19
|
26 |
|
$this->user = $user; |
|
|
|
|
20
|
26 |
|
$this->rootpath = $rootpath; |
|
|
|
|
21
|
26 |
|
} |
22
|
|
|
|
23
|
1 |
|
public function getFunctions() |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
1 |
|
new \Twig_SimpleFunction('generamenu', [$this, 'generamenu'], [ |
27
|
1 |
|
'needs_environment' => true, |
28
|
|
|
'is_safe' => ['html'], |
29
|
|
|
]), |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
12 |
|
public function generamenu(\Twig_Environment $environment) |
34
|
|
|
{ |
35
|
12 |
|
$router = $this->urlgenerator->match('/')['_route']; |
|
|
|
|
36
|
12 |
|
$rispostahome = array(); |
|
|
|
|
37
|
12 |
|
$rispostahome[] = array('percorso' => $this->getUrlObject('', $router, ''), |
38
|
12 |
|
'nome' => 'Home', |
39
|
12 |
|
'target' => '_self', |
40
|
|
|
); |
41
|
|
|
|
42
|
12 |
|
$em = $this->em; |
43
|
|
|
/* @var $qb \Doctrine\ORM\QueryBuilder */ |
44
|
12 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
45
|
12 |
|
$qb->select(array('a')); |
46
|
12 |
|
$qb->from('BiCoreBundle:Menuapplicazione', 'a'); |
47
|
12 |
|
$qb->where('a.attivo = :attivo and (a.padre is null or a.padre = 0)'); |
48
|
12 |
|
$qb->setParameter('attivo', true); |
49
|
12 |
|
$qb->orderBy('a.padre', 'ASC'); |
50
|
12 |
|
$qb->orderBy('a.ordine', 'ASC'); |
51
|
12 |
|
$menu = $qb->getQuery()->getResult(); |
52
|
|
|
|
53
|
12 |
|
$risposta = array_merge($rispostahome, $this->getMenu($menu)); |
54
|
|
|
|
55
|
12 |
|
$pathmanuale = $this->rootpath.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'manuale.pdf'; |
56
|
12 |
|
$username = ''; |
|
|
|
|
57
|
12 |
|
$urlLogout = ''; |
|
|
|
|
58
|
|
|
|
59
|
12 |
|
if (file_exists($pathmanuale)) { |
60
|
|
|
$risposta[] = array( |
61
|
|
|
'percorso' => $this->getUrlObject('Manuale', $pathmanuale, '_blank'), |
62
|
|
|
'nome' => 'Manuale', 'target' => '_blank', ); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
if ('ssocdf' === $this->user->getToken()->getProviderKey()) { |
|
|
|
|
66
|
|
|
$username = $this->user->getToken()->getUser()->getUsername(); |
|
|
|
|
67
|
|
|
$urlLogout = $this->urlgenerator->generate('fi_autenticazione_signout'); |
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
if ('main' === $this->user->getToken()->getProviderKey()) { |
71
|
12 |
|
$username = $this->user->getToken()->getUser()->getUsername(); |
|
|
|
|
72
|
12 |
|
$urlLogout = $this->urlgenerator->generate('fos_user_security_logout'); |
73
|
|
|
} |
74
|
|
|
|
75
|
12 |
|
$risposta[] = array('percorso' => $this->getUrlObject($username, '', ''), 'nome' => $username, 'target' => '', |
76
|
|
|
'sottolivello' => array( |
77
|
12 |
|
array('percorso' => $urlLogout, 'nome' => 'Logout', 'target' => '_self'), |
78
|
|
|
), |
79
|
|
|
); |
80
|
|
|
|
81
|
12 |
|
return $environment->render('BiCoreBundle:Menu:menu.html.twig', array('risposta' => $risposta)); |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
protected function getMenu($menu) |
85
|
|
|
{ |
86
|
12 |
|
$risposta = array(); |
87
|
12 |
|
$em = $this->em; |
|
|
|
|
88
|
|
|
|
89
|
12 |
|
foreach ($menu as $item) { |
90
|
12 |
|
$visualizzare = true; |
91
|
|
|
|
92
|
12 |
|
if ($item->isAutorizzazionerichiesta()) { |
93
|
12 |
|
$permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user); |
|
|
|
|
94
|
12 |
|
$visualizzare = $permessi->canRead($item->getTag()); |
95
|
|
|
} |
96
|
|
|
|
97
|
12 |
|
if ($visualizzare) { |
98
|
12 |
|
$qb = $em->createQueryBuilder(); |
99
|
12 |
|
$qb->select(array('a')); |
100
|
12 |
|
$qb->from('BiCoreBundle:Menuapplicazione', 'a'); |
101
|
12 |
|
$qb->where('a.padre = :padre_id'); |
102
|
12 |
|
$qb->andWhere('a.attivo = :attivo'); |
103
|
12 |
|
$qb->orderBy('a.padre', 'ASC'); |
104
|
12 |
|
$qb->orderBy('a.ordine', 'ASC'); |
105
|
12 |
|
$qb->setParameter('padre_id', $item->getId()); |
106
|
12 |
|
$qb->setParameter('attivo', true); |
107
|
12 |
|
$submenu = $qb->getQuery()->getResult(); |
108
|
|
|
|
109
|
12 |
|
$sottomenutabelle = $this->getSubMenu($submenu); |
110
|
|
|
|
111
|
12 |
|
$percorso = $this->getUrlObject($item->getNome(), $item->getPercorso(), $item->getTarget()); |
|
|
|
|
112
|
12 |
|
$risposta[] = array( |
113
|
12 |
|
'percorso' => $percorso, |
114
|
12 |
|
'nome' => $item->getNome(), |
115
|
12 |
|
'sottolivello' => $sottomenutabelle, |
116
|
12 |
|
'target' => $item->getTarget(), |
117
|
12 |
|
'notifiche' => $item->hasNotifiche(), |
118
|
12 |
|
'tag' => $item->getTag(), |
119
|
12 |
|
'percorsonotifiche' => $this->getUrlObject($item->getNome(), $item->getPercorsonotifiche(), ''), |
120
|
|
|
); |
121
|
12 |
|
unset($submenu); |
122
|
12 |
|
unset($sottomenutabelle); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
12 |
|
return $risposta; |
127
|
|
|
} |
128
|
|
|
|
129
|
12 |
|
protected function getSubMenu($submenu) |
130
|
|
|
{ |
131
|
12 |
|
$sottomenutabelle = array(); |
132
|
12 |
|
foreach ($submenu as $subitem) { |
133
|
12 |
|
$visualizzare = true; |
134
|
12 |
|
if ($subitem->isAutorizzazionerichiesta()) { |
135
|
|
|
$permessi = new \Cdf\BiCoreBundle\Service\Permessi\PermessiManager($this->em, $this->user); |
|
|
|
|
136
|
|
|
$visualizzare = $permessi->canRead($subitem->getTag()); |
137
|
|
|
} |
138
|
|
|
|
139
|
12 |
|
if ($visualizzare) { |
140
|
12 |
|
$vettoresottomenu = $this->getMenu(array($subitem)); |
141
|
12 |
|
$sottomenu = $vettoresottomenu[0]; |
|
|
|
|
142
|
|
|
|
143
|
12 |
|
if (isset($sottomenu['sottolivello']) && count($sottomenu['sottolivello']) > 0) { |
144
|
11 |
|
$sottolivellomenu = array('sottolivello' => $sottomenu['sottolivello']); |
|
|
|
|
145
|
11 |
|
$menuobj = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget()); |
|
|
|
|
146
|
11 |
|
$sottomenutabelle[] = array_merge($menuobj, $sottolivellomenu); |
147
|
|
|
} else { |
148
|
12 |
|
$sottomenutabelle[] = $this->getUrlObject($subitem->getNome(), $subitem->getPercorso(), $subitem->getTarget()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
12 |
|
return $sottomenutabelle; |
154
|
|
|
} |
155
|
|
|
|
156
|
12 |
|
protected function getUrlObject($nome, $percorso, $target) |
157
|
|
|
{ |
158
|
12 |
|
if ($this->routeExists($percorso)) { |
159
|
12 |
|
$percorso = $this->urlgenerator->generate($percorso); |
160
|
|
|
} else { |
161
|
12 |
|
$percorso = '#'; |
162
|
|
|
} |
163
|
12 |
|
if (!$target) { |
164
|
12 |
|
$target = '_self'; |
165
|
|
|
} |
166
|
|
|
|
167
|
12 |
|
return array('percorso' => $percorso, 'nome' => $nome, 'target' => $target); |
168
|
|
|
} |
169
|
|
|
|
170
|
12 |
|
protected function routeExists($name) |
171
|
|
|
{ |
172
|
12 |
|
$router = $this->urlgenerator; |
173
|
|
|
|
174
|
12 |
|
if ((null === $router->getRouteCollection()->get($name)) ? false : true) { |
|
|
|
|
175
|
12 |
|
return true; |
176
|
|
|
} else { |
177
|
12 |
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function urlExists($name) |
182
|
|
|
{ |
183
|
|
|
if ($this->checkUrl($name, false)) { |
184
|
|
|
return true; |
185
|
|
|
} else { |
186
|
|
|
if ($this->checkUrl($name, true)) { |
187
|
|
|
return true; |
188
|
|
|
} else { |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function checkUrl($name, $proxy) |
195
|
|
|
{ |
196
|
|
|
$ch = curl_init($name); |
197
|
|
|
|
198
|
|
|
curl_setopt($ch, CURLOPT_URL, $name); |
199
|
|
|
if ($proxy) { |
200
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $proxy); |
201
|
|
|
} else { |
202
|
|
|
curl_setopt($ch, CURLOPT_PROXY, null); |
203
|
|
|
} |
204
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
205
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); |
206
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds |
207
|
|
|
curl_exec($ch); |
208
|
|
|
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
209
|
|
|
if (200 === $retcode || 401 === $retcode) { |
210
|
|
|
$exist = true; |
211
|
|
|
} else { |
212
|
|
|
$exist = false; |
213
|
|
|
} |
214
|
|
|
curl_close($ch); |
215
|
|
|
|
216
|
|
|
return $exist; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.