264 lines
8.9 KiB
PHP
264 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
|
|
/**
|
|
* @property string|null $title Offer title.
|
|
* @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.
|
|
* @property string|null $contract Offer contract.
|
|
* @property string|null $contract_period Offer contract period.
|
|
* @property string|null $function Offer function.
|
|
* @property string|null $experience_level Offer experience level.
|
|
* @property string|null $qualifications Offer qualifications.
|
|
* @property string|null $skills Offer skills.
|
|
* @property string|null $picture Offer picture.
|
|
* @property string|null $picture_alt Picture alt attribute.
|
|
* @property string|null $picture_title Picture title attribute.
|
|
* @property string|null $link Offer link.
|
|
* @property string|null $description Offer description.
|
|
* @property string|null $recruiter_firstname Offer recruiter firstname.
|
|
* @property string|null $recruiter_lastname Offer recruiter lastname.
|
|
* @property string|null $recruiter_job Offer recruiter job.
|
|
* @property string|null $recruiter_picture Offer recruiter picture.
|
|
* @property boolean $published Offer publish status.
|
|
* @property \Carbon\Carbon $published_at Offer published date.
|
|
* @property \Carbon\Carbon $created_at Created date.
|
|
* @property \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Product $products Product polymorphic relationship.
|
|
*/
|
|
class Offer extends Model implements SchemaOrgInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SchemaOrgTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'location_street',
|
|
'location_zip',
|
|
'location_city',
|
|
'location_state',
|
|
'location_country',
|
|
'location_details',
|
|
'location_latitude',
|
|
'location_longitude',
|
|
'contract',
|
|
'contract_period',
|
|
'function',
|
|
'experience_level',
|
|
'qualifications',
|
|
'skills',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'link',
|
|
'description',
|
|
'recruiter_firstname',
|
|
'recruiter_lastname',
|
|
'recruiter_job',
|
|
'recruiter_picture',
|
|
'published',
|
|
'published_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'skills' => 'array',
|
|
'published' => 'boolean',
|
|
'published_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $offer) {
|
|
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
|
|
$offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $offer) {
|
|
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
|
|
$offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true);
|
|
});
|
|
static::updated(function (self $offer) {
|
|
FileStorageHelper::removeOldFile($offer, 'picture');
|
|
FileStorageHelper::removeOldFile($offer, 'recruiter_picture');
|
|
self::resetCache();
|
|
});
|
|
static::deleted(function (self $offer) {
|
|
FileStorageHelper::removeFile($offer, 'picture');
|
|
FileStorageHelper::removeFile($offer, 'recruiter_picture');
|
|
self::resetCache();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('offersCached');
|
|
Cache::forget('offersCountCached');
|
|
self::getOffersCached();
|
|
self::getOffersCountCached();
|
|
}
|
|
|
|
/**
|
|
* Get all offers.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getOffersCached(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('offersCached', 86400, function () {
|
|
return self::query()->where('published', true)->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get offers count.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public static function getOffersCountCached(): int
|
|
{
|
|
return Cache::remember('offersCountCached', 86400, function () {
|
|
return GeneralSetting::query()->where('type', SettingsType::offersCount)->firstOrFail()->value['count'];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @return \Spatie\SchemaOrg\JobPosting
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
public function toSchemaOrg(): \Spatie\SchemaOrg\JobPosting
|
|
{
|
|
$schemaJob = Schema::jobPosting()
|
|
->title($this->title)
|
|
->hiringOrganization($this->getOrganizationSchema());
|
|
|
|
// Recruiter.
|
|
$recruiter = Schema::contactPoint();
|
|
if (!empty($this->recruiter_firstname) || !empty($this->recruiter_lastname)) {
|
|
$recruiter->name(sprintf(
|
|
"%s %s",
|
|
$this->recruiter_firstname ?? '',
|
|
$this->recruiter_lastname ?? '',
|
|
));
|
|
}
|
|
if (!empty($this->recruiter_job)) {
|
|
$recruiter->contactType($this->recruiter_job);
|
|
}
|
|
if (!empty($this->recruiter_picture)) {
|
|
$recruiter->image(
|
|
Schema::imageObject()
|
|
->url(asset($this->recruiter_picture))
|
|
->headline(sprintf(
|
|
"Photo du recruteur %s %s",
|
|
$this->recruiter_firstname ?? '',
|
|
$this->recruiter_lastname ?? '',
|
|
))
|
|
);
|
|
$schemaJob->applicationContact($recruiter);
|
|
}
|
|
|
|
// Location.
|
|
$location = Schema::place()
|
|
->isAccessibleForFree(true)
|
|
->publicAccess(true);
|
|
if (!empty($this->location_longitude)) {
|
|
$location->longitude($this->location_longitude);
|
|
}
|
|
if (!empty($this->location_latitude)) {
|
|
$location->latitude($this->location_latitude);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
$schemaJob->jobLocation($location);
|
|
|
|
// Other.
|
|
if (!empty($this->description)) {
|
|
$schemaJob->description($this->description);
|
|
}
|
|
if (!empty($this->contract)) {
|
|
$schemaJob->employmentType($this->contract);
|
|
}
|
|
if (!empty($this->created_at)) {
|
|
$schemaJob->datePosted($this->created_at);
|
|
}
|
|
|
|
return $schemaJob;
|
|
}
|
|
}
|