156 lines
5.1 KiB
PHP
156 lines
5.1 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 Titre de l'engagement local.
|
|
* @property string $text WYSIWYG - Texte à propos de l'engagement local.
|
|
* @property string $picture1 Path de la première image (587px * 429px).
|
|
* @property string $picture1_alt Attribut alt de la première image.
|
|
* @property string $picture1_title Attribut title de la première image.
|
|
* @property string|null $picture2 Path de la deuxième image (587px * 429px).
|
|
* @property string|null $picture2_alt Attribut alt de la deuxième image.
|
|
* @property string|null $picture2_title Attribut title de la deuxième image.
|
|
* @property string|null $picture3 Path de la troisième image (587px * 429px).
|
|
* @property string|null $picture3_alt Attribut alt de la troisième image
|
|
* @property string|null $picture3_title Attribut title de la troisième image.
|
|
* @property integer $order Numéro permettant d'ordonner les engagements locaux.
|
|
* @property boolean $published L'engagement local est-il visible ?
|
|
* Maximum 2 devront être visibles simultanément
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class LocalCommitment extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'text',
|
|
'picture1',
|
|
'picture1_alt',
|
|
'picture1_title',
|
|
'picture2',
|
|
'picture2_alt',
|
|
'picture2_title',
|
|
'picture3',
|
|
'picture3_alt',
|
|
'picture3_title',
|
|
'order',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* 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::manageFields($model);
|
|
self::setOrder($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
self::cleanPicturesAttributes($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture1');
|
|
FileStorageHelper::removeOldFile($model, 'picture2');
|
|
FileStorageHelper::removeOldFile($model, 'picture3');
|
|
});
|
|
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture1');
|
|
FileStorageHelper::removeFile($model, 'picture2');
|
|
FileStorageHelper::removeFile($model, 'picture3');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
self::checkPublished($model);
|
|
$model->picture1 = FileStorageHelper::storeFile($model, $model->picture1, true);
|
|
$model->picture2 = FileStorageHelper::storeFile($model, $model->picture2, true);
|
|
$model->picture3 = FileStorageHelper::storeFile($model, $model->picture3, true);
|
|
}
|
|
|
|
/**
|
|
* 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 there are already 2 published local commitments.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function checkPublished(self $model)
|
|
{
|
|
Validator::make(['published' => $model->published], [
|
|
'published' => Rule::prohibitedIf(
|
|
$model::where('published', true)->count() === 2
|
|
&& $model->published === true
|
|
&& $model->published !== $model->getOriginal('published')
|
|
),
|
|
], [
|
|
'published.prohibited' => trans('Seuls 2 engagements locaux peuvent être publiés en même temps.')
|
|
])->validate();
|
|
}
|
|
|
|
/**
|
|
* Clean pictures alt/title field when there's no picture.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function cleanPicturesAttributes(self $model)
|
|
{
|
|
if (empty($model->picture2)) {
|
|
$model->picture2_alt = "";
|
|
$model->picture2_title = "";
|
|
};
|
|
if (empty($model->picture3)) {
|
|
$model->picture3_alt = "";
|
|
$model->picture3_title = "";
|
|
};
|
|
}
|
|
}
|