186 lines
5.7 KiB
PHP
186 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use App\Traits\Models\PlanificatedStatus;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Sitemap\Contracts\Sitemapable;
|
|
use Spatie\Sitemap\Tags\Url;
|
|
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $seo_title SEO title.
|
|
* @property string $seo_description SEO description.
|
|
* @property string $title Title.
|
|
* @property string $slug Slug of the title.
|
|
* @property string $introduction WYSIWYG - Introduction.
|
|
* @property string|null $content WYSIWYG - Content.
|
|
* @property string|null $linkedin_url Url of the linkedin related post.
|
|
* @property string $thumbnail Path of the thumbnail.
|
|
* @property string $thumbnail_alt Thumbnail alt attribute.
|
|
* @property string $thumbnail_title Thumbnail title attribute.
|
|
* @property string $picture Path of the picture.
|
|
* @property string $picture_alt Picture alt attribute..
|
|
* @property string $picture_title Picture title attribute.
|
|
* @property integer $order Order.
|
|
* @property boolean $published Publication status.
|
|
* @property \Carbon\Carbon|null $publish_at Publication date.
|
|
* @property \Carbon\Carbon|null $unpublish_at Unpublication status update date.
|
|
* @property \Carbon\Carbon $displayed_date News dipslayed date.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
|
|
*/
|
|
class News extends Model implements Sitemapable
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use PlanificatedStatus;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'slug',
|
|
'introduction',
|
|
'content',
|
|
'linkedin_url',
|
|
'thumbnail',
|
|
'thumbnail_alt',
|
|
'thumbnail_title',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'order',
|
|
'published',
|
|
'publish_at',
|
|
'unpublish_at',
|
|
'displayed_date',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'bool',
|
|
'publish_at' => 'datetime',
|
|
'unpublish_at' => 'datetime',
|
|
'displayed_date' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
});
|
|
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
// * ASSERTIONS
|
|
self::assertFieldIsUnique('title', $model->title, $model->id);
|
|
self::assertFieldIsUnique('slug', Str::slug($model->title), $model->id);
|
|
|
|
ToolboxHelper::assertNoInternalBoUrls('introduction', $model->introduction);
|
|
$model->introduction = clean($model->introduction);
|
|
if ($model->content) {
|
|
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
|
|
$model->content = clean($model->content);
|
|
}
|
|
|
|
// * TRANSFORMATIONS
|
|
self::setSlug($model, $model->title);
|
|
self::makeStatusSense($model);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->thumbnail = FileStorageHelper::storeFile($model, $model->thumbnail, true);
|
|
}
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param self $model
|
|
* @param string $original
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(self $model, string $original): void
|
|
{
|
|
$model->slug = Str::slug($original);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the field is unique.
|
|
*
|
|
* @param string $nameField
|
|
* @param string $valueField
|
|
* @param integer|null $id
|
|
* @return void
|
|
*/
|
|
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
/**
|
|
* toSitemapTag method
|
|
*
|
|
* @return Url|string|array
|
|
*/
|
|
public function toSitemapTag(): Url | string | array
|
|
{
|
|
$model = $this;
|
|
$route = 'fo.news.show';
|
|
$params = ['publishedNews' => $model];
|
|
return SitemapHelper::generateUrl($route, $params, $model);
|
|
}
|
|
}
|