151 lines
4.8 KiB
PHP
151 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\HasFlashStatus;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Slide extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasFlashStatus;
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'link_new_tab' => 'bool',
|
|
'order' => 'int',
|
|
'status' => 'bool',
|
|
'publish_at' => 'datetime',
|
|
'unpublish_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'title', 'text', 'link', 'link_label',
|
|
'link_new_tab', 'picture',
|
|
'status', 'publish_at',
|
|
'unpublish_at', 'slider_id'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title);
|
|
ToolboxHelper::assertNoInternalBoUrls('text', $model->text ?? '');
|
|
ToolboxHelper::assertNoInternalBoUrls('link', $model->link ?? '');
|
|
// * TRANSFORMATIONS
|
|
if ($model->text) {
|
|
$model->text = clean($model->text);
|
|
$model->text = FileStorageHelper::storeModelFilesFromHtml($model, $model->text);
|
|
}
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->makeStatusSense();
|
|
});
|
|
static::created(function (self $model) {
|
|
self::flashStatus($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title, $model->id);
|
|
ToolboxHelper::assertNoInternalBoUrls('text', $model->text ?? '');
|
|
ToolboxHelper::assertNoInternalBoUrls('link', $model->link ?? '');
|
|
// * TRANSFORMATIONS
|
|
if ($model->text) {
|
|
$model->text = clean($model->text);
|
|
$model->text = FileStorageHelper::storeModelFilesFromHtml($model, $model->text);
|
|
}
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->makeStatusSense();
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldModelFilesFromHtml($model->text ?? '', $model->getOriginal('text') ?? '');
|
|
self::flashStatus($model, true);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeModelFilesOfHtml($model->text ?? '');
|
|
});
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
|
|
/**
|
|
* Adjusts model status using current status and publish dates
|
|
*
|
|
* @return void
|
|
*/
|
|
public function makeStatusSense(): void
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $this->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $this->unpublish_at;
|
|
// * Ignore unpublish_at in the past
|
|
if ($unpublish_at and $unpublish_at->lessThan(now())) {
|
|
return;
|
|
}
|
|
// * Ignore publish_at in the futur
|
|
if ($publish_at and $publish_at->greaterThan(now())) {
|
|
return;
|
|
}
|
|
if ($publish_at or $unpublish_at) {
|
|
$this->status = $this->planificated_status;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getter the current status following planification dates
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function getPlanificatedStatusAttribute(): bool
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $this->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $this->unpublish_at;
|
|
if ($publish_at and $unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(now()) and $publish_at->lessThan(now());
|
|
}
|
|
if ($publish_at) {
|
|
return $publish_at->lessThan(now());
|
|
}
|
|
if ($unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(now());
|
|
}
|
|
return false;
|
|
}
|
|
}
|