ldc-legaulois/app/Models/NewsCategory.php
2026-09-25 16:25:31 +02:00

147 lines
4.2 KiB
PHP

<?php
namespace App\Models;
use App\Casts\HtmlColor;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Spatie\SchemaOrg\DefinedTermSet;
use Spatie\SchemaOrg\Schema;
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
/**
* @property string $title The title of the news.
* @property string $seo_title Seo title about 70 chars as recommended.
* @property string $seo_description Seo description about 160 chars as recommended.
* @property int $color Range color.
* @property string $slug The slug url of the category.
* @property bool $published Category publish status.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class NewsCategory extends Model implements SchemaOrgInterface
{
use ActivityLog;
use HasFactory;
use SchemaOrgTrait;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'seo_title',
'seo_description',
'color',
'slug',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'color' => HtmlColor::class,
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*/
protected static function booted(): void
{
static::creating(function (self $newsCategory) {
self::assertTitleIsUnique($newsCategory->title);
self::setOrder($newsCategory);
self::manageFields($newsCategory);
});
static::created(function () {
self::resetCache();
News::resetCache();
});
static::updating(function (self $newsCategory) {
self::assertTitleIsUnique($newsCategory->title, $newsCategory->getKey());
self::manageFields($newsCategory);
});
static::updated(function () {
self::resetCache();
News::resetCache();
});
static::deleted(function () {
self::resetCache();
News::resetCache();
});
}
// * METHODS
/**
* Set model's order after the last element of the list.
*/
private static function setOrder(self $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
/**
* Manage model fields.
*/
private static function manageFields(self $model): void
{
$model->slug = Str::slug($model->title);
}
/**
* Asserts using validation that the title is unique.
*
* @throws \Illuminate\Validation\ValidationException If an news category title already exists.
*/
private static function assertTitleIsUnique(?string $title, ?int $id = null): void
{
$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.
*/
private static function resetCache(): void
{
Cache::forget('publishedNewsCategoriesOrderedByOrder');
self::getPublishedOrdered();
}
/**
* Get all published NewsCategories ordered by order.
*/
public static function getPublishedOrdered(): Collection
{
return Cache::remember('publishedNewsCategoriesOrderedByOrder', 86400, function () {
return self::query()->where('published', true)->orderBy('order')->get();
});
}
/**
* Create a SchemaOrg.
*/
public function toSchemaOrg(): DefinedTermSet
{
return Schema::definedTermSet()
->hasDefinedTerm(
Schema::definedTerm()
->name($this->seo_title)
->description($this->seo_description)
);
}
}