Skip to content

Commit

Permalink
Merge pull request #954 from SlovakNationalGallery/MG-59
Browse files Browse the repository at this point in the history
[import] natural sort jp2 images
  • Loading branch information
rastislav-chynoransky authored Feb 21, 2024
2 parents 2c3bfac + 2588732 commit b542034
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
28 changes: 17 additions & 11 deletions app/Importers/AbstractImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,22 @@ protected function importSingle(array $record, ImportRecord $import_record): ?It
}
}

$this->getJp2Files($import_record, $image_filename_format)
->filter(
fn(SplFileInfo $jp2File) => !$item
->images()
->where('iipimg_url', $jp2File)
->first()
)
->each(function (SplFileInfo $jp2File) use ($item, $import_record) {
$item->images()->create(['iipimg_url' => $jp2File]);
$import_record->imported_iip++;
$jp2Files = $this->getJp2Files($import_record, $image_filename_format);
$jp2Files
->each(function (SplFileInfo $jp2File, int $index) use ($item, $import_record) {
if ($image = $item->images()->where('iipimg_url', $jp2File)->first()) {
$image->update(['order_column' => $index]);
} else {
$item->images()->create([
'iipimg_url' => $jp2File,
'order_column' => $index,
]);
}
});
$item
->images()
->whereNotIn('iipimg_url', $jp2Files)
->delete();

$ids = $this->authorityMatcher
->matchAll($item)
Expand Down Expand Up @@ -259,6 +264,7 @@ protected function getJp2Files(
sprintf('#^%s\.jp2$#', $image_filename_format),
$file->getBasename()
)
);
)
->sort(SORT_NATURAL);
}
}
4 changes: 3 additions & 1 deletion app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ public function getTranslations() {

public function images()
{
return $this->hasMany(ItemImage::class)->orderBy('iipimg_url');
return $this->hasMany(ItemImage::class)
->orderBy('order_column')
->orderBy('iipimg_url');
}

public function getImages() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('item_images', function (Blueprint $table) {
$table->integer('order_column')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('item_images', function (Blueprint $table) {
$table->dropColumn('order_column');
});
}
};

0 comments on commit b542034

Please sign in to comment.