156 lines
4 KiB
PHP
156 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Sliders\TypeEnum;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property \App\Enums\Sliders\TypeEnum $type
|
|
* @property string $title
|
|
* @property string $subtitle
|
|
* @property string $description
|
|
* @property string $link
|
|
* @property string $link_title
|
|
* @property boolean $link_new_tab
|
|
* @property string $picture
|
|
* @property boolean $status
|
|
* @property integer $order
|
|
* @property string|null $unpublish_at
|
|
* @property string|null $publish_at
|
|
* @property Carbon\Carbon $created_at
|
|
* @property Carbon\Carbon $updated_at
|
|
*/
|
|
class Slider extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* @var array<string>
|
|
*/
|
|
protected $fillable = [
|
|
'type',
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'link',
|
|
'link_title',
|
|
'link_new_tab',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'status',
|
|
'order',
|
|
'locales',
|
|
'unpublish_at',
|
|
'publish_at',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'link',
|
|
'link_title',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => TypeEnum::class,
|
|
'status' => 'bool',
|
|
'publish_at' => 'datetime',
|
|
'unpublish_at' => 'datetime',
|
|
'locales' => 'array',
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('title', $model->title);
|
|
self::manageFields($model);
|
|
self::setOrder($model);
|
|
if (!$model->status) {
|
|
$model->publish_at = null;
|
|
} else {
|
|
$model->unpublish_at = null;
|
|
}
|
|
});
|
|
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
|
|
|
|
/**
|
|
* Common methods function.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model)
|
|
{
|
|
if ($model->link) {
|
|
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
|
|
}
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
}
|