diff --git a/src/Controller/BrowserController.php b/src/Controller/BrowserController.php index afd47ca..1bfb7ec 100644 --- a/src/Controller/BrowserController.php +++ b/src/Controller/BrowserController.php @@ -21,6 +21,7 @@ use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FileTooBigException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotCreatedException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotDeletedException; +use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotRenamedException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\InvalidMimeTypeException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\InvalidTypeException; use MonsieurBiz\SyliusMediaManagerPlugin\Helper\FileHelperInterface; @@ -178,4 +179,26 @@ public function deleteFileAction( return new JsonResponse(['folder' => '.' === $fileFolder ? '' : $fileFolder]); } + + public function renameFolderAction( + FileHelperInterface $fileHelper, + Request $request, + TranslatorInterface $translator + ): ?Response + { + $newName = (string) $request->request->get('newName', ''); + $path = (string) $request->request->get('path', ''); + $folder = (string) $request->request->get('folder', ''); + $newPath = $path; + + try { + $newPath = $fileHelper->renameFolder($newName, $path, $folder); + } catch (FolderNotRenamedException $e) { + return new JsonResponse([ + 'error' => $translator->trans('monsieurbiz_sylius_media_manager.error.cannot_rename_folder'), + ], Response::HTTP_BAD_REQUEST); + } + + return new JsonResponse(['path' => $newPath]); + } } diff --git a/src/Exception/FolderNotRenamedException.php b/src/Exception/FolderNotRenamedException.php new file mode 100644 index 0000000..f4e0362 --- /dev/null +++ b/src/Exception/FolderNotRenamedException.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusMediaManagerPlugin\Exception; + +use Exception; + +final class FolderNotRenamedException extends Exception +{ + private string $folder; + + public function __construct(string $folder) + { + $this->folder = $folder; + parent::__construct(sprintf('Folder `%s` couldn\'t be renamed', $folder)); + } + + public function getFolder(): string + { + return $this->folder; + } +} diff --git a/src/Helper/FileHelper.php b/src/Helper/FileHelper.php index cc95cc9..dd80f00 100644 --- a/src/Helper/FileHelper.php +++ b/src/Helper/FileHelper.php @@ -21,6 +21,7 @@ use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FileTooBigException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotCreatedException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotDeletedException; +use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FolderNotRenamedException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\InvalidMimeTypeException; use MonsieurBiz\SyliusMediaManagerPlugin\Exception\InvalidTypeException; use MonsieurBiz\SyliusMediaManagerPlugin\Model\File; @@ -292,6 +293,30 @@ public function deleteFile(string $path, ?string $folder = null): string return $parentPath; } + public function renameFolder(string $newFolderName, string $path, ?string $folder = null): string + { + // Append the wanted folder from the root public media if necessary + if (!empty($folder)) { + $this->currentDirectory = $this->mediaDirectory . '/' . $this->cleanPath($folder); + } + + // We remove the last part of the path to get the parent path + $arrayPath = explode('/', $path, -1); + $arrayPath[] = $newFolderName; + $newPath = implode('/', $arrayPath); + + $oldPath = $this->getFullPath($path); + $newFolderName = (string) $this->slugger->slug($newFolderName); + $newFolderName = mb_strtolower($newFolderName, 'UTF-8'); + $newFolderPath = $this->getFullPath($newPath); + + if (!@rename($oldPath, $newFolderPath)) { + throw new FolderNotRenamedException($newFolderName); + } + + return $newPath; + } + /** * Clean path to avoid server intrusions. */ diff --git a/src/Helper/FileHelperInterface.php b/src/Helper/FileHelperInterface.php index 7188bc2..2b0a123 100644 --- a/src/Helper/FileHelperInterface.php +++ b/src/Helper/FileHelperInterface.php @@ -94,6 +94,8 @@ public function deleteFolder(string $path, ?string $folder = null): string; public function deleteFile(string $path, ?string $folder = null): string; + public function renameFolder(string $newFolderName, string $path, ?string $folder = null): string; + /** * Clean path to avoid server intrusions. */ diff --git a/src/Resources/config/routes/admin.yaml b/src/Resources/config/routes/admin.yaml index b79a9a3..6f309af 100644 --- a/src/Resources/config/routes/admin.yaml +++ b/src/Resources/config/routes/admin.yaml @@ -40,3 +40,10 @@ monsieurbiz_sylius_media_manager_admin_browser_delete_file: defaults: _controller: 'MonsieurBiz\SyliusMediaManagerPlugin\Controller\BrowserController::deleteFileAction' condition: 'request.headers.get("X-Requested-With") == "XMLHttpRequest"' + +monsieurbiz_sylius_media_manager_admin_browser_rename_folder: + path: browser/renameFolder + methods: [ POST ] + defaults: + _controller: 'MonsieurBiz\SyliusMediaManagerPlugin\Controller\BrowserController::renameFolderAction' + condition: 'request.headers.get("X-Requested-With") == "XMLHttpRequest"' diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 6ffbf47..14eb7e2 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -10,12 +10,16 @@ monsieurbiz_sylius_media_manager: choose_video: Choose a video create_folder: New folder delete_folder: Delete this folder + rename_folder: Rename this folder delete_file: Delete this file create: Create + rename: Rename upload_file_in_this_folder: New file in this folder delete_folder_confirm: Are you sure you want to delete the folder `%path%` ? delete_file_confirm: Are you sure you want to delete the file `%path%` ? delete_file_confirm_bis: If it is used it will no longer be accessible, do you confirm this? + rename_folder_confirm: Are you sure you want to rename the folder `%path%` ? + rename_folder_confirm_bis: If files are in this folder or subfolders they will not be accessible anymore, do you confirm this ? remove_file: Remove file remove_image: Remove image remove_video: Remove video @@ -27,6 +31,7 @@ monsieurbiz_sylius_media_manager: cannot_find_input: Form input cannot be found. cannot_find_type: Cannot find type of file. cannot_upload_file: Cannot upload the file. + cannot_rename_folder: Cannot rename the folder. invalid_type_input: Invalid file type in input. file_not_found: File not found. folder_not_readable: Cannot read the folder '%folder%'. @@ -37,4 +42,4 @@ monsieurbiz_sylius_media_manager: pdf: The chosen file is not a PDF. favicon: The chosen file is not a favicon. file: The chosen file is not a valid file. - max_file_size: The file is bigger than the maxc allowed size of %maxSize%. + max_file_size: The file is bigger than the max allowed size of %maxSize%. diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index aa10492..6a2a099 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -10,12 +10,16 @@ monsieurbiz_sylius_media_manager: choose_video: Choisir une vidéo create_folder: Nouveau dossier delete_folder: Supprimer ce dossier + rename_folder: Renommer le dossier delete_file: Supprimer ce fichier create: Créer + rename: Renommer upload_file_in_this_folder: Nouveau fichier dans ce dossier delete_folder_confirm: Êtes-vous sûr de vouloir supprimer le dossier `%path%` ? delete_file_confirm: Êtes-vous sûr de vouloir supprimer le fichier `%path%` ? delete_file_confirm_bis: S'il est utilisé il ne sera plus accessible, confirmez-vous cela ? + rename_folder_confirm: Êtes-vous sûr de vouloir renommer le dossier `%path%` ? + rename_folder_confirm_bis: Si des fichiers sont contenus dans ce dossier ou ses enfants, ils ne seront plus accessible, confirmez-vous cela ? remove_file: Retirer le fichier remove_image: Retirer l'image remove_video: Retirer la video @@ -27,6 +31,7 @@ monsieurbiz_sylius_media_manager: cannot_find_input: Champ de formulaire introuvable. cannot_find_type: Type de fichier introuvable. cannot_upload_file: Impossible d'ajouter le fichier. + cannot_rename_folder: Impossible de renommer le dossier. invalid_type_input: Type de fichier invalide dans le champ. file_not_found: Fichier introuvable. folder_not_readable: Impossible de lire le dossier '%folder%'. diff --git a/src/Resources/views/Admin/MediaManager/Modal/Content/_newFolder.html.twig b/src/Resources/views/Admin/MediaManager/Modal/Content/_newFolder.html.twig index 94493e6..8aa57fb 100644 --- a/src/Resources/views/Admin/MediaManager/Modal/Content/_newFolder.html.twig +++ b/src/Resources/views/Admin/MediaManager/Modal/Content/_newFolder.html.twig @@ -4,7 +4,7 @@
{# Button to create folder #}
diff --git a/src/Resources/views/Admin/MediaManager/Modal/Content/_renameFolder.html.twig b/src/Resources/views/Admin/MediaManager/Modal/Content/_renameFolder.html.twig new file mode 100644 index 0000000..3ce27c6 --- /dev/null +++ b/src/Resources/views/Admin/MediaManager/Modal/Content/_renameFolder.html.twig @@ -0,0 +1,33 @@ +{% if path is not empty %} +
+
+ +
+
+ {{ 'monsieurbiz_sylius_media_manager.ui.rename_folder' | trans }} +
+ + {# Form to rename folder #} + +
+
+
+{% endif %} diff --git a/src/Resources/views/Admin/MediaManager/Modal/_content.html.twig b/src/Resources/views/Admin/MediaManager/Modal/_content.html.twig index 7770d94..cfc5b35 100644 --- a/src/Resources/views/Admin/MediaManager/Modal/_content.html.twig +++ b/src/Resources/views/Admin/MediaManager/Modal/_content.html.twig @@ -1,3 +1,4 @@ {% include '@MonsieurBizSyliusMediaManagerPlugin/Admin/MediaManager/Modal/Content/_newFolder.html.twig' %} {% include '@MonsieurBizSyliusMediaManagerPlugin/Admin/MediaManager/Modal/Content/_deleteFolder.html.twig' %} +{% include '@MonsieurBizSyliusMediaManagerPlugin/Admin/MediaManager/Modal/Content/_renameFolder.html.twig' %} {% include '@MonsieurBizSyliusMediaManagerPlugin/Admin/MediaManager/Modal/Content/_files.html.twig' %} diff --git a/src/Resources/views/Admin/MediaManager/app.html.twig b/src/Resources/views/Admin/MediaManager/app.html.twig index f756819..d31ff00 100644 --- a/src/Resources/views/Admin/MediaManager/app.html.twig +++ b/src/Resources/views/Admin/MediaManager/app.html.twig @@ -251,6 +251,35 @@ monsieurbizSyliusMediaManagerAddLoaderModal(); } + function monsieurbizSyliusMediaManagerRenameFolder(folderInput, inputName, folder, path, confirmationMessage, confirmationMessageBis) + { + if (!confirm(confirmationMessage) || !confirm(confirmationMessageBis)) { + return; + } + + let req = new XMLHttpRequest(); + + let data = new FormData(); + data.append('newName', folderInput.value); + data.append('folder', folder); + data.append('path', path); + + req.onload = function(progress) { + let response = JSON.parse(this.responseText); + if (this.status === 200) { + monsieurbizSyliusMediaManagerRemoveLoaderModal(); + monsieurbizSyliusMediaManagerDisplayPath(inputName, folder, response['path']); + } else { + alert(response['error']); + monsieurbizSyliusMediaManagerRemoveLoaderModal(); + } + }; + req.open("post", "{{ path('monsieurbiz_sylius_media_manager_admin_browser_rename_folder') }}", true); + req.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + req.send(data); + monsieurbizSyliusMediaManagerAddLoaderModal(); + } + function monsieurbizSyliusMediaManagerCloseModal() { $('.monsieurbiz-sylius-file-manager__modal').modal('hide'); @@ -281,15 +310,15 @@ overflow-y: auto; } - .monsieurbiz-sylius-file-manager__item:not(.monsieurbiz-sylius-file-manager__item-new-folder) { + .monsieurbiz-sylius-file-manager__item:not(.monsieurbiz-sylius-file-manager__item-new-folder, .monsieurbiz-sylius-file-manager__item-rename-folder) { height: 30px; } - .monsieurbiz-sylius-file-manager__item:not(.monsieurbiz-sylius-file-manager__item-new-folder) { + .monsieurbiz-sylius-file-manager__item:not(.monsieurbiz-sylius-file-manager__item-new-folder, .monsieurbiz-sylius-file-manager__item-rename-folder) { cursor: pointer; } - .monsieurbiz-sylius-file-manager__item:hover:not(.monsieurbiz-sylius-file-manager__item-new-folder) { + .monsieurbiz-sylius-file-manager__item:hover:not(.monsieurbiz-sylius-file-manager__item-new-folder, .monsieurbiz-sylius-file-manager__item-rename-folder) { background-color: #cce2ff; }