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