From 0fac3dfe7c4fe27ee6048f3c263b2e6b48bcc22c Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Wed, 18 Jun 2025 10:25:22 +0200 Subject: [PATCH] fix: :bug: explode offer address location in multiple fields --- app/Jobs/FetchDigitalRecruiterOffers.php | 8 +++- app/Models/Offer.php | 47 +++++++++++++++++-- ...d_offer_location_address_details_table.php | 42 +++++++++++++++++ .../bo/pages/offers/index-list.blade.php | 8 ++-- .../views/bo/pages/offers/show.blade.php | 28 +++++++---- .../views/fo/partials/offer-card.blade.php | 9 ++-- 6 files changed, 120 insertions(+), 22 deletions(-) create mode 100644 database/migrations/2025_06_18_095049_add_offer_location_address_details_table.php diff --git a/app/Jobs/FetchDigitalRecruiterOffers.php b/app/Jobs/FetchDigitalRecruiterOffers.php index 2f386365..7010828a 100644 --- a/app/Jobs/FetchDigitalRecruiterOffers.php +++ b/app/Jobs/FetchDigitalRecruiterOffers.php @@ -67,7 +67,7 @@ class FetchDigitalRecruiterOffers * * @param \Illuminate\Support\Collection $offers * @return void - * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh + * @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded */ private function saveOffers(Collection $offers): void { @@ -76,7 +76,11 @@ class FetchDigitalRecruiterOffers $offers->splice(0, 6)->map(function (object $offer) { $offerModel = new Offer(); $offerModel->title = $offer->title; - $offerModel->location = $offer->address->formatted ?? null; + $offerModel->location_street = $offer->address->parts->street ?? null; + $offerModel->location_zip = $offer->address->parts->zip ?? null; + $offerModel->location_city = $offer->address->parts->city ?? null; + $offerModel->location_state = $offer->address->parts->state ?? null; + $offerModel->location_country = $offer->address->parts->country ?? null; $offerModel->location_details = $offer->entity->around ? strip_tags(clean($offer->entity->around)) : null; diff --git a/app/Models/Offer.php b/app/Models/Offer.php index 42d8dd27..24f3531c 100644 --- a/app/Models/Offer.php +++ b/app/Models/Offer.php @@ -14,7 +14,11 @@ use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait; /** * @property string|null $title Offer title. - * @property string|null $location Offer location. + * @property string|null $location_street Offer location street. + * @property string|null $location_zip Offer location zip. + * @property string|null $location_city Offer location city. + * @property string|null $location_state Offer location state. + * @property string|null $location_country Offer location country. * @property string|null $location_details Offer location details. * @property string|null $location_latitude Offer location latitude. * @property string|null $location_longitude Offer location longitude. @@ -53,7 +57,11 @@ class Offer extends Model implements SchemaOrgInterface */ protected $fillable = [ 'title', - 'location', + 'location_street', + 'location_zip', + 'location_city', + 'location_state', + 'location_country', 'location_details', 'location_latitude', 'location_longitude', @@ -154,6 +162,22 @@ class Offer extends Model implements SchemaOrgInterface }); } + /** + * Get formatted address location. + * + * @return string + */ + public function getFormattedAddressAttribute(): string + { + return trim(sprintf( + '%s %s %s %s', + $this->location_street ?? '', + $this->location_zip ?? '', + $this->location_city ?? '', + $this->location_country ?? '' + )); + } + /** * Create a SchemaOrg. * @@ -201,9 +225,24 @@ class Offer extends Model implements SchemaOrgInterface if (!empty($this->location_latitude)) { $location->latitude($this->location_latitude); } - if (!empty($this->location)) { - $location->address($this->location); + + // Address location. + $fields = [ + 'streetAddress' => $this->location_street, + 'postalCode' => $this->location_zip, + 'addressLocality' => $this->location_city, + 'addressRegion' => $this->location_state, + 'addressCountry' => $this->location_country, + ]; + $hasAddress = array_filter($fields); + if (!empty($hasAddress)) { + $postalAddress = Schema::postalAddress(); + foreach ($hasAddress as $method => $value) { + $postalAddress->$method($value); + } + $location->address($postalAddress); } + if (!empty($this->location_details)) { $location->description($this->location_details); } diff --git a/database/migrations/2025_06_18_095049_add_offer_location_address_details_table.php b/database/migrations/2025_06_18_095049_add_offer_location_address_details_table.php new file mode 100644 index 00000000..58479f04 --- /dev/null +++ b/database/migrations/2025_06_18_095049_add_offer_location_address_details_table.php @@ -0,0 +1,42 @@ +dropColumn('location'); + $table->string('location_street')->nullable()->comment('Offer location street.')->after('title'); + $table->string('location_zip')->nullable()->comment('Offer location zip.')->after('location_street'); + $table->string('location_city')->nullable()->comment('Offer location city.')->after('location_zip'); + $table->string('location_state')->nullable()->comment('Offer location state.')->after('location_city'); + $table->string('location_country')->nullable()->comment('Offer location country.')->after('location_state'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('offers', function (Blueprint $table) { + $table->dropColumn('location_street'); + $table->dropColumn('location_zip'); + $table->dropColumn('location_city'); + $table->dropColumn('location_state'); + $table->dropColumn('location_country'); + $table->string('location')->nullable()->comment('Offer location.')->after('title'); + }); + } +}; diff --git a/resources/views/bo/pages/offers/index-list.blade.php b/resources/views/bo/pages/offers/index-list.blade.php index 893138a9..f6250175 100644 --- a/resources/views/bo/pages/offers/index-list.blade.php +++ b/resources/views/bo/pages/offers/index-list.blade.php @@ -17,12 +17,14 @@ @foreach ($offerModels as $offerModel) {{ $offerModel->title }} - {{ $offerModel->location }} + + {{ $offerModel->getFormattedAddressAttribute() }} + {{ $offerModel->contract }} {{ $offerModel->function }} - {{ $offerModel->picture_alt }} + {{ $offerModel->picture_alt }} @include('bo.modules.change-published-status', [ 'routeName' => 'offers', diff --git a/resources/views/bo/pages/offers/show.blade.php b/resources/views/bo/pages/offers/show.blade.php index 332e2fff..c8d3295b 100644 --- a/resources/views/bo/pages/offers/show.blade.php +++ b/resources/views/bo/pages/offers/show.blade.php @@ -48,37 +48,43 @@ - + - + - + - + - + - + - + - + diff --git a/resources/views/fo/partials/offer-card.blade.php b/resources/views/fo/partials/offer-card.blade.php index 623b45f6..802430f5 100644 --- a/resources/views/fo/partials/offer-card.blade.php +++ b/resources/views/fo/partials/offer-card.blade.php @@ -23,9 +23,12 @@
{{ $offerModel->contract }}
-
- {{ $offerModel->location }} -
+ @php $locationAddress = $offerModel->getFormattedAddressAttribute(); @endphp + @if (strlen($locationAddress)) +
+ {{ $locationAddress }} +
+ @endif
{{ $offerModel->function }}
{{ str(__('validation.attributes.title'))->ucFirst() }} + {{ str(__('validation.attributes.title'))->ucFirst() }} {{ $offerModel->title }}
{{ str(__('validation.custom.location'))->ucFirst() }} + {{ str(__('validation.custom.location'))->ucFirst() }} - {{ $offerModel->location }} + {{ $offerModel->getFormattedAddressAttribute() }}
{{ str(__('validation.custom.contract'))->ucFirst() }} + {{ str(__('validation.custom.contract'))->ucFirst() }} {{ $offerModel->contract }}
{{ str(__('validation.custom.function'))->ucFirst() }} + {{ str(__('validation.custom.function'))->ucFirst() }} {{ $offerModel->function }}
{{ str(__('validation.attributes.description'))->ucFirst() }} + {{ str(__('validation.attributes.description'))->ucFirst() }} {{ $offerModel->description }}
{{ str(__('validation.attributes.image'))->ucFirst() }} + {{ str(__('validation.attributes.image'))->ucFirst() }} {{ $offerModel->picture_alt }} @@ -86,20 +92,22 @@
- {{ str(sprintf("%s du bouton", __('validation.custom.publication')))->ucFirst() }} + {{ str(sprintf('%s du bouton', __('validation.custom.publication')))->ucFirst() }} {{ $offerModel->published ? __('Oui') : __('Non') }}
{{ str(__('validation.attributes.created_at'))->ucFirst() }} + {{ str(__('validation.attributes.created_at'))->ucFirst() }} {{ str($offerModel->created_at->isoFormat('LLLL'))->ucFirst() }}
{{ str(__('validation.attributes.updated_at'))->ucFirst() }} + {{ str(__('validation.attributes.updated_at'))->ucFirst() }} {{ str($offerModel->updated_at->isoFormat('LLLL'))->ucFirst() }}