|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dominikzogg\EnergyCalculator\Menu; |
|
4
|
|
|
|
|
5
|
|
|
use Dominikzogg\EnergyCalculator\Entity\User; |
|
6
|
|
|
use Knp\Menu\FactoryInterface; |
|
7
|
|
|
use Knp\Menu\ItemInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
11
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MenuBuilder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var FactoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $menuFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var AuthorizationCheckerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $authorizationChecker; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TokenStorageInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $tokenStorage; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TranslatorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $translator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param FactoryInterface $menuFactory |
|
37
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
|
38
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
39
|
|
|
* @param TranslatorInterface $translator |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
FactoryInterface $menuFactory, |
|
43
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
|
44
|
|
|
TokenStorageInterface $tokenStorage, |
|
45
|
|
|
TranslatorInterface $translator |
|
46
|
|
|
) { |
|
47
|
|
|
$this->menuFactory = $menuFactory; |
|
48
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
49
|
|
|
$this->tokenStorage = $tokenStorage; |
|
50
|
|
|
$this->translator = $translator; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function buildMenu(Request $request) |
|
54
|
|
|
{ |
|
55
|
|
|
$menu = $this->menuFactory->createItem('root'); |
|
56
|
|
|
|
|
57
|
|
|
if ($this->getUser() instanceof User) { |
|
58
|
|
|
$this->createManageMenu($menu, $request); |
|
59
|
|
|
$this->createChartMenu($menu, $request); |
|
60
|
|
|
|
|
61
|
|
|
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) { |
|
62
|
|
|
$this->createAdminMenu($menu, $request); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->createUserMenu($menu, $request); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $menu; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param ItemInterface $menu |
|
73
|
|
|
* @param Request $request |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function createManageMenu(ItemInterface $menu, Request $request) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$menu->addChild($this->translator->trans('nav.day'), array( |
|
78
|
|
|
'route' => 'day_list', |
|
79
|
|
|
)); |
|
80
|
|
|
$menu->addChild($this->translator->trans('nav.comestible'), array( |
|
81
|
|
|
'route' => 'comestible_list', |
|
82
|
|
|
)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param ItemInterface $menu |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function createChartMenu(ItemInterface $menu, Request $request) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$chartMenu = $menu->addChild($this->translator->trans('nav.chart.title')); |
|
92
|
|
|
|
|
93
|
|
|
$chartMenu->addChild($this->translator->trans('nav.chart.weight'), array( |
|
94
|
|
|
'route' => 'chart_weight', |
|
95
|
|
|
)); |
|
96
|
|
|
|
|
97
|
|
|
$chartMenu->addChild($this->translator->trans('nav.chart.calorie'), array( |
|
98
|
|
|
'route' => 'chart_calorie', |
|
99
|
|
|
)); |
|
100
|
|
|
|
|
101
|
|
|
$chartMenu->addChild($this->translator->trans('nav.chart.energymix'), array( |
|
102
|
|
|
'route' => 'chart_energymix', |
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param ItemInterface $menu |
|
108
|
|
|
* @param Request $request |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function createAdminMenu(ItemInterface $menu, Request $request) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$menu->addChild($this->translator->trans('nav.user'), array( |
|
113
|
|
|
'route' => 'user_list', |
|
114
|
|
|
)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param ItemInterface $menu |
|
119
|
|
|
* @param Request $request |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function createUserMenu(ItemInterface $menu, Request $request) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$menu->addChild($this->translator->trans('nav.logout'), array( |
|
124
|
|
|
'route' => 'logout', |
|
125
|
|
|
)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return User|null |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getUser() |
|
132
|
|
|
{ |
|
133
|
|
|
$token = $this->tokenStorage->getToken(); |
|
134
|
|
|
|
|
135
|
|
|
if (is_null($token)) { |
|
136
|
|
|
return null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $token->getUser(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.