157 lines
4.6 KiB
PHP
157 lines
4.6 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;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* @property string $catchline Testimonial catchline.
|
|
* @property string $content Testimonial content.
|
|
* @property string $identity Identity of the person.
|
|
* @property string $job Job of the person.
|
|
* @property string $picture Path of the picture of the testimonial (550x315).
|
|
* @property string $picture_alt Alt attribute of the picture.
|
|
* @property string $picture_title Title attribute of the picture.
|
|
* @property string|null $video_id Id of the video to display (youtube).
|
|
* @property int $order Testimonial order.
|
|
* @property boolean $published Testimonial publish status.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Testimonial extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'catchline',
|
|
'content',
|
|
'identity',
|
|
'job',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'video_id',
|
|
'published',
|
|
'order',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
self::resetCache();
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
self::resetCache();
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
self::checkPublishedCount($model, 5);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* Check if the max amount of published models is reached.
|
|
*
|
|
* @param self $model
|
|
* @param integer $max
|
|
* @return void
|
|
*/
|
|
private static function checkPublishedCount(self $model, int $max): void
|
|
{
|
|
Validator::make(['published' => $model->published], [
|
|
'published' => Rule::prohibitedIf(
|
|
$model::where('published', true)->count() >= $max
|
|
&& $model->published === true
|
|
&& $model->published !== $model->getOriginal('published')
|
|
),
|
|
], [
|
|
'published.prohibited' => trans('Il ne peut y avoir que :max :model:plural publié:plural en même temps.', [
|
|
'max' => $max,
|
|
'model' => str(trans('models.classes.testimonial')),
|
|
'plural' => $max > 1 ? 's' : '',
|
|
])
|
|
])->validate();
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedTestimonialsOrderedByOrder');
|
|
self::getPublishedOrdered();
|
|
}
|
|
|
|
/**
|
|
* Get all published Testimonials ordered by order.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getPublishedOrdered(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::rememberForever('publishedTestimonialsOrderedByOrder', function () {
|
|
return self::query()->where('published', true)->orderBy('order')->get();
|
|
});
|
|
}
|
|
}
|