174 lines
5.2 KiB
PHP
174 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Str;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
|
|
/**
|
|
* @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 $title Title for the page.
|
|
* @property string $slug Slug of the title for the page.
|
|
* @property string $content WYSIWYG text content using ckeditor.
|
|
* @property string|null $tenant_id Associated tenant.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
|
|
*/
|
|
class StaticPage extends Model
|
|
{
|
|
use ActivityLog;
|
|
use BelongsToTenant;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'content',
|
|
'tenant_id',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => StaticPageType::class,
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model);
|
|
|
|
// * CHANGES
|
|
$model->slug = Str::slug($model->title);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model);
|
|
|
|
// * CHANGES
|
|
$model->slug = Str::slug($model->title);
|
|
});
|
|
}
|
|
|
|
// * ROUTABLE
|
|
|
|
/**
|
|
* Get the value of the model's route key.
|
|
*
|
|
* @return string|integer
|
|
*/
|
|
public function getRouteKey(): string|int
|
|
{
|
|
/** @var \App\Enums\StaticPageType $enum */
|
|
$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 self $model
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If a static page title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(self $model): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique(
|
|
$table,
|
|
'title',
|
|
$model->title,
|
|
$model->getKey(),
|
|
function (Builder $query) use ($model) {
|
|
$query->where('tenant_id', $model->tenant->getKey());
|
|
}
|
|
);
|
|
ToolboxHelper::assertFieldIsUnique(
|
|
$table,
|
|
'slug',
|
|
Str::slug($model->title),
|
|
$model->getKey(),
|
|
function (Builder $query) use ($model) {
|
|
$query->where('tenant_id', $model->tenant->getKey());
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Set default settings for a specific tenant.
|
|
*
|
|
* @param \App\Models\Tenant $tenant
|
|
* @return void
|
|
*/
|
|
public function setDefaultPages(Tenant $tenant): void
|
|
{
|
|
collect([StaticPageType::confidentiality, StaticPageType::legals, StaticPageType::rules])
|
|
->map(function (StaticPageType $enum) use ($tenant) {
|
|
$staticPageModel = new StaticPage();
|
|
$staticPageModel->seo_title = sprintf('Seo title pour la page de %s', $enum->label());
|
|
$staticPageModel->seo_description = sprintf(
|
|
'Seo description pour la page de %s',
|
|
$enum->label()
|
|
);
|
|
$staticPageModel->title = sprintf('Page de %s', $enum->label());
|
|
$staticPageModel->type = $enum;
|
|
$staticPageModel->content = 'Ceci est le contenu de la page.';
|
|
$staticPageModel->tenant_id = $tenant->getkey();
|
|
$staticPageModel->saveOrFail();
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Activities logs One-to-many relationship.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ActivityLog::class);
|
|
}
|
|
}
|