142 lines
5.1 KiB
PHP
142 lines
5.1 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;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* @property integer $id Id.
|
|
* @property string $title Title.
|
|
* @property integer $content WYSWYG content.
|
|
* @property boolean $button_display Display the button.
|
|
* @property string $button_text Title of the button.
|
|
* @property string $button_url Link of the button.
|
|
* @property boolean $button_targetblank Open in new window.
|
|
* @property string $picture Path of a picture.
|
|
* @property string $picture_alt Picture's attribute alt.
|
|
* @property string $picture_title Picture's attribute title.
|
|
* @property string $picture_thumbnail Path of a thumbnail picture.
|
|
* @property string $picture_thumbnail_alt Thumbnail picture's attribute alt.
|
|
* @property string $picture_thumbnail_title Thumbnail picture's attribute title.
|
|
* @property integer $order Order.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*/
|
|
class Slide extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'content',
|
|
'button_display',
|
|
'button_text',
|
|
'button_url',
|
|
'button_targetblank',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'picture_thumbnail',
|
|
'picture_thumbnail_alt',
|
|
'picture_thumbnail_title',
|
|
'order'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'button_display' => 'bool',
|
|
'button_targetblank' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
if (count(self::get()) >= 4) {
|
|
self::assertNumberOfSlides();
|
|
}
|
|
self::manageFields($model);
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
|
|
});
|
|
static::deleting(function () {
|
|
if (1 >= count(self::get())) {
|
|
self::assertNumberOfSlides();
|
|
}
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'picture_thumbnail');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Common methods function.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model): void
|
|
{
|
|
self::assertTitleIsUnique($model->title, $model->id);
|
|
ToolboxHelper::assertNoInternalBoUrls($model->content, 'content');
|
|
$model->content = clean($model->content);
|
|
if ($model->button_url) {
|
|
ToolboxHelper::assertNoInternalBoUrls($model->button_url, 'button_url');
|
|
$model->button_url = clean($model->button_url);
|
|
}
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
|
|
}
|
|
|
|
/**
|
|
* Asserts the number of slides in the slider.
|
|
*
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If there is less 1 or more than 4 slides.
|
|
*/
|
|
private static function assertNumberOfSlides(): void
|
|
{
|
|
throw ValidationException::withMessages([
|
|
'Opération impossible, minimum un et maximum quatre slides doivent être présentes.'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique.
|
|
*
|
|
* @param string $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
*/
|
|
private static function assertTitleIsUnique(string $title, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
}
|
|
}
|