From ec9bc5c40c1d09f11783c52b3313ead70e7a97c3 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:42:06 +0200 Subject: [PATCH] Allow file metadata editing from panel --- .../src/Panel/Controllers/PagesController.php | 50 ++++++ panel/routes.php | 2 +- panel/translations/de.yaml | 1 + panel/translations/en.yaml | 1 + panel/translations/es.yaml | 1 + panel/translations/fr.yaml | 1 + panel/translations/it.yaml | 1 + panel/translations/pt.yaml | 1 + panel/translations/ru.yaml | 1 + panel/views/pages/file.php | 165 ++++++++++-------- 10 files changed, 146 insertions(+), 78 deletions(-) diff --git a/formwork/src/Panel/Controllers/PagesController.php b/formwork/src/Panel/Controllers/PagesController.php index 2d1c212d..b68faac7 100644 --- a/formwork/src/Panel/Controllers/PagesController.php +++ b/formwork/src/Panel/Controllers/PagesController.php @@ -5,6 +5,7 @@ namespace Formwork\Panel\Controllers; use Formwork\Exceptions\TranslatedException; use Formwork\Fields\Exceptions\ValidationException; use Formwork\Fields\FieldCollection; +use Formwork\Files\File; use Formwork\Files\FileUploader; use Formwork\Http\Files\UploadedFile; use Formwork\Http\JsonResponse; @@ -509,10 +510,32 @@ class PagesController extends AbstractController $files = $page->files(); $file = $files->get($filename); + + switch ($this->request->method()) { + case RequestMethod::GET: + $data = $file->data(); + + $file->fields()->setValues($data); + + break; + + case RequestMethod::POST: + $data = $this->request->input(); + + $file->fields()->setValues($data)->validate(); + + $this->updateFileMetadata($file, $file->fields()); + + $this->panel()->notify($this->translate('panel.files.metadata.updated'), 'success'); + + return $this->redirect($this->generateRoute('panel.pages.file', ['page' => $page->route(), 'filename' => $filename])); + } + $fileIndex = $files->indexOf($file); $this->modal('renameFile'); $this->modal('deleteFile'); + $this->modal('changes'); return new Response($this->view('pages.file', [ 'title' => $file->name(), @@ -586,6 +609,33 @@ class PagesController extends AbstractController return $this->site()->retrievePage($path); } + protected function updateFileMetadata(File $file, FieldCollection $fieldCollection): void + { + $data = $file->data(); + + $scheme = $file->scheme(); + + $defaults = $scheme->fields()->pluck('default'); + + foreach ($fieldCollection as $field) { + if ($field->isEmpty() || (Arr::has($defaults, $field->name()) && Arr::get($defaults, $field->name()) === $field->value())) { + unset($data[$field->name()]); + continue; + } + + $data[$field->name()] = $field->value(); + } + + $metaFile = $file->path() . $this->config->get('system.files.metadataExtension'); + + if ($data === [] && FileSystem::exists($metaFile)) { + FileSystem::delete($metaFile); + return; + } + + FileSystem::write($metaFile, Yaml::encode($data)); + } + /** * Update a page */ diff --git a/panel/routes.php b/panel/routes.php index d0ff9bc3..6c146e1c 100644 --- a/panel/routes.php +++ b/panel/routes.php @@ -163,7 +163,7 @@ return [ 'panel.pages.file' => [ 'path' => '/pages/{page}/file/{filename}/', 'action' => 'Formwork\Panel\Controllers\PagesController@file', - 'methods' => ['GET'], + 'methods' => ['GET', 'POST'], ], 'panel.pages.delete' => [ diff --git a/panel/translations/de.yaml b/panel/translations/de.yaml index 602585fd..68483231 100644 --- a/panel/translations/de.yaml +++ b/panel/translations/de.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Nicht gefunden panel.files.actions: Aktionen panel.files.metadata: Metadaten panel.files.metadata.alternativeText: Alternativtext +panel.files.metadata.updated: Dateimetadaten aktualisiert panel.files.viewAsList: Als Liste anzeigen panel.files.viewAsThumbnails: Als Miniaturansichten anzeigen panel.login.attempt.failed: Anmeldeversuch fehlgeschlagen! Versuchen Sie es erneut. diff --git a/panel/translations/en.yaml b/panel/translations/en.yaml index 0fff3ad3..31f20bd5 100644 --- a/panel/translations/en.yaml +++ b/panel/translations/en.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Not found panel.files.actions: Actions panel.files.metadata: Metadata panel.files.metadata.alternativeText: Alternative text +panel.files.metadata.updated: File metadata updated panel.files.viewAsList: View as list panel.files.viewAsThumbnails: View as thumbnails panel.login.attempt.failed: Login attempt failed! Try again. diff --git a/panel/translations/es.yaml b/panel/translations/es.yaml index 0eb290cf..2fc5ab2c 100644 --- a/panel/translations/es.yaml +++ b/panel/translations/es.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: No encontrado panel.files.actions: Acciones panel.files.metadata: Metadatos panel.files.metadata.alternativeText: Texto alternativo +panel.files.metadata.updated: Metadatos del archivo actualizados panel.files.viewAsList: Ver como lista panel.files.viewAsThumbnails: Ver como miniaturas panel.login.attempt.failed: ¡Intento de inicio de sesión fallido! Intenta de nuevo. diff --git a/panel/translations/fr.yaml b/panel/translations/fr.yaml index 1de8336c..099e2728 100644 --- a/panel/translations/fr.yaml +++ b/panel/translations/fr.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Pas trouvé panel.files.actions: Actions panel.files.metadata: Métadonnées panel.files.metadata.alternativeText: Texte alternatif +panel.files.metadata.updated: Métadonnées du fichier mises à jour panel.files.viewAsList: Afficher en liste panel.files.viewAsThumbnails: Afficher en vignettes panel.login.attempt.failed: La tentative de connexion a échoué! Réessayer. diff --git a/panel/translations/it.yaml b/panel/translations/it.yaml index d96953ab..695e18ab 100644 --- a/panel/translations/it.yaml +++ b/panel/translations/it.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Non trovato panel.files.actions: Azioni panel.files.metadata: Metadati panel.files.metadata.alternativeText: Testo alternativo +panel.files.metadata.updated: Metadati del file aggiornati panel.files.viewAsList: Visualizza come lista panel.files.viewAsThumbnails: Visualizza come miniature panel.login.attempt.failed: Tentativo di accesso fallito! Riprova. diff --git a/panel/translations/pt.yaml b/panel/translations/pt.yaml index 48ff0a99..09bc8f37 100644 --- a/panel/translations/pt.yaml +++ b/panel/translations/pt.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Não encontrado panel.files.actions: Ações panel.files.metadata: Metadados panel.files.metadata.alternativeText: Texto alternativo +panel.files.metadata.updated: Metadados do ficheiro atualizados panel.files.viewAsList: Ver como lista panel.files.viewAsThumbnails: Ver como miniaturas panel.login.attempt.failed: Falha ao tentar efetuar login! Tente novamente. diff --git a/panel/translations/ru.yaml b/panel/translations/ru.yaml index b6b150b5..ed02c617 100644 --- a/panel/translations/ru.yaml +++ b/panel/translations/ru.yaml @@ -35,6 +35,7 @@ panel.errors.error.notFound.status: Не обнаружена panel.files.actions: Действия panel.files.metadata: Метаданные panel.files.metadata.alternativeText: Альтернативный текст +panel.files.metadata.updated: Метаданные файла обновлены panel.files.viewAsList: Просмотр в виде списка panel.files.viewAsThumbnails: Просмотр в виде миниатюр panel.login.attempt.failed: Войти попытка не удалась! Попробуйте еще раз. diff --git a/panel/views/pages/file.php b/panel/views/pages/file.php index 9c92cda0..5c78ea67 100644 --- a/panel/views/pages/file.php +++ b/panel/views/pages/file.php @@ -1,94 +1,105 @@ layout('panel') ?> -
-
-
icon(is_null($file->type()) ? 'file' : 'file-' . $file->type()) ?> name() ?>
-
icon('arrow-left-circle') ?>translate('panel.pages.file.backToPage') ?>
+
+
+
+
icon(is_null($file->type()) ? 'file' : 'file-' . $file->type()) ?> name() ?>
+ +
+
+ href="uri('/pages/' . trim($page->route(), '/') . '/file/' . ($previousFile->name()) . '/') ?>" title="translate('panel.pages.previousFile') ?>" aria-label="translate('panel.pages.previousFile') ?>">icon('chevron-left') ?> + href="uri('/pages/' . trim($page->route(), '/') . '/file/' . ($nextFile->name()) . '/') ?>" title="translate('panel.pages.nextFile') ?>" aria-label="translate('panel.pages.nextFile') ?>">icon('chevron-right') ?> + user()->permissions()->has('pages.renameFiles')) : ?> + + + user()->permissions()->has('pages.replaceFiles')) : ?> + + + user()->permissions()->has('pages.deleteFiles')) : ?> + + + fields()->isEmpty()): ?> + + +
- href="uri('/pages/' . trim($page->route(), '/') . '/file/' . ($previousFile->name()) . '/') ?>" title="translate('panel.pages.previousFile') ?>" aria-label="translate('panel.pages.previousFile') ?>">icon('chevron-left') ?> - href="uri('/pages/' . trim($page->route(), '/') . '/file/' . ($nextFile->name()) . '/') ?>" title="translate('panel.pages.nextFile') ?>" aria-label="translate('panel.pages.nextFile') ?>">icon('chevron-right') ?> - user()->permissions()->has('pages.renameFiles')) : ?> - + type() === 'image') : ?> +
+
+
+ translate('panel.pages.file.preview') ?> +
+
+ +
+
+
- user()->permissions()->has('pages.replaceFiles')) : ?> - - - user()->permissions()->has('pages.deleteFiles')) : ?> - - -
-type() === 'image') : ?> -
+ type() === 'video') : ?>
translate('panel.pages.file.preview') ?>
- +
-
- -type() === 'video') : ?> +
- translate('panel.pages.file.preview') ?> + translate('panel.pages.file.info') ?>
-
- +
+
+
+
translate('panel.pages.file.info.mimeType') ?>:
+ mimeType() ?> +
+
+
translate('panel.pages.file.info.size') ?>:
+ size() ?> +
+
+
translate('panel.pages.file.info.lastModifiedTime') ?>:
+ datetime($file->lastModifiedTime()) ?> +
+
+
translate('panel.pages.file.info.uri') ?>:
+ uri() ?> +
+ type() === 'image') : ?> + insert('_files/images/info/info', ['file' => $file]) ?> + +
- -
-
- translate('panel.pages.file.info') ?> -
-
-
-
-
translate('panel.pages.file.info.mimeType') ?>:
- mimeType() ?> -
-
-
translate('panel.pages.file.info.size') ?>:
- size() ?> -
-
-
translate('panel.pages.file.info.lastModifiedTime') ?>:
- datetime($file->lastModifiedTime()) ?> -
-
-
translate('panel.pages.file.info.uri') ?>:
- uri() ?> -
- type() === 'image') : ?> - insert('_files/images/info/info', ['file' => $file]) ?> - -
-
-
-type() === 'image') : ?> - hasExifData() && $file->getExifData()->hasPositionData()) : ?> -
-
- - translate('panel.pages.file.position') ?> -
-
- insert('_files/images/position/map', ['exif' => $file->getExifData()]) ?> -
-
+ type() === 'image') : ?> + hasExifData() && $file->getExifData()->hasPositionData()) : ?> +
+
+ + translate('panel.pages.file.position') ?> +
+
+ insert('_files/images/position/map', ['exif' => $file->getExifData()]) ?> +
+
+ + hasExifData()) : ?> + + - hasExifData()) : ?> - + + fields()->isEmpty()): ?> + insert('fields', ['fields' => $file->fields()]) ?> - \ No newline at end of file + \ No newline at end of file