phpvms/app/Filament/Imports/AirportImporter.php
Arthur Parienté acd38c9fa8
[8.x] feat: Filament Import Export (#2136)
* add migrations

* add setting to choose which importers to use

* bump filament

* enable database notifications

* add AirportImporter and AirportExporter

* move the untrash code to beforeUpdate

* fix validation rules

* add AircraftImporter and AircraftExporter

* fix phpstan

* add ExpenseImporter and ExpenseExporter

* add FareImporter and FareExporter

* add FlightImporter and FlightExporter

* add SubfleetImporter and SubfleetExporter

* change how relationships are processed in FlightImporter

* restore records if they are trashed after import

* phpstan and types

* fix packages

* remove foreign keys

* apply some of the fixes suggested by CodeRabbit

* fix docstring

* re add the fk

* bump filament
2026-01-09 16:10:53 -06:00

96 lines
3.2 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\Airport;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Illuminate\Support\Number;
use Illuminate\Validation\Rule;
use League\ISO3166\ISO3166;
/**
* @property Airport $record
*/
class AirportImporter extends Importer
{
protected static ?string $model = Airport::class;
public static function getColumns(): array
{
return [
ImportColumn::make('iata'),
ImportColumn::make('icao')
->fillRecordUsing(function (Airport $record, string $state): void {
$record->id = $state;
$record->icao = $state;
})
->requiredMapping()
->rules(['required', 'max:4']),
ImportColumn::make('name')
->requiredMapping()
->rules(['required']),
ImportColumn::make('location'),
ImportColumn::make('region'),
ImportColumn::make('country')
->fillRecordUsing(function (Airport $record, string $state): void {
$record->country = strtolower($state);
})
->rules([Rule::in(array_column((new ISO3166())->all(), 'alpha2'))]),
ImportColumn::make('timezone'),
ImportColumn::make('hub')
->requiredMapping()
->boolean()
->rules(['required', 'boolean']),
ImportColumn::make('notes'),
ImportColumn::make('lat')
->numeric(decimalPlaces: 5)
->rules(['numeric']),
ImportColumn::make('lon')
->numeric(decimalPlaces: 5)
->rules(['numeric']),
ImportColumn::make('elevation')
->integer()
->rules(['integer', 'nullable']),
ImportColumn::make('ground_handling_cost')
->numeric()
->rules(['numeric', 'nullable']),
ImportColumn::make('fuel_100ll_cost')
->numeric()
->rules(['numeric', 'nullable']),
ImportColumn::make('fuel_jeta_cost')
->numeric()
->rules(['numeric', 'nullable']),
ImportColumn::make('fuel_mogas_cost')
->numeric()
->rules(['numeric', 'nullable']),
];
}
public function resolveRecord(): Airport
{
return Airport::withTrashed()->firstOrNew([
'id' => $this->data['icao'],
]);
}
protected function beforeUpdate(): void
{
if ($this->record->trashed()) {
$this->record->restore();
}
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your airport import has completed and '.Number::format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
if (($failedRowsCount = $import->getFailedRowsCount()) !== 0) {
$body .= ' '.Number::format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
}
return $body;
}
}