diff --git a/app/Importers/AbstractImporter.php b/app/Importers/AbstractImporter.php index 0dfd6065c..db07a346b 100644 --- a/app/Importers/AbstractImporter.php +++ b/app/Importers/AbstractImporter.php @@ -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) @@ -259,6 +264,7 @@ protected function getJp2Files( sprintf('#^%s\.jp2$#', $image_filename_format), $file->getBasename() ) - ); + ) + ->sort(SORT_NATURAL); } } diff --git a/app/Item.php b/app/Item.php index 160d10ada..e0d188828 100644 --- a/app/Item.php +++ b/app/Item.php @@ -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() { diff --git a/database/migrations/2024_02_06_134402_add_order_column_to_item_images_table.php b/database/migrations/2024_02_06_134402_add_order_column_to_item_images_table.php new file mode 100644 index 000000000..916337afd --- /dev/null +++ b/database/migrations/2024_02_06_134402_add_order_column_to_item_images_table.php @@ -0,0 +1,28 @@ +integer('order_column')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('item_images', function (Blueprint $table) { + $table->dropColumn('order_column'); + }); + } +};