Completed
Push — master ( 371d59...f8efc0 )
by Julito
28:47
created

NavBuilder::rightMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 2
dl 0
loc 53
rs 9.5797
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
     * @param FactoryInterface $factory
214
     * @param array $options
215
     * @return ItemInterface
216
     */
217
    public function profileMenu(FactoryInterface $factory, array $options)
218
    {
219
        $menu = $factory->createItem('root');
220
        $security = $this->container->get('security.context');
221
        $translator = $this->container->get('translator');
222
223
        if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
224
            $menu->setChildrenAttribute('class', 'nav nav-pills nav-stacked');
225
226
            $menu->addChild(
227
                $translator->trans('Inbox'),
228
                array(
229
                    'route' => 'main',
230
                    'routeParameters' => array(
231
                        'name' => 'messages/inbox.php',
232
                    ),
233
                )
234
            );
235
236
            $menu->addChild(
237
                $translator->trans('Compose'),
238
                array(
239
                    'route' => 'main',
240
                    'routeParameters' => array(
241
                        'name' => 'messages/new_message.php',
242
                    ),
243
                )
244
            );
245
246
            $menu->addChild(
247
                $translator->trans('PendingInvitations'),
248
                array(
249
                    'route' => 'main',
250
                    'routeParameters' => array(
251
                        'name' => 'social/invitations.php',
252
                    ),
253
                )
254
            );
255
256
            $menu->addChild(
257
                $translator->trans('MyFiles'),
258
                array(
259
                    'route' => 'main',
260
                    'routeParameters' => array(
261
                        'name' => 'social/myfiles.php',
262
                    ),
263
                )
264
            );
265
266
            $menu->addChild(
267
                $translator->trans('EditProfile'),
268
                array(
269
                    'route' => 'main',
270
                    'routeParameters' => array(
271
                        'name' => 'messages/inbox.php',
272
                    ),
273
                )
274
            );
275
276
            $menu->addChild(
277
                $translator->trans('Inbox'),
278
                array(
279
                    'route' => 'main',
280
                    'routeParameters' => array(
281
                        'name' => 'messages/inbox.php',
282
                    ),
283
                )
284
            );
285
        }
286
287
        return $menu;
288
    }
289
290
    /**
291
     * @todo add validations
292
     * @param FactoryInterface $factory
293
     * @param array $options
294
     * @return ItemInterface
295
     */
296
    public function socialMenu(FactoryInterface $factory, array $options)
297
    {
298
        $menu = $factory->createItem('root');
299
        $security = $this->container->get('security.context');
300
        $translator = $this->container->get('translator');
301
302
        if ($security->isGranted('IS_AUTHENTICATED_FULLY')) {
303
            $menu->setChildrenAttribute('class', 'nav nav-pills nav-stacked');
304
305
            $menu->addChild(
306
                $translator->trans('Home'),
307
                array(
308
                    'route' => 'main',
309
                    'routeParameters' => array(
310
                        'name' => 'social/home.php',
311
                    ),
312
                )
313
            );
314
315
            $menu->addChild(
316
                $translator->trans('Messages'),
317
                array(
318
                    'route' => 'main',
319
                    'routeParameters' => array(
320
                        'name' => 'messages/inbox.php',
321
                    ),
322
                )
323
            );
324
325
            $menu->addChild(
326
                $translator->trans('Invitations'),
327
                array(
328
                    'route' => 'main',
329
                    'routeParameters' => array(
330
                        'name' => 'social/invitations.php',
331
                    ),
332
                )
333
            );
334
335
336
            $menu->addChild(
337
                $translator->trans('ViewMySharedProfile'),
338
                array(
339
                    'route' => 'main',
340
                    'routeParameters' => array(
341
                        'name' => 'social/profile.php',
342
                    ),
343
                )
344
            );
345
346
            $menu->addChild(
347
                $translator->trans('Friends'),
348
                array(
349
                    'route' => 'main',
350
                    'routeParameters' => array(
351
                        'name' => 'social/friends.php',
352
                    ),
353
                )
354
            );
355
356
            $menu->addChild(
357
                $translator->trans('SocialGroups'),
358
                array(
359
                    'route' => 'main',
360
                    'routeParameters' => array(
361
                        'name' => 'social/groups.php',
362
                    ),
363
                )
364
            );
365
366
            $menu->addChild(
367
                $translator->trans('Search'),
368
                array(
369
                    'route' => 'main',
370
                    'routeParameters' => array(
371
                        'name' => 'social/search.php',
372
                    ),
373
                )
374
            );
375
376
            $menu->addChild(
377
                $translator->trans('MyFiles'),
378
                array(
379
                    'route' => 'main',
380
                    'routeParameters' => array(
381
                        'name' => 'social/myfiles.php',
382
                    ),
383
                )
384
            );
385
        }
386
387
        return $menu;
388
    }
389
}
390