ldc-loue/app/Models/Slider.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

108 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use App\Casts\HtmlColor;
use App\Casts\NiceDate;
use App\Enums\Sliders\SliderPosition;
use App\Enums\Sliders\SliderType;
use App\Lib\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Slider extends Model
{
use CastsEnums;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'title_color',
'title_color_bg',
'subtitle',
'subtitle_color',
'subtitle_color_bg',
'position',
'type',
'picture',
'link',
'link_title',
'link_color',
'link_color_bg',
'link_new_tab',
'status',
'order'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'position' => SliderPosition::class,
'type' => SliderType::class
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'title_color' => HtmlColor::class,
'title_color_bg' => HtmlColor::class,
'subtitle_color' => HtmlColor::class,
'subtitle_color_bg' => HtmlColor::class,
'link_color' => HtmlColor::class,
'link_color_bg' => HtmlColor::class,
'link_new_tab' => 'bool',
'status' => 'bool'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
$model->order = self::whereType($model->type)->max('order') ?? 0;
});
static::updating(function (self $model) {
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
}
});
static::updated(function (self $model) {
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = ToolboxHelper::removeOldImage($model, 'picture');
}
});
static::deleting(function (self $model) {
ToolboxHelper::removeImage($model, 'picture');
});
}
// * METHODS
/**
* updated_at_date getter
*
* @return string
*/
public function getUpdatedAtDateAttribute(): string
{
return (new NiceDate())->get($this, 'updated_at', $this->updated_at, []);
}
}