Completed
Push — master ( 911a15...50617c )
by Julito
30:21
created

NavBuilder::profileMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 43
nc 2
nop 2
dl 0
loc 72
rs 9.102
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Menu;
5
6
use Chamilo\PageBundle\Entity\Page;
7
use Chamilo\PageBundle\Entity\Site;
8
use Chamilo\UserBundle\Entity\User;
9
use Knp\Menu\FactoryInterface;
10
use Knp\Menu\ItemInterface;
11
use Symfony\Component\DependencyInjection\ContainerAware;
12
use Symfony\Component\Routing\RouterInterface;
13
14
/**
15
 * Class NavBuilder
16
 * @package Chamilo\CoreBundle\Menu
17
 */
18
class NavBuilder extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated with message: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /**
21
     * @param array  $itemOptions The options given to the created menuItem
22
     * @param string $currentUri  The current URI
23
     *
24
     * @return \Knp\Menu\ItemInterface
25
     */
26
    public function createCategoryMenu(array $itemOptions = array(), $currentUri = null)
27
    {
28
        $factory = $this->container->get('knp_menu.factory');
29
        $menu = $factory->createItem('categories', $itemOptions);
30
31
        $this->buildCategoryMenu($menu, $itemOptions, $currentUri);
32
33
        return $menu;
34
    }
35
36
    /**
37
     * @param \Knp\Menu\ItemInterface $menu        The item to fill with $routes
38
     * @param array                   $options     The item options
39
     * @param string                  $currentUri  The current URI
40
     */
41
    public function buildCategoryMenu(ItemInterface $menu, array $options = array(), $currentUri = null)
42
    {
43
        //$categories = $this->categoryManager->getCategoryTree();
44
45
        //$this->fillMenu($menu, $categories, $options, $currentUri);
46
        $menu->addChild('home', array('route' => 'home'));
47
    }
48
49
    /**
50
     * Top menu left
51
     * @param FactoryInterface $factory
52
     * @param array $options
53
     * @return \Knp\Menu\ItemInterface
54
     */
55
    public function leftMenu(FactoryInterface $factory, array $options)
56
    {
57
        $checker = $this->container->get('security.authorization_checker');
58
        $translator = $this->container->get('translator');
59
60
        $menu = $factory->createItem('root');
61
        $menu->setChildrenAttribute('class', 'nav navbar-nav');
62
63
        $menu->addChild(
64
            $translator->trans('Home'),
65
            array('route' => 'home')
66
        );
67
68
        if ($checker->isGranted('IS_AUTHENTICATED_FULLY')) {
69
70
            $menu->addChild(
71
                $translator->trans('MyCourses'),
72
                array('route' => 'userportal')
73
            );
74
75
            $menu->addChild(
76
                $translator->trans('Calendar'),
77
                array(
78
                    'route' => 'main',
79
                    'routeParameters' => array(
80
                        'name' => 'calendar/agenda_js.php',
81
                    ),
82
                )
83
            );
84
85
            $menu->addChild(
86
                $translator->trans('Reporting'),
87
                array(
88
                    'route' => 'main',
89
                    'routeParameters' => array(
90
                        'name' => 'mySpace/index.php',
91
                    ),
92
                )
93
            );
94
95
            $menu->addChild(
96
                $translator->trans('Social'),
97
                array(
98
                    'route' => 'main',
99
                    'routeParameters' => array(
100
                        'name' => 'social/home.php',
101
                    ),
102
                )
103
            );
104
105
            if ($checker->isGranted('ROLE_ADMIN')) {
106
107
                $menu->addChild(
108
                    $translator->trans('Dashboard'),
109
                    array(
110
                        'route' => 'main',
111
                        'routeParameters' => array(
112
                            'name' => 'dashboard/index.php',
113
                        ),
114
                    )
115
                );
116
117
                $menu->addChild(
118
                    $translator->trans('Administration'),
119
                    array(
120
                        'route' => 'administration',
121
                    )
122
                );
123
            }
124
        }
125
126
        $categories = $this->container->get('faq.entity.category_repository')->retrieveActive();
127
        if ($categories) {
128
            $faq = $menu->addChild(
129
                'FAQ',
130
                [
131
                    'route' => 'faq_index',
132
                ]
133
            );
134
            /** @var Category $category */
135
            foreach ($categories as $category) {
136
                 $faq->addChild(
137
                    $category->getHeadline(),
138
                    array(
139
                        'route' => 'faq',
140
                        'routeParameters' => array(
141
                            'categorySlug' => $category->getSlug(),
142
                            'questionSlug' => '',
143
                        ),
144
                    )
145
                )->setAttribute('divider_append', true);
146
            }
147
        }
148
149
        // Getting site information
150
151
        $site = $this->container->get('sonata.page.site.selector');
152
        $host = $site->getRequestContext()->getHost();
153
        $siteManager = $this->container->get('sonata.page.manager.site');
154
        /** @var Site $site */
155
        $site = $siteManager->findOneBy(array(
156
            'host'    => array($host, 'localhost'),
157
            'enabled' => true,
158
        ));
159
160
        if ($site) {
161
            $pageManager = $this->container->get('sonata.page.manager.page');
162
163
            // Parents only of homepage
164
            $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1];
165
            $pages = $pageManager->findBy($criteria);
166
167
            //$pages = $pageManager->loadPages($site);
168
            /** @var Page $page */
169
            foreach ($pages as $page) {
170
                /*if ($page->getRouteName() !== 'page_slug') {
171
                    continue;
172
                }*/
173
174
                // Avoid home
175
                if ($page->getUrl() === '/') {
176
                    continue;
177
                }
178
179
                if (!$page->isCms()) {
180
                    continue;
181
                }
182
183
                $subMenu = $menu->addChild(
184
                    $page->getName(),
185
                    [
186
                        'route' => $page->getRouteName(),
187
                        'routeParameters' => [
188
                            'path' => $page->getUrl(),
189
                        ],
190
                    ]
191
                );
192
193
                /** @var Page $child */
194
                foreach ($page->getChildren() as $child) {
195
                    $subMenu->addChild(
196
                        $child->getName(),
197
                        array(
198
                            'route' => $page->getRouteName(),
199
                            'routeParameters' => array(
200
                                'path' => $child->getUrl(),
201
                            ),
202
                        )
203
                    )->setAttribute('divider_append', true);
204
                }
205
            }
206
        }
