2021-04-14 17:33:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2021-04-14 17:33:55 +02:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Interfaces\ModelWithOrder;
|
|
|
|
|
use App\Interfaces\ModelWithStatus;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Traits\Models\HasOrder;
|
|
|
|
|
use App\Traits\Models\HasStatus;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Contracts\Session\Session;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2021-04-14 17:33:55 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Shared\Helpers\DatesHelper;
|
2021-04-14 17:33:55 +02:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/**
|
|
|
|
|
* @property int $id
|
|
|
|
|
* @property string $title
|
|
|
|
|
* @property string|null $text
|
2026-09-29 15:38:59 +02:00
|
|
|
* @property string|null $picture
|
2026-06-14 21:32:07 +02:00
|
|
|
* @property string|null $picture_alt
|
|
|
|
|
* @property string|null $picture_title
|
|
|
|
|
* @property string|null $link_label
|
|
|
|
|
* @property string|null $link
|
|
|
|
|
* @property bool $link_new_tab
|
|
|
|
|
* @property bool $connected Slide visible only for connected customers.
|
|
|
|
|
* @property int $order
|
|
|
|
|
* @property bool $status
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $date_start
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $date_end
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
|
* @property-read bool $planificated_status
|
|
|
|
|
*
|
|
|
|
|
* @method static \Database\Factories\SlideFactory factory($count = null, $state = [])
|
|
|
|
|
*
|
|
|
|
|
* @mixin \Eloquent
|
|
|
|
|
*/
|
|
|
|
|
#[Fillable([
|
|
|
|
|
'title',
|
|
|
|
|
'text',
|
|
|
|
|
'link',
|
|
|
|
|
'link_label',
|
|
|
|
|
'link_new_tab',
|
|
|
|
|
'connected',
|
|
|
|
|
'picture',
|
|
|
|
|
'picture_alt',
|
|
|
|
|
'picture_title',
|
|
|
|
|
'status',
|
|
|
|
|
'date_start',
|
|
|
|
|
'date_end',
|
|
|
|
|
])]
|
|
|
|
|
#[Guarded([
|
|
|
|
|
'status',
|
|
|
|
|
'order',
|
|
|
|
|
])]
|
2026-09-29 14:51:46 +02:00
|
|
|
class Slide extends Model implements ModelWithOrder, ModelWithStatus
|
2021-04-14 17:33:55 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @use HasFactory<\Database\Factories\SlideFactory> */
|
2021-04-14 17:33:55 +02:00
|
|
|
use HasFactory;
|
|
|
|
|
|
2026-09-29 14:51:46 +02:00
|
|
|
use HasOrder;
|
|
|
|
|
use HasStatus;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
#[\Override]
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model): void {
|
|
|
|
|
self::manageFields($model);
|
|
|
|
|
self::makeStatusSense($model);
|
|
|
|
|
});
|
|
|
|
|
static::updating(function (self $model): void {
|
|
|
|
|
self::manageFields($model);
|
|
|
|
|
self::makeStatusSense($model);
|
|
|
|
|
});
|
|
|
|
|
static::deleting(function (self $model): void {
|
|
|
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 19:44:29 +02:00
|
|
|
public function isVisible(): bool
|
|
|
|
|
{
|
|
|
|
|
return DatesHelper::isIn($this->date_start, $this->date_end);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<bool,never> */
|
|
|
|
|
protected function planificatedStatus(): Attribute
|
|
|
|
|
{
|
2026-10-02 19:44:29 +02:00
|
|
|
return Attribute::get(fn (): bool => $this->isVisible());
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function manageFields(self $model): void
|
|
|
|
|
{
|
|
|
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
2026-10-28 08:53:34 +01:00
|
|
|
|
|
|
|
|
if ($model->order === null) {
|
|
|
|
|
$max = self::query()->max('order');
|
|
|
|
|
$model->order = is_scalar($max) ? intval($max) + 1 : 1;
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function makeStatusSense(self $model): void
|
|
|
|
|
{
|
|
|
|
|
$actualStatus = $model->status;
|
|
|
|
|
$model->status = $model->planificatedStatus;
|
|
|
|
|
|
|
|
|
|
if ($actualStatus !== $model->status) {
|
|
|
|
|
resolve(Session::class)->flash('bo.info', \trans(
|
2026-10-03 10:50:06 +02:00
|
|
|
'The publication status is updated according to the scheduled dates. '
|
2026-10-28 08:53:34 +01:00
|
|
|
. 'To change the publication status, please edit or remove the corresponding dates.'
|
2026-06-14 21:32:07 +02:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function casts(): array
|
2022-09-08 11:29:39 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return [
|
|
|
|
|
'link_new_tab' => 'bool',
|
|
|
|
|
'connected' => 'bool',
|
|
|
|
|
'status' => 'bool',
|
|
|
|
|
'order' => 'int',
|
|
|
|
|
'date_start' => 'datetime',
|
|
|
|
|
'date_end' => 'datetime',
|
|
|
|
|
];
|
2021-04-14 17:33:55 +02:00
|
|
|
}
|
|
|
|
|
}
|