|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
namespace Chamilo\CoreBundle\Controller; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Knp\Menu\Matcher\Matcher; |
|
12
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
14
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
16
|
|
|
|
|
17
|
|
|
use Knp\Menu\FactoryInterface as MenuFactoryInterface; |
|
18
|
|
|
use Knp\Menu\ItemInterface as MenuItemInterface; |
|
19
|
|
|
use Knp\Menu\Renderer\ListRenderer; |
|
20
|
|
|
|
|
21
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Each entity controller must extends this class. |
|
25
|
|
|
* |
|
26
|
|
|
* @abstract |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
abstract class BaseResourceController extends ResourceController |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @return \Symfony\Component\Security\Core\SecurityContextInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
/*public function getSecurity() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->container->get('security.context'); |
|
36
|
|
|
}*/ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return object |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getTemplate() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->container->get('templating'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return NotFoundHttpException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function abort() |
|
50
|
|
|
{ |
|
51
|
|
|
return new NotFoundHttpException(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Converts string 'Chamilo\CoreBundle\Controller\Admin\QuestionManager' into |
|
56
|
|
|
* 'admin/question_manager' |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getTemplatePath() |
|
59
|
|
|
{ |
|
60
|
|
|
$parts = $this->classParts; |
|
61
|
|
|
|
|
62
|
|
|
$newPath = array(); |
|
63
|
|
|
foreach ($parts as $part) { |
|
64
|
|
|
if (in_array($part, array('chamilo_lms', 'controller')) |
|
65
|
|
|
//strpos($part, '_controller') > 0 |
|
66
|
|
|
) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
$newPath[] = $part; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$template = implode('/', $newPath); |
|
73
|
|
|
return str_replace('_controller', '', $template); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Transforms 'QuestionManagerController' to 'question_manager.controller' |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getControllerAlias() |
|
81
|
|
|
{ |
|
82
|
|
|
$parts = $this->classParts; |
|
83
|
|
|
$parts = array_reverse($parts); |
|
84
|
|
|
$alias = str_replace('_controller', '.controller', $parts[0]); |
|
85
|
|
|
return $alias; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Translator shortcut |
|
90
|
|
|
* @param string $variable |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function trans($variable) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->container->get('translator')->trans($variable); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the class name label |
|
100
|
|
|
* @example RoleController -> Role |
|
101
|
|
|
* |
|
102
|
|
|
* @return string the class name label |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getClassNameLabel() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->classNameLabel; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return MenuFactoryInterface |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getMenuFactory() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->container->get('knp_menu.factory'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $action |
|
119
|
|
|
* @return MenuItemInterface |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getBreadcrumbs($action) |
|
122
|
|
|
{ |
|
123
|
|
|
$breadcrumbs = $this->buildBreadcrumbs($action); |
|
124
|
|
|
|
|
125
|
|
|
return $breadcrumbs; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** Main home URL |
|
129
|
|
|
* @return MenuItemInterface |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getHomeBreadCrumb() |
|
132
|
|
|
{ |
|
133
|
|
|
$menu = $this->getMenuFactory()->createItem( |
|
134
|
|
|
'root', |
|
135
|
|
|
array( |
|
136
|
|
|
'childrenAttributes' => array( |
|
137
|
|
|
'class' => 'breadcrumb', |
|
138
|
|
|
'currentClass' => 'active' |
|
139
|
|
|
) |
|
140
|
|
|
) |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$menu->addChild( |
|
144
|
|
|
$this->trans('Home'), |
|
145
|
|
|
array('uri' => $this->generateUrl('home')) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return $menu; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param $action |
|
153
|
|
|
* @param MenuItemInterface $menu |
|
154
|
|
|
* @return MenuItemInterface |
|
155
|
|
|
*/ |
|
156
|
|
|
public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
|
157
|
|
|
{ |
|
158
|
|
|
if (!$menu) { |
|
159
|
|
|
$menu = $this->getHomeBreadCrumb(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$menu->addChild( |
|
163
|
|
|
$this->trans($this->getClassnameLabel().'List'), |
|
164
|
|
|
array('uri' => $this->generateControllerUrl('listingAction')) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$action = str_replace( |
|
168
|
|
|
array($this->getControllerAlias().':', 'Action'), |
|
169
|
|
|
'', |
|
170
|
|
|
$action |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
switch ($action) { |
|
174
|
|
|
case 'add': |
|
175
|
|
|
case 'edit': |
|
176
|
|
|
$menu->addChild( |
|
177
|
|
|
$this->trans($this->getClassnameLabel().ucfirst($action)) |
|
178
|
|
|
//array('uri' => $this->generateControllerUrl($action.'Action')) |
|
179
|
|
|
); |
|
180
|
|
|
break; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $menu; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param array $breadCrumbList |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function parseLegacyBreadCrumb($breadCrumbList = array()) |
|
191
|
|
|
{ |
|
192
|
|
|
$menu = $this->getHomeBreadCrumb(); |
|
193
|
|
|
foreach ($breadCrumbList as $item) { |
|
194
|
|
|
$menu->addChild( |
|
195
|
|
|
$this->trans($item['title']), |
|
196
|
|
|
array('uri' => $item['url']) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
201
|
|
|
$result = $renderer->render($menu); |
|
202
|
|
|
|
|
203
|
|
|
return $result; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Renders the current controller template |
|
208
|
|
|
* @param string $name |
|
209
|
|
|
* @param array $elements |
|
210
|
|
|
* @return mixed |
|
211
|
|
|
*/ |
|
212
|
|
|
public function renderTemplate($name, $elements = array()) |
|
213
|
|
|
{ |
|
214
|
|
|
$name = $this->getTemplatePath().'/'.$name; |
|
215
|
|
|
|
|
216
|
|
|
$renderer = new ListRenderer(new \Knp\Menu\Matcher\Matcher()); |
|
217
|
|
|
$action = $this->getRequest()->get('_route'); |
|
|
|
|
|
|
218
|
|
|
$result = $renderer->render($this->getBreadcrumbs($action)); |
|
219
|
|
|
$elements['new_breadcrumb'] = $result; |
|
220
|
|
|
|
|
221
|
|
|
return $this->getTemplate()->renderTemplate($name, $elements); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @inheritdoc |
|
226
|
|
|
**/ |
|
227
|
|
|
public function isGranted($attributes, $object = null) |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->get('security.authorization_checker')->isGranted($attributes, $object); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: