| Total Complexity | 81 |
| Total Lines | 1235 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| 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 |
||
| 52 | class ResourceController extends AbstractResourceController implements CourseControllerInterface |
||
| 53 | { |
||
| 54 | use CourseControllerTrait; |
||
| 55 | private $fileContentName = 'file_content'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @Route("/{tool}/{type}", name="chamilo_core_resource_index") |
||
| 59 | * |
||
| 60 | * Example: /document/files (See the 'tool' and the 'resource_type' DB tables.) |
||
| 61 | * For the tool value check the Tool entity. |
||
| 62 | * For the type value check the ResourceType entity. |
||
| 63 | */ |
||
| 64 | public function indexAction(Request $request, Grid $grid): Response |
||
| 94 | ] |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @Route("/{tool}/{type}/{id}/list", name="chamilo_core_resource_list") |
||
| 100 | * |
||
| 101 | * If node has children show it |
||
| 102 | */ |
||
| 103 | public function listAction(Request $request, Grid $grid): Response |
||
| 126 | ] |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getGrid(Request $request, ResourceRepository $repository, Grid $grid, int $resourceNodeId): Grid |
||
| 131 | { |
||
| 132 | $class = $repository->getRepository()->getClassName(); |
||
| 133 | |||
| 134 | // The group 'resource' is set in the @GRID\Source annotation in the entity. |
||
| 135 | $source = new Entity($class, 'resource'); |
||
| 136 | /** @var ResourceNode $parentNode */ |
||
| 137 | $parentNode = $repository->getResourceNodeRepository()->find($resourceNodeId); |
||
| 138 | |||
| 139 | $this->denyAccessUnlessGranted( |
||
| 140 | ResourceNodeVoter::VIEW, |
||
| 141 | $parentNode, |
||
| 142 | $this->trans('Unauthorised access to resource') |
||
| 143 | ); |
||
| 144 | |||
| 145 | $settings = $repository->getResourceSettings(); |
||
| 146 | |||
| 147 | $course = $this->getCourse(); |
||
| 148 | $session = $this->getSession(); |
||
| 149 | /** @var QueryBuilder $qb */ |
||
| 150 | $qb = $repository->getResources($this->getUser(), $parentNode, $course, $session, null); |
||
| 151 | |||
| 152 | // 3. Set QueryBuilder to the source. |
||
| 153 | $source->initQueryBuilder($qb); |
||
| 154 | $grid->setSource($source); |
||
| 155 | |||
| 156 | $resourceParams = $this->getResourceParams($request); |
||
| 157 | |||
| 158 | if (0 === $resourceParams['id']) { |
||
| 159 | $resourceParams['id'] = $resourceNodeId; |
||
| 160 | } |
||
| 161 | |||
| 162 | $grid->setRouteUrl($this->generateUrl('chamilo_core_resource_list', $resourceParams)); |
||
| 163 | |||
| 164 | //$grid->hideFilters(); |
||
| 165 | //$grid->setLimits(20); |
||
| 166 | //$grid->isReadyForRedirect(); |
||
| 167 | //$grid->setMaxResults(1); |
||
| 168 | //$grid->setLimits(2); |
||
| 169 | //$grid->setColumns($columns); |
||
| 170 | $routeParams = $resourceParams; |
||
| 171 | $routeParams['id'] = null; |
||
| 172 | |||
| 173 | /** @var Column $titleColumn */ |
||
| 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 | $titleColumn->manipulateRenderCell( |
||
| 181 | function ($value, Row $row, $router) use ($routeParams, $settings) { |
||
| 182 | /** @var Router $router */ |
||
| 183 | /** @var ResourceInterface $entity */ |
||
| 184 | $entity = $row->getEntity(); |
||
| 185 | $resourceNode = $entity->getResourceNode(); |
||
| 186 | $id = $resourceNode->getId(); |
||
| 187 | |||
| 188 | $myParams = $routeParams; |
||
| 189 | $myParams['id'] = $id; |
||
| 190 | unset($myParams[0]); |
||
| 191 | |||
| 192 | $icon = $resourceNode->getIcon().' '; |
||
| 193 | if ($resourceNode->hasResourceFile()) { |
||
| 194 | // Process node that contains a file, process previews. |
||
| 195 | if ($resourceNode->isResourceFileAnImage()) { |
||
| 196 | $url = $router->generate('chamilo_core_resource_view_file', $myParams); |
||
| 197 | |||
| 198 | return $icon.'<a data-fancybox="gallery" href="'.$url.'">'.$value.'</a>'; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($resourceNode->isResourceFileAVideo()) { |
||
| 202 | $url = $router->generate('chamilo_core_resource_view_file', $myParams); |
||
| 203 | |||
| 204 | return ' |
||
| 205 | <video width="640" height="320" controls id="video'.$id.'" controls preload="metadata" style="display:none;"> |
||
| 206 | <source src="'.$url.'" type="video/mp4"> |
||
| 207 | Your browser doesn\'t support HTML5 video tag. |
||
| 208 | </video> |
||
| 209 | '.$icon.' <a data-fancybox="gallery" data-width="640" data-height="360" href="#video'.$id.'">'.$value.'</a>'; |
||
| 210 | } |
||
| 211 | |||
| 212 | $url = $router->generate('chamilo_core_resource_preview', $myParams); |
||
| 213 | |||
| 214 | return $icon. |
||
| 215 | '<a data-fancybox="gallery" data-type="iframe" data-src="'.$url.'" href="javascript:;" >'. |
||
| 216 | $value.'</a>'; |
||
| 217 | } else { |
||
| 218 | if ($settings->isAllowNodeCreation()) { |
||
| 219 | $url = $router->generate( |
||
| 220 | 'chamilo_core_resource_list', |
||
| 221 | $myParams |
||
| 222 | ); |
||
| 223 | } else { |
||
| 224 | $url = $router->generate('chamilo_core_resource_view_resource', $myParams); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $icon.'<a href="'.$url.'">'.$value.'</a>'; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | ); |
||
| 231 | |||
| 232 | if ($grid->hasColumn('filetype')) { |
||
| 233 | $grid->getColumn('filetype')->manipulateRenderCell( |
||
| 234 | function ($value, Row $row, $router) { |
||
|
|
|||
| 235 | /** @var AbstractResource $entity */ |
||
| 236 | $entity = $row->getEntity(); |
||
| 237 | $resourceNode = $entity->getResourceNode(); |
||
| 238 | |||
| 239 | if ($resourceNode->hasResourceFile()) { |
||
| 240 | $file = $resourceNode->getResourceFile(); |
||
| 241 | |||
| 242 | return $file->getMimeType(); |
||
| 243 | } |
||
| 244 | |||
| 245 | return $this->trans('Folder'); |
||
| 246 | } |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($grid->hasColumn('iid')) { |
||
| 251 | $grid->setHiddenColumns(['iid']); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Delete mass action. |
||
| 255 | if ($this->isGranted(ResourceNodeVoter::DELETE, $parentNode)) { |
||
| 256 | $deleteMassAction = new MassAction( |
||
| 257 | 'Delete', |
||
| 258 | 'ChamiloCoreBundle:Resource:deleteMass', |
||
| 259 | true, |
||
| 260 | $routeParams |
||
| 261 | ); |
||
| 262 | $grid->addMassAction($deleteMassAction); |
||
| 263 | } |
||
| 264 | |||
| 265 | // Info action. |
||
| 266 | $myRowAction = new RowAction( |
||
| 267 | $this->trans('Info'), |
||
| 268 | 'chamilo_core_resource_info', |
||
| 269 | false, |
||
| 270 | '_self', |
||
| 271 | [ |
||
| 272 | 'class' => 'btn btn-secondary info_action', |
||
| 273 | 'icon' => 'fa-info-circle', |
||
| 274 | 'iframe' => false, |
||
| 275 | ] |
||
| 276 | ); |
||
| 277 | |||
| 278 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 279 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 280 | $routeParams['id'] = $id; |
||
| 281 | $action->setRouteParameters($routeParams); |
||
| 282 | $attributes = $action->getAttributes(); |
||
| 283 | $attributes['data-action'] = $action->getRoute(); |
||
| 284 | $attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 285 | $attributes['data-node-id'] = $id; |
||
| 286 | $attributes['title'] = $this->trans('Info').' '.$row->getEntity()->getResourceName(); |
||
| 287 | $action->setAttributes($attributes); |
||
| 288 | |||
| 289 | return $action; |
||
| 290 | }; |
||
| 291 | |||
| 292 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 293 | $grid->addRowAction($myRowAction); |
||
| 294 | |||
| 295 | // Download action |
||
| 296 | $myRowAction = new RowAction( |
||
| 297 | $this->trans('Download'), |
||
| 298 | 'chamilo_core_resource_download', |
||
| 299 | false, |
||
| 300 | '_self', |
||
| 301 | [ |
||
| 302 | 'class' => 'btn btn-secondary download_action', |
||
| 303 | 'icon' => 'fa-download', |
||
| 304 | 'title' => $this->trans('Download'), |
||
| 305 | ] |
||
| 306 | ); |
||
| 307 | |||
| 308 | $setNodeDownloadParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 309 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 310 | $routeParams['id'] = $id; |
||
| 311 | $action->setRouteParameters($routeParams); |
||
| 312 | $attributes = $action->getAttributes(); |
||
| 313 | $action->setAttributes($attributes); |
||
| 314 | |||
| 315 | return $action; |
||
| 316 | }; |
||
| 317 | $myRowAction->addManipulateRender($setNodeDownloadParameters); |
||
| 318 | $grid->addRowAction($myRowAction); |
||
| 319 | |||
| 320 | // Set EDIT/DELETE |
||
| 321 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 322 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 323 | $allowedEdit = $this->isGranted(ResourceNodeVoter::EDIT, $row->getEntity()->getResourceNode()); |
||
| 324 | |||
| 325 | if (false === $allowedEdit) { |
||
| 326 | return null; |
||
| 327 | } |
||
| 328 | |||
| 329 | $routeParams['id'] = $id; |
||
| 330 | |||
| 331 | $action->setRouteParameters($routeParams); |
||
| 332 | $attributes = $action->getAttributes(); |
||
| 333 | //$attributes['data-action'] = $action->getRoute(); |
||
| 334 | //$attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 335 | //$attributes['data-node-id'] = $id; |
||
| 336 | $action->setAttributes($attributes); |
||
| 337 | |||
| 338 | return $action; |
||
| 339 | }; |
||
| 340 | |||
| 341 | if ($this->isGranted(ResourceNodeVoter::EDIT, $parentNode)) { |
||
| 342 | // Enable/Disable |
||
| 343 | $myRowAction = new RowAction( |
||
| 344 | '', |
||
| 345 | 'chamilo_core_resource_change_visibility', |
||
| 346 | false, |
||
| 347 | '_self' |
||
| 348 | ); |
||
| 349 | |||
| 350 | $setVisibleParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 351 | /** @var AbstractResource $resource */ |
||
| 352 | $resource = $row->getEntity(); |
||
| 353 | $allowedEdit = $this->isGranted(ResourceNodeVoter::EDIT, $resource->getResourceNode()); |
||
| 354 | |||
| 355 | if (false === $allowedEdit) { |
||
| 356 | return null; |
||
| 357 | } |
||
| 358 | |||
| 359 | $id = $resource->getResourceNode()->getId(); |
||
| 360 | |||
| 361 | $icon = 'fa-eye-slash'; |
||
| 362 | if ($this->hasCourse()) { |
||
| 363 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 364 | } else { |
||
| 365 | $link = $resource->getFirstResourceLink(); |
||
| 366 | } |
||
| 367 | |||
| 368 | if (null === $link) { |
||
| 369 | return null; |
||
| 370 | } |
||
| 371 | if (ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) { |
||
| 372 | $icon = 'fa-eye'; |
||
| 373 | } |
||
| 374 | $routeParams['id'] = $id; |
||
| 375 | $action->setRouteParameters($routeParams); |
||
| 376 | $attributes = [ |
||
| 377 | 'class' => 'btn btn-secondary change_visibility', |
||
| 378 | 'data-id' => $id, |
||
| 379 | 'icon' => $icon, |
||
| 380 | ]; |
||
| 381 | $action->setAttributes($attributes); |
||
| 382 | |||
| 383 | return $action; |
||
| 384 | }; |
||
| 385 | |||
| 386 | $myRowAction->addManipulateRender($setVisibleParameters); |
||
| 387 | $grid->addRowAction($myRowAction); |
||
| 388 | |||
| 389 | if ($settings->isAllowResourceEdit()) { |
||
| 390 | // Edit action. |
||
| 391 | $myRowAction = new RowAction( |
||
| 392 | $this->trans('Edit'), |
||
| 393 | 'chamilo_core_resource_edit', |
||
| 394 | false, |
||
| 395 | '_self', |
||
| 396 | ['class' => 'btn btn-secondary', 'icon' => 'fa fa-pen'] |
||
| 397 | ); |
||
| 398 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 399 | $grid->addRowAction($myRowAction); |
||
| 400 | } |
||
| 401 | |||
| 402 | // More action. |
||
| 403 | /*$myRowAction = new RowAction( |
||
| 404 | $this->trans('More'), |
||
| 405 | 'chamilo_core_resource_preview', |
||
| 406 | false, |
||
| 407 | '_self', |
||
| 408 | ['class' => 'btn btn-secondary edit_resource', 'icon' => 'fa fa-ellipsis-h'] |
||
| 409 | ); |
||
| 410 | |||
| 411 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 412 | $grid->addRowAction($myRowAction);*/ |
||
| 413 | |||
| 414 | // Delete action. |
||
| 415 | $myRowAction = new RowAction( |
||
| 416 | $this->trans('Delete'), |
||
| 417 | 'chamilo_core_resource_delete', |
||
| 418 | true, |
||
| 419 | '_self', |
||
| 420 | [ |
||
| 421 | 'class' => 'btn btn-danger', |
||
| 422 | //'data_hidden' => true, |
||
| 423 | ] |
||
| 424 | ); |
||
| 425 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 426 | $grid->addRowAction($myRowAction); |
||
| 427 | } |
||
| 428 | |||
| 429 | /*$grid->addExport(new CSVExport($this->trans('CSV export'), 'export', ['course' => $courseIdentifier])); |
||
| 430 | $grid->addExport( |
||
| 431 | new ExcelExport( |
||
| 432 | $this->trans('Excel export'), |
||
| 433 | 'export', |
||
| 434 | ['course' => $courseIdentifier] |
||
| 435 | ) |
||
| 436 | );*/ |
||
| 437 | |||
| 438 | return $grid; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @Route("/{tool}/{type}/{id}/new_folder", methods={"GET", "POST"}, name="chamilo_core_resource_new_folder") |
||
| 443 | */ |
||
| 444 | public function newFolderAction(Request $request): Response |
||
| 445 | { |
||
| 446 | $this->setBreadCrumb($request); |
||
| 447 | |||
| 448 | return $this->createResource($request, 'folder'); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @Route("/{tool}/{type}/{id}/new", methods={"GET", "POST"}, name="chamilo_core_resource_new") |
||
| 453 | */ |
||
| 454 | public function newAction(Request $request): Response |
||
| 455 | { |
||
| 456 | $this->setBreadCrumb($request); |
||
| 457 | |||
| 458 | return $this->createResource($request, 'file'); |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @Route("/{tool}/{type}/{id}/disk_space", methods={"GET", "POST"}, name="chamilo_core_resource_disk_space") |
||
| 463 | */ |
||
| 464 | public function diskSpaceAction(Request $request): Response |
||
| 465 | { |
||
| 466 | $this->setBreadCrumb($request); |
||
| 467 | $nodeId = $request->get('id'); |
||
| 468 | $repository = $this->getRepositoryFromRequest($request); |
||
| 469 | |||
| 470 | /** @var ResourceNode $resourceNode */ |
||
| 471 | $resourceNode = $repository->getResourceNodeRepository()->find($nodeId); |
||
| 472 | |||
| 473 | $this->denyAccessUnlessGranted( |
||
| 474 | ResourceNodeVoter::VIEW, |
||
| 475 | $resourceNode, |
||
| 476 | $this->trans('Unauthorised access to resource') |
||
| 477 | ); |
||
| 478 | |||
| 479 | $course = $this->getCourse(); |
||
| 480 | $totalSize = 0; |
||
| 481 | if ($course) { |
||
| 482 | $totalSize = $course->getDiskQuota(); |
||
| 483 | } |
||
| 484 | |||
| 485 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 486 | $resourceNode, |
||
| 487 | $repository->getResourceType(), |
||
| 488 | $course |
||
| 489 | ); |
||
| 490 | |||
| 491 | $labels[] = $course->getTitle(); |
||
| 492 | $data[] = $size; |
||
| 493 | $sessions = $course->getSessions(); |
||
| 494 | |||
| 495 | foreach ($sessions as $session) { |
||
| 496 | $labels[] = $course->getTitle().' - '.$session->getName(); |
||
| 497 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 498 | $resourceNode, |
||
| 499 | $repository->getResourceType(), |
||
| 500 | $course, |
||
| 501 | $session |
||
| 502 | ); |
||
| 503 | $data[] = $size; |
||
| 504 | } |
||
| 505 | |||
| 506 | $groups = $course->getGroups(); |
||
| 507 | foreach ($groups as $group) { |
||
| 508 | $labels[] = $course->getTitle().' - '.$group->getName(); |
||
| 509 | $size = $repository->getResourceNodeRepository()->getSize( |
||
| 510 | $resourceNode, |
||
| 511 | $repository->getResourceType(), |
||
| 512 | $course, |
||
| 513 | null, |
||
| 514 | $group |
||
| 515 | ); |
||
| 516 | $data[] = $size; |
||
| 517 | } |
||
| 518 | |||
| 519 | $used = array_sum($data); |
||
| 520 | $labels[] = $this->trans('Free'); |
||
| 521 | $data[] = $totalSize - $used; |
||
| 522 | |||
| 523 | return $this->render( |
||
| 524 | '@ChamiloTheme/Resource/disk_space.html.twig', |
||
| 525 | [ |
||
| 526 | 'resourceNode' => $resourceNode, |
||
| 527 | 'labels' => $labels, |
||
| 528 | 'data' => $data, |
||
| 529 | ] |
||
| 530 | ); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @Route("/{tool}/{type}/{id}/edit", methods={"GET", "POST"}) |
||
| 535 | */ |
||
| 536 | public function editAction(Request $request, IllustrationRepository $illustrationRepository): Response |
||
| 537 | { |
||
| 538 | $resourceNodeId = $request->get('id'); |
||
| 539 | |||
| 540 | $this->setBreadCrumb($request); |
||
| 541 | $repository = $this->getRepositoryFromRequest($request); |
||
| 542 | $resource = $repository->getResourceFromResourceNode($resourceNodeId); |
||
| 543 | $this->denyAccessUnlessValidResource($resource); |
||
| 544 | $settings = $repository->getResourceSettings(); |
||
| 545 | $resourceNode = $resource->getResourceNode(); |
||
| 546 | |||
| 547 | $this->denyAccessUnlessGranted( |
||
| 548 | ResourceNodeVoter::EDIT, |
||
| 549 | $resourceNode, |
||
| 550 | $this->trans('Unauthorised access to resource') |
||
| 551 | ); |
||
| 552 | |||
| 553 | $resourceNodeParentId = $resourceNode->getId(); |
||
| 554 | |||
| 555 | $routeParams = $this->getResourceParams($request); |
||
| 556 | $routeParams['id'] = $resourceNodeParentId; |
||
| 557 | |||
| 558 | $form = $repository->getForm($this->container->get('form.factory'), $resource); |
||
| 559 | |||
| 560 | if ($resourceNode->hasEditableContent() && $settings->isAllowToSaveEditorToResourceFile()) { |
||
| 561 | $form->add( |
||
| 562 | $this->fileContentName, |
||
| 563 | CKEditorType::class, |
||
| 564 | [ |
||
| 565 | 'mapped' => false, |
||
| 566 | 'config' => [ |
||
| 567 | 'filebrowserImageBrowseRoute' => 'resources_filemanager', |
||
| 568 | 'filebrowserImageBrowseRouteParameters' => $routeParams, |
||
| 569 | ], |
||
| 570 | ] |
||
| 571 | ); |
||
| 572 | $content = $repository->getResourceNodeFileContent($resourceNode); |
||
| 573 | $form->get($this->fileContentName)->setData($content); |
||
| 574 | } |
||
| 575 | |||
| 576 | $form->handleRequest($request); |
||
| 577 | |||
| 578 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 579 | /** @var AbstractResource $newResource */ |
||
| 580 | $newResource = $form->getData(); |
||
| 581 | |||
| 582 | if ($form->has($this->fileContentName)) { |
||
| 583 | $data = $form->get($this->fileContentName)->getData(); |
||
| 584 | $repository->updateResourceFileContent($newResource, $data); |
||
| 585 | } |
||
| 586 | |||
| 587 | $repository->updateNodeForResource($newResource); |
||
| 588 | |||
| 589 | if ($form->has('illustration')) { |
||
| 590 | $illustration = $form->get('illustration')->getData(); |
||
| 591 | if ($illustration) { |
||
| 592 | $file = $illustrationRepository->addIllustration($newResource, $this->getUser(), $illustration); |
||
| 593 | $em = $illustrationRepository->getEntityManager(); |
||
| 594 | $em->persist($file); |
||
| 595 | $em->flush(); |
||
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | $this->addFlash('success', $this->trans('Updated')); |
||
| 600 | |||
| 601 | //if ($newResource->getResourceNode()->hasResourceFile()) { |
||
| 602 | $resourceNodeParentId = $newResource->getResourceNode()->getParent()->getId(); |
||
| 603 | //} |
||
| 604 | $routeParams['id'] = $resourceNodeParentId; |
||
| 605 | |||
| 606 | return $this->redirectToRoute('chamilo_core_resource_list', $routeParams); |
||
| 607 | } |
||
| 608 | |||
| 609 | return $this->render( |
||
| 610 | '@ChamiloTheme/Resource/edit.html.twig', |
||
| 611 | [ |
||
| 612 | 'form' => $form->createView(), |
||
| 613 | 'parent' => $resourceNodeParentId, |
||
| 614 | ] |
||
| 615 | ); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Shows a resource information. |
||
| 620 | * |
||
| 621 | * @Route("/{tool}/{type}/{id}/info", methods={"GET"}, name="chamilo_core_resource_info") |
||
| 622 | */ |
||
| 623 | public function infoAction(Request $request, IllustrationRepository $illustrationRepository): Response |
||
| 624 | { |
||
| 625 | $this->setBreadCrumb($request); |
||
| 626 | $nodeId = $request->get('id'); |
||
| 627 | |||
| 628 | $repository = $this->getRepositoryFromRequest($request); |
||
| 629 | |||
| 630 | /** @var AbstractResource $resource */ |
||
| 631 | $resource = $repository->getResourceFromResourceNode($nodeId); |
||
| 632 | $this->denyAccessUnlessValidResource($resource); |
||
| 633 | |||
| 634 | $resourceNode = $resource->getResourceNode(); |
||
| 635 | $this->denyAccessUnlessGranted( |
||
| 636 | ResourceNodeVoter::VIEW, |
||
| 637 | $resourceNode, |
||
| 638 | $this->trans('Unauthorised access to resource') |
||
| 639 | ); |
||
| 640 | |||
| 641 | $tool = $request->get('tool'); |
||
| 642 | $type = $request->get('type'); |
||
| 643 | |||
| 644 | $illustration = $illustrationRepository->getIllustrationUrlFromNode($resource->getResourceNode()); |
||
| 645 | |||
| 646 | $params = [ |
||
| 647 | 'resource' => $resource, |
||
| 648 | 'illustration' => $illustration, |
||
| 649 | 'tool' => $tool, |
||
| 650 | 'type' => $type, |
||
| 651 | ]; |
||
| 652 | |||
| 653 | return $this->render('@ChamiloTheme/Resource/info.html.twig', $params); |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Preview a file. Mostly used when using a modal. |
||
| 658 | * |
||
| 659 | * @Route("/{tool}/{type}/{id}/preview", methods={"GET"}, name="chamilo_core_resource_preview") |
||
| 660 | */ |
||
| 661 | public function previewAction(Request $request): Response |
||
| 662 | { |
||
| 663 | $this->setBreadCrumb($request); |
||
| 664 | $nodeId = $request->get('id'); |
||
| 665 | |||
| 666 | $repository = $this->getRepositoryFromRequest($request); |
||
| 667 | |||
| 668 | /** @var AbstractResource $resource */ |
||
| 669 | $resource = $repository->getResourceFromResourceNode($nodeId); |
||
| 670 | $this->denyAccessUnlessValidResource($resource); |
||
| 671 | |||
| 672 | $resourceNode = $resource->getResourceNode(); |
||
| 673 | |||
| 674 | $this->denyAccessUnlessGranted( |
||
| 675 | ResourceNodeVoter::VIEW, |
||
| 676 | $resourceNode, |
||
| 677 | $this->trans('Unauthorised access to resource') |
||
| 678 | ); |
||
| 679 | |||
| 680 | $tool = $request->get('tool'); |
||
| 681 | $type = $request->get('type'); |
||
| 682 | |||
| 683 | $params = [ |
||
| 684 | 'resource' => $resource, |
||
| 685 | 'tool' => $tool, |
||
| 686 | 'type' => $type, |
||
| 687 | ]; |
||
| 688 | |||
| 689 | return $this->render('@ChamiloTheme/Resource/preview.html.twig', $params); |
||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @Route("/{tool}/{type}/{id}/change_visibility", name="chamilo_core_resource_change_visibility") |
||
| 694 | */ |
||
| 695 | public function changeVisibilityAction(Request $request): Response |
||
| 696 | { |
||
| 697 | $id = $request->get('id'); |
||
| 698 | |||
| 699 | $repository = $this->getRepositoryFromRequest($request); |
||
| 700 | |||
| 701 | /** @var AbstractResource $resource */ |
||
| 702 | $resource = $repository->getResourceFromResourceNode($id); |
||
| 703 | |||
| 704 | $this->denyAccessUnlessValidResource($resource); |
||
| 705 | |||
| 706 | $resourceNode = $resource->getResourceNode(); |
||
| 707 | |||
| 708 | $this->denyAccessUnlessGranted( |
||
| 709 | ResourceNodeVoter::EDIT, |
||
| 710 | $resourceNode, |
||
| 711 | $this->trans('Unauthorised access to resource') |
||
| 712 | ); |
||
| 713 | |||
| 714 | /** @var ResourceLink $link */ |
||
| 715 | if ($this->hasCourse()) { |
||
| 716 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 717 | } else { |
||
| 718 | $link = $resource->getFirstResourceLink(); |
||
| 719 | } |
||
| 720 | |||
| 721 | $icon = 'fa-eye'; |
||
| 722 | // Use repository to change settings easily. |
||
| 723 | if ($link && ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) { |
||
| 724 | $repository->setVisibilityDraft($resource); |
||
| 725 | $icon = 'fa-eye-slash'; |
||
| 726 | } else { |
||
| 727 | $repository->setVisibilityPublished($resource); |
||
| 728 | } |
||
| 729 | |||
| 730 | $result = ['icon' => $icon]; |
||
| 731 | |||
| 732 | return new JsonResponse($result); |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @Route("/{tool}/{type}/{id}", name="chamilo_core_resource_delete") |
||
| 737 | */ |
||
| 738 | public function deleteAction(Request $request): Response |
||
| 769 | } |
||
| 770 | |||
| 771 | /** |
||
| 772 | * @Route("/{tool}/{type}/{id}", methods={"DELETE"}, name="chamilo_core_resource_delete_mass") |
||
| 773 | */ |
||
| 774 | public function deleteMassAction($primaryKeys, $allPrimaryKeys, Request $request): Response |
||
| 775 | { |
||
| 776 | $em = $this->getDoctrine()->getManager(); |
||
| 777 | $repo = $this->getRepositoryFromRequest($request); |
||
| 778 | |||
| 779 | $parentId = 0; |
||
| 780 | foreach ($primaryKeys as $id) { |
||
| 781 | $resource = $repo->find($id); |
||
| 782 | $resourceNode = $resource->getResourceNode(); |
||
| 783 | |||
| 784 | if (null === $resourceNode) { |
||
| 785 | continue; |
||
| 786 | } |
||
| 787 | |||
| 788 | $this->denyAccessUnlessGranted( |
||
| 789 | ResourceNodeVoter::DELETE, |
||
| 790 | $resourceNode, |
||
| 791 | $this->trans('Unauthorised access to resource') |
||
| 792 | ); |
||
| 793 | |||
| 794 | $parentId = $resourceNode->getParent()->getId(); |
||
| 795 | $em->remove($resource); |
||
| 796 | } |
||
| 797 | |||
| 798 | $this->addFlash('success', $this->trans('Deleted')); |
||
| 799 | $em->flush(); |
||
| 800 | |||
| 801 | $routeParams = $this->getResourceParams($request); |
||
| 802 | $routeParams['id'] = $parentId; |
||
| 803 | |||
| 804 | return $this->redirectToRoute('chamilo_core_resource_list', $routeParams); |
||
| 805 | } |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Shows the associated resource file. |
||
| 809 | * |
||
| 810 | * @Route("/{tool}/{type}/{id}/view", methods={"GET"}, name="chamilo_core_resource_view_file") |
||
| 811 | */ |
||
| 812 | public function viewAction(Request $request, RouterInterface $router): Response |
||
| 813 | { |
||
| 814 | $id = $request->get('id'); |
||
| 815 | $filter = $request->get('filter'); |
||
| 816 | $mode = $request->get('mode'); |
||
| 817 | $em = $this->getDoctrine(); |
||
| 818 | /** @var ResourceNode $resourceNode */ |
||
| 819 | $resourceNode = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($id); |
||
| 820 | |||
| 821 | if (null === $resourceNode) { |
||
| 822 | throw new FileNotFoundException('Resource not found'); |
||
| 823 | } |
||
| 824 | |||
| 825 | $repo = $this->getRepositoryFromRequest($request); |
||
| 826 | |||
| 827 | if ($repo instanceof ResourceWithLinkInterface) { |
||
| 828 | $resource = $repo->getResourceFromResourceNode($resourceNode->getId()); |
||
| 829 | $url = $repo->getLink($resource, $router, $this->getCourseUrlQueryToArray()); |
||
| 830 | |||
| 831 | return $this->redirect($url); |
||
| 832 | } |
||
| 833 | |||
| 834 | return $this->showFile($request, $resourceNode, $mode, $filter); |
||
| 835 | } |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Shows the associated resource file. |
||
| 839 | * |
||
| 840 | * @Route("/{tool}/{type}/{id}/view_resource", methods={"GET"}, name="chamilo_core_resource_view_resource") |
||
| 841 | */ |
||
| 842 | public function viewResourceAction(Request $request, RouterInterface $router): Response |
||
| 870 | |||
| 871 | |||
| 872 | //return $this->showFile($request, $resourceNode, $mode, $filter); |
||
| 873 | } |
||
| 874 | |||
| 875 | /** |
||
| 876 | * @Route("/{tool}/{type}/{id}/download", methods={"GET"}, name="chamilo_core_resource_download") |
||
| 877 | */ |
||
| 878 | public function downloadAction(Request $request) |
||
| 879 | { |
||
| 880 | $resourceNodeId = (int) $request->get('id'); |
||
| 881 | $courseNode = $this->getCourse()->getResourceNode(); |
||
| 882 | |||
| 883 | $repo = $this->getRepositoryFromRequest($request); |
||
| 884 | |||
| 885 | if (empty($resourceNodeId)) { |
||
| 886 | $resourceNode = $courseNode; |
||
| 887 | } else { |
||
| 888 | $resourceNode = $repo->getResourceNodeRepository()->find($resourceNodeId); |
||
| 889 | } |
||
| 890 | |||
| 891 | $this->denyAccessUnlessGranted( |
||
| 892 | ResourceNodeVoter::VIEW, |
||
| 893 | $resourceNode, |
||
| 894 | $this->trans('Unauthorised access to resource') |
||
| 895 | ); |
||
| 896 | |||
| 897 | // If resource node has a file just download it. Don't download the children. |
||
| 898 | if ($resourceNode->hasResourceFile()) { |
||
| 899 | // Redirect to download single file. |
||
| 900 | return $this->showFile($request, $resourceNode, 'download'); |
||
| 901 | } |
||
| 902 | |||
| 903 | $zipName = $resourceNode->getSlug().'.zip'; |
||
| 904 | $rootNodePath = $resourceNode->getPathForDisplay(); |
||
| 905 | $resourceNodeRepo = $repo->getResourceNodeRepository(); |
||
| 906 | |||
| 907 | $criteria = Criteria::create() |
||
| 908 | ->where(Criteria::expr()->neq('resourceFile', null)) // must have a file |
||
| 909 | // ->andWhere(Criteria::expr()->eq('resourceType', $type)) |
||
| 910 | ; |
||
| 911 | |||
| 912 | /** @var ArrayCollection|ResourceNode[] $children */ |
||
| 913 | /** @var QueryBuilder $children */ |
||
| 914 | $qb = $resourceNodeRepo->getChildrenQueryBuilder($resourceNode); |
||
| 915 | $qb->addCriteria($criteria); |
||
| 916 | $children = $qb->getQuery()->getResult(); |
||
| 917 | $count = count($children); |
||
| 918 | if (0 === $count) { |
||
| 919 | $params = $this->getResourceParams($request); |
||
| 920 | $params['id'] = $resourceNodeId; |
||
| 921 | |||
| 922 | $this->addFlash('warning', $this->trans('No files')); |
||
| 923 | |||
| 924 | return $this->redirectToRoute('chamilo_core_resource_list', $params); |
||
| 925 | } |
||
| 926 | |||
| 927 | $response = new StreamedResponse( |
||
| 928 | function () use ($rootNodePath, $zipName, $children, $repo) { |
||
| 929 | // Define suitable options for ZipStream Archive. |
||
| 930 | $options = new Archive(); |
||
| 931 | $options->setContentType('application/octet-stream'); |
||
| 932 | //initialise zipstream with output zip filename and options. |
||
| 933 | $zip = new ZipStream($zipName, $options); |
||
| 934 | |||
| 935 | /** @var ResourceNode $node */ |
||
| 936 | foreach ($children as $node) { |
||
| 937 | //$resourceFile = $node->getResourceFile(); |
||
| 938 | //$systemName = $resourceFile->getFile()->getPathname(); |
||
| 939 | $stream = $repo->getResourceNodeFileStream($node); |
||
| 940 | //error_log($node->getPathForDisplay()); |
||
| 941 | $fileToDisplay = str_replace($rootNodePath, '', $node->getPathForDisplay()); |
||
| 942 | $zip->addFileFromStream($fileToDisplay, $stream); |
||
| 943 | } |
||
| 944 | $zip->finish(); |
||
| 945 | } |
||
| 946 | ); |
||
| 947 | |||
| 948 | $disposition = $response->headers->makeDisposition( |
||
| 949 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 950 | $zipName //Transliterator::transliterate($zipName) |
||
| 951 | ); |
||
| 952 | $response->headers->set('Content-Disposition', $disposition); |
||
| 953 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
| 954 | |||
| 955 | return $response; |
||
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Upload form. |
||
| 960 | * |
||
| 961 | * @Route("/{tool}/{type}/{id}/upload", name="chamilo_core_resource_upload", methods={"GET", "POST"}, |
||
| 962 | * options={"expose"=true}) |
||
| 963 | */ |
||
| 964 | public function uploadAction(Request $request, $tool, $type, $id): Response |
||
| 983 | } |
||
| 984 | |||
| 985 | public function setBreadCrumb(Request $request) |
||
| 986 | { |
||
| 987 | $tool = $request->get('tool'); |
||
| 988 | $type = $request->get('type'); |
||
| 989 | $resourceNodeId = $request->get('id'); |
||
| 990 | |||
| 991 | $routeParams = $this->getResourceParams($request); |
||
| 992 | |||
| 993 | if (!empty($resourceNodeId)) { |
||
| 994 | $breadcrumb = $this->getBreadCrumb(); |
||
| 995 | $toolParams = $routeParams; |
||
| 996 | $toolParams['id'] = null; |
||
| 997 | |||
| 998 | // Root tool link |
||
| 999 | $breadcrumb->addChild( |
||
| 1000 | $this->trans($tool), |
||
| 1001 | [ |
||
| 1002 | 'uri' => $this->generateUrl('chamilo_core_resource_index', $toolParams), |
||
| 1003 | ] |
||
| 1004 | ); |
||
| 1005 | |||
| 1006 | $repo = $this->getRepositoryFromRequest($request); |
||
| 1007 | |||
| 1008 | /** @var ResourceInterface $parent */ |
||
| 1009 | $originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]); |
||
| 1010 | if (null === $originalResource) { |
||
| 1011 | return; |
||
| 1012 | } |
||
| 1013 | $parent = $originalParent = $originalResource->getResourceNode(); |
||
| 1014 | |||
| 1015 | $parentList = []; |
||
| 1016 | while (null !== $parent) { |
||
| 1017 | if ($type !== $parent->getResourceType()->getName()) { |
||
| 1018 | break; |
||
| 1019 | } |
||
| 1020 | $parent = $parent->getParent(); |
||
| 1021 | if ($parent) { |
||
| 1022 | $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]); |
||
| 1023 | if ($resource) { |
||
| 1024 | $parentList[] = $resource; |
||
| 1025 | } |
||
| 1026 | } |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | $parentList = array_reverse($parentList); |
||
| 1030 | /** @var ResourceInterface $item */ |
||
| 1031 | foreach ($parentList as $item) { |
||
| 1032 | $params = $routeParams; |
||
| 1033 | $params['id'] = $item->getResourceNode()->getId(); |
||
| 1034 | $breadcrumb->addChild( |
||
| 1035 | $item->getResourceName(), |
||
| 1036 | [ |
||
| 1037 | 'uri' => $this->generateUrl('chamilo_core_resource_list', $params), |
||
| 1038 | ] |
||
| 1039 | ); |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | $params = $routeParams; |
||
| 1043 | $params['id'] = $originalParent->getId(); |
||
| 1044 | |||
| 1045 | $breadcrumb->addChild( |
||
| 1046 | $originalResource->getResourceName(), |
||
| 1047 | [ |
||
| 1048 | 'uri' => $this->generateUrl('chamilo_core_resource_list', $params), |
||
| 1049 | ] |
||
| 1050 | ); |
||
| 1051 | } |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * @param string $mode |
||
| 1056 | * @param string $filter |
||
| 1057 | * |
||
| 1058 | * @return mixed|StreamedResponse |
||
| 1059 | */ |
||
| 1060 | private function showFile(Request $request, ResourceNode $resourceNode, $mode = 'show', $filter = '') |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * @param string $fileType |
||
| 1132 | * |
||
| 1133 | * @return RedirectResponse|Response |
||
| 1134 | */ |
||
| 1135 | private function createResource(Request $request, $fileType = 'file') |
||
| 1287 | ] |
||
| 1288 | ); |
||
| 1291 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.