ldc-legaulois/app/Models/SectionPage.php

86 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Enums\StaticPageType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Validation\ValidationException;
2026-06-11 10:47:10 +02:00
/**
* SectionPage
*
* Represents a section belonging to a static page.
*
* @property int $id
* @property int $page_id
* @property string $name
* @property bool $visibility
* @property object $data
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class SectionPage extends Model
{
protected $table = 'section_pages';
protected $fillable = [
'page_id',
'name',
'visibility',
'data',
];
protected $casts = [
'visibility' => 'boolean',
'data' => 'object',
];
2026-06-11 10:47:10 +02:00
/**
* Returns the static page this section belongs to.
*
* @return BelongsTo
*/
public function page(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
2026-06-11 10:47:10 +02:00
/**
* Returns all blocks associated with this section.
*
* @return HasMany
*/
public function blocks(): HasMany
{
return $this->hasMany(Block::class, 'section_id');
}
2026-06-11 10:47:10 +02:00
/**
* Returns all pictos associated with this section, ordered by their position.
*
* @return HasMany
*/
public function pictos(): HasMany
{
return $this->hasMany(Picto::class, 'section_id')->orderBy('order');
}
protected static function booted()
{
static::saving(function ($section) {
$section->load('page');
if (
$section->page->type === StaticPageType::raiseSustainably
&& $section->name === 'section 3'
&& !str_contains($section->data->title->value, ':nbrKeyList')
) {
throw ValidationException::withMessages([
'data.title.value' => __('validation.custom.data_title_value.contains_placeholder'),
]);
}
});
}
}