From d3d7f5787a8003b7dbd456c45b2d04aa98e8ecee Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Sat, 29 May 2021 12:59:51 +0200 Subject: [PATCH] Refactor "save documents" code using the Resources --- .../Controller/Api/BaseResourceFileAction.php | 162 ++++++++++++++++++ .../Api/CreateDocumentFileAction.php | 40 +++++ .../Api/CreateResourceNodeFileAction.php | 100 ----------- .../Api/UpdateDocumentFileAction.php | 29 ++++ src/CourseBundle/Entity/CDocument.php | 8 +- 5 files changed, 235 insertions(+), 104 deletions(-) create mode 100644 src/CoreBundle/Controller/Api/BaseResourceFileAction.php create mode 100644 src/CoreBundle/Controller/Api/CreateDocumentFileAction.php delete mode 100644 src/CoreBundle/Controller/Api/CreateResourceNodeFileAction.php create mode 100644 src/CoreBundle/Controller/Api/UpdateDocumentFileAction.php diff --git a/src/CoreBundle/Controller/Api/BaseResourceFileAction.php b/src/CoreBundle/Controller/Api/BaseResourceFileAction.php new file mode 100644 index 0000000000..779c82611a --- /dev/null +++ b/src/CoreBundle/Controller/Api/BaseResourceFileAction.php @@ -0,0 +1,162 @@ +getContent(); + if (!empty($contentData)) { + $contentData = json_decode($contentData, true); + var_dump($contentData); + $title = $contentData['title']; + $comment = $contentData['comment']; + } else { + $title = $request->get('title'); + $comment = $request->get('comment'); + } + + $fileType = 'folder'; + if ($request->request->has('filetype')) { + $fileType = $request->get('filetype'); + } + + if (empty($fileType)) { + throw new \Exception('filetype needed: folder or file'); + } + + $nodeId = (int) $request->get('parentResourceNodeId'); + + if (0 === $nodeId) { + throw new \Exception('parentResourceNodeId int value needed'); + } + + $resource->setParentResourceNode($nodeId); + + switch ($fileType) { + case 'file': + $content = ''; + if ($request->request->has('contentFile')) { + $content = $request->request->get('contentFile'); + } + $fileParsed = false; + // File upload. + if ($request->files->count() > 0) { + if (!$request->files->has('uploadFile')) { + throw new BadRequestHttpException('"uploadFile" is required'); + } + + /** @var UploadedFile $uploadedFile */ + $uploadedFile = $request->files->get('uploadFile'); + $title = $uploadedFile->getClientOriginalName(); + $resource->setUploadFile($uploadedFile); + $fileParsed = true; + } + + // Get data in content and create a HTML file. + if (!$fileParsed && $content) { + $handle = tmpfile(); + fwrite($handle, $content); + $meta = stream_get_meta_data($handle); + $file = new UploadedFile($meta['uri'], $title.'.html', 'text/html', null, true); + $resource->setUploadFile($file); + $fileParsed = true; + } + + if (!$fileParsed) { + throw new InvalidArgumentException('filetype was set to "file" but not upload found'); + } + + break; + case 'folder': + break; + } + + if (empty($title)) { + throw new InvalidArgumentException('title required'); + } + + $resource->setResourceName($title); + } + + protected function handleUpdateRequest(AbstractResource $resource, $repo, Request $request) + { + error_log('handleUpdateRequest'); + $contentData = $request->getContent(); + $resourceLinkList = []; + if (!empty($contentData)) { + error_log('contentData'); + $contentData = json_decode($contentData, true); + $title = $contentData['title']; + $content = $contentData['contentFile']; + //$comment = $contentData['comment'] ?? ''; + $resourceLinkList = $contentData['resourceLinkListFromEntity'] ?? []; + } else { + $title = $request->get('title'); + $content = $request->request->get('contentFile'); + //$comment = $request->request->get('comment'); + } + + $repo->setResourceName($resource, $title); + + $hasFile = $resource->getResourceNode()->hasResourceFile(); + + //if ('file' === $fileType && !empty($content)) { + if ($hasFile && !empty($content)) { + $resourceNode = $resource->getResourceNode(); + if ($resourceNode->hasResourceFile()) { + $resourceNode->setContent($content); + $resourceNode->getResourceFile()->setSize(\strlen($content)); + } + $resourceNode->setUpdatedAt(new DateTime()); + $resourceNode->getResourceFile()->setUpdatedAt(new DateTime()); + $resource->setResourceNode($resourceNode); + } + + $link = null; + if (!empty($resourceLinkList)) { + foreach ($resourceLinkList as $linkArray) { + // Find the exact link. + $linkId = $linkArray['id']; + /** @var ResourceLink $link */ + $link = $resource->getResourceNode()->getResourceLinks() + ->filter( + fn ($link) => $link->getId() === $linkId + )->first(); + + if (null !== $link) { + $link->setVisibility((int) $linkArray['visibility']); + + break; + } + } + } + + //$isRecursive = 'folder' === $fileType; + $isRecursive = !$hasFile; + // If it's a folder then change the visibility to the children (That have the same link). + if ($isRecursive && null !== $link) { + $repo->copyVisibilityToChildren($resource->getResourceNode(), $link); + } + + //$document->setComment($comment); + + error_log('Finish update resource node file action'); + + return $resource; + } +} diff --git a/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php b/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php new file mode 100644 index 0000000000..0f667e0283 --- /dev/null +++ b/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php @@ -0,0 +1,40 @@ +handleCreateRequest($document, $request); + if ($request->request->has('filetype')) { + $document->setFiletype($request->get('filetype')); + } + + if ($request->request->has('resourceLinkList')) { + $links = $request->get('resourceLinkList'); + $links = false === strpos($links, '[') ? json_decode('['.$links.']', true) : json_decode($links, true); + if (empty($links)) { + $message = 'resourceLinkList is not a valid json. Use for example: [{"c_id":1, "visibility":1}]'; + + throw new InvalidArgumentException($message); + } + $document->setResourceLinkArray($links); + } + + //$document->setComment($comment); + + return $document; + } +} diff --git a/src/CoreBundle/Controller/Api/CreateResourceNodeFileAction.php b/src/CoreBundle/Controller/Api/CreateResourceNodeFileAction.php deleted file mode 100644 index bbf29c8af1..0000000000 --- a/src/CoreBundle/Controller/Api/CreateResourceNodeFileAction.php +++ /dev/null @@ -1,100 +0,0 @@ -getContent(); - if (!empty($contentData)) { - $contentData = json_decode($contentData, true); - //error_log(print_r($contentData, 1)); - $title = $contentData['title']; - $comment = $contentData['comment']; - } else { - $title = $request->get('title'); - $comment = $request->get('comment'); - } - - $document = new CDocument(); - if ($request->request->has('filetype')) { - $document->setFiletype($request->get('filetype')); - } - - $nodeId = (int) $request->get('parentResourceNodeId'); - $document->setParentResourceNode($nodeId); - - switch ($document->getFiletype()) { - case 'file': - $content = ''; - if ($request->request->has('contentFile')) { - $content = $request->request->get('contentFile'); - } - $fileParsed = false; - // File upload. - if ($request->files->count() > 0) { - if (!$request->files->has('uploadFile')) { - throw new BadRequestHttpException('"uploadFile" is required'); - } - - /** @var UploadedFile $uploadedFile */ - $uploadedFile = $request->files->get('uploadFile'); - $title = $uploadedFile->getClientOriginalName(); - $document->setUploadFile($uploadedFile); - $fileParsed = true; - } - - // Get data in content and create a HTML file. - if (!$fileParsed && $content) { - $handle = tmpfile(); - fwrite($handle, $content); - $meta = stream_get_meta_data($handle); - $file = new UploadedFile($meta['uri'], $title.'.html', 'text/html', null, true); - $document->setUploadFile($file); - $fileParsed = true; - } - - if (!$fileParsed) { - throw new InvalidArgumentException('filetype was set to "file" but not upload found'); - } - - break; - case 'folder': - break; - } - - if (empty($title)) { - throw new InvalidArgumentException('title required'); - } - - $document->setTitle($title); - - if ($request->request->has('resourceLinkList')) { - $links = $request->get('resourceLinkList'); - $links = false === strpos($links, '[') ? json_decode('['.$links.']', true) : json_decode($links, true); - if (empty($links)) { - $message = 'resourceLinkList is not a valid json. Use for example: [{"c_id":1, "visibility":1}]'; - - throw new InvalidArgumentException($message); - } - $document->setResourceLinkArray($links); - } - - $document->setComment($comment); - - return $document; - } -} diff --git a/src/CoreBundle/Controller/Api/UpdateDocumentFileAction.php b/src/CoreBundle/Controller/Api/UpdateDocumentFileAction.php new file mode 100644 index 0000000000..f508b029ae --- /dev/null +++ b/src/CoreBundle/Controller/Api/UpdateDocumentFileAction.php @@ -0,0 +1,29 @@ +handleUpdateRequest($document, $repo, $request); + + //$document->setComment($comment); + + error_log('Finish update resource node file action'); + + return $document; + } +} diff --git a/src/CourseBundle/Entity/CDocument.php b/src/CourseBundle/Entity/CDocument.php index a0001bcf77..dfb3a749ea 100644 --- a/src/CourseBundle/Entity/CDocument.php +++ b/src/CourseBundle/Entity/CDocument.php @@ -11,8 +11,8 @@ use ApiPlatform\Core\Annotation\ApiResource; use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter; use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter; use ApiPlatform\Core\Serializer\Filter\PropertyFilter; -use Chamilo\CoreBundle\Controller\Api\CreateResourceNodeFileAction; -use Chamilo\CoreBundle\Controller\Api\UpdateResourceNodeFileAction; +use Chamilo\CoreBundle\Controller\Api\UpdateDocumentFileAction; +use Chamilo\CoreBundle\Controller\Api\CreateDocumentFileAction; use Chamilo\CoreBundle\Entity\AbstractResource; use Chamilo\CoreBundle\Entity\ResourceInterface; use Chamilo\CourseBundle\Traits\ShowCourseResourcesInSessionTrait; @@ -27,7 +27,7 @@ use Symfony\Component\Validator\Constraints as Assert; * denormalizationContext={"groups"={"document:write"}}, * itemOperations={ * "put" ={ - * "controller"=UpdateResourceNodeFileAction::class, + * "controller"=UpdateDocumentFileAction::class, * "deserialize"=false, * "security" = "is_granted('EDIT', object.resourceNode)", * "validation_groups"={"media_object_create", "document:write"}, @@ -41,7 +41,7 @@ use Symfony\Component\Validator\Constraints as Assert; * }, * collectionOperations={ * "post"={ - * "controller"=CreateResourceNodeFileAction::class, + * "controller"=CreateDocumentFileAction::class, * "deserialize"=false, * "security"="is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER')", * "validation_groups"={"Default", "media_object_create", "document:write"},