| Total Complexity | 73 |
| Total Lines | 1138 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ResourceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class ResourceController extends AbstractResourceController implements CourseControllerInterface |
||
| 48 | { |
||
| 49 | use CourseControllerTrait; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @Route("/{tool}/{type}", name="chamilo_core_resource_index") |
||
| 53 | * |
||
| 54 | * Example: /document/files (See the 'tool' and the 'resource_type' DB tables.) |
||
| 55 | * For the tool value check the Tool entity. |
||
| 56 | * For the type value check the ResourceType entity. |
||
| 57 | */ |
||
| 58 | public function indexAction(Request $request, Grid $grid): Response |
||
| 59 | { |
||
| 60 | $tool = $request->get('tool'); |
||
| 61 | $type = $request->get('type'); |
||
| 62 | |||
| 63 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 64 | $grid = $this->getGrid($request, $grid, $parentResourceNode->getId()); |
||
| 65 | |||
| 66 | $breadcrumb = $this->getBreadCrumb(); |
||
| 67 | $breadcrumb->addChild( |
||
| 68 | $this->trans($tool), |
||
| 69 | [ |
||
| 70 | 'uri' => '#', |
||
| 71 | ] |
||
| 72 | ); |
||
| 73 | |||
| 74 | // The base resource node is the course. |
||
| 75 | $id = $parentResourceNode->getId(); |
||
| 76 | |||
| 77 | return $grid->getGridResponse( |
||
| 78 | '@ChamiloTheme/Resource/index.html.twig', |
||
| 79 | ['tool' => $tool, 'type' => $type, 'id' => $id, 'parent_resource_node' => $parentResourceNode] |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | private function getParentResourceNode(Request $request) |
||
| 84 | { |
||
| 85 | $parentNodeId = $request->get('id'); |
||
| 86 | |||
| 87 | if (empty($parentNodeId)) { |
||
| 88 | if ($this->hasCourse()) { |
||
| 89 | $parentResourceNode = $this->getCourse()->getResourceNode(); |
||
| 90 | } else { |
||
| 91 | /** @var User $user */ |
||
| 92 | $parentResourceNode = $this->getUser()->getResourceNode(); |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | $repo = $this->getDoctrine()->getRepository('ChamiloCoreBundle:Resource\ResourceNode'); |
||
| 96 | $parentResourceNode = $repo->find($parentNodeId); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $parentResourceNode; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Route("/{tool}/{type}/{id}/list", name="chamilo_core_resource_list") |
||
| 104 | * |
||
| 105 | * If node has children show it |
||
| 106 | */ |
||
| 107 | public function listAction(Request $request, Grid $grid): Response |
||
| 108 | { |
||
| 109 | $tool = $request->get('tool'); |
||
| 110 | $type = $request->get('type'); |
||
| 111 | $resourceNodeId = $request->get('id'); |
||
| 112 | |||
| 113 | $grid = $this->getGrid($request, $grid, $resourceNodeId); |
||
| 114 | |||
| 115 | $this->setBreadCrumb($request); |
||
| 116 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 117 | |||
| 118 | return $grid->getGridResponse( |
||
| 119 | '@ChamiloTheme/Resource/index.html.twig', |
||
| 120 | [ |
||
| 121 | 'parent_id' => $resourceNodeId, |
||
| 122 | 'tool' => $tool, |
||
| 123 | 'type' => $type, |
||
| 124 | 'id' => $resourceNodeId, |
||
| 125 | 'parent_resource_node' => $parentResourceNode, |
||
| 126 | ] |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getGrid(Request $request, Grid $grid, $resourceNodeId): Grid |
||
| 131 | { |
||
| 132 | $id = (int) $request->get('id'); |
||
| 133 | |||
| 134 | $repository = $this->getRepositoryFromRequest($request); |
||
| 135 | $class = $repository->getRepository()->getClassName(); |
||
| 136 | |||
| 137 | // The group 'resource' is set in the @GRID\Source annotation in the entity. |
||
| 138 | $source = new Entity($class, 'resource'); |
||
| 139 | $parentNode = $repository->getResourceNodeRepository()->find($resourceNodeId); |
||
| 140 | |||
| 141 | $this->denyAccessUnlessGranted( |
||
| 142 | ResourceNodeVoter::VIEW, |
||
| 143 | $parentNode, |
||
| 144 | $this->trans('Unauthorised access to resource') |
||
| 145 | ); |
||
| 146 | |||
| 147 | $course = null; |
||
| 148 | $session = null; |
||
| 149 | if ($this->hasCourse()) { |
||
| 150 | $course = $this->getCourse(); |
||
| 151 | $session = $this->getSession(); |
||
| 152 | $qb = $repository->getResourcesByCourse($course, $session, null, $parentNode); |
||
| 153 | } else { |
||
| 154 | $qb = $repository->getResourcesByCreator($this->getUser(), $parentNode); |
||
| 155 | } |
||
| 156 | |||
| 157 | // 3. Set QueryBuilder to the source. |
||
| 158 | $source->initQueryBuilder($qb); |
||
| 159 | $grid->setSource($source); |
||
| 160 | |||
| 161 | $resourceParams = $this->getResourceParams($request); |
||
| 162 | |||
| 163 | $grid->setRouteUrl($this->generateUrl('chamilo_core_resource_list', $resourceParams)); |
||
| 164 | |||
| 165 | //$grid->hideFilters(); |
||
| 166 | //$grid->setLimits(20); |
||
| 167 | //$grid->isReadyForRedirect(); |
||
| 168 | //$grid->setMaxResults(1); |
||
| 169 | //$grid->setLimits(2); |
||
| 170 | //$grid->setColumns($columns); |
||
| 171 | $routeParams = $resourceParams; |
||
| 172 | $routeParams['id'] = null; |
||
| 173 | |||
| 174 | $titleColumn = $repository->getTitleColumn($grid); |
||
| 175 | $titleColumn->setSafe(false); // allows links in the title |
||
| 176 | |||
| 177 | // Title link. |
||
| 178 | $titleColumn->setTitle($this->trans('Name')); |
||
| 179 | |||
| 180 | //$repository->formatGrid(); |
||
| 181 | /*if ($grid->hasColumn('filetype')) { |
||
| 182 | $grid->getColumn('filetype')->setTitle($this->trans('Type')); |
||
| 183 | }*/ |
||
| 184 | |||
| 185 | $titleColumn->manipulateRenderCell( |
||
| 186 | function ($value, Row $row, $router) use ($routeParams) { |
||
| 187 | /** @var Router $router */ |
||
| 188 | /** @var AbstractResource $entity */ |
||
| 189 | $entity = $row->getEntity(); |
||
| 190 | $resourceNode = $entity->getResourceNode(); |
||
| 191 | $id = $resourceNode->getId(); |
||
| 192 | |||
| 193 | $myParams = $routeParams; |
||
| 194 | $myParams['id'] = $id; |
||
| 195 | unset($myParams[0]); |
||
| 196 | |||
| 197 | $icon = $resourceNode->getIcon().' '; |
||
| 198 | if ($resourceNode->hasResourceFile()) { |
||
| 199 | /*$url = $router->generate( |
||
| 200 | 'chamilo_core_resource_show', |
||
| 201 | $myParams |
||
| 202 | );*/ |
||
| 203 | if ($resourceNode->isResourceFileAnImage()) { |
||
| 204 | $url = $router->generate( |
||
| 205 | 'chamilo_core_resource_view', |
||
| 206 | $myParams |
||
| 207 | ); |
||
| 208 | |||
| 209 | return $icon.'<a data-fancybox="gallery" href="'.$url.'">'.$value.'</a>'; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($resourceNode->isResourceFileAVideo()) { |
||
| 213 | $url = $router->generate( |
||
| 214 | 'chamilo_core_resource_view', |
||
| 215 | $myParams |
||
| 216 | ); |
||
| 217 | |||
| 218 | return ' |
||
| 219 | <video width="640" height="320" controls id="video'.$id.'" controls preload="metadata" style="display:none;"> |
||
| 220 | <source src="'.$url.'" type="video/mp4"> |
||
| 221 | Your browser doesn\'t support HTML5 video tag. |
||
| 222 | </video> |
||
| 223 | '.$icon.' <a data-fancybox="gallery" data-width="640" data-height="360" href="#video'.$id.'">'.$value.'</a>'; |
||
| 224 | } |
||
| 225 | |||
| 226 | $url = $router->generate( |
||
| 227 | 'chamilo_core_resource_preview', |
||
| 228 | $myParams |
||
| 229 | ); |
||
| 230 | |||
| 231 | return $icon.'<a data-fancybox="gallery" data-type="iframe" data-src="'.$url.'" href="javascript:;" >'.$value.'</a>'; |
||
| 232 | } else { |
||
| 233 | $url = $router->generate( |
||
| 234 | 'chamilo_core_resource_list', |
||
| 235 | $myParams |
||
| 236 | ); |
||
| 237 | |||
| 238 | return $icon.'<a href="'.$url.'">'.$value.'</a>'; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | ); |
||
| 242 | |||
| 243 | if ($grid->hasColumn('filetype')) { |
||
| 244 | $grid->getColumn('filetype')->manipulateRenderCell( |
||
| 245 | function ($value, Row $row, $router) use ($routeParams) { |
||
| 246 | /** @var AbstractResource $entity */ |
||
| 247 | $entity = $row->getEntity(); |
||
| 248 | $resourceNode = $entity->getResourceNode(); |
||
| 249 | |||
| 250 | if ($resourceNode->hasResourceFile()) { |
||
| 251 | $file = $resourceNode->getResourceFile(); |
||
| 252 | |||
| 253 | return $file->getMimeType(); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $this->trans('Folder'); |
||
| 257 | } |
||
| 258 | ); |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($grid->hasColumn('iid')) { |
||
| 262 | $grid->setHiddenColumns(['iid']); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Delete mass action. |
||
| 266 | if ($this->isGranted(ResourceNodeVoter::DELETE, $this->getCourse())) { |
||
| 267 | $deleteMassAction = new MassAction( |
||
| 268 | 'Delete', |
||
| 269 | 'ChamiloCoreBundle:Resource:deleteMass', |
||
| 270 | true, |
||
| 271 | $routeParams |
||
| 272 | ); |
||
| 273 | $grid->addMassAction($deleteMassAction); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Info action. |
||
| 277 | $myRowAction = new RowAction( |
||
| 278 | $this->trans('Info'), |
||
| 279 | 'chamilo_core_resource_info', |
||
| 280 | false, |
||
| 281 | '_self', |
||
| 282 | [ |
||
| 283 | 'class' => 'btn btn-secondary info_action', |
||
| 284 | 'icon' => 'fa-info-circle', |
||
| 285 | 'iframe' => false, |
||
| 286 | ] |
||
| 287 | ); |
||
| 288 | |||
| 289 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 290 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 291 | $routeParams['id'] = $id; |
||
| 292 | $action->setRouteParameters($routeParams); |
||
| 293 | $attributes = $action->getAttributes(); |
||
| 294 | $attributes['data-action'] = $action->getRoute(); |
||
| 295 | $attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 296 | $attributes['data-node-id'] = $id; |
||
| 297 | |||
| 298 | $action->setAttributes($attributes); |
||
| 299 | |||
| 300 | return $action; |
||
| 301 | }; |
||
| 302 | |||
| 303 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 304 | $grid->addRowAction($myRowAction); |
||
| 305 | |||
| 306 | // Download action |
||
| 307 | $myRowAction = new RowAction( |
||
| 308 | $this->trans('Download'), |
||
| 309 | 'chamilo_core_resource_download', |
||
| 310 | false, |
||
| 311 | '_self', |
||
| 312 | [ |
||
| 313 | 'class' => 'btn btn-secondary download_action', |
||
| 314 | 'icon' => 'fa-download', |
||
| 315 | ] |
||
| 316 | ); |
||
| 317 | |||
| 318 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 319 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 320 | $routeParams['id'] = $id; |
||
| 321 | |||
| 322 | $action->setRouteParameters($routeParams); |
||
| 323 | $attributes = $action->getAttributes(); |
||
| 324 | //$attributes['data-action'] = $action->getRoute(); |
||
| 325 | //$attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 326 | //$attributes['data-node-id'] = $id; |
||
| 327 | $action->setAttributes($attributes); |
||
| 328 | |||
| 329 | return $action; |
||
| 330 | }; |
||
| 331 | |||
| 332 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 333 | $grid->addRowAction($myRowAction); |
||
| 334 | |||
| 335 | if ($this->isGranted(ResourceNodeVoter::EDIT, $this->getCourse())) { |
||
| 336 | // Enable/Disable |
||
| 337 | $myRowAction = new RowAction( |
||
| 338 | '', |
||
| 339 | 'chamilo_core_resource_change_visibility', |
||
| 340 | false, |
||
| 341 | '_self' |
||
| 342 | ); |
||
| 343 | |||
| 344 | $setVisibleParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 345 | /** @var AbstractResource $resource */ |
||
| 346 | $resource = $row->getEntity(); |
||
| 347 | $id = $resource->getResourceNode()->getId(); |
||
| 348 | |||
| 349 | $icon = 'fa-eye-slash'; |
||
| 350 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 351 | |||
| 352 | if ($link === null) { |
||
| 353 | return null; |
||
| 354 | } |
||
| 355 | if ($link->getVisibility() === ResourceLink::VISIBILITY_PUBLISHED) { |
||
| 356 | $icon = 'fa-eye'; |
||
| 357 | } |
||
| 358 | $routeParams['id'] = $id; |
||
| 359 | $action->setRouteParameters($routeParams); |
||
| 360 | $attributes = [ |
||
| 361 | 'class' => 'btn btn-secondary change_visibility', |
||
| 362 | 'data-id' => $id, |
||
| 363 | 'icon' => $icon, |
||
| 364 | ]; |
||
| 365 | $action->setAttributes($attributes); |
||
| 366 | |||
| 367 | return $action; |
||
| 368 | }; |
||
| 369 | |||
| 370 | $myRowAction->addManipulateRender($setVisibleParameters); |
||
| 371 | $grid->addRowAction($myRowAction); |
||
| 372 | |||
| 373 | // Edit action. |
||
| 374 | $myRowAction = new RowAction( |
||
| 375 | $this->trans('Edit'), |
||
| 376 | 'chamilo_core_resource_edit', |
||
| 377 | false, |
||
| 378 | '_self', |
||
| 379 | ['class' => 'btn btn-secondary', 'icon' => 'fa fa-pen'] |
||
| 380 | ); |
||
| 381 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 382 | $grid->addRowAction($myRowAction); |
||
| 383 | |||
| 384 | // More action. |
||
| 385 | $myRowAction = new RowAction( |
||
| 386 | $this->trans('More'), |
||
| 387 | 'chamilo_core_resource_preview', |
||
| 388 | false, |
||
| 389 | '_self', |
||
| 390 | ['class' => 'btn btn-secondary edit_resource', 'icon' => 'fa fa-ellipsis-h'] |
||
| 391 | ); |
||
| 392 | |||
| 393 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 394 | $grid->addRowAction($myRowAction); |
||
| 395 | |||
| 396 | // Delete action. |
||
| 397 | $myRowAction = new RowAction( |
||
| 398 | $this->trans('Delete'), |
||
| 399 | 'chamilo_core_resource_delete', |
||
| 400 | true, |
||
| 401 | '_self', |
||
| 402 | ['class' => 'btn btn-danger', 'data_hidden' => true] |
||
| 403 | ); |
||
| 404 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 405 | $grid->addRowAction($myRowAction); |
||
| 406 | } |
||
| 407 | |||
| 408 | /*$grid->addExport(new CSVExport($this->trans('CSV export'), 'export', ['course' => $courseIdentifier])); |
||
| 409 | $grid->addExport( |
||
| 410 | new ExcelExport( |
||
| 411 | $this->trans('Excel export'), |
||
| 412 | 'export', |
||
| 413 | ['course' => $courseIdentifier] |
||
| 414 | ) |
||
| 415 | );*/ |
||
| 416 | |||
| 417 | return $grid; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @Route("/{tool}/{type}/{id}/new_folder", methods={"GET", "POST"}, name="chamilo_core_resource_new_folder") |
||
| 422 | */ |
||
| 423 | public function newFolderAction(Request $request): Response |
||
| 424 | { |
||
| 425 | $this->setBreadCrumb($request); |
||
| 426 | |||
| 427 | return $this->createResource($request, 'folder'); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @Route("/{tool}/{type}/{id}/new", methods={"GET", "POST"}, name="chamilo_core_resource_new") |
||
| 432 | */ |
||
| 433 | public function newAction(Request $request): Response |
||
| 434 | { |
||
| 435 | $this->setBreadCrumb($request); |
||
| 436 | |||
| 437 | return $this->createResource($request, 'file'); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @Route("/{tool}/{type}/{id}/edit", methods={"GET", "POST"}) |
||
| 442 | */ |
||
| 443 | public function editAction(Request $request): Response |
||
| 444 | { |
||
| 445 | $tool = $request->get('tool'); |
||
| 446 | $type = $request->get('type'); |
||
| 447 | $resourceNodeId = $request->get('id'); |
||
| 448 | |||
| 449 | $this->setBreadCrumb($request); |
||
| 450 | $repository = $this->getRepositoryFromRequest($request); |
||
| 451 | /** @var AbstractResource $resource */ |
||
| 452 | $resource = $repository->getRepository()->findOneBy(['resourceNode' => $resourceNodeId]); |
||
| 453 | $resourceNode = $resource->getResourceNode(); |
||
| 454 | |||
| 455 | $this->denyAccessUnlessGranted( |
||
| 456 | ResourceNodeVoter::EDIT, |
||
| 457 | $resourceNode, |
||
| 458 | $this->trans('Unauthorised access to resource') |
||
| 459 | ); |
||
| 460 | |||
| 461 | $resourceNodeParentId = $resourceNode->getId(); |
||
| 462 | |||
| 463 | $routeParams = $this->getResourceParams($request); |
||
| 464 | $routeParams['id'] = $resourceNodeParentId; |
||
| 465 | |||
| 466 | $form = $repository->getForm($this->container->get('form.factory'), $resource); |
||
| 467 | |||
| 468 | if ($resourceNode->hasEditableContent()) { |
||
| 469 | $form->add( |
||
| 470 | 'content', |
||
| 471 | CKEditorType::class, |
||
| 472 | [ |
||
| 473 | 'mapped' => false, |
||
| 474 | 'config' => [ |
||
| 475 | 'filebrowserImageBrowseRoute' => 'resources_filemanager', |
||
| 476 | 'filebrowserImageBrowseRouteParameters' => $routeParams, |
||
| 477 | ], |
||
| 478 | ] |
||
| 479 | ); |
||
| 480 | $content = $repository->getResourceNodeFileContent($resourceNode); |
||
| 481 | $form->get('content')->setData($content); |
||
| 482 | } |
||
| 483 | |||
| 484 | $form->handleRequest($request); |
||
| 485 | |||
| 486 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 487 | /** @var AbstractResource $newResource */ |
||
| 488 | $newResource = $form->getData(); |
||
| 489 | |||
| 490 | if ($form->has('content')) { |
||
| 491 | $data = $form->get('content')->getData(); |
||
| 492 | $repository->updateResourceFileContent($newResource, $data); |
||
| 493 | } |
||
| 494 | |||
| 495 | //$newResource->setTitle($form->get('title')->getData()); // already set in $form->getData() |
||
| 496 | $repository->updateNodeForResource($newResource); |
||
| 497 | |||
| 498 | $this->addFlash('success', $this->trans('Updated')); |
||
| 499 | |||
| 500 | if ($newResource->getResourceNode()->hasResourceFile()) { |
||
| 501 | //$resourceNodeParentId = $newResource->getResourceNode()->getParent()->getId(); |
||
| 502 | } |
||
| 503 | |||
| 504 | return $this->redirectToRoute('chamilo_core_resource_list', $routeParams); |
||
| 505 | } |
||
| 506 | |||
| 507 | return $this->render( |
||
| 508 | '@ChamiloTheme/Resource/edit.html.twig', |
||
| 509 | [ |
||
| 510 | 'form' => $form->createView(), |
||
| 511 | 'parent' => $resourceNodeParentId, |
||
| 512 | ] |
||
| 513 | ); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Shows a resource information. |
||
| 518 | * |
||
| 519 | * @Route("/{tool}/{type}/{id}/info", methods={"GET"}, name="chamilo_core_resource_info") |
||
| 520 | */ |
||
| 521 | public function infoAction(Request $request): Response |
||
| 522 | { |
||
| 523 | $this->setBreadCrumb($request); |
||
| 524 | $nodeId = $request->get('id'); |
||
| 525 | |||
| 526 | $repository = $this->getRepositoryFromRequest($request); |
||
| 527 | |||
| 528 | /** @var AbstractResource $resource */ |
||
| 529 | $resource = $repository->getRepository()->findOneBy(['resourceNode' => $nodeId]); |
||
| 530 | |||
| 531 | if (null === $resource) { |
||
| 532 | throw new NotFoundHttpException(); |
||
| 533 | } |
||
| 534 | |||
| 535 | $resourceNode = $resource->getResourceNode(); |
||
| 536 | |||
| 537 | if (null === $resourceNode) { |
||
| 538 | throw new NotFoundHttpException(); |
||
| 539 | } |
||
| 540 | |||
| 541 | $this->denyAccessUnlessGranted( |
||
| 542 | ResourceNodeVoter::VIEW, |
||
| 543 | $resourceNode, |
||
| 544 | $this->trans('Unauthorised access to resource') |
||
| 545 | ); |
||
| 546 | |||
| 547 | $tool = $request->get('tool'); |
||
| 548 | $type = $request->get('type'); |
||
| 549 | |||
| 550 | $params = [ |
||
| 551 | 'resource' => $resource, |
||
| 552 | 'tool' => $tool, |
||
| 553 | 'type' => $type, |
||
| 554 | ]; |
||
| 555 | |||
| 556 | return $this->render('@ChamiloTheme/Resource/show.html.twig', $params); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Preview a file. Mostly used when using a modal. |
||
| 561 | * |
||
| 562 | * @Route("/{tool}/{type}/{id}/preview", methods={"GET"}, name="chamilo_core_resource_preview") |
||
| 563 | */ |
||
| 564 | public function previewAction(Request $request): Response |
||
| 565 | { |
||
| 566 | $this->setBreadCrumb($request); |
||
| 567 | $nodeId = $request->get('id'); |
||
| 568 | |||
| 569 | $repository = $this->getRepositoryFromRequest($request); |
||
| 570 | |||
| 571 | /** @var AbstractResource $resource */ |
||
| 572 | $resource = $repository->getRepository()->findOneBy(['resourceNode' => $nodeId]); |
||
| 573 | |||
| 574 | if (null === $resource) { |
||
| 575 | throw new NotFoundHttpException(); |
||
| 576 | } |
||
| 577 | |||
| 578 | $resourceNode = $resource->getResourceNode(); |
||
| 579 | |||
| 580 | if (null === $resourceNode) { |
||
| 581 | throw new NotFoundHttpException(); |
||
| 582 | } |
||
| 583 | |||
| 584 | $this->denyAccessUnlessGranted( |
||
| 585 | ResourceNodeVoter::VIEW, |
||
| 586 | $resourceNode, |
||
| 587 | $this->trans('Unauthorised access to resource') |
||
| 588 | ); |
||
| 589 | |||
| 590 | $tool = $request->get('tool'); |
||
| 591 | $type = $request->get('type'); |
||
| 592 | |||
| 593 | $params = [ |
||
| 594 | 'resource' => $resource, |
||
| 595 | 'tool' => $tool, |
||
| 596 | 'type' => $type, |
||
| 597 | ]; |
||
| 598 | |||
| 599 | return $this->render('@ChamiloTheme/Resource/preview.html.twig', $params); |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @Route("/{tool}/{type}/{id}/change_visibility", name="chamilo_core_resource_change_visibility") |
||
| 604 | */ |
||
| 605 | public function changeVisibilityAction(Request $request): Response |
||
| 606 | { |
||
| 607 | $id = $request->get('id'); |
||
| 608 | |||
| 609 | $repository = $this->getRepositoryFromRequest($request); |
||
| 610 | |||
| 611 | /** @var AbstractResource $resource */ |
||
| 612 | $resource = $repository->getRepository()->findOneBy(['resourceNode' => $id]); |
||
| 613 | |||
| 614 | if (null === $resource) { |
||
| 615 | throw new NotFoundHttpException(); |
||
| 616 | } |
||
| 617 | |||
| 618 | $resourceNode = $resource->getResourceNode(); |
||
| 619 | |||
| 620 | $this->denyAccessUnlessGranted( |
||
| 621 | ResourceNodeVoter::EDIT, |
||
| 622 | $resourceNode, |
||
| 623 | $this->trans('Unauthorised access to resource') |
||
| 624 | ); |
||
| 625 | |||
| 626 | /** @var ResourceLink $link */ |
||
| 627 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 628 | |||
| 629 | $icon = 'fa-eye'; |
||
| 630 | // Use repository to change settings easily. |
||
| 631 | if ($link && $link->getVisibility() === ResourceLink::VISIBILITY_PUBLISHED) { |
||
| 632 | $repository->setVisibilityDraft($resource); |
||
| 633 | $icon = 'fa-eye-slash'; |
||
| 634 | } else { |
||
| 635 | $repository->setVisibilityPublished($resource); |
||
| 636 | } |
||
| 637 | |||
| 638 | $result = ['icon' => $icon]; |
||
| 639 | |||
| 640 | return new JsonResponse($result); |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @Route("/{tool}/{type}/{id}", name="chamilo_core_resource_delete") |
||
| 645 | */ |
||
| 646 | public function deleteAction(Request $request): Response |
||
| 677 | ); |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @Route("/{tool}/{type}/{id}", methods={"DELETE"}, name="chamilo_core_resource_delete_mass") |
||
| 682 | */ |
||
| 683 | public function deleteMassAction($primaryKeys, $allPrimaryKeys, Request $request): Response |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Shows the associated resource file. |
||
| 720 | * |
||
| 721 | * @Route("/{tool}/{type}/{id}/view", methods={"GET"}, name="chamilo_core_resource_view") |
||
| 722 | */ |
||
| 723 | public function viewAction(Request $request, Glide $glide): Response |
||
| 724 | { |
||
| 725 | $id = $request->get('id'); |
||
| 726 | $filter = $request->get('filter'); |
||
| 727 | $mode = $request->get('mode'); |
||
| 728 | $em = $this->getDoctrine(); |
||
| 729 | $resourceNode = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($id); |
||
| 730 | |||
| 731 | if ($resourceNode === null) { |
||
| 732 | throw new FileNotFoundException('Resource not found'); |
||
| 733 | } |
||
| 734 | |||
| 735 | return $this->showFile($request, $resourceNode, $mode, $glide, $filter); |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Gets a document when calling route resources_document_get_file |
||
| 740 | * @deprecated |
||
| 741 | * |
||
| 742 | * @throws \League\Flysystem\FileNotFoundException |
||
| 743 | */ |
||
| 744 | public function getDocumentAction(Request $request, Glide $glide): Response |
||
| 745 | { |
||
| 746 | /*$file = $request->get('file'); |
||
| 747 | $mode = $request->get('mode'); |
||
| 748 | |||
| 749 | // see list of filters in config/services.yaml |
||
| 750 | $filter = $request->get('filter'); |
||
| 751 | $mode = !empty($mode) ? $mode : 'show'; |
||
| 752 | |||
| 753 | $repository = $this->getRepository('document', 'files'); |
||
| 754 | $nodeRepository = $repository->getResourceNodeRepository(); |
||
| 755 | |||
| 756 | $title = basename($file); |
||
| 757 | // @todo improve criteria to avoid giving the wrong file. |
||
| 758 | $criteria = ['slug' => $title]; |
||
| 759 | |||
| 760 | $resourceNode = $nodeRepository->findOneBy($criteria); |
||
| 761 | |||
| 762 | if (null === $resourceNode) { |
||
| 763 | throw new NotFoundHttpException(); |
||
| 764 | } |
||
| 765 | |||
| 766 | return $this->showFile($request, $resourceNode, $mode, $glide,$filter);*/ |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @Route("/{tool}/{type}/{id}/download", methods={"GET"}, name="chamilo_core_resource_download") |
||
| 771 | */ |
||
| 772 | public function downloadAction(Request $request) |
||
| 773 | { |
||
| 774 | $resourceNodeId = (int) $request->get('id'); |
||
| 775 | $courseNode = $this->getCourse()->getResourceNode(); |
||
| 776 | |||
| 777 | $repo = $this->getRepositoryFromRequest($request); |
||
| 778 | |||
| 779 | if (empty($resourceNodeId)) { |
||
| 780 | $resourceNode = $courseNode; |
||
| 781 | } else { |
||
| 782 | $resourceNode = $repo->getResourceNodeRepository()->find($resourceNodeId); |
||
| 783 | } |
||
| 784 | |||
| 785 | $type = $repo->getResourceType(); |
||
| 786 | |||
| 787 | if (null === $resourceNode) { |
||
| 788 | throw new NotFoundHttpException(); |
||
| 789 | } |
||
| 790 | |||
| 791 | $this->denyAccessUnlessGranted( |
||
| 792 | ResourceNodeVoter::VIEW, |
||
| 793 | $resourceNode, |
||
| 794 | $this->trans('Unauthorised access to resource') |
||
| 795 | ); |
||
| 796 | |||
| 797 | // If resource node has a file just download it. Don't download the children. |
||
| 798 | if ($resourceNode->hasResourceFile()) { |
||
| 799 | // Redirect to download single file. |
||
| 800 | return $this->showFile($request, $resourceNode, 'download'); |
||
| 801 | } |
||
| 802 | |||
| 803 | $zipName = $resourceNode->getSlug().'.zip'; |
||
| 804 | $rootNodePath = $resourceNode->getPathForDisplay(); |
||
| 805 | |||
| 806 | $resourceNodeRepo = $repo->getResourceNodeRepository(); |
||
| 807 | |||
| 808 | $criteria = Criteria::create() |
||
| 809 | ->where(Criteria::expr()->neq('resourceFile', null)) // must have a file |
||
| 810 | // ->andWhere(Criteria::expr()->eq('resourceType', $type)) |
||
| 811 | ; |
||
| 812 | |||
| 813 | /** @var ArrayCollection|ResourceNode[] $children */ |
||
| 814 | /** @var QueryBuilder $children */ |
||
| 815 | $qb = $resourceNodeRepo->getChildrenQueryBuilder($resourceNode); |
||
| 816 | $qb->addCriteria($criteria); |
||
| 817 | $children = $qb->getQuery()->getResult(); |
||
| 818 | |||
| 819 | $response = new StreamedResponse(function () use ($rootNodePath, $zipName, $children, $repo) { |
||
| 820 | // Define suitable options for ZipStream Archive. |
||
| 821 | $options = new Archive(); |
||
| 822 | $options->setContentType('application/octet-stream'); |
||
| 823 | //initialise zipstream with output zip filename and options. |
||
| 824 | $zip = new ZipStream($zipName, $options); |
||
| 825 | |||
| 826 | /** @var ResourceNode $node */ |
||
| 827 | foreach ($children as $node) { |
||
| 828 | $resourceFile = $node->getResourceFile(); |
||
| 829 | $systemName = $resourceFile->getFile()->getPathname(); |
||
| 830 | $stream = $repo->getResourceNodeFileStream($node); |
||
| 831 | //error_log($node->getPathForDisplay()); |
||
| 832 | $fileToDisplay = str_replace($rootNodePath, '', $node->getPathForDisplay()); |
||
| 833 | $zip->addFileFromStream($fileToDisplay, $stream); |
||
| 834 | } |
||
| 835 | $zip->finish(); |
||
| 836 | }); |
||
| 837 | |||
| 838 | $disposition = $response->headers->makeDisposition( |
||
| 839 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 840 | Transliterator::transliterate($zipName) |
||
| 841 | ); |
||
| 842 | $response->headers->set('Content-Disposition', $disposition); |
||
| 843 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
| 844 | |||
| 845 | return $response; |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Upload form. |
||
| 850 | * |
||
| 851 | * @Route("/{tool}/{type}/{id}/upload", name="chamilo_core_resource_upload", methods={"GET", "POST"}, |
||
| 852 | * options={"expose"=true}) |
||
| 853 | */ |
||
| 854 | public function uploadAction(Request $request, $tool, $type, $id): Response |
||
| 875 | ); |
||
| 876 | } |
||
| 877 | |||
| 878 | public function setBreadCrumb(Request $request) |
||
| 879 | { |
||
| 880 | $tool = $request->get('tool'); |
||
| 881 | $type = $request->get('type'); |
||
| 882 | $resourceNodeId = $request->get('id'); |
||
| 883 | |||
| 884 | $routeParams = $this->getResourceParams($request); |
||
| 885 | |||
| 886 | if (!empty($resourceNodeId)) { |
||
| 887 | $breadcrumb = $this->getBreadCrumb(); |
||
| 888 | |||
| 889 | // Root tool link |
||
| 890 | $breadcrumb->addChild( |
||
| 891 | $this->trans($tool), |
||
| 892 | [ |
||
| 893 | 'uri' => $this->generateUrl( |
||
| 894 | 'chamilo_core_resource_index', |
||
| 895 | $routeParams |
||
| 896 | ), |
||
| 897 | ] |
||
| 898 | ); |
||
| 899 | |||
| 900 | $repo = $this->getRepositoryFromRequest($request); |
||
| 901 | |||
| 902 | /** @var AbstractResource $parent */ |
||
| 903 | $originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]); |
||
| 904 | if ($originalResource === null) { |
||
| 905 | return; |
||
| 906 | } |
||
| 907 | $parent = $originalParent = $originalResource->getResourceNode(); |
||
| 908 | |||
| 909 | $parentList = []; |
||
| 910 | while ($parent !== null) { |
||
| 911 | if ($type !== $parent->getResourceType()->getName()) { |
||
| 912 | break; |
||
| 913 | } |
||
| 914 | $parent = $parent->getParent(); |
||
| 915 | if ($parent) { |
||
| 916 | $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]); |
||
| 917 | if ($resource) { |
||
| 918 | $parentList[] = $resource; |
||
| 919 | } |
||
| 920 | } |
||
| 921 | } |
||
| 922 | |||
| 923 | $parentList = array_reverse($parentList); |
||
| 924 | /** @var AbstractResource $item */ |
||
| 925 | foreach ($parentList as $item) { |
||
| 926 | $params = $routeParams; |
||
| 927 | $params['id'] = $item->getResourceNode()->getId(); |
||
| 928 | $breadcrumb->addChild( |
||
| 929 | $item->getResourceName(), |
||
| 930 | [ |
||
| 931 | 'uri' => $this->generateUrl( |
||
| 932 | 'chamilo_core_resource_list', |
||
| 933 | $params |
||
| 934 | ), |
||
| 935 | ] |
||
| 936 | ); |
||
| 937 | } |
||
| 938 | |||
| 939 | $params = $routeParams; |
||
| 940 | $params['id'] = $originalParent->getId(); |
||
| 941 | |||
| 942 | $breadcrumb->addChild( |
||
| 943 | $originalResource->getResourceName(), |
||
| 944 | [ |
||
| 945 | 'uri' => $this->generateUrl( |
||
| 946 | 'chamilo_core_resource_list', |
||
| 947 | $params |
||
| 948 | ), |
||
| 949 | ] |
||
| 950 | ); |
||
| 951 | } |
||
| 952 | } |
||
| 953 | |||
| 954 | /** |
||
| 955 | * @param Request $request |
||
| 956 | * @param ResourceNode $resourceNode |
||
| 957 | * @param string $mode |
||
| 958 | * @param Glide|null $glide |
||
| 959 | * @param string $filter |
||
| 960 | * |
||
| 961 | * @return mixed|StreamedResponse |
||
| 962 | */ |
||
| 963 | private function showFile(Request $request, ResourceNode $resourceNode, $mode = 'show', Glide $glide = null, $filter = '') |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * @param string $fileType |
||
| 1031 | * |
||
| 1032 | * @return RedirectResponse|Response |
||
| 1033 | */ |
||
| 1034 | private function createResource(Request $request, $fileType = 'file') |
||
| 1185 | ] |
||
| 1186 | ); |
||
| 1187 | } |
||
| 1189 |
This check looks for imports that have been defined, but are not used in the scope.