| Total Complexity | 86 |
| Total Lines | 1255 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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 |
||
| 55 | class ResourceController extends AbstractResourceController implements CourseControllerInterface |
||
| 56 | { |
||
| 57 | use CourseControllerTrait; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/{tool}/{type}", name="chamilo_core_resource_index") |
||
| 61 | * |
||
| 62 | * Example: /document/files (See the 'tool' and the 'resource_type' DB tables.) |
||
| 63 | * For the tool value check the Tool entity. |
||
| 64 | * For the type value check the ResourceType entity. |
||
| 65 | */ |
||
| 66 | public function indexAction(Request $request, Grid $grid): Response |
||
| 67 | { |
||
| 68 | $tool = $request->get('tool'); |
||
| 69 | $type = $request->get('type'); |
||
| 70 | |||
| 71 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 72 | $repository = $this->getRepositoryFromRequest($request); |
||
| 73 | $settings = $repository->getResourceSettings(); |
||
| 74 | |||
| 75 | $grid = $this->getGrid($request, $repository, $grid, $parentResourceNode->getId()); |
||
| 76 | |||
| 77 | $breadcrumb = $this->getBreadCrumb(); |
||
| 78 | $breadcrumb->addChild( |
||
| 79 | $this->trans($tool), |
||
| 80 | [ |
||
| 81 | 'uri' => '#', |
||
| 82 | ] |
||
| 83 | ); |
||
| 84 | |||
| 85 | // The base resource node is the course. |
||
| 86 | $id = $parentResourceNode->getId(); |
||
| 87 | |||
| 88 | return $grid->getGridResponse( |
||
| 89 | '@ChamiloTheme/Resource/index.html.twig', |
||
| 90 | [ |
||
| 91 | 'tool' => $tool, |
||
| 92 | 'type' => $type, |
||
| 93 | 'id' => $id, |
||
| 94 | 'parent_resource_node' => $parentResourceNode, |
||
| 95 | 'resource_settings' => $settings, |
||
| 96 | ] |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @Route("/{tool}/{type}/{id}/list", name="chamilo_core_resource_list") |
||
| 102 | * |
||
| 103 | * If node has children show it |
||
| 104 | */ |
||
| 105 | public function listAction(Request $request, Grid $grid): Response |
||
| 106 | { |
||
| 107 | $tool = $request->get('tool'); |
||
| 108 | $type = $request->get('type'); |
||
| 109 | $resourceNodeId = $request->get('id'); |
||
| 110 | |||
| 111 | $repository = $this->getRepositoryFromRequest($request); |
||
| 112 | $settings = $repository->getResourceSettings(); |
||
| 113 | |||
| 114 | $grid = $this->getGrid($request, $repository, $grid, $resourceNodeId); |
||
| 115 | |||
| 116 | $this->setBreadCrumb($request); |
||
| 117 | $parentResourceNode = $this->getParentResourceNode($request); |
||
| 118 | |||
| 119 | return $grid->getGridResponse( |
||
| 120 | '@ChamiloTheme/Resource/index.html.twig', |
||
| 121 | [ |
||
| 122 | 'parent_id' => $resourceNodeId, |
||
| 123 | 'tool' => $tool, |
||
| 124 | 'type' => $type, |
||
| 125 | 'id' => $resourceNodeId, |
||
| 126 | 'parent_resource_node' => $parentResourceNode, |
||
| 127 | 'resource_settings' => $settings, |
||
| 128 | ] |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getGrid(Request $request, ResourceRepository $repository, Grid $grid, int $resourceNodeId): Grid |
||
| 133 | { |
||
| 134 | $class = $repository->getRepository()->getClassName(); |
||
| 135 | |||
| 136 | // The group 'resource' is set in the @GRID\Source annotation in the entity. |
||
| 137 | $source = new Entity($class, 'resource'); |
||
| 138 | /** @var ResourceNode $parentNode */ |
||
| 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 | $settings = $repository->getResourceSettings(); |
||
| 148 | |||
| 149 | $course = $this->getCourse(); |
||
| 150 | $session = $this->getSession(); |
||
| 151 | /** @var QueryBuilder $qb */ |
||
| 152 | $qb = $repository->getResources($this->getUser(), $parentNode, $course, $session, null); |
||
| 153 | |||
| 154 | // 3. Set QueryBuilder to the source. |
||
| 155 | $source->initQueryBuilder($qb); |
||
| 156 | $grid->setSource($source); |
||
| 157 | |||
| 158 | $resourceParams = $this->getResourceParams($request); |
||
| 159 | |||
| 160 | $grid->setRouteUrl($this->generateUrl('chamilo_core_resource_list', $resourceParams)); |
||
| 161 | |||
| 162 | //$grid->hideFilters(); |
||
| 163 | //$grid->setLimits(20); |
||
| 164 | //$grid->isReadyForRedirect(); |
||
| 165 | //$grid->setMaxResults(1); |
||
| 166 | //$grid->setLimits(2); |
||
| 167 | //$grid->setColumns($columns); |
||
| 168 | $routeParams = $resourceParams; |
||
| 169 | $routeParams['id'] = null; |
||
| 170 | |||
| 171 | /** @var Column $titleColumn */ |
||
| 172 | $titleColumn = $repository->getTitleColumn($grid); |
||
| 173 | $titleColumn->setSafe(false); // allows links in the title |
||
| 174 | |||
| 175 | // Title link. |
||
| 176 | $titleColumn->setTitle($this->trans('Name')); |
||
| 177 | |||
| 178 | //$repository->formatGrid(); |
||
| 179 | /*if ($grid->hasColumn('filetype')) { |
||
| 180 | $grid->getColumn('filetype')->setTitle($this->trans('Type')); |
||
| 181 | }*/ |
||
| 182 | |||
| 183 | $titleColumn->manipulateRenderCell( |
||
| 184 | function ($value, Row $row, $router) use ($routeParams) { |
||
| 185 | /** @var Router $router */ |
||
| 186 | /** @var ResourceInterface $entity */ |
||
| 187 | $entity = $row->getEntity(); |
||
| 188 | $resourceNode = $entity->getResourceNode(); |
||
| 189 | $id = $resourceNode->getId(); |
||
| 190 | |||
| 191 | $myParams = $routeParams; |
||
| 192 | $myParams['id'] = $id; |
||
| 193 | unset($myParams[0]); |
||
| 194 | |||
| 195 | $icon = $resourceNode->getIcon().' '; |
||
| 196 | if ($resourceNode->hasResourceFile()) { |
||
| 197 | if ($resourceNode->isResourceFileAnImage()) { |
||
| 198 | $url = $router->generate( |
||
| 199 | 'chamilo_core_resource_view', |
||
| 200 | $myParams |
||
| 201 | ); |
||
| 202 | |||
| 203 | return $icon.'<a data-fancybox="gallery" href="'.$url.'">'.$value.'</a>'; |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($resourceNode->isResourceFileAVideo()) { |
||
| 207 | $url = $router->generate( |
||
| 208 | 'chamilo_core_resource_view', |
||
| 209 | $myParams |
||
| 210 | ); |
||
| 211 | |||
| 212 | return ' |
||
| 213 | <video width="640" height="320" controls id="video'.$id.'" controls preload="metadata" style="display:none;"> |
||
| 214 | <source src="'.$url.'" type="video/mp4"> |
||
| 215 | Your browser doesn\'t support HTML5 video tag. |
||
| 216 | </video> |
||
| 217 | '.$icon.' <a data-fancybox="gallery" data-width="640" data-height="360" href="#video'.$id.'">'.$value.'</a>'; |
||
| 218 | } |
||
| 219 | |||
| 220 | $url = $router->generate( |
||
| 221 | 'chamilo_core_resource_preview', |
||
| 222 | $myParams |
||
| 223 | ); |
||
| 224 | |||
| 225 | return $icon.'<a data-fancybox="gallery" data-type="iframe" data-src="'.$url.'" href="javascript:;" >'.$value.'</a>'; |
||
| 226 | } else { |
||
| 227 | $url = $router->generate( |
||
| 228 | 'chamilo_core_resource_list', |
||
| 229 | $myParams |
||
| 230 | ); |
||
| 231 | |||
| 232 | return $icon.'<a href="'.$url.'">'.$value.'</a>'; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | ); |
||
| 236 | |||
| 237 | if ($grid->hasColumn('filetype')) { |
||
| 238 | $grid->getColumn('filetype')->manipulateRenderCell( |
||
| 239 | function ($value, Row $row, $router) { |
||
|
|
|||
| 240 | /** @var AbstractResource $entity */ |
||
| 241 | $entity = $row->getEntity(); |
||
| 242 | $resourceNode = $entity->getResourceNode(); |
||
| 243 | |||
| 244 | if ($resourceNode->hasResourceFile()) { |
||
| 245 | $file = $resourceNode->getResourceFile(); |
||
| 246 | |||
| 247 | return $file->getMimeType(); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $this->trans('Folder'); |
||
| 251 | } |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($grid->hasColumn('iid')) { |
||
| 256 | $grid->setHiddenColumns(['iid']); |
||
| 257 | } |
||
| 258 | |||
| 259 | // Delete mass action. |
||
| 260 | if ($this->isGranted(ResourceNodeVoter::DELETE, $parentNode)) { |
||
| 261 | $deleteMassAction = new MassAction( |
||
| 262 | 'Delete', |
||
| 263 | 'ChamiloCoreBundle:Resource:deleteMass', |
||
| 264 | true, |
||
| 265 | $routeParams |
||
| 266 | ); |
||
| 267 | $grid->addMassAction($deleteMassAction); |
||
| 268 | } |
||
| 269 | |||
| 270 | // Info action. |
||
| 271 | $myRowAction = new RowAction( |
||
| 272 | $this->trans('Info'), |
||
| 273 | 'chamilo_core_resource_info', |
||
| 274 | false, |
||
| 275 | '_self', |
||
| 276 | [ |
||
| 277 | 'class' => 'btn btn-secondary info_action', |
||
| 278 | 'icon' => 'fa-info-circle', |
||
| 279 | 'iframe' => false, |
||
| 280 | ] |
||
| 281 | ); |
||
| 282 | |||
| 283 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 284 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 285 | $routeParams['id'] = $id; |
||
| 286 | $action->setRouteParameters($routeParams); |
||
| 287 | $attributes = $action->getAttributes(); |
||
| 288 | $attributes['data-action'] = $action->getRoute(); |
||
| 289 | $attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 290 | $attributes['data-node-id'] = $id; |
||
| 291 | $attributes['title'] = $this->trans('Info').' '.$row->getEntity()->getResourceName(); |
||
| 292 | $action->setAttributes($attributes); |
||
| 293 | |||
| 294 | return $action; |
||
| 295 | }; |
||
| 296 | |||
| 297 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 298 | $grid->addRowAction($myRowAction); |
||
| 299 | |||
| 300 | // Download action |
||
| 301 | $myRowAction = new RowAction( |
||
| 302 | $this->trans('Download'), |
||
| 303 | 'chamilo_core_resource_download', |
||
| 304 | false, |
||
| 305 | '_self', |
||
| 306 | [ |
||
| 307 | 'class' => 'btn btn-secondary download_action', |
||
| 308 | 'icon' => 'fa-download', |
||
| 309 | 'title' => $this->trans('Download'), |
||
| 310 | ] |
||
| 311 | ); |
||
| 312 | |||
| 313 | $setNodeDownloadParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 314 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 315 | $routeParams['id'] = $id; |
||
| 316 | $action->setRouteParameters($routeParams); |
||
| 317 | $attributes = $action->getAttributes(); |
||
| 318 | $action->setAttributes($attributes); |
||
| 319 | |||
| 320 | return $action; |
||
| 321 | }; |
||
| 322 | $myRowAction->addManipulateRender($setNodeDownloadParameters); |
||
| 323 | $grid->addRowAction($myRowAction); |
||
| 324 | |||
| 325 | // Set EDIT/DELETE |
||
| 326 | $setNodeParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 327 | $id = $row->getEntity()->getResourceNode()->getId(); |
||
| 328 | |||
| 329 | $allowedEdit = $this->isGranted(ResourceNodeVoter::EDIT, $row->getEntity()->getResourceNode()); |
||
| 330 | |||
| 331 | if (false === $allowedEdit) { |
||
| 332 | return null; |
||
| 333 | } |
||
| 334 | |||
| 335 | $routeParams['id'] = $id; |
||
| 336 | |||
| 337 | $action->setRouteParameters($routeParams); |
||
| 338 | $attributes = $action->getAttributes(); |
||
| 339 | //$attributes['data-action'] = $action->getRoute(); |
||
| 340 | //$attributes['data-action-id'] = $action->getRoute().'_'.$id; |
||
| 341 | //$attributes['data-node-id'] = $id; |
||
| 342 | $action->setAttributes($attributes); |
||
| 343 | |||
| 344 | return $action; |
||
| 345 | }; |
||
| 346 | |||
| 347 | if ($this->isGranted(ResourceNodeVoter::EDIT, $parentNode)) { |
||
| 348 | // Enable/Disable |
||
| 349 | $myRowAction = new RowAction( |
||
| 350 | '', |
||
| 351 | 'chamilo_core_resource_change_visibility', |
||
| 352 | false, |
||
| 353 | '_self' |
||
| 354 | ); |
||
| 355 | |||
| 356 | $setVisibleParameters = function (RowAction $action, Row $row) use ($routeParams) { |
||
| 357 | /** @var AbstractResource $resource */ |
||
| 358 | $resource = $row->getEntity(); |
||
| 359 | $allowedEdit = $this->isGranted(ResourceNodeVoter::EDIT, $resource->getResourceNode()); |
||
| 360 | |||
| 361 | if (false === $allowedEdit) { |
||
| 362 | return null; |
||
| 363 | } |
||
| 364 | |||
| 365 | $id = $resource->getResourceNode()->getId(); |
||
| 366 | |||
| 367 | $icon = 'fa-eye-slash'; |
||
| 368 | if ($this->hasCourse()) { |
||
| 369 | $link = $resource->getCourseSessionResourceLink($this->getCourse(), $this->getSession()); |
||
| 370 | } else { |
||
| 371 | $link = $resource->getFirstResourceLink(); |
||
| 372 | } |
||
| 373 | |||
| 374 | if (null === $link) { |
||
| 375 | return null; |
||
| 376 | } |
||
| 377 | if (ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility()) { |
||
| 378 | $icon = 'fa-eye'; |
||
| 379 | } |
||
| 380 | $routeParams['id'] = $id; |
||
| 381 | $action->setRouteParameters($routeParams); |
||
| 382 | $attributes = [ |
||
| 383 | 'class' => 'btn btn-secondary change_visibility', |
||
| 384 | 'data-id' => $id, |
||
| 385 | 'icon' => $icon, |
||
| 386 | ]; |
||
| 387 | $action->setAttributes($attributes); |
||
| 388 | |||
| 389 | return $action; |
||
| 390 | }; |
||
| 391 | |||
| 392 | $myRowAction->addManipulateRender($setVisibleParameters); |
||
| 393 | $grid->addRowAction($myRowAction); |
||
| 394 | |||
| 395 | if ($settings->isAllowResourceEdit()) { |
||
| 396 | // Edit action. |
||
| 397 | $myRowAction = new RowAction( |
||
| 398 | $this->trans('Edit'), |
||
| 399 | 'chamilo_core_resource_edit', |
||
| 400 | false, |
||
| 401 | '_self', |
||
| 402 | ['class' => 'btn btn-secondary', 'icon' => 'fa fa-pen'] |
||
| 403 | ); |
||
| 404 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 405 | $grid->addRowAction($myRowAction); |
||
| 406 | } |
||
| 407 | |||
| 408 | // More action. |
||
| 409 | /*$myRowAction = new RowAction( |
||
| 410 | $this->trans('More'), |
||
| 411 | 'chamilo_core_resource_preview', |
||
| 412 | false, |
||
| 413 | '_self', |
||
| 414 | ['class' => 'btn btn-secondary edit_resource', 'icon' => 'fa fa-ellipsis-h'] |
||
| 415 | ); |
||
| 416 | |||
| 417 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 418 | $grid->addRowAction($myRowAction);*/ |
||
| 419 | |||
| 420 | // Delete action. |
||
| 421 | $myRowAction = new RowAction( |
||
| 422 | $this->trans('Delete'), |
||
| 423 | 'chamilo_core_resource_delete', |
||
| 424 | true, |
||
| 425 | '_self', |
||
| 426 | [ |
||
| 427 | 'class' => 'btn btn-danger', |
||
| 428 | //'data_hidden' => true, |
||
| 429 | ] |
||
| 430 | ); |
||
| 431 | $myRowAction->addManipulateRender($setNodeParameters); |
||
| 432 | $grid->addRowAction($myRowAction); |
||
| 433 | } |
||
| 434 | |||
| 435 | /*$grid->addExport(new CSVExport($this->trans('CSV export'), 'export', ['course' => $courseIdentifier])); |
||
| 436 | $grid->addExport( |
||
| 437 | new ExcelExport( |
||
| 438 | $this->trans('Excel export'), |
||
| 439 | 'export', |
||
| 440 | ['course' => $courseIdentifier] |
||
| 441 | ) |
||
| 442 | );*/ |
||
| 443 | |||
| 444 | return $grid; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @Route("/{tool}/{type}/{id}/new_folder", methods={"GET", "POST"}, name="chamilo_core_resource_new_folder") |
||
| 449 | */ |
||
| 450 | public function newFolderAction(Request $request): Response |
||
| 451 | { |
||
| 452 | $this->setBreadCrumb($request); |
||
| 453 | |||
| 454 | return $this->createResource($request, 'folder'); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @Route("/{tool}/{type}/{id}/new", methods={"GET", "POST"}, name="chamilo_core_resource_new") |
||
| 459 | */ |
||
| 460 | public function newAction(Request $request): Response |
||
| 461 | { |
||
| 462 | $this->setBreadCrumb($request); |
||
| 463 | |||
| 464 | return $this->createResource($request, 'file'); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Route("/{tool}/{type}/{id}/disk_space", methods={"GET", "POST"}, name="chamilo_core_resource_disk_space") |
||
| 469 | */ |
||
| 470 | public function diskSpaceAction(Request $request): Response |
||
| 471 | { |
||
| 472 | $this->setBreadCrumb($request); |
||
| 473 | |||
| 474 | $nodeId = $request->get('id'); |
||
| 475 | |||
| 476 | $repository = $this->getRepositoryFromRequest($request); |
||
| 477 | |||
| 478 | /** @var ResourceNode $resourceNode */ |
||
| 479 | $resourceNode = $repository->getResourceNodeRepository()->find($nodeId); |
||
| 480 | $size = $repository->getResourceNodeRepository()->getSize($resourceNode, $repository->getResourceType()); |
||
| 481 | |||
| 482 | $this->denyAccessUnlessGranted( |
||
| 483 | ResourceNodeVoter::VIEW, |
||
| 484 | $resourceNode, |
||
| 485 | $this->trans('Unauthorised access to resource') |
||
| 486 | ); |
||
| 487 | |||
| 488 | return $this->render( |
||
| 489 | '@ChamiloTheme/Resource/disk_space.html.twig', |
||
| 490 | [ |
||
| 491 | 'resourceNode' => $resourceNode, |
||
| 492 | 'size' => $size, |
||
| 493 | ] |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @Route("/{tool}/{type}/{id}/edit", methods={"GET", "POST"}) |
||
| 499 | */ |
||
| 500 | public function editAction(Request $request, IllustrationRepository $illustrationRepository): Response |
||
| 501 | { |
||
| 502 | $resourceNodeId = $request->get('id'); |
||
| 503 | |||
| 504 | $this->setBreadCrumb($request); |
||
| 505 | $repository = $this->getRepositoryFromRequest($request); |
||
| 506 | $resource = $repository->getResourceFromResourceNode($resourceNodeId); |
||
| 507 | $resourceNode = $resource->getResourceNode(); |
||
| 508 | |||
| 509 | $this->denyAccessUnlessGranted( |
||
| 510 | ResourceNodeVoter::EDIT, |
||
| 511 | $resourceNode, |
||
| 512 | $this->trans('Unauthorised access to resource') |
||
| 513 | ); |
||
| 514 | |||
| 515 | $resourceNodeParentId = $resourceNode->getId(); |
||
| 516 | |||
| 517 | $routeParams = $this->getResourceParams($request); |
||
| 518 | $routeParams['id'] = $resourceNodeParentId; |
||
| 519 | |||
| 520 | $form = $repository->getForm($this->container->get('form.factory'), $resource); |
||
| 521 | |||
| 522 | if ($resourceNode->hasEditableContent()) { |
||
| 523 | $form->add( |
||
| 524 | 'content', |
||
| 525 | CKEditorType::class, |
||
| 526 | [ |
||
| 527 | 'mapped' => false, |
||
| 528 | 'config' => [ |
||
| 529 | 'filebrowserImageBrowseRoute' => 'resources_filemanager', |
||
| 530 | 'filebrowserImageBrowseRouteParameters' => $routeParams, |
||
| 531 | ], |
||
| 532 | ] |
||
| 533 | ); |
||
| 534 | $content = $repository->getResourceNodeFileContent($resourceNode); |
||
| 535 | $form->get('content')->setData($content); |
||
| 536 | } |
||
| 537 | |||
| 538 | $form->handleRequest($request); |
||
| 539 | |||
| 540 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 541 | /** @var AbstractResource $newResource */ |
||
| 542 | $newResource = $form->getData(); |
||
| 543 | |||
| 544 | if ($form->has('content')) { |
||
| 545 | $data = $form->get('content')->getData(); |
||
| 546 | $repository->updateResourceFileContent($newResource, $data); |
||
| 547 | } |
||
| 548 | |||
| 549 | //$newResource->setTitle($form->get('title')->getData()); // already set in $form->getData() |
||
| 550 | $repository->updateNodeForResource($newResource); |
||
| 551 | |||
| 552 | if ($form->has('illustration')) { |
||
| 553 | $illustration = $form->get('illustration')->getData(); |
||
| 554 | if ($illustration) { |
||
| 555 | $file = $illustrationRepository->addIllustration($newResource, $this->getUser(), $illustration); |
||
| 556 | $em = $illustrationRepository->getEntityManager(); |
||
| 557 | $em->persist($file); |
||
| 558 | $em->flush(); |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | $this->addFlash('success', $this->trans('Updated')); |
||
| 563 | |||
| 564 | //if ($newResource->getResourceNode()->hasResourceFile()) { |
||
| 565 | $resourceNodeParentId = $newResource->getResourceNode()->getParent()->getId(); |
||
| 566 | //} |
||
| 567 | $routeParams['id'] = $resourceNodeParentId; |
||
| 568 | |||
| 569 | return $this->redirectToRoute('chamilo_core_resource_list', $routeParams); |
||
| 570 | } |
||
| 571 | |||
| 572 | return $this->render( |
||
| 573 | '@ChamiloTheme/Resource/edit.html.twig', |
||
| 574 | [ |
||
| 575 | 'form' => $form->createView(), |
||
| 576 | 'parent' => $resourceNodeParentId, |
||
| 577 | ] |
||
| 578 | ); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Shows a resource information. |
||
| 583 | * |
||
| 584 | * @Route("/{tool}/{type}/{id}/info", methods={"GET"}, name="chamilo_core_resource_info") |
||
| 585 | */ |
||
| 586 | public function infoAction(Request $request, IllustrationRepository $illustrationRepository): Response |
||
| 587 | { |
||
| 588 | $this->setBreadCrumb($request); |
||
| 589 | $nodeId = $request->get('id'); |
||
| 590 | |||
| 591 | $repository = $this->getRepositoryFromRequest($request); |
||
| 592 | |||
| 593 | /** @var AbstractResource $resource */ |
||
| 594 | $resource = $repository->getResourceFromResourceNode($nodeId); |
||
| 595 | |||
| 596 | if (null === $resource) { |
||
| 597 | throw new NotFoundHttpException(); |
||
| 598 | } |
||
| 599 | |||
| 600 | $resourceNode = $resource->getResourceNode(); |
||
| 601 | |||
| 602 | if (null === $resourceNode) { |
||
| 603 | throw new NotFoundHttpException(); |
||
| 604 | } |
||
| 605 | |||
| 606 | $this->denyAccessUnlessGranted( |
||
| 607 | ResourceNodeVoter::VIEW, |
||
| 608 | $resourceNode, |
||
| 609 | $this->trans('Unauthorised access to resource') |
||
| 610 | ); |
||
| 611 | |||
| 612 | $tool = $request->get('tool'); |
||
| 613 | $type = $request->get('type'); |
||
| 614 | |||
| 615 | $illustration = $illustrationRepository->getIllustrationUrlFromNode($resource->getResourceNode()); |
||
| 616 | |||
| 617 | $params = [ |
||
| 618 | 'resource' => $resource, |
||
| 619 | 'illustration' => $illustration, |
||
| 620 | 'tool' => $tool, |
||
| 621 | 'type' => $type, |
||
| 622 | ]; |
||
| 623 | |||
| 624 | return $this->render('@ChamiloTheme/Resource/info.html.twig', $params); |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Preview a file. Mostly used when using a modal. |
||
| 629 | * |
||
| 630 | * @Route("/{tool}/{type}/{id}/preview", methods={"GET"}, name="chamilo_core_resource_preview") |
||
| 631 | */ |
||
| 632 | public function previewAction(Request $request): Response |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @Route("/{tool}/{type}/{id}/change_visibility", name="chamilo_core_resource_change_visibility") |
||
| 672 | */ |
||
| 673 | public function changeVisibilityAction(Request $request): Response |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @Route("/{tool}/{type}/{id}", name="chamilo_core_resource_delete") |
||
| 717 | */ |
||
| 718 | public function deleteAction(Request $request): Response |
||
| 755 | ); |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @Route("/{tool}/{type}/{id}", methods={"DELETE"}, name="chamilo_core_resource_delete_mass") |
||
| 760 | */ |
||
| 761 | public function deleteMassAction($primaryKeys, $allPrimaryKeys, Request $request): Response |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Shows the associated resource file. |
||
| 796 | * |
||
| 797 | * @Route("/{tool}/{type}/{id}/view", methods={"GET"}, name="chamilo_core_resource_view") |
||
| 798 | */ |
||
| 799 | public function viewAction(Request $request, RouterInterface $router): Response |
||
| 800 | { |
||
| 801 | $id = $request->get('id'); |
||
| 802 | $filter = $request->get('filter'); |
||
| 803 | $mode = $request->get('mode'); |
||
| 804 | $em = $this->getDoctrine(); |
||
| 805 | /** @var ResourceNode $resourceNode */ |
||
| 806 | $resourceNode = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($id); |
||
| 807 | |||
| 808 | if (null === $resourceNode) { |
||
| 809 | throw new FileNotFoundException('Resource not found'); |
||
| 810 | } |
||
| 811 | |||
| 812 | $repo = $this->getRepositoryFromRequest($request); |
||
| 813 | |||
| 814 | if ($repo instanceof ResourceWithLinkInterface) { |
||
| 815 | $resource = $repo->getResourceFromResourceNode($resourceNode->getId()); |
||
| 816 | $url = $repo->getLink($resource, $router, $this->getCourseUrlQueryToArray()); |
||
| 817 | |||
| 818 | return $this->redirect($url); |
||
| 819 | } |
||
| 820 | |||
| 821 | return $this->showFile($request, $resourceNode, $mode, $filter); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Gets a document when calling route resources_document_get_file. |
||
| 826 | 1 * |
||
| 827 | * @deprecated |
||
| 828 | * |
||
| 829 | * @throws \League\Flysystem\FileNotFoundException |
||
| 830 | */ |
||
| 831 | public function getDocumentAction(Request $request): Response |
||
| 832 | { |
||
| 833 | /*$file = $request->get('file'); |
||
| 834 | $mode = $request->get('mode'); |
||
| 835 | |||
| 836 | // see list of filters in config/services.yaml |
||
| 837 | $filter = $request->get('filter'); |
||
| 838 | $mode = !empty($mode) ? $mode : 'show'; |
||
| 839 | |||
| 840 | $repository = $this->getRepository('document', 'files'); |
||
| 841 | $nodeRepository = $repository->getResourceNodeRepository(); |
||
| 842 | |||
| 843 | $title = basename($file); |
||
| 844 | // @todo improve criteria to avoid giving the wrong file. |
||
| 845 | $criteria = ['slug' => $title]; |
||
| 846 | |||
| 847 | $resourceNode = $nodeRepository->findOneBy($criteria); |
||
| 848 | |||
| 849 | if (null === $resourceNode) { |
||
| 850 | throw new NotFoundHttpException(); |
||
| 851 | } |
||
| 852 | |||
| 853 | return $this->showFile($request, $resourceNode, $mode, $glide,$filter);*/ |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @Route("/{tool}/{type}/{id}/download", methods={"GET"}, name="chamilo_core_resource_download") |
||
| 858 | */ |
||
| 859 | public function downloadAction(Request $request) |
||
| 860 | { |
||
| 861 | $resourceNodeId = (int) $request->get('id'); |
||
| 862 | $courseNode = $this->getCourse()->getResourceNode(); |
||
| 863 | |||
| 864 | $repo = $this->getRepositoryFromRequest($request); |
||
| 865 | |||
| 866 | if (empty($resourceNodeId)) { |
||
| 867 | $resourceNode = $courseNode; |
||
| 868 | } else { |
||
| 869 | $resourceNode = $repo->getResourceNodeRepository()->find($resourceNodeId); |
||
| 870 | } |
||
| 871 | |||
| 872 | if (null === $resourceNode) { |
||
| 873 | throw new NotFoundHttpException(); |
||
| 874 | } |
||
| 875 | |||
| 876 | $this->denyAccessUnlessGranted( |
||
| 877 | ResourceNodeVoter::VIEW, |
||
| 878 | $resourceNode, |
||
| 879 | $this->trans('Unauthorised access to resource') |
||
| 880 | ); |
||
| 881 | |||
| 882 | // If resource node has a file just download it. Don't download the children. |
||
| 883 | if ($resourceNode->hasResourceFile()) { |
||
| 884 | // Redirect to download single file. |
||
| 885 | return $this->showFile($request, $resourceNode, 'download'); |
||
| 886 | } |
||
| 887 | |||
| 888 | $zipName = $resourceNode->getSlug().'.zip'; |
||
| 889 | $rootNodePath = $resourceNode->getPathForDisplay(); |
||
| 890 | $resourceNodeRepo = $repo->getResourceNodeRepository(); |
||
| 891 | |||
| 892 | $criteria = Criteria::create() |
||
| 893 | ->where(Criteria::expr()->neq('resourceFile', null)) // must have a file |
||
| 894 | // ->andWhere(Criteria::expr()->eq('resourceType', $type)) |
||
| 895 | ; |
||
| 896 | |||
| 897 | /** @var ArrayCollection|ResourceNode[] $children */ |
||
| 898 | /** @var QueryBuilder $children */ |
||
| 899 | $qb = $resourceNodeRepo->getChildrenQueryBuilder($resourceNode); |
||
| 900 | $qb->addCriteria($criteria); |
||
| 901 | $children = $qb->getQuery()->getResult(); |
||
| 902 | $count = count($children); |
||
| 903 | if (0 === $count) { |
||
| 904 | $params = $this->getResourceParams($request); |
||
| 905 | $params['id'] = $resourceNodeId; |
||
| 906 | |||
| 907 | $this->addFlash('warning', $this->trans('No files')); |
||
| 908 | |||
| 909 | return $this->redirectToRoute( |
||
| 910 | 'chamilo_core_resource_list', |
||
| 911 | $params |
||
| 912 | ); |
||
| 913 | } |
||
| 914 | |||
| 915 | $response = new StreamedResponse(function () use ($rootNodePath, $zipName, $children, $repo) { |
||
| 916 | // Define suitable options for ZipStream Archive. |
||
| 917 | $options = new Archive(); |
||
| 918 | $options->setContentType('application/octet-stream'); |
||
| 919 | //initialise zipstream with output zip filename and options. |
||
| 920 | $zip = new ZipStream($zipName, $options); |
||
| 921 | |||
| 922 | /** @var ResourceNode $node */ |
||
| 923 | foreach ($children as $node) { |
||
| 924 | //$resourceFile = $node->getResourceFile(); |
||
| 925 | //$systemName = $resourceFile->getFile()->getPathname(); |
||
| 926 | $stream = $repo->getResourceNodeFileStream($node); |
||
| 927 | //error_log($node->getPathForDisplay()); |
||
| 928 | $fileToDisplay = str_replace($rootNodePath, '', $node->getPathForDisplay()); |
||
| 929 | $zip->addFileFromStream($fileToDisplay, $stream); |
||
| 930 | } |
||
| 931 | $zip->finish(); |
||
| 932 | }); |
||
| 933 | |||
| 934 | $disposition = $response->headers->makeDisposition( |
||
| 935 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 936 | $zipName //Transliterator::transliterate($zipName) |
||
| 937 | ); |
||
| 938 | $response->headers->set('Content-Disposition', $disposition); |
||
| 939 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
| 940 | |||
| 941 | return $response; |
||
| 942 | } |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Upload form. |
||
| 946 | * |
||
| 947 | * @Route("/{tool}/{type}/{id}/upload", name="chamilo_core_resource_upload", methods={"GET", "POST"}, |
||
| 948 | * options={"expose"=true}) |
||
| 949 | */ |
||
| 950 | public function uploadAction(Request $request, $tool, $type, $id): Response |
||
| 971 | ); |
||
| 972 | } |
||
| 973 | |||
| 974 | public function setBreadCrumb(Request $request) |
||
| 975 | { |
||
| 976 | $tool = $request->get('tool'); |
||
| 977 | $type = $request->get('type'); |
||
| 978 | $resourceNodeId = $request->get('id'); |
||
| 979 | |||
| 980 | $routeParams = $this->getResourceParams($request); |
||
| 981 | |||
| 982 | if (!empty($resourceNodeId)) { |
||
| 983 | $breadcrumb = $this->getBreadCrumb(); |
||
| 984 | $toolParams = $routeParams; |
||
| 985 | $toolParams['id'] = null; |
||
| 986 | |||
| 987 | // Root tool link |
||
| 988 | $breadcrumb->addChild( |
||
| 989 | $this->trans($tool), |
||
| 990 | [ |
||
| 991 | 'uri' => $this->generateUrl( |
||
| 992 | 'chamilo_core_resource_index', |
||
| 993 | $toolParams |
||
| 994 | ), |
||
| 995 | ] |
||
| 996 | ); |
||
| 997 | |||
| 998 | $repo = $this->getRepositoryFromRequest($request); |
||
| 999 | |||
| 1000 | /** @var ResourceInterface $parent */ |
||
| 1001 | $originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]); |
||
| 1002 | if (null === $originalResource) { |
||
| 1003 | return; |
||
| 1004 | } |
||
| 1005 | $parent = $originalParent = $originalResource->getResourceNode(); |
||
| 1006 | |||
| 1007 | $parentList = []; |
||
| 1008 | while (null !== $parent) { |
||
| 1009 | if ($type !== $parent->getResourceType()->getName()) { |
||
| 1010 | break; |
||
| 1011 | } |
||
| 1012 | $parent = $parent->getParent(); |
||
| 1013 | if ($parent) { |
||
| 1014 | $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]); |
||
| 1015 | if ($resource) { |
||
| 1016 | $parentList[] = $resource; |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | $parentList = array_reverse($parentList); |
||
| 1022 | /** @var ResourceInterface $item */ |
||
| 1023 | foreach ($parentList as $item) { |
||
| 1024 | $params = $routeParams; |
||
| 1025 | $params['id'] = $item->getResourceNode()->getId(); |
||
| 1026 | $breadcrumb->addChild( |
||
| 1027 | $item->getResourceName(), |
||
| 1028 | [ |
||
| 1029 | 'uri' => $this->generateUrl( |
||
| 1030 | 'chamilo_core_resource_list', |
||
| 1031 | $params |
||
| 1032 | ), |
||
| 1033 | ] |
||
| 1034 | ); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | $params = $routeParams; |
||
| 1038 | $params['id'] = $originalParent->getId(); |
||
| 1039 | |||
| 1040 | $breadcrumb->addChild( |
||
| 1041 | $originalResource->getResourceName(), |
||
| 1042 | [ |
||
| 1043 | 'uri' => $this->generateUrl( |
||
| 1044 | 'chamilo_core_resource_list', |
||
| 1045 | $params |
||
| 1046 | ), |
||
| 1047 | ] |
||
| 1048 | ); |
||
| 1049 | } |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | private function getParentResourceNode(Request $request): ResourceNode |
||
| 1053 | { |
||
| 1054 | $parentNodeId = $request->get('id'); |
||
| 1055 | |||
| 1056 | $parentResourceNode = null; |
||
| 1057 | |||
| 1058 | if (empty($parentNodeId)) { |
||
| 1059 | if ($this->hasCourse()) { |
||
| 1060 | $parentResourceNode = $this->getCourse()->getResourceNode(); |
||
| 1061 | } else { |
||
| 1062 | if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
||
| 1063 | /** @var User $user */ |
||
| 1064 | $parentResourceNode = $this->getUser()->getResourceNode(); |
||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | } else { |
||
| 1068 | $repo = $this->getDoctrine()->getRepository('ChamiloCoreBundle:Resource\ResourceNode'); |
||
| 1069 | $parentResourceNode = $repo->find($parentNodeId); |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | if (null === $parentResourceNode) { |
||
| 1073 | throw new AccessDeniedException(); |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | return $parentResourceNode; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * @param string $mode |
||
| 1081 | * @param string $filter |
||
| 1082 | * |
||
| 1083 | * @return mixed|StreamedResponse |
||
| 1084 | */ |
||
| 1085 | private function showFile(Request $request, ResourceNode $resourceNode, $mode = 'show', $filter = '') |
||
| 1151 | } |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * @param string $fileType |
||
| 1155 | * |
||
| 1156 | * @return RedirectResponse|Response |
||
| 1157 | */ |
||
| 1158 | private function createResource(Request $request, $fileType = 'file') |
||
| 1314 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.