2024-01-09 15:20:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Enums\StaticPageType;
|
2024-05-31 11:27:46 +02:00
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
2024-01-09 15:20:18 +00:00
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
2024-01-10 14:49:46 +01:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2024-07-10 17:10:33 +02:00
|
|
|
use App\Traits\Models\MiniCrudModel;
|
2026-09-25 16:25:31 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2024-01-09 15:20:18 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-02-24 17:33:28 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-09-24 11:32:36 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-01-09 15:20:18 +00:00
|
|
|
use Illuminate\Support\Str;
|
2025-06-03 15:47:52 +02:00
|
|
|
use Spatie\SchemaOrg\Schema;
|
2025-06-16 16:38:23 +02:00
|
|
|
use Spatie\SchemaOrg\WebPage;
|
2025-06-19 12:31:10 +02:00
|
|
|
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
|
|
|
|
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
2025-06-03 15:47:52 +02:00
|
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
|
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
2024-01-09 15:20:18 +00:00
|
|
|
|
|
|
|
|
/**
|
2026-09-25 16:25:31 +02:00
|
|
|
* @property \App\Enums\StaticPageType $type Page type (StaticPageEnum).
|
|
|
|
|
* @property string $seo_title Seo title about 70 chars as recommended.
|
|
|
|
|
* @property string $seo_description Seo description about 160 chars as recommended.
|
|
|
|
|
* @property string|null $title Title for the page.
|
|
|
|
|
* @property string|null $subtitle The subtitle for the static page.
|
|
|
|
|
* @property string $slug Slug of the title for the page.
|
|
|
|
|
* @property string|null $content WYSIWYG text content using ckeditor.
|
|
|
|
|
* @property string|null $picture Picture path.
|
|
|
|
|
* @property string|null $picture_alt Picture alt.
|
|
|
|
|
* @property string|null $picture_title Picture title.
|
|
|
|
|
* @property array<string, array<string, mixed>> $data Static page json data.
|
2024-07-10 17:10:33 +02:00
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
2024-01-09 15:20:18 +00:00
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
class StaticPage extends Model implements RedirectableInterface, SchemaOrgInterface
|
2024-01-09 15:20:18 +00:00
|
|
|
{
|
2024-01-10 14:49:46 +01:00
|
|
|
use ActivityLog;
|
2024-01-09 15:20:18 +00:00
|
|
|
use HasFactory;
|
2026-09-25 16:25:31 +02:00
|
|
|
use IsRedirectable;
|
2024-07-10 17:10:33 +02:00
|
|
|
use MiniCrudModel;
|
2025-06-03 15:47:52 +02:00
|
|
|
use SchemaOrgTrait;
|
2024-01-09 15:20:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
2024-09-12 17:30:53 +02:00
|
|
|
'type',
|
2024-01-09 15:20:18 +00:00
|
|
|
'seo_title',
|
|
|
|
|
'seo_description',
|
|
|
|
|
'title',
|
2024-07-10 17:10:33 +02:00
|
|
|
'subtitle',
|
2024-01-09 15:20:18 +00:00
|
|
|
'content',
|
2024-05-31 11:27:46 +02:00
|
|
|
'picture',
|
|
|
|
|
'picture_alt',
|
|
|
|
|
'picture_title',
|
2024-07-10 17:10:33 +02:00
|
|
|
'data',
|
2024-01-09 15:20:18 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-10 14:49:46 +01:00
|
|
|
* The attributes that should be cast.
|
2024-01-09 15:20:18 +00:00
|
|
|
*
|
2024-01-10 14:49:46 +01:00
|
|
|
* @var array<string, string>
|
2024-01-09 15:20:18 +00:00
|
|
|
*/
|
2024-01-10 14:49:46 +01:00
|
|
|
protected $casts = [
|
|
|
|
|
'type' => StaticPageType::class,
|
2024-07-10 17:10:33 +02:00
|
|
|
'data' => 'object',
|
2024-01-09 15:20:18 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
2024-07-10 17:10:33 +02:00
|
|
|
static::creating(function () {
|
2026-09-25 16:25:31 +02:00
|
|
|
throw new \RuntimeException('Creating a new page is not authorized');
|
2024-01-09 15:20:18 +00:00
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
2024-10-01 14:22:57 +02:00
|
|
|
self::assertSeoTitleIsUnique($model->seo_title, $model->getKey());
|
|
|
|
|
self::assertTitleIsUnique($model->title, $model->getKey());
|
2024-10-09 14:35:27 +02:00
|
|
|
$model->slug = Str::slug($model->title ?? $model->seo_title ?? Str::random(20));
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! is_null($model->picture)) {
|
2024-07-10 17:10:33 +02:00
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
|
|
|
}
|
2024-05-31 11:27:46 +02:00
|
|
|
});
|
|
|
|
|
static::updated(function (self $model) {
|
|
|
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
2024-09-24 11:32:36 +02:00
|
|
|
|
|
|
|
|
self::resetCache($model->type);
|
2024-05-31 11:27:46 +02:00
|
|
|
});
|
2024-07-10 17:10:33 +02:00
|
|
|
static::deleted(function () {
|
|
|
|
|
throw new \RuntimeException('Deleting a page is not authorized');
|
2024-01-09 15:20:18 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 14:49:46 +01:00
|
|
|
// * ROUTABLE
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the value of the model's route key.
|
|
|
|
|
*/
|
2024-10-04 17:03:04 +02:00
|
|
|
public function getRouteKey(): mixed
|
2024-01-10 14:49:46 +01:00
|
|
|
{
|
|
|
|
|
/** @var \App\Enums\StaticPageType */
|
|
|
|
|
$enum = $this->getAttribute($this->getRouteKeyName());
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2024-01-10 14:49:46 +01:00
|
|
|
return $enum->value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the route key for the model.
|
|
|
|
|
*
|
|
|
|
|
* ! Type is the routable parameter
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'type';
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 15:20:18 +00:00
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asserts using validation that the title is unique
|
|
|
|
|
*
|
2024-07-10 17:10:33 +02:00
|
|
|
* @throws \Illuminate\Validation\ValidationException If an actu seo_title already exists.
|
2024-01-09 15:20:18 +00:00
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
private static function assertSeoTitleIsUnique(?string $seo_title, ?int $id = null): void
|
2024-01-09 15:20:18 +00:00
|
|
|
{
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! is_null($seo_title)) {
|
2024-09-20 15:58:44 +02:00
|
|
|
$table = (new self())->getTable();
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'seo_title', $seo_title, $id);
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($seo_title), $id);
|
|
|
|
|
}
|
2024-01-09 15:20:18 +00:00
|
|
|
}
|
2024-09-24 11:32:36 +02:00
|
|
|
|
2024-10-01 14:22:57 +02:00
|
|
|
/**
|
|
|
|
|
* Asserts using validation that the title is unique
|
|
|
|
|
*
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If an actu seo_title already exists.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
private static function assertTitleIsUnique(?string $title, ?int $id = null): void
|
2024-10-01 14:22:57 +02:00
|
|
|
{
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! is_null($title)) {
|
2024-10-01 14:22:57 +02:00
|
|
|
$table = (new self())->getTable();
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 11:32:36 +02:00
|
|
|
/**
|
|
|
|
|
* Reset all cache elements related to this model.
|
|
|
|
|
*/
|
2024-10-04 17:03:04 +02:00
|
|
|
private static function resetCache(StaticPageType $type): void
|
2024-09-24 11:32:36 +02:00
|
|
|
{
|
|
|
|
|
Cache::forget(sprintf('%sStaticPage', $type->name));
|
|
|
|
|
self::getCachedStaticPage($type);
|
|
|
|
|
|
|
|
|
|
// Breeding pages.
|
|
|
|
|
Cache::forget('breedingStaticPages');
|
|
|
|
|
self::getCachedBreedingStaticPages();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cached static page associated to a given type.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public static function getCachedStaticPage(StaticPageType $type): StaticPage
|
2024-09-24 11:32:36 +02:00
|
|
|
{
|
2025-04-01 12:53:37 +02:00
|
|
|
return Cache::remember(
|
2024-09-24 11:32:36 +02:00
|
|
|
sprintf('%sStaticPage', $type->name),
|
2025-04-01 12:53:37 +02:00
|
|
|
86400,
|
2026-09-25 16:25:31 +02:00
|
|
|
fn () => self::query()->where('type', $type)->firstOrFail()
|
2024-09-24 11:32:36 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cached static pages associated to all breeding types.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public static function getCachedBreedingStaticPages(): Collection
|
2024-09-24 11:32:36 +02:00
|
|
|
{
|
2025-04-01 12:53:37 +02:00
|
|
|
return Cache::remember(
|
2024-09-24 11:32:36 +02:00
|
|
|
'breedingStaticPages',
|
2025-04-01 12:53:37 +02:00
|
|
|
86400,
|
2026-09-25 16:25:31 +02:00
|
|
|
fn () => self::query()->whereIn('type', StaticPageType::getBreedingPages())->get()
|
2024-09-24 11:32:36 +02:00
|
|
|
);
|
|
|
|
|
}
|
2026-09-25 16:25:31 +02:00
|
|
|
|
2026-06-05 12:15:40 +02:00
|
|
|
/**
|
|
|
|
|
* Get cached static pages associated to all commitmments types.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public static function getCachedCommitmentsStaticPages(): ?Collection
|
2026-06-05 12:15:40 +02:00
|
|
|
{
|
2026-06-22 15:11:31 +02:00
|
|
|
$orderedTypes = StaticPageType::getCommitmentsPages();
|
|
|
|
|
|
2026-06-11 10:47:10 +02:00
|
|
|
$pages = self::query()
|
2026-06-22 15:11:31 +02:00
|
|
|
->whereIn('type', $orderedTypes)
|
|
|
|
|
->get()
|
2026-09-25 16:25:31 +02:00
|
|
|
->sortBy(fn ($page) => array_search($page->type, $orderedTypes, true))
|
2026-06-22 15:11:31 +02:00
|
|
|
->values();
|
2026-06-11 10:47:10 +02:00
|
|
|
|
|
|
|
|
return $pages->isEmpty() ? null : $pages;
|
2026-06-05 12:15:40 +02:00
|
|
|
}
|
2025-06-03 15:47:52 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a SchemaOrg.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function toSchemaOrg(): WebPage
|
2025-06-03 15:47:52 +02:00
|
|
|
{
|
2025-06-16 16:38:23 +02:00
|
|
|
$schema = Schema::webPage()
|
2025-06-03 15:47:52 +02:00
|
|
|
->name($this->seo_title)
|
|
|
|
|
->description($this->seo_description)
|
|
|
|
|
->headline($this->title)
|
|
|
|
|
->isAccessibleForFree(true)
|
|
|
|
|
->keywords(['page'])
|
|
|
|
|
->dateCreated($this->created_at)
|
|
|
|
|
->dateModified($this->updated_at)
|
|
|
|
|
->image(
|
|
|
|
|
Schema::imageObject()
|
|
|
|
|
->url(asset($this->picture))
|
|
|
|
|
->headline($this->picture_title)
|
|
|
|
|
)
|
|
|
|
|
->reviewedBy($this->getOrganizationSchema())
|
|
|
|
|
->publisher($this->getOrganizationSchema())
|
2025-06-12 16:57:20 +02:00
|
|
|
->author($this->getOrganizationSchema())
|
|
|
|
|
->isPartOf($this->getWebsiteSchema());
|
2025-06-16 16:38:23 +02:00
|
|
|
|
|
|
|
|
if (request()->routeIs('fo.recruitment.show')) {
|
|
|
|
|
return $this->toSchemaOrgRecruitment($schema);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $schema;
|
2025-06-03 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a SchemaOrg for the recruitment page.
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function toSchemaOrgRecruitment(WebPage $schemaBase): WebPage
|
2025-06-03 15:47:52 +02:00
|
|
|
{
|
|
|
|
|
// @phpcs:enable
|
2025-06-16 16:38:23 +02:00
|
|
|
$schema = $schemaBase->keywords(['recrutement', 'emplois']);
|
2025-06-03 15:47:52 +02:00
|
|
|
$offers = [];
|
|
|
|
|
|
|
|
|
|
// @phpstan-ignore argument.type
|
|
|
|
|
Offer::getOffersCached()->each(function (Offer $offerModel) use (&$offers) {
|
2025-06-16 16:38:23 +02:00
|
|
|
$offers[] = $offerModel->toSchemaOrg();
|
2025-06-03 15:47:52 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-25 16:25:31 +02:00
|
|
|
if (! empty($offers)) {
|
2025-06-03 15:47:52 +02:00
|
|
|
$schema->about(Schema::ListItem()->item($offers));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
|
}
|
2025-06-19 12:31:10 +02:00
|
|
|
|
|
|
|
|
// * REDIRECTIONS.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* All models that belongs to it.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string>
|
|
|
|
|
*/
|
|
|
|
|
protected function getRouteChildrenModels(): array
|
|
|
|
|
{
|
|
|
|
|
return (in_array($this->type, StaticPageType::getBreedingPages())) ? ['App\Models\NewsBreeding'] : [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the front url of this model.
|
|
|
|
|
* Should return the model.show route, with all binded elements if needed.
|
|
|
|
|
*/
|
|
|
|
|
public function getFrontUrl(string $modelKey): string
|
|
|
|
|
{
|
2026-09-25 16:25:31 +02:00
|
|
|
return (in_array($this->type, StaticPageType::getBreedingPages())) ? route('fo.breeding.show', $modelKey) : '';
|
2025-06-19 12:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Breeding static page (has-many relationship).
|
|
|
|
|
*/
|
2026-09-25 16:25:31 +02:00
|
|
|
public function newsBreedings(): HasMany
|
2025-06-19 12:31:10 +02:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(NewsBreeding::class);
|
|
|
|
|
}
|
2026-02-24 17:33:28 +01:00
|
|
|
|
|
|
|
|
public function sectionsApproaches(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(SectionApproach::class, 'page_id');
|
|
|
|
|
}
|
2026-06-04 14:20:18 +02:00
|
|
|
|
|
|
|
|
public function sections(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(SectionPage::class, 'page_id');
|
|
|
|
|
}
|
2024-01-09 15:20:18 +00:00
|
|
|
}
|