326 lines
10 KiB
PHP
326 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use App\Traits\Models\MiniCrudModel;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Spatie\SchemaOrg\WebPage;
|
|
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
|
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
|
|
/**
|
|
* @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.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*/
|
|
class StaticPage extends Model implements SchemaOrgInterface, RedirectableInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use MiniCrudModel;
|
|
use SchemaOrgTrait;
|
|
use IsRedirectable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'subtitle',
|
|
'content',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'data',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => StaticPageType::class,
|
|
'data' => 'object',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function () {
|
|
throw new \RuntimeException("Creating a new page is not authorized");
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::assertSeoTitleIsUnique($model->seo_title, $model->getKey());
|
|
self::assertTitleIsUnique($model->title, $model->getKey());
|
|
$model->slug = Str::slug($model->title ?? $model->seo_title ?? Str::random(20));
|
|
if (!is_null($model->picture)) {
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
|
|
self::resetCache($model->type);
|
|
});
|
|
static::deleted(function () {
|
|
throw new \RuntimeException('Deleting a page is not authorized');
|
|
});
|
|
}
|
|
|
|
// * ROUTABLE
|
|
|
|
/**
|
|
* Get the value of the model's route key.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function getRouteKey(): mixed
|
|
{
|
|
/** @var \App\Enums\StaticPageType */
|
|
$enum = $this->getAttribute($this->getRouteKeyName());
|
|
return $enum->value();
|
|
}
|
|
|
|
/**
|
|
* Get the route key for the model.
|
|
*
|
|
* ! Type is the routable parameter
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'type';
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique
|
|
*
|
|
* @param string|null $seo_title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu seo_title already exists.
|
|
*/
|
|
private static function assertSeoTitleIsUnique(string|null $seo_title, ?int $id = null): void
|
|
{
|
|
if (!is_null($seo_title)) {
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'seo_title', $seo_title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($seo_title), $id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique
|
|
*
|
|
* @param string|null $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu seo_title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(string|null $title, ?int $id = null): void
|
|
{
|
|
if (!is_null($title)) {
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return void
|
|
*/
|
|
private static function resetCache(StaticPageType $type): void
|
|
{
|
|
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.
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return \App\Models\StaticPage
|
|
*/
|
|
public static function getCachedStaticPage(StaticPageType $type): \App\Models\StaticPage
|
|
{
|
|
return Cache::remember(
|
|
sprintf('%sStaticPage', $type->name),
|
|
86400,
|
|
fn() => self::query()->where('type', $type)->first()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get cached static pages associated to all breeding types.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getCachedBreedingStaticPages(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember(
|
|
'breedingStaticPages',
|
|
86400,
|
|
fn() => self::query()->whereIn('type', StaticPageType::getBreedingPages())->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg.
|
|
*
|
|
* @return \Spatie\SchemaOrg\WebPage
|
|
*/
|
|
public function toSchemaOrg(): \Spatie\SchemaOrg\WebPage
|
|
{
|
|
$schema = Schema::webPage()
|
|
->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())
|
|
->author($this->getOrganizationSchema())
|
|
->isPartOf($this->getWebsiteSchema());
|
|
|
|
if (request()->routeIs('fo.recruitment.show')) {
|
|
return $this->toSchemaOrgRecruitment($schema);
|
|
}
|
|
|
|
return $schema;
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg for the recruitment page.
|
|
*
|
|
* @param \Spatie\SchemaOrg\WebPage $schemaBase
|
|
* @return \Spatie\SchemaOrg\WebPage
|
|
*/
|
|
public function toSchemaOrgRecruitment(WebPage $schemaBase): \Spatie\SchemaOrg\WebPage
|
|
{
|
|
// @phpcs:enable
|
|
$schema = $schemaBase->keywords(['recrutement', 'emplois']);
|
|
$offers = [];
|
|
|
|
// @phpstan-ignore argument.type
|
|
Offer::getOffersCached()->each(function (Offer $offerModel) use (&$offers) {
|
|
$offers[] = $offerModel->toSchemaOrg();
|
|
});
|
|
|
|
if (!empty($offers)) {
|
|
$schema->about(Schema::ListItem()->item($offers));
|
|
}
|
|
|
|
return $schema;
|
|
}
|
|
|
|
// * 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.
|
|
*
|
|
* @param string $modelKey
|
|
* @return string
|
|
*/
|
|
public function getFrontUrl(string $modelKey): string
|
|
{
|
|
return (in_array($this->type, StaticPageType::getBreedingPages())) ? route('fo.breeding.show', $modelKey) : "";
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get Breeding static page (has-many relationship).
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function newsBreedings(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(NewsBreeding::class);
|
|
}
|
|
|
|
/**
|
|
* Get categories
|
|
*/
|
|
public function getCategorySectionAttribute(): array
|
|
{
|
|
$data = $this->data ?? [];
|
|
|
|
// Convertit objet => array
|
|
if (is_object($data)) {
|
|
$data = json_decode(json_encode($data), true);
|
|
}
|
|
|
|
if (!is_array($data)) {
|
|
return [];
|
|
}
|
|
|
|
return collect($data)
|
|
->filter(fn ($v, $k) => is_string($k) && str_starts_with($k, 'tab_title_'))
|
|
->map(function ($v) {
|
|
// $v peut être array ou objet
|
|
$value = is_array($v) ? ($v['value'] ?? null) : ($v->value ?? null);
|
|
return is_string($value) ? trim($value) : null;
|
|
})
|
|
->filter(fn ($v) => is_string($v) && $v !== '')
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|