Completed
Push — master ( d9bece...2ea354 )
by Andrea
10:06
created

MenuController::getMenu()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 4.0032

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 32
cts 34
cp 0.9412
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 31
nc 5
nop 1
crap 4.0032
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/**
6
 * Menu controller.
7
 */
8
class MenuController extends FiCoreController
9
{
10
11 10
    protected function initGestionePermessi()
12
    {
13 10
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
14
15 10
        return $gestionepermessi;
16
    }
17
18 10
    public function generamenuAction()
19
    {
20 10
        $router = $this->get('router')->match('/')['_route'];
21
        //$homeurl = $this->generateUrl($router, array(), true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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