ldc-loue/app/Models/News.php
Jérémy Munsch 0751114f4a
BO News, Tags, Sliders
CKeditor + word counter + Image Input + Taggable VUE3
Better Xss Sanitizer
Handle Video Youtube + Image Upload + Inline Image from url
Better Image Input component with reset, easizer resize
Full Responsive BO
2022-06-04 15:55:36 +02:00

161 lines
4.8 KiB
PHP

<?php
namespace App\Models;
use App\Casts\NiceDate;
use App\Enums\PublishedStatus;
use App\Lib\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Kwaadpepper\Enum\Traits\CastsEnums;
class News extends Model
{
use CastsEnums;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'slug',
'seo_title',
'seo_description',
'content',
'picture',
'picture_alt',
'picture_title',
'published'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'published' => PublishedStatus::class
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'published_at' => 'datetime'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
static::assertTitleIsUnique($model->title);
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
$model->slug = Str::slug($model->title);
$model->content = clean($model->content);
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
if ($model->published->equals(PublishedStatus::published())) {
$model->published_at = now();
}
});
static::updating(function (self $model) {
static::assertTitleIsUnique($model->title, $model->id);
ToolboxHelper::assertNoInternalBoUrls('content', $model->content);
$model->slug = Str::slug($model->title);
$model->content = clean($model->content);
// * Update published_at it publish status changed
if (
$model->published->value != $model->getOriginal('published') and
$model->published->equals(PublishedStatus::published())
) {
$model->published_at = now();
}
// * Store new picture if changed
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
}
});
static::updated(function (self $model) {
// * Remove unused inline uploaded images
if ($model->content != $model->getOriginal('content')) {
$updatedPicturesPath = collect(
ToolboxHelper::getCkEditorLocalUploadedFiles($model->content)
);
collect(ToolboxHelper::getCkEditorLocalUploadedFiles($model->getOriginal('content')))
->map(function (string $originalPicturePath) use ($updatedPicturesPath) {
// * If a picture is not used anymore
if (!$updatedPicturesPath->contains($originalPicturePath)) {
if (File::exists(\public_path($originalPicturePath))) {
File::delete(\public_path($originalPicturePath));
}
}
});
}
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = ToolboxHelper::removeOldImage($model, 'picture');
}
});
static::deleting(function (self $model) {
ToolboxHelper::removeImage($model, 'picture');
});
}
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName(): string
{
return 'slug';
}
// * RELATIONSHIPS
/**
* MorphToMany tags relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
// * METHODS
/**
* Asserts using validation that the title is unique
*
* @param mixed $title
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
*/
private static function assertTitleIsUnique($title, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
}
/**
* updated_at_date getter
*
* @return string
*/
public function getUpdatedAtDateAttribute(): string
{
return (new NiceDate())->get($this, 'published_at', $this->published_at, []);
}
}