-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportItems.php
95 lines (87 loc) · 3.06 KB
/
ImportItems.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace App\Console\Commands;
use Airtable;
use App\Models\Item;
use Illuminate\Support\Facades\File;
use App\Models\Exhibition;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\QueryException;
use Spatie\SimpleExcel\SimpleExcelReader;
class ImportItems extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:items {file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import items from a CSV file or folder containing CSV files';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$filePath = $this->argument('file');
if (File::isDirectory($filePath)) {
$csvFiles = glob($filePath . '/*.csv');
foreach ($csvFiles as $csvFile) {
$this->importFile($csvFile);
}
} else {
$this->importFile($filePath);
}
$this->info('Items imported successfully.');
}
private function importFile($filePath)
{
$filename = pathinfo($filePath, PATHINFO_FILENAME);
$this->info("Importing data from: $filename");
$rows = SimpleExcelReader::create($filePath)->useDelimiter(';')->getRows();
$rows->each(function (array $row) {
try {
Item::upsert([
'id' => Arr::get($row, 'Inventarnummer'),
'object' => Arr::get($row, 'Objektbezeichnung'),
'title' => Arr::get($row, 'Titel'),
'author' => Arr::get($row, 'Beteiligte Person(en) / Institution(en)'),
// 'description' => Arr::g$row, et(''),
'material' => Arr::get($row, 'Material'),
'technique' => Arr::get($row, 'Technik'),
'iconography' => Str::limit(Arr::get($row, 'Ikonographie'), 255),
'style' => Arr::get($row, 'Stil'),
// 'image_src' => Arr::g$row, et(''),
// 'web_url' => Arr::g$row, et(''),
'collection' => Arr::get($row, 'Sammlung'),
'year_from' => Arr::get($row, 'Datierung von'),
'year_to' => Arr::get($row, 'Datierung bis'),
'acquisition_date' => Arr::get($row, 'Zugangsdatum'),
], ['id']);
} catch (QueryException $e) {
// Log the exception and continue with the next row
Log::error('Error processing item: ' . Arr::get($row, 'Inventarnummer'));
Log::error('Exception: ' . $e->getMessage());
}
// in the first pass $rowProperties will contain
// ['email' => '[email protected]', 'first_name' => 'john']
});
}
}