207
208
209
        return $menu;
210
    }
211
212
    /**
213
     * Top menu right
214
     * @param FactoryInterface $factory
215
     * @param array $options
216
     * @return \Knp\Menu\ItemInterface
217
     */
218
    public function rightMenu(FactoryInterface $factory, array $options)
219
    {
220
        $checker = $this->container->get('security.authorization_checker');
221
222
        $translator = $this->container->get('translator');
223
        $menu = $factory->createItem('root');
224
225
        // <nav class="navbar navbar-default">
226
        if ($checker->isGranted('IS_AUTHENTICATED_FULLY')) {
227
228
            $token = $this->container->get('security.token_storage');
229
            /** @var User $user */
230
            $user = $token->getToken()->getUser();
231
            $menu->setChildrenAttribute('class', 'nav navbar-nav navbar-right');
232
            $dropdown = $menu->addChild($user->getUsername())->setAttribute('dropdown', true);
233
234
            /*$dropdown->addChild(
235
                $translator->trans('Profile'),
236
                array('route' => 'fos_user_profile_show')
237
            )->setAttribute('divider_append', true);*/
238
            $dropdown->addChild(
239
                $translator->trans('Inbox'),
240
                array(
241
                    'route' => 'main',
242
                    'routeParameters' => array(
243
                        'name' => 'messages/inbox.php',
244
                    ),
245
                )
246
            )->setAttribute('divider_append', true);
247
248
            // legacy logout
249
            $logoutLink = $menu->addChild(
250
                $translator->trans('Logout'),
251
                array(
252
                    'route' => 'logout',
253
                    //'icon' => 'fa fa-sign-out'
254
                )
255
            );
256
257
            $logoutLink
258
                ->setLinkAttributes(array(
259
                    'id' => 'logout_button',
260
                    'class' => 'fa fa-power-off',
261
                ))
262
                ->setAttributes(array(
263
                    /*'id' => 'signin',
264
                    'class' => 'dropdown'*/
265
                ))
266
            ;
267
        }
268
269
        return $menu;
270
    }
271
272
    /**
273
     * @param FactoryInterface $factory
274
     * @param array $options
275
     * @return ItemInterface
276
     */
277
    public function profileMenu(FactoryInterface $factory, array $options)
