173 lines
5.8 KiB
PHP
173 lines
5.8 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\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* @property string $title_line1 Première ligne du titre.
|
|
* @property string $title_line2 Deuxième ligne du titre.
|
|
* @property string $picture Path de l'image d'aperçu du jeu (585px * 415px).
|
|
* @property string $picture_alt Attribut alt de l'image d'aperçu du jeu.
|
|
* @property string $picture_title Attribut title de l'image d'aperçu du jeu.
|
|
* @property string $text WYSIWYG - Contenu de la partie "jeu du moment".
|
|
* Minimum 15 caractères, maximum 32 mots.
|
|
* @property boolean $button_status Le bouton est-il visible ?.
|
|
* @property string $button_label Libellé du bouton, max 35 caractères.
|
|
* Obligatoire si button_status est true.
|
|
* @property string $button_url Url du bouton, obligatoire si button_status est true.
|
|
* @property boolean $button_new_tab Le lien du bouton doit-il s'ouvrir dans un nouvel onglet ?.
|
|
* @property boolean $published Le jeu du moment est-il visible ? 1 seul à la fois maximum
|
|
* @property \Carbon\Carbon|null $publish_at Date de publication du jeu du moment.
|
|
* @property \Carbon\Carbon|null $unpublish_at Date de dépublication du jeu du moment.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Game extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title_line1',
|
|
'title_line2',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'text',
|
|
'button_status',
|
|
'button_label',
|
|
'button_url',
|
|
'button_new_tab',
|
|
'published',
|
|
'publish_at',
|
|
'unpublish_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'button_status' => 'boolean',
|
|
'button_new_tab' => 'boolean',
|
|
'published' => '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 $model) {
|
|
self::manageFields($model);
|
|
});
|
|
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
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
self::makeStatusSense($model);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* Adjusts model status using current status and publish dates
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function makeStatusSense(self $model): void
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $model->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $model->unpublish_at;
|
|
$wantedStatus = $model->published;
|
|
if ($publish_at or $unpublish_at) {
|
|
$model->published = $model->planificated_status;
|
|
}
|
|
self::checkPublished($model);
|
|
if ($wantedStatus !== $model->published) {
|
|
$previousMessage = session()->get('bo.info', '');
|
|
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.'
|
|
) . " \n{$previousMessage}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Check if there is already 1 published game.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function checkPublished(self $model)
|
|
{
|
|
Validator::make(['published' => $model->published], [
|
|
'published' => Rule::prohibitedIf(
|
|
$model::where('published', true)->count() === 1
|
|
&& $model->published === true
|
|
&& $model->published !== $model->getOriginal('published')
|
|
),
|
|
], [
|
|
'published.prohibited' => trans('Seul 1 jeu du moment peut être publié.')
|
|
])->validate();
|
|
}
|
|
}
|