/**
  * POST Route annotation because it'll use 'PATCH' by default.
  * php app/console router:debug submit_resource_form for routing informations.
  *
  * @Post("/resources/{resourceType}/parent/{parent}/encoding/{encoding}/submit")
  * @View(serializerGroups={"api_resource_node"})
  * Set $parent to 0 for personal workspace !
  * Encoding should be 'none' by default.
  * The form fields must be like 'file_form[name] file_form[file]' and so on...
  */
 public function submitResourceFormAction($resourceType, $parent = 0, $encoding = 'none')
 {
     /* @todo add security to kick anon out of this method*/
     $user = $this->tokenStorage->getToken()->getUser();
     $parent = (int) $parent;
     //uncomment for debug via oauth
     /*
             if ($user === 'anon.' || $user === null) {
                 $user = $this->container->get('claroline.manager.user_manager')->getUserByUsername('root');
             }*/
     //not strict because it could be a string '0'
     $parent = $parent === 0 ? $this->resourceManager->getWorkspaceRoot($user->getPersonalWorkspace()) : ($parent = $this->resourceManager->getById($parent));
     //be sure we can create resources
     //these lines won't work for oauth
     $collection = new ResourceCollection(array($parent));
     $collection->setAttributes(array('type' => $resourceType));
     if (!$this->authorization->isGranted('CREATE', $collection)) {
         $errors = $collection->getErrors();
         //gotta think about error handling later
     }
     //end of oauth not working
     //maybe init this from the form. I don't know. It could be removed imo.
     $isPublished = true;
     $nodes = [];
     //Handles the resource creation for any type because I'm lazy and it's better like this anyway.
     //@See FileListener for implementation
     $event = $this->dispatcher->dispatch('create_api_' . $resourceType, 'CreateResource', array($parent, $resourceType, $encoding));
     if (count($event->getResources()) > 0) {
         //Foreach is here because when we unzip a resource, we may add a crapton of stuff at one here.
         //It should have been easier if we created a root directory with everything inside.
         foreach ($event->getResources() as $resource) {
             if ($event->getProcess()) {
                 $createdResource = $this->resourceManager->create($resource, $this->resourceManager->getResourceTypeByName($resourceType), $user, $parent->getWorkspace(), $parent, null, array(), $isPublished);
                 $this->dispatcher->dispatch('resource_created_' . $resourceType, 'ResourceCreated', array($createdResource->getResourceNode()));
                 $nodes[] = $createdResource->getResourceNode();
             }
         }
     } else {
         return $event->getErrorFormContent();
     }
     return $nodes;
 }
Example #2
0
 /**
  * @EXT\Route(
  *     "/tinymce/upload/{parent}",
  *     name="claro_file_upload_with_tinymce",
  *     options={"expose"=true}
  * )
  * @EXT\ParamConverter("user", options={"authenticatedUser" = true})
  *
  * Creates a resource from uploaded file.
  *
  * @param integer $parentId the parent id
  *
  * @throws \Exception
  * @return Response
  */
 public function uploadWithTinyMceAction($parent, User $user)
 {
     $parent = $this->get('claroline.manager.resource_manager')->getById($parent);
     $workspace = $parent ? $parent->getWorkspace() : null;
     $collection = new ResourceCollection(array($parent));
     $collection->setAttributes(array('type' => 'file'));
     $this->checkAccess('CREATE', $collection);
     if (!$this->get('security.authorization_checker')->isGranted('CREATE', $collection)) {
         //use different header so we know something went wrong
         $content = $this->get('translator')->trans('resource_creation_denied', array('%path%' => $parent->getPathForDisplay()), 'platform');
         $response = new Response($content, 403);
         $response->headers->add(array('XXX-Claroline' => 'resource-error'));
         return $response;
     }
     //let's create the file !
     $request = $this->getRequest();
     $fileForm = $request->files->get('file_form');
     $file = $fileForm['file'];
     $fileListener = $this->get('claroline.listener.file_listener');
     $ext = strtolower($file->getClientOriginalExtension());
     $mimeType = $this->container->get('claroline.utilities.mime_type_guesser')->guess($ext);
     $file = $fileListener->createFile(new File(), $file, $file->getClientOriginalName(), $mimeType, $workspace);
     $resourceManager = $this->get('claroline.manager.resource_manager');
     if ($workspace) {
         $rights = array();
     } else {
         $rights = array('ROLE_ANONYMOUS' => array('open' => true, 'export' => true, 'create' => array(), 'role' => $this->container->get('claroline.manager.role_manager')->getRoleByName('ROLE_ANONYMOUS')));
     }
     $file = $resourceManager->create($file, $resourceManager->getResourceTypeByName('file'), $user, $workspace, $parent, null, $rights, true);
     $nodesArray[0] = $resourceManager->toArray($file->getResourceNode(), $this->get('security.token_storage')->getToken());
     return new JsonResponse($nodesArray);
 }
 /**
  * @EXT\Route(
  *     "/shortcut/{parent}/create",
  *     name="claro_resource_create_shortcut",
  *     options={"expose"=true}
  * )
  * @EXT\ParamConverter("creator", options={"authenticatedUser" = true})
  * @EXT\ParamConverter(
  *     "nodes",
  *     class="ClarolineCoreBundle:Resource\ResourceNode",
  *     options={"multipleIds" = true}
  * )
  *
  * Creates (one or several) shortcuts.
  * Takes an array of ids to be functionnal (query string: "ids[]=1&ids[]=2" ...).
  *
  * @param ResourceNode $parent  the new parent
  * @param User         $creator the shortcut creator
  * @param array        $nodes   the resources going to be linked
  *
  * @return Response
  */
 public function createShortcutAction(ResourceNode $parent, User $creator, array $nodes)
 {
     $collection = new ResourceCollection([$parent]);
     $collection->setAttributes(['type' => 'resource_shortcut']);
     $this->checkAccess('CREATE', $collection);
     foreach ($nodes as $node) {
         $shortcut = $this->resourceManager->makeShortcut($node, $parent, $creator, new ResourceShortcut());
         $links[] = $this->resourceManager->toArray($shortcut->getResourceNode(), $this->tokenStorage->getToken());
     }
     return new JsonResponse($links);
 }