Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename folder feature #18

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
matrix:
php: ['7.4' ,'8.0', '8.1']
sylius: [ "~1.9.0", "~1.10.0", "~1.11.0", "~1.12.0"]
include:
- php: '8.1'
sylius: '~1.12.0'
sylius_paypal: '~1.5.0'
exclude:
- php: 8.1
sylius: "~1.9.0"
Expand Down Expand Up @@ -73,6 +77,13 @@ jobs:
run: |
composer require --no-install --no-scripts --no-progress sylius/sylius="${{ matrix.sylius }}"

# Fix Paypal 1.5 on Sylius 1.12 and PHP 8.1
- name: Make sure to install the required version of Sylius Paypal Plugin
if: ${{ matrix.sylius_paypal }}
working-directory: ./sylius
run: |
composer require --no-install --no-scripts --no-progress sylius/paypal-plugin="${{ matrix.sylius_paypal }}" # @see https://github.com/Sylius/PayPalPlugin/issues/295

- name: Setup some requirements
working-directory: ./sylius
run: |
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
SHELL=/bin/bash
APP_DIR=tests/Application
SYLIUS_VERSION=1.12.0
SYLIUS_PAYPAL_VERSION=1.5.0
SYMFONY=cd ${APP_DIR} && symfony
COMPOSER=symfony composer
CONSOLE=${SYMFONY} console
Expand Down Expand Up @@ -72,6 +73,7 @@ setup_application:
(cd ${APP_DIR} && ${COMPOSER} config --no-plugins allow-plugins true)
(cd ${APP_DIR} && ${COMPOSER} config --no-plugins --json extra.symfony.endpoint '["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master","flex://defaults"]')
(cd ${APP_DIR} && ${COMPOSER} require --no-install --no-scripts --no-progress sylius/sylius="~${SYLIUS_VERSION}") # Make sure to install the required version of sylius because the sylius-standard has a soft constraint
(cd ${APP_DIR} && ${COMPOSER} require --no-install --no-scripts --no-progress sylius/paypal-plugin="~${SYLIUS_PAYPAL_VERSION}") # @see https://github.com/Sylius/PayPalPlugin/issues/295
$(MAKE) ${APP_DIR}/.php-version
$(MAKE) ${APP_DIR}/php.ini
(cd ${APP_DIR} && ${COMPOSER} install --no-interaction)
Expand Down
22 changes: 22 additions & 0 deletions src/Controller/BrowserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,4 +179,25 @@ 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]);
}
}
32 changes: 32 additions & 0 deletions src/Exception/FolderNotRenamedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of Monsieur Biz' Media Manager plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* 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;
}
}
28 changes: 28 additions & 0 deletions src/Helper/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -292,6 +293,33 @@ public function deleteFile(string $path, ?string $folder = null): string
return $parentPath;
}

/**
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
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.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Helper/FileHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
7 changes: 6 additions & 1 deletion src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%'.
Expand All @@ -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%.
5 changes: 5 additions & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 accessibles, confirmez-vous cela ?
remove_file: Retirer le fichier
remove_image: Retirer l'image
remove_video: Retirer la video
Expand All @@ -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%'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="content">
{# Button to create folder #}
<div
class="description monsieurbiz-sylius-file-manager__new-folder-label ui button mini basic"
class="description monsieurbiz-sylius-file-manager__new-folder-label ui button mini basic"
style="max-width: 200px;"
onclick="this.style.display='none';this.parentNode.querySelector('.monsieurbiz-sylius-file-manager__new-folder').style.display='block';this.parentNode.querySelector('.monsieurbiz-sylius-file-manager__new-folder input').focus();"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% if path is not empty %}
<div class="ui list">
<div class="item monsieurbiz-sylius-file-manager__item monsieurbiz-sylius-file-manager__item-rename-folder">
<i class="edit icon"></i>
<div class="content">
<div
class="description monsieurbiz-sylius-file-manager__rename-folder-label ui button mini secondary"
style="max-width: 200px;color: white;"
onclick="this.style.display='none';this.parentNode.querySelector('.monsieurbiz-sylius-file-manager__rename-folder').style.display='block';this.parentNode.querySelector('.monsieurbiz-sylius-file-manager__rename-folder input').focus();"
>
{{ 'monsieurbiz_sylius_media_manager.ui.rename_folder' | trans }}
</div>

{# Form to rename folder #}
<div class="description monsieurbiz-sylius-file-manager__rename-folder ui form mini" style="display: none;">
<input type="text" name="folder" class="ui field" style="max-width: 200px;height:28px;"/>
<div
class="ui button mini"
onclick="monsieurbizSyliusMediaManagerRenameFolder(this.parentNode.querySelector('input'), '{{ inputName | escape('js') }}', '{{ folder | escape('js')}}', '{{ path | escape('js')}}', '{{ 'monsieurbiz_sylius_media_manager.ui.rename_folder_confirm'| trans({'%path%': path}) | escape('js') }}', '{{ 'monsieurbiz_sylius_media_manager.ui.rename_folder_confirm_bis'| trans() }}')"
>
{{ 'monsieurbiz_sylius_media_manager.ui.rename' | trans }}
</div>
<div
class="ui button mini basic"
onclick="this.parentNode.parentNode.querySelector('.monsieurbiz-sylius-file-manager__rename-folder').style.display='none';this.parentNode.parentNode.querySelector('.monsieurbiz-sylius-file-manager__rename-folder-label').style.display='block';"
>
{{ 'monsieurbiz_sylius_media_manager.ui.cancel' | trans }}
</div>
</div>
</div>
</div>
</div>
{% endif %}
Original file line number Diff line number Diff line change
@@ -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' %}
35 changes: 32 additions & 3 deletions src/Resources/views/Admin/MediaManager/app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}

Expand Down
Loading