From 7999ce9a3637456b0a842fd54ea6f00b6e028b18 Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe Date: Mon, 7 Oct 2024 22:29:38 +0300 Subject: [PATCH] fix importer --- app/Filament/Imports/PointImporter.php | 51 +++++++++++++++++++++++--- app/Models/County.php | 2 + lang/ro/validation.php | 1 + 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/app/Filament/Imports/PointImporter.php b/app/Filament/Imports/PointImporter.php index 494dde03..6c865343 100644 --- a/app/Filament/Imports/PointImporter.php +++ b/app/Filament/Imports/PointImporter.php @@ -19,6 +19,7 @@ use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Toggle; use Filament\Forms\Get; +use Illuminate\Validation\ValidationException; use MatanYadaev\EloquentSpatial\Objects\Point as PointObject; class PointImporter extends Importer @@ -60,7 +61,17 @@ public static function getColumns(): array ->example('București') ->label(__('map_points.county')) ->validationAttribute('county') - ->relationship(name:'county', resolveUsing: 'name') + ->fillRecordUsing(function (Point $record, string $state) { + $city = City::search($state)->where('county', $state)->first(); + if (! $city) { + throw ValidationException::withMessages( + [ + 'county' => __('validation.county_city_exists'), + ] + ); + } + $record->county_id = $city->county_id; + }) ->rules( [ 'required', @@ -68,15 +79,22 @@ public static function getColumns(): array ] ), - ImportColumn::make('city_id') + ImportColumn::make('city') ->label(__('map_points.city')) ->example('Sector 2') ->validationAttribute('city') ->fillRecordUsing(function (Point $record, string $state) { - $cityId = City::search($state)->where('county', $record->county->name)->first()?->id ?? 0; - if ($cityId !== 0) { - $record->city_id = $cityId; + $cityId = City::search($state)->where('county', $record->county->name)->first()?->id; + if (! $cityId) { + throw ValidationException::withMessages( + [ + 'city' => __('validation.exists', [ + 'attribute' => __('map_points.city'), + ]), + ] + ); } + $record->city_id = $cityId; }) ->requiredMapping() ->rules( @@ -264,4 +282,27 @@ public function afterValidate(): void $this->import->save(); } } + + public function getValidationAttributes(): array + { + return [ + 'latitude' => __('map_points.fields.latitude'), + 'longitude' => __('map_points.fields.longitude'), + 'pointType' => __('map_points.fields.point_type'), + 'county' => __('map_points.county'), + 'city' => __('map_points.city'), + 'address' => __('map_points.fields.address'), + 'notes' => __('map_points.fields.notes'), + 'observations' => __('map_points.fields.observations'), + 'administered_by' => __('map_points.fields.administered_by'), + 'business_name' => __('map_points.fields.business_name'), + 'offers_money' => __('map_points.fields.offers_money'), + 'offers_transport' => __('map_points.fields.offers_transport'), + 'schedule' => __('map_points.fields.schedule'), + 'email' => __('map_points.fields.email'), + 'website' => __('map_points.fields.website'), + 'materials' => __('map_points.fields.materials'), + + ]; + } } diff --git a/app/Models/County.php b/app/Models/County.php index 49d2934e..edaf6829 100644 --- a/app/Models/County.php +++ b/app/Models/County.php @@ -6,9 +6,11 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Laravel\Scout\Searchable; class County extends Model { + use Searchable; public $timestamps = false; protected $fillable = [ diff --git a/lang/ro/validation.php b/lang/ro/validation.php index d00f7019..de14f918 100644 --- a/lang/ro/validation.php +++ b/lang/ro/validation.php @@ -42,6 +42,7 @@ 'ends_with' => 'Câmpul :attribute trebuie să se încheie cu una din următoarele valori: :values', 'enum' => 'Câmpul :attribute selectat nu este valid.', 'exists' => 'Câmpul :attribute selectat nu este valid.', + 'county_city_exists' => 'Nu am putut găsi județul sau localitatea introduse.', 'file' => 'Câmpul :attribute trebuie să fie un fișier.', 'filled' => 'Câmpul :attribute trebuie completat.', 'gt' => [