ldc-nature-and-respect/app/Models/Slider.php

156 lines
3.8 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-03-24 17:25:16 +01:00
use App\Enums\Sliders\TypeEnum;
2023-03-15 13:51:01 +01:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
2023-03-15 13:51:01 +01:00
/**
* @property integer $id
* @property boolean $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 $unpublish_at
* @property string $publish_at
* @property string $created_at
* @property string $updated_at
*/
class Slider extends Model
{
2023-03-16 09:47:16 +01:00
use HasFactory;
use HasTranslations;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
* @var array
*/
protected $fillable = [
'type',
'title',
'subtitle',
'description',
'link',
'link_title',
'link_new_tab',
'picture',
2023-03-24 17:25:16 +01:00
'picture_alt',
'picture_title',
2023-03-15 13:51:01 +01:00
'status',
'order',
'unpublish_at',
'publish_at',
'created_at',
'updated_at'
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
*/
public $translatable = [
'title',
'subtitle',
'description',
2023-04-20 10:22:06 +00:00
'link',
'link_title',
'picture_alt',
'picture_title'
];
2023-03-15 13:51:01 +01:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
2023-03-24 17:25:16 +01:00
'type' => TypeEnum::class,
2023-04-20 12:16:20 +00:00
'status' => 'bool',
2023-03-24 17:25:16 +01:00
'publish_at' => 'datetime',
'unpublish_at' => 'datetime'
2023-03-15 13:51:01 +01:00
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
2023-03-24 17:25:16 +01:00
self::assertFieldIsUnique('title', $model->title);
2023-03-15 13:51:01 +01:00
self::manageFields($model);
2023-03-24 17:25:16 +01:00
self::setOrder($model);
if (!$model->status) {
$model->publish_at = null;
} else {
$model->unpublish_at = null;
}
2023-03-15 13:51:01 +01:00
});
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
/**
2023-03-24 17:25:16 +01:00
* Common methods function.
2023-03-15 13:51:01 +01:00
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
2023-03-24 17:25:16 +01:00
if ($model->link) {
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
}
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
$model->description = clean($model->description);
2023-04-18 10:49:39 +02:00
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
2023-03-15 13:51:01 +01:00
}
2023-03-24 17:25:16 +01:00
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function setOrder(Model $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
2023-03-15 13:51:01 +01:00
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param mixed $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);
}
}