ldc-legaulois/app/Models/DemarcheSlide.php
2024-10-08 11:57:56 +02:00

212 lines
7.2 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;
use Illuminate\Support\Facades\Cache;
/**
* @property string $title Slide title.
* @property string $text Slide picture.
* @property string $picture Slide picture.
* @property string $picture_alt Slide alt attribute.
* @property string $picture_title Slide title attribute.
* @property string|null $button_1_text Slide button n°1 text.
* @property string|null $button_1_url Slide button n°1 url.
* @property string|null $button_2_text Slide button n°2 text.
* @property string|null $button_2_url Slide button n°2 url.
* @property integer $order Slide order.
* @property boolean $button_1_status Slide button n°1 publish status.
* @property boolean $button_1_target Slide button n°1 target.
* @property boolean $button_2_status Slide button n°2 publish status.
* @property boolean $button_2_target Slide button n°2 target.
* @property boolean $published Slide publish status.
* @property-read \Illuminate\Support\Carbon $published_at Slide published_at.
* @property-read \Illuminate\Support\Carbon $unpublished_at Slide unpublished_at.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class DemarcheSlide extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'text',
'picture',
'picture_alt',
'picture_title',
'button_1_status',
'button_1_text',
'button_1_url',
'button_1_target',
'button_2_status',
'button_2_text',
'button_2_url',
'button_2_target',
'order',
'published',
'publish_at',
'unpublish_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'button_1_status' => 'boolean',
'button_1_target' => 'boolean',
'button_2_status' => 'boolean',
'button_2_target' => 'boolean',
'publish_at' => 'datetime',
'unpublish_at' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $demarcheSlide) {
self::setOrder($demarcheSlide);
self::makeStatusSense($demarcheSlide);
self::setButtons($demarcheSlide);
$demarcheSlide->picture = FileStorageHelper::storeFile($demarcheSlide, $demarcheSlide->picture, true);
});
static::created(function () {
self::resetCache();
});
static::updating(function (self $demarcheSlide) {
self::makeStatusSense($demarcheSlide);
self::setButtons($demarcheSlide);
$demarcheSlide->picture = FileStorageHelper::storeFile($demarcheSlide, $demarcheSlide->picture, true);
});
static::updated(function (self $demarcheSlide) {
FileStorageHelper::removeOldFile($demarcheSlide, 'picture');
self::resetCache();
});
static::deleted(function (self $demarcheSlide) {
FileStorageHelper::removeFile($demarcheSlide, 'picture');
self::resetCache();
});
}
// * METHODS
/**
* Set model's published date.
*
* @param self $demarcheSlide
* @return void
*/
private static function setButtons(self $demarcheSlide): void
{
if (!$demarcheSlide->button_1_status) {
$demarcheSlide->button_1_text = null;
$demarcheSlide->button_1_url = null;
$demarcheSlide->button_1_target = false;
}
if (!$demarcheSlide->button_2_status) {
$demarcheSlide->button_2_text = null;
$demarcheSlide->button_2_url = null;
$demarcheSlide->button_2_target = false;
}
}
/**
* Adjusts demarcheSlide status using current status and publish dates
*
* @param self $demarcheSlide
* @return void
*/
private static function makeStatusSense(self $demarcheSlide): void
{
/** @var \Carbon\Carbon|null */
$publish_at = $demarcheSlide->publish_at;
/** @var \Carbon\Carbon|null */
$unpublish_at = $demarcheSlide->unpublish_at;
$wantedStatus = $demarcheSlide->published;
if ($publish_at or $unpublish_at) {
$demarcheSlide->published = $demarcheSlide->planificated_status;
}
if ($wantedStatus !== $demarcheSlide->published) {
session()->flash('bo.info', \trans(
'La publication est modifiée en concordance avec les dates enregistrées.
Pour modifier l\'état de publication, veuillez modifier ou enlever les dates correspondantes.'
));
}
}
/**
* Getter the current status following planification dates planificated_status
*
* @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;
}
/**
* Set model's order after the last element of the list.
*
* @param self $demarcheSlide
* @return void
*/
private static function setOrder(self $demarcheSlide): void
{
$demarcheSlide->order = \intval(self::query()->max('order')) + 1;
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('publishedDemarcheSlidesOrderedByOrder');
self::getPublishedOrdered();
}
/**
* Get all published demarche slides ordered by order.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getPublishedOrdered(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('publishedDemarcheSlidesOrderedByOrder', function () {
return self::query()
->where('published', true)
->orderBy('order')
->get();
});
}
}