Skip to content

Commit

Permalink
[import] Add UPM importer
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Dec 12, 2024
1 parent 9851bdd commit a264ef7
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Enums/FrontendEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum FrontendEnum: string
{
case WEBUMENIA = 'webumenia';
case MORAVSKA_GALERIE = 'moravska-galerie';
case UPM = 'upm';
}
8 changes: 8 additions & 0 deletions app/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
125 changes: 125 additions & 0 deletions app/Importers/UpmImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Importers;

use App\Enums\FrontendEnum;

class UpmImporter extends AbstractImporter
{
protected $mapping = [
'identifier' => '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);
}
}
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 27 additions & 0 deletions database/seeders/ImportsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Seeders;

use App\Import;
use App\Importers\UpmImporter;
use Illuminate\Database\Seeder;

class ImportsTableSeeder extends Seeder
{
public function run()
{
$imports = [
[
'name' => 'UPM',
'dir_path' => 'UPM',
'iip_dir_path' => 'UPM',
'class_name' => UpmImporter::class,
'disk' => 'import_iip',
],
];

foreach ($imports as $import) {
Import::create($import);
}
}
}

0 comments on commit a264ef7

Please sign in to comment.