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