Skip to content

Commit

Permalink
fix importer
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 7, 2024
1 parent f937f2f commit 7999ce9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
51 changes: 46 additions & 5 deletions app/Filament/Imports/PointImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,23 +61,40 @@ 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',
'string',
]
),

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(
Expand Down Expand Up @@ -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'),

];
}
}
2 changes: 2 additions & 0 deletions app/Models/County.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions lang/ro/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 7999ce9

Please sign in to comment.