-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportAssets.php
77 lines (67 loc) · 2.34 KB
/
ImportAssets.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
<?php
namespace App\Console\Commands;
use Airtable;
use App\Models\Item;
use App\Models\ItemAsset;
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 ImportAssets extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:assets {file} {--tif}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import assets from a CSV file';
/**
* 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');
// Read the CSV file using spatie/simple-excel
$rows = SimpleExcelReader::create($filePath)->useDelimiter(';')->getRows();
$rows->each(function (array $row) {
try {
if (Str::endsWith(Arr::get($row, 'Asset-Name'), '.jpg') && !Str::contains(Arr::get($row, 'Asset-Name'), '+')) {
$id = Str::remove('.jpg', Arr::get($row, 'Asset-Name'));
Item::where('id', $id)->update(['asset_id' => Arr::get($row, 'Media Delivery Cloud Asset ID')]);
}
if ($this->option('tif') && Str::endsWith(Arr::get($row, 'Asset-Name'), '.tif') && !Str::contains(Arr::get($row, 'Asset-Name'), '+')) {
$id = Str::remove('.tif', Arr::get($row, 'Asset-Name'));
$item = Item::whereNull('asset_id')->where('id', $id)->first();
if ($item) {
Item::where('id', $id)->update(['asset_id' => Arr::get($row, 'Media Delivery Cloud Asset 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());
}
});
$this->info('Assets imported successfully.');
}
}