ldc-boutique-cnc/app/Models/Slide.php
2022-11-08 15:41:10 +01:00

173 lines
5.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Slide extends Model
{
use HasFactory;
/**
* 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()
{
$flashStatus = function (self $model, bool $old = false) {
if ($old and $model->getOriginal('status') == $model->status) {
return;
}
if ($model->status) {
session()->flash('info', trans('Slide est maintenant publié'));
} else {
session()->flash('warning', trans('Slide est maintenant dépublié'));
}
};
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
static::assertTitleIsUnique($model->title);
// * TRANSFORMATIONS
if ($model->text) {
ToolboxHelper::assertNoInternalBoUrls('text', $model->text);
$model->text = clean($model->text);
$model->text = FileStorageHelper::storeModelFilesFromHtml($model, $model->text);
}
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
$model->makeStatusSense();
});
static::created(function (self $model) use ($flashStatus) {
$flashStatus($model);
});
static::updating(function (self $model) {
// * ASSERTIONS
static::assertTitleIsUnique($model->title, $model->id);
// * TRANSFORMATIONS
if ($model->text) {
ToolboxHelper::assertNoInternalBoUrls('text', $model->text);
$model->text = clean($model->text);
$model->text = FileStorageHelper::storeModelFilesFromHtml($model, $model->text);
}
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
$model->makeStatusSense();
});
static::updated(function (self $model) use ($flashStatus) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldModelFilesFromHtml($model->text ?? '', $model->getOriginal('text') ?? '');
$flashStatus($model, true);
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeModelFilesOfHtml($model->text ?? '');
});
}
// * RELATIONSHIPS
/**
* BelongsTo slider relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function slider()
{
return $this->belongsTo(Slider::class);
}
// * 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;
}
}