97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property \App\Enums\Slides\SliderTypeEnum $slider Slider that contains the slide.
|
|
* @property string $title Title.
|
|
* @property string $content WYSIWYG - Content.
|
|
* @property string $video_id Id of the Youtube video.
|
|
* @property boolean $button_status Is the button visible ?.
|
|
* @property string $button_label Button label, max 35 chars.
|
|
* Required if button_status == true.
|
|
* @property string $button_url Button URL. equired if the button is visible.
|
|
* @property boolean $button_new_tab Does the link need to be opened in a new tab ?
|
|
* @property string $picture Path of the image.
|
|
* @property string $picture_alt Alt attribute of the image.
|
|
* @property string $picture_title Title attribute of the image.
|
|
* @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 Slide extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'slider',
|
|
'title',
|
|
'content',
|
|
'video_id',
|
|
'button_status',
|
|
'button_label',
|
|
'button_url',
|
|
'button_new_tab',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'slider' => \App\Enums\Slides\SliderTypeEnum::class,
|
|
'button_status' => 'boolean',
|
|
'button_new_tab' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $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
|
|
{
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
}
|