Add renameFile modal

This commit is contained in:
Giuseppe Criscione 2023-07-30 17:32:20 +02:00
parent c5daf47c59
commit 2e2481469b
13 changed files with 124 additions and 33 deletions

View File

@ -8,6 +8,7 @@ use Formwork\Fields\FieldCollection;
use Formwork\Http\Files\UploadedFile;
use Formwork\Http\JsonResponse;
use Formwork\Http\RedirectResponse;
use Formwork\Http\Request;
use Formwork\Http\RequestData;
use Formwork\Http\RequestMethod;
use Formwork\Http\Response;
@ -187,6 +188,8 @@ class PagesController extends AbstractController
$this->modal('deleteFile');
$this->modal('renameFile');
return new Response($this->view('pages.editor', [
'title' => $this->translate('panel.pages.editPage', $page->title()),
'page' => $page,
@ -296,7 +299,7 @@ class PagesController extends AbstractController
$page = $this->site()->findPage($params->get('page'));
if ($page === null) {
$this->panel()->notify($this->translate('panel.pages.page.cannotUploadFile.cannotUploadFound'), 'error');
$this->panel()->notify($this->translate('panel.pages.page.cannotUploadFile.pageNotFound'), 'error');
return $this->redirectToReferer(default: '/pages/');
}
@ -324,12 +327,12 @@ class PagesController extends AbstractController
$page = $this->site()->findPage($params->get('page'));
if ($page === null) {
$this->panel()->notify($this->translate('panel.pages.page.cannotDeleteFile.cannotDeleteFound'), 'error');
$this->panel()->notify($this->translate('panel.pages.page.cannotDeleteFile.pageNotFound'), 'error');
return $this->redirectToReferer(default: '/pages/');
}
if (!$page->files()->has($params->get('filename'))) {
$this->panel()->notify($this->translate('panel.pages.page.cannotDeleteFile.cannotDeleteFound'), 'error');
$this->panel()->notify($this->translate('panel.pages.page.cannotDeleteFile.fileNotFound'), 'error');
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $params->get('page')]));
}
@ -340,6 +343,45 @@ class PagesController extends AbstractController
}
/**
* Pages@renameFile action
*/
public function renameFile(RouteParams $params, Request $request): RedirectResponse
{
$this->ensurePermission('pages.renameFiles');
$page = $this->site()->findPage($params->get('page'));
if ($page === null) {
$this->panel()->notify($this->translate('panel.pages.page.cannotRenameeFile.pageNotFound'), 'error');
return $this->redirectToReferer(default: '/pages/');
}
if (!$page->files()->has($params->get('filename'))) {
$this->panel()->notify($this->translate('panel.pages.page.cannotRenameFile.fileNotFound'), 'error');
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $params->get('page')]));
}
$name = Str::slug(FileSystem::name($request->input()->get('filename')));
$extension = FileSystem::extension($params->get('filename'));
$newName = $name . '.' . $extension;
$previousName = $params->get('filename');
if ($newName !== $previousName) {
if ($page->files()->has($newName)) {
$this->panel()->notify($this->translate('panel.pages.page.cannotRenameFile.fileAlreadyExists'), 'error');
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $params->get('page')]));
}
FileSystem::move($page->path() . $previousName, $page->path() . $newName);
$this->panel()->notify($this->translate('panel.pages.page.fileRenamed'), 'success');
}
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $params->get('page')]));
}
/**
* Create a new page
*/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -122,6 +122,12 @@ return [
'methods' => ['POST'],
],
'panel.pages.renameFile' => [
'path' => '/pages/{page}/file/{filename}/rename/',
'action' => 'Formwork\Panel\Controllers\PagesController@renameFile',
'methods' => ['POST'],
],
'panel.pages.delete' => [
'path' => '/pages/{page}/delete/',
'action' => 'Formwork\Panel\Controllers\PagesController@delete',

View File

@ -6,7 +6,6 @@ import Utils from './utils';
export default {
init: function () {
const commandExpandAllPages = $('[data-command=expand-all-pages]');
const commandCollapseAllPages = $('[data-command=collapse-all-pages]');
const commandReorderPages = $('[data-command=reorder-pages]');
@ -167,6 +166,15 @@ export default {
});
}
$$(['[data-modal=renameFileModal]']).forEach((element) => {
element.addEventListener('click', () => {
const modal = document.getElementById('renameFileModal');
const input = $('#file-name', modal);
input.value = element.getAttribute('data-filename');
input.setSelectionRange(0, input.value.lastIndexOf('.'));
});
});
function expandAllPages() {
$$('.pages-item').forEach((element) => {
element.classList.add('is-expanded');

View File

@ -51,6 +51,10 @@
background-color: $color-accent-500;
color: $color-white;
}
& .icon {
display: inline-block;
margin-right: 0.25rem;
}
}
.dropdown-separator {

View File

@ -94,6 +94,8 @@
}
.is-thumbnails .dropdown-button {
margin-top: 0.25rem;
margin-right: 0.25rem;
background-color: $image-picker-thumbnail-caption-background-color;
&:hover, &:focus {
background-color: $color-base-500;

View File

@ -57,11 +57,11 @@
}
.notification-warning {
background-color: $color-warning-400;
background-color: $color-warning-500;
color: $color-white;
}
.notification-error {
background-color: $color-error-400;
background-color: $color-error-500;
color: $color-white;
}

View File

@ -39,6 +39,7 @@ panel.manage: Manage
panel.modal.action.cancel: Cancel
panel.modal.action.continue: Continue
panel.modal.action.delete: Delete
panel.modal.action.rename: Rename
panel.modal.action.save: Save
panel.modal.action.uploadFile: Upload a File
panel.modal.images.noImages: There are no images here
@ -61,8 +62,8 @@ panel.options.site.info.language: Language
panel.options.site.info.title: Title
panel.options.site.maintenance: Maintenance
panel.options.site.maintenance.enabled: Maintenance Mode
panel.options.site.maintenance.enabled.enabled: Enabled
panel.options.site.maintenance.enabled.disabled: Disabled
panel.options.site.maintenance.enabled.enabled: Enabled
panel.options.site.maintenance.page: Maintenance Page
panel.options.system: System
panel.options.system.adminPanel: Administration Panel
@ -95,9 +96,6 @@ panel.options.system.images.jpegSaveProgressive: Save JPEG images as progressive
panel.options.system.images.jpegSaveProgressive.disabled: Disabled
panel.options.system.images.jpegSaveProgressive.enabled: Enabled
panel.options.system.images.pngCompressionLevel: PNG Compression Level
panel.options.system.uploads.processImages: Process (optimize) uploaded images
panel.options.system.uploads.processImages.disabled: Disabled
panel.options.system.uploads.processImages.enabled: Enabled
panel.options.system.images.webpQuality: WebP Quality
panel.options.system.languages: Languages
panel.options.system.languages.availableLanguages: Available Languages
@ -105,6 +103,9 @@ panel.options.system.languages.availableLanguages.noLanguages: No Languages
panel.options.system.languages.preferredLanguage: Use Browser Prefered Language
panel.options.system.languages.preferredLanguage.disabled: Disabled
panel.options.system.languages.preferredLanguage.enabled: Enabled
panel.options.system.uploads.processImages: Process (optimize) uploaded images
panel.options.system.uploads.processImages.disabled: Disabled
panel.options.system.uploads.processImages.enabled: Enabled
panel.options.updated: Options updated
panel.options.updates: Updates
panel.pages.attributes: Attributes
@ -165,11 +166,15 @@ panel.pages.page.cannotEdit.invalidTemplate: Cannot edit page, invalid template
panel.pages.page.cannotEdit.pageNotFound: Cannot edit page, page not found
panel.pages.page.cannotEdit.varMissing: Cannot edit page, missing a variable
panel.pages.page.cannotMove: Cannot move page
panel.pages.page.cannotRenameFile.fileAlreadyExists: Cannot rename file, a file with the same name already exists
panel.pages.page.cannotRenameFile.fileNotFound: Cannot rename file, file not found
panel.pages.page.cannotRenameFile.pageNotFound: Cannot rename file, page not found
panel.pages.page.cannotUploadFile.pageNotFound: Cannot upload file, page not found
panel.pages.page.created: Page created!
panel.pages.page.deleted: Page deleted
panel.pages.page.edited: Page edited
panel.pages.page.fileDeleted: File deleted
panel.pages.page.fileRenamed: File renamed
panel.pages.page.image: Image
panel.pages.page.lastModified: Last Modified
panel.pages.page.listed: Visible in the menu
@ -192,6 +197,8 @@ panel.pages.pages.search: Search Pages...
panel.pages.parent: Parent Page
panel.pages.preview: Preview
panel.pages.previewFile: Preview
panel.pages.renameFile: Rename File
panel.pages.renameFile.name: Name
panel.pages.save: Save
panel.pages.status.notPublished: Not Published
panel.pages.status.notRoutable: Not Routable
@ -234,14 +241,14 @@ panel.uploader.error.phpExtension: File upload stopped by extension
panel.uploader.error.size: The uploaded file exceeds the maximum file size
panel.uploader.uploaded: File uploaded
panel.user.actions: Actions
panel.user.image: Image
panel.user.image.uploaded: User image uploaded
panel.user.colorScheme: Color Scheme
panel.user.colorScheme.auto: Auto
panel.user.colorScheme.dark: Dark
panel.user.colorScheme.light: Light
panel.user.email: Email
panel.user.fullname: Full Name
panel.user.image: Image
panel.user.image.uploaded: User image uploaded
panel.user.language: Language
panel.user.lastAccess: Last access
panel.user.password: Password

View File

@ -39,6 +39,7 @@ panel.manage: Gestione
panel.modal.action.cancel: Annulla
panel.modal.action.continue: Continua
panel.modal.action.delete: Elimina
panel.modal.action.rename: Rinomina
panel.modal.action.save: Salva
panel.modal.action.uploadFile: Carica file
panel.modal.images.noImages: Qui non ci sono immagini
@ -61,8 +62,8 @@ panel.options.site.info.language: Lingua
panel.options.site.info.title: Titolo
panel.options.site.maintenance: Manutenzione
panel.options.site.maintenance.enabled: Modalità manutenzione
panel.options.site.maintenance.enabled.enabled: Attivata
panel.options.site.maintenance.enabled.disabled: Disattivata
panel.options.site.maintenance.enabled.enabled: Attivata
panel.options.site.maintenance.page: Pagina manutenzione
panel.options.system: Sistema
panel.options.system.adminPanel: Pannello di amministrazione
@ -95,9 +96,6 @@ panel.options.system.images.jpegSaveProgressive: Salva le immagini JPEG come pro
panel.options.system.images.jpegSaveProgressive.disabled: Disabilitato
panel.options.system.images.jpegSaveProgressive.enabled: Abilitato
panel.options.system.images.pngCompressionLevel: Livello di compressione PNG
panel.options.system.uploads.processImages: Elabora (ottimizza) le immagini caricate
panel.options.system.uploads.processImages.disabled: Disabilitato
panel.options.system.uploads.processImages.enabled: Abilitato
panel.options.system.images.webpQuality: Qualità WebP
panel.options.system.languages: Lingue
panel.options.system.languages.availableLanguages: Lingue disponibili
@ -105,6 +103,9 @@ panel.options.system.languages.availableLanguages.noLanguages: Nessuna lingua
panel.options.system.languages.preferredLanguage: Usa lingua preferita dal browser
panel.options.system.languages.preferredLanguage.disabled: Disabilitato
panel.options.system.languages.preferredLanguage.enabled: Abilitato
panel.options.system.uploads.processImages: Elabora (ottimizza) le immagini caricate
panel.options.system.uploads.processImages.disabled: Disabilitato
panel.options.system.uploads.processImages.enabled: Abilitato
panel.options.updated: Opzioni aggiornate
panel.options.updates: Aggiornamenti
panel.pages.attributes: Attributi
@ -165,11 +166,15 @@ panel.pages.page.cannotEdit.invalidTemplate: Impossibile modificare la pagina, t
panel.pages.page.cannotEdit.pageNotFound: Impossibile modificare la pagina, pagina non trovata
panel.pages.page.cannotEdit.varMissing: Impossibile modificare la pagina, manca una variabile
panel.pages.page.cannotMove: Impossibile spostare la pagina
panel.pages.page.cannotRenameFile.fileAlreadyExists: Impossibile rinominare il file, un file con lo stesso nome esiste già
panel.pages.page.cannotRenameFile.fileNotFound: Impossibile rinominare il file, file non trovato
panel.pages.page.cannotRenameFile.pageNotFound: Impossibile rinominare il file, pagina non trovata
panel.pages.page.cannotUploadFile.pageNotFound: Impossibile caricare il file, pagina non trovata
panel.pages.page.created: Pagina creata!
panel.pages.page.deleted: Pagina eliminata
panel.pages.page.edited: Pagina modificata!
panel.pages.page.fileDeleted: File eliminato
panel.pages.page.fileRenamed: File rinominato
panel.pages.page.image: Immagine
panel.pages.page.lastModified: Ultime modifiche
panel.pages.page.listed: Visibile nel menu
@ -192,6 +197,8 @@ panel.pages.pages.search: Cerca pagine...
panel.pages.parent: Pagina superiore
panel.pages.preview: Anteprima
panel.pages.previewFile: Anteprima
panel.pages.renameFile: Rinomina file
panel.pages.renameFile.name: Nome
panel.pages.save: Salva
panel.pages.status.notPublished: Non pubblicato
panel.pages.status.notRoutable: Non raggiungibile
@ -234,14 +241,14 @@ panel.uploader.error.phpExtension: Il caricamento è stato interrotto da unes
panel.uploader.error.size: Il file caricato supera la dimensione massima consentita
panel.uploader.uploaded: File caricato
panel.user.actions: Azioni
panel.user.image: Immagine
panel.user.image.uploaded: Immagine caricata
panel.user.colorScheme: Combinazione di colori
panel.user.colorScheme.auto: Automatica
panel.user.colorScheme.dark: Scura
panel.user.colorScheme.light: Chiara
panel.user.email: E-mail
panel.user.fullname: Nome completo
panel.user.image: Immagine
panel.user.image.uploaded: Immagine caricata
panel.user.language: Lingua
panel.user.lastAccess: Ultimo accesso
panel.user.password: Password

View File

@ -18,6 +18,9 @@
<button type="button" class="button-link dropdown-button" title="<?= $this->translate('panel.files.actions') ?>" data-dropdown="dropdown-<?= $file->hash() ?>"><?= $this->icon('ellipsis-v') ?></button>
<div class="dropdown-menu" id="dropdown-<?= $file->hash() ?>">
<a class="dropdown-item" href="<?= $page->uri($file->name(), includeLanguage: false) ?>" target="formwork-preview-file-<?= $file->hash() ?>"><?= $this->icon('eye') ?> <?= $this->translate('panel.pages.previewFile') ?></a>
<?php if ($panel->user()->permissions()->has('pages.renameFiles')): ?>
<a class="dropdown-item" data-modal="renameFileModal" data-modal-action="<?= $panel->uri('/pages/' . trim($page->route(), '/') . '/file/' . $file->name() . '/rename/') ?>" data-filename="<?= $file->name() ?>"><?= $this->icon('pencil') ?> <?= $this->translate('panel.pages.renameFile') ?></a>
<?php endif ?>
<?php if ($panel->user()->permissions()->has('pages.deleteFiles')): ?>
<a class="dropdown-item" data-modal="deleteFileModal" data-modal-action="<?= $panel->uri('/pages/' . trim($page->route(), '/') . '/file/' . $file->name() . '/delete/') ?>"><?= $this->icon('trash') ?> <?= $this->translate('panel.pages.deleteFile') ?></a>
<?php endif ?>

View File

@ -0,0 +1,12 @@
<div id="renameFileModal" class="modal">
<div class="modal-content">
<form action="" method="post">
<h3 class="caption"><?= $this->translate('panel.pages.renameFile') ?></h3>
<label class="label-required" for="filename"><?= $this->translate('panel.pages.renameFile.name')?>:</label>
<input id="file-name" type="text" required name="filename" autofocus>
<input type="hidden" name="csrf-token" value="<?= $csrfToken ?>">
<button type="button" data-dismiss="renameFileModal"><?= $this->icon('times-circle') ?> <?= $this->translate('panel.modal.action.cancel') ?></button>
<button type="submit" class="button-accent button-right" data-command="delete"><?= $this->icon('pencil') ?> <?= $this->translate('panel.modal.action.rename') ?></button>
</form>
</div>
</div>