ldc-le-gaulois-pro/app/Models/Slide.php

159 lines
6 KiB
PHP
Raw Permalink Normal View History

2022-12-01 12:25:55 +01:00
<?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;
2022-12-08 13:10:13 +01:00
use Illuminate\Validation\ValidationException;
2022-12-01 12:25:55 +01:00
/**
* @property integer $id Id.
* @property string $title Title.
2024-02-05 09:33:08 +01:00
* @property string $content WYSWYG content.
* @property boolean $button_display Display the button.
2024-02-05 09:33:08 +01:00
* @property string|null $button_text Title of the button.
* @property string|null $button_url Link of the button.
* @property boolean|null $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 boolean $published Is the slide published ?
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
2022-12-01 12:25:55 +01:00
class Slide extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
2022-12-01 12:25:55 +01:00
*/
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',
'published'
2022-12-01 12:25:55 +01:00
];
/**
* The attributes that should be cast.
*
2024-02-05 09:33:08 +01:00
* @var array<string,string>
2022-12-01 12:25:55 +01:00
*/
protected $casts = [
'button_display' => 'bool',
'button_targetblank' => 'bool',
'published' => 'bool'
2022-12-01 12:25:55 +01:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
2022-12-01 12:25:55 +01:00
{
static::creating(function (self $model) {
2022-12-07 14:31:54 +01:00
self::manageFields($model);
$model->order = \intval(self::query()->max('order')) + 1;
2022-12-01 12:25:55 +01:00
});
static::updating(function (self $model) {
2022-12-07 14:31:54 +01:00
self::manageFields($model);
2022-12-01 12:25:55 +01:00
});
static::updated(function (self $model) {
2022-12-01 16:08:01 +01:00
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
2022-12-01 12:25:55 +01:00
});
static::deleting(function (self $model) {
self::assertNumberOfSlides($model, true);
2022-12-08 13:10:13 +01:00
});
2022-12-01 16:08:01 +01:00
static::deleted(function (self $model) {
2022-12-01 12:25:55 +01:00
FileStorageHelper::removeFile($model, 'picture');
2022-12-01 16:08:01 +01:00
FileStorageHelper::removeFile($model, 'picture_thumbnail');
2022-12-01 12:25:55 +01:00
});
}
// * METHODS
2022-12-07 14:31:54 +01:00
2022-12-01 12:25:55 +01:00
/**
* Common methods function.
2022-12-01 12:25:55 +01:00
*
2024-02-05 09:33:08 +01:00
* @param self $model
2022-12-01 12:25:55 +01:00
* @return void
*/
2024-02-05 09:33:08 +01:00
private static function manageFields(self $model): void
2022-12-01 12:25:55 +01:00
{
2022-12-08 13:10:13 +01:00
self::assertTitleIsUnique($model->title, $model->id);
self::assertNumberOfSlides($model);
2022-12-07 14:31:54 +01:00
ToolboxHelper::assertNoInternalBoUrls($model->content, 'content');
2022-12-08 13:10:13 +01:00
$model->content = clean($model->content);
2022-12-07 14:31:54 +01:00
if ($model->button_url) {
ToolboxHelper::assertNoInternalBoUrls($model->button_url, 'button_url');
$model->button_url = clean($model->button_url);
2022-12-01 12:25:55 +01:00
}
2022-12-07 14:31:54 +01:00
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
}
/**
2022-12-08 13:10:13 +01:00
* Asserts the number of slides in the slider.
*
* @param self $model
* @param boolean $isDeleting
2022-12-08 13:10:13 +01:00
* @return void
* @throws \Illuminate\Validation\ValidationException If there is less 1 or more than 4 slides.
*/
private static function assertNumberOfSlides(self $model, bool $isDeleting = false): void
2022-12-08 13:10:13 +01:00
{
if (
$isDeleting &&
$model->published &&
self::query()->where('published', true)->count() <= 1
) {
throw ValidationException::withMessages(
[trans('Minimum 1 slide doit être publiée, la slide n\'a pas été supprimée.')]
);
}
if ($model->published !== $model->getOriginal('published')) {
if (self::query()->where('published', true)->count() + 1 > 4 && $model->published) {
$model->published = false;
\session()->flash('info', \trans('Maximum 4 slides peuvent être publiées, la slide a été dépubliée.'));
} elseif (self::query()->where('published', true)->count() <= 1 && !$model->published) {
$model->published = true;
\session()->flash('info', \trans('Minimum 1 slide doit être publiée, la slide a été publiée.'));
}
}
2022-12-08 13:10:13 +01:00
}
/**
* Asserts using validation that the title is unique.
2022-12-07 14:31:54 +01:00
*
* @param string $title
2022-12-07 14:31:54 +01:00
* @param integer|null $id
* @return void
*/
private static function assertTitleIsUnique(string $title, ?int $id = null): void
2022-12-07 14:31:54 +01:00
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
2022-12-01 12:25:55 +01:00
}
}