ldc-legaulois/app/Models/SectionPage.php
2026-08-10 16:07:46 +02:00

85 lines
2.1 KiB
PHP

<?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;
/**
* 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',
];
/**
* Returns the static page this section belongs to.
*
* @return BelongsTo
*/
public function page(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
/**
* Returns all blocks associated with this section.
*
* @return HasMany
*/
public function blocks(): HasMany
{
return $this->hasMany(Block::class, 'section_id');
}
/**
* 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'),
]);
}
});
}
}