278
    {
279
        $menu = $factory->createItem('root');
280
        $security = $this->container->get('security.context');
281
        $translator = $this->container->get('translator');
282
283
        if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
284
            $menu->setChildrenAttribute('class', 'nav nav-pills nav-stacked');
285
286
            $menu->addChild(
287
                $translator->trans('Inbox'),
288
                array(
289
                    'route' => 'main',
290
                    'routeParameters' => array(
291
                        'name' => 'messages/inbox.php',
292
                    ),
293
                )
294
            );
295
296
            $menu->addChild(
297
                $translator->trans('Compose'),
298
                array(
299
                    'route' => 'main',
300
                    'routeParameters' => array(
301
                        'name' => 'messages/new_message.php',
302
                    ),
303
                )
304
            );
305
306
            $menu->addChild(
307
                $translator->trans('PendingInvitations'),
308
                array(
309
                    'route' => 'main',
310
                    'routeParameters' => array(
311
                        'name' => 'social/invitations.php',
312
                    ),
313
                )
314
            );
315
316
            $menu->addChild(
317
                $translator->trans('MyFiles'),
318
                array(
319
                    'route' => 'main',
320
                    'routeParameters' => array(
321
                        'name' => 'social/myfiles.php',
322
                    ),
323
                )
324
            );
325
326
            $menu->addChild(
327
                $translator->trans('EditProfile'),
328
                array(
329
                    'route' => 'main',
330
                    'routeParameters' => array(
331
                        'name' => 'messages/inbox.php',
332
                    ),
333
                )
334
            );
335
336
            $menu->addChild(
337
                $translator->trans('Inbox'),
338
                array(
339
                    'route' => 'main',
340
                    'routeParameters' => array(
341
                        'name' => 'messages/inbox.php',
342
                    ),
343
                )
344
            );
345
        }
346
347
        return $menu;
348
    }
349
350
    /**
351
     * @todo add validations
352
     * @param FactoryInterface $factory
353
     * @param array $options
354
     * @return ItemInterface
355
     */
356
    public function socialMenu(FactoryInterface $factory, array $options)
357
    {
358
        $menu = $factory->createItem('root');
359
        $security = $this->container->get('security.context');
360
        $translator = $this->container->get('translator');
361
362
        if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
363
            $menu->setChildrenAttribute('class', 'nav nav-pills nav-stacked');
364
365
            $menu->addChild(
366
                $translator->trans('Home'),
367
                array(
368
                    'route' => 'main',
369
                    'routeParameters' => array(
370
                        'name' => 'social/home.php',
371
                    ),
372
                )
373
            );
374
375
            $menu->addChild(
376
                $translator->trans('Messages'),
377
                array(
378
                    'route' => 'main',
379
                    'routeParameters' => array(
380
                        'name' => 'messages/inbox.php',
381
                    ),
382
                )
383
            );
384
385
            $menu->addChild(
386
                $translator->trans('Invitations'),
387
                array(
388
                    'route' => 'main',
389
                    'routeParameters' => array(
390
                        'name' => 'social/invitations.php',
391
                    ),
392
                )
393
            );
394
395
396
            $menu->addChild(
397
                $translator->trans('ViewMySharedProfile'),
398
                array(
399
                    'route' => 'main',
400
                    'routeParameters' => array(
401
                        'name' => 'social/profile.php',
402
                    ),
403
                )
404
            );
405
406
            $menu->addChild(
407
                $translator->trans('Friends'),
408
                array(
409
                    'route' => 'main',
410
                    'routeParameters' => array(
411
                        'name' => 'social/friends.php',
412
                    ),
413
                )
414
            );
415
416
            $menu->addChild(
417
                $translator->trans('SocialGroups'),
418
                array(
419
                    'route' => 'main',
420
                    'routeParameters' => array(
421
                        'name' => 'social/groups.php',
422
                    ),
423
                )
424
            );
425
426
            $menu->addChild(
427
                $translator->trans('Search'),
428
                array(
429
                    'route' => 'main',
430
                    'routeParameters' => array(
431
                        'name' => 'social/search.php',
432
                    ),
433
                )
434
            );
435
436
            $menu->addChild(
437
                $translator->trans('MyFiles'),
438
                array(
439
                    'route' => 'main',
440
                    'routeParameters' => array(
441
                        'name' => 'social/myfiles.php',
442
                    ),
443
                )
444
            );
445
        }
446
447
        return $menu;
448
    }
449
}
450