From a264ef7c70fda4911a78b402258936f4be65dc44 Mon Sep 17 00:00:00 2001 From: Rastislav Chynoransky Date: Wed, 11 Dec 2024 18:57:04 +0700 Subject: [PATCH] [import] Add UPM importer --- app/Enums/FrontendEnum.php | 1 + app/Import.php | 8 ++ app/Importers/UpmImporter.php | 125 ++++++++++++++++++++++++ database/seeders/DatabaseSeeder.php | 1 + database/seeders/ImportsTableSeeder.php | 27 +++++ 5 files changed, 162 insertions(+) create mode 100644 app/Importers/UpmImporter.php create mode 100644 database/seeders/ImportsTableSeeder.php diff --git a/app/Enums/FrontendEnum.php b/app/Enums/FrontendEnum.php index e5e550622..1f92a3ece 100644 --- a/app/Enums/FrontendEnum.php +++ b/app/Enums/FrontendEnum.php @@ -6,4 +6,5 @@ enum FrontendEnum: string { case WEBUMENIA = 'webumenia'; case MORAVSKA_GALERIE = 'moravska-galerie'; + case UPM = 'upm'; } diff --git a/app/Import.php b/app/Import.php index b6c821708..e65ea7080 100644 --- a/app/Import.php +++ b/app/Import.php @@ -30,6 +30,14 @@ class Import extends Model 'completed_at' => 'datetime', ]; + protected $fillable = [ + 'name', + 'dir_path', + 'iip_dir_path', + 'class_name', + 'disk', + ]; + public function user() { return $this->belongsTo(User::class); diff --git a/app/Importers/UpmImporter.php b/app/Importers/UpmImporter.php new file mode 100644 index 000000000..33ee465e5 --- /dev/null +++ b/app/Importers/UpmImporter.php @@ -0,0 +1,125 @@ + 'Inventárníčíslo', + 'title:cs' => 'Název', + 'title:sk' => 'Název', + 'title:en' => 'Název EN', + 'author' => 'Autor', + 'dating:cs' => 'Datace', + 'dating:sk' => 'Datace', + 'date_earliest' => 'Od', + 'date_latest' => 'Do', + 'work_type:cs' => 'Výtvarný druh', + 'work_type:sk' => 'Výtvarný druh', + 'object_type:cs' => 'Typ', + 'object_type:sk' => 'Typ', + 'medium:cs' => 'Materiál', + 'medium:sk' => 'Materiál', + 'technique:cs' => 'Technika', + 'technique:sk' => 'Technika', + 'topic:cs' => 'Námět', + 'topic:sk' => 'Námět', + 'inscription:cs' => 'Značení', + 'inscription:sk' => 'Značení', + 'related_work:sk' => 'Sbírka', + 'related_work:cs' => 'Sbírka', + 'acquisition_date' => 'Datum akvizice', + ]; + + protected $defaults = [ + 'relationship_type:sk' => 'zo súboru', + 'relationship_type:cs' => 'ze souboru', + 'relationship_type:en' => 'collection', + 'author' => 'Neznámý autor', + 'gallery:cs' => 'Uměleckoprůmyslové museum v Praze, UPM', + 'gallery:sk' => 'Umeleckopriemyselné múzeum v Prahe, UPM', + 'frontends' => [ + FrontendEnum::UPM, + FrontendEnum::WEBUMENIA, + ], + ]; + + protected static $options = [ + 'delimiter' => ';', + 'enclosure' => '"', + 'escape' => '\\', + 'newline' => "\n", + ]; + + protected function init() + { + $this->sanitizers[] = function ($value) { + return empty_to_null(trim($value)); + }; + } + + protected function getItemId(array $record) + { + return 'CZE:UPM.' . str($record['ID']) + ->explode('_') + ->transform(fn ($part) => str($part) + ->trim() + ->replaceMatches('/[^\w\d_]/', '-') + ) + ->join('_'); + } + + protected function getItemImageFilenameFormat(array $record): string + { + return str($record['ID']) + ->explode('_') + ->transform(fn ($part) => '0*' . preg_quote($part)) + ->join('_') . '(_.*)?'; + } + + protected function hydratePlace(array $record, string $locale): ?string + { + if (!in_array($locale, ['cs', 'sk'])) { + return null; + } + + $place = str($record['Vznik'])->match('/^([^;]+)/'); + return $place->isNotEmpty() ? $place->toString() : null; + } + + protected function hydrateAdditionals(array $record, string $locale): ?array + { + if ($locale !== 'cs') { + return null; + } + + $additionals = []; + + if ($record['Způsob akvizice'] !== null) { + $additionals['acquisition'] = $record['Způsob akvizice']; + } + + if ($record['Výstava'] !== null) { + $additionals['exhibiton'] = $record['Výstava']; + } + + $producer = str($record['Vznik'])->match('/;(.+)/'); + if ($producer->isNotEmpty()) { + $additionals['producer'] = $producer->toString(); + } + + return $additionals ?: null; + } + + protected function hydrateMeasurement(array $record, $locale): ?string + { + if (empty($record['Rozměry'])) { + return null; + } + + $replacements = trans('item.measurement_replacements', [], $locale); + return strtr($record['Rozměry'], $replacements); + } +} \ No newline at end of file diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 78b93ca72..037ceb127 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -19,6 +19,7 @@ public function run() $this->call(CollectionsTableSeeder::class); $this->call(CategoriesTableSeeder::class); $this->call(ArticlesTableSeeder::class); + $this->call(ImportsTableSeeder::class); // $this->call(SketchbooksTableSeeder::class); } diff --git a/database/seeders/ImportsTableSeeder.php b/database/seeders/ImportsTableSeeder.php new file mode 100644 index 000000000..e3c984061 --- /dev/null +++ b/database/seeders/ImportsTableSeeder.php @@ -0,0 +1,27 @@ + 'UPM', + 'dir_path' => 'UPM', + 'iip_dir_path' => 'UPM', + 'class_name' => UpmImporter::class, + 'disk' => 'import_iip', + ], + ]; + + foreach ($imports as $import) { + Import::create($import); + } + } +} \ No newline at end of file