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