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