172 lines
4.8 KiB
PHP
172 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
|
use Uwil\Seo\Laravel\Objects\SeoItem;
|
|
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
|
|
|
/**
|
|
* @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-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*/
|
|
class StaticPage extends Model implements SeoInterface, SchemaOrgInterface
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use SeoTrait;
|
|
use SchemaOrgTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'content',
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'slug',
|
|
'content',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => StaticPageType::class,
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model->title);
|
|
|
|
// * CHANGES
|
|
$model->slug = Str::slug($model->title);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertTitleIsUnique($model->title, $model->id);
|
|
|
|
// * 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 = $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 $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique(string|null $title, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg.
|
|
*
|
|
* @return \Spatie\SchemaOrg\WebPage
|
|
*/
|
|
public function toSchemaOrg(): \Spatie\SchemaOrg\WebPage
|
|
{
|
|
return 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)
|
|
->reviewedBy($this->getOrganizationSchema())
|
|
->publisher($this->getOrganizationSchema())
|
|
->author($this->getOrganizationSchema())
|
|
->isPartOf($this->getWebsiteSchema());
|
|
}
|
|
|
|
/**
|
|
* Get the SEO object.
|
|
*
|
|
* @return \Uwil\Seo\Laravel\Objects\SeoItem
|
|
*/
|
|
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
|
|
{
|
|
return new SeoItem(
|
|
\trans($this->getTable()),
|
|
$this->seo_title,
|
|
$this->seo_description,
|
|
true,
|
|
route('bo.static_pages.edit', $this),
|
|
'#',
|
|
);
|
|
}
|
|
}
|