62 lines
2 KiB
PHP
62 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Models;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
/**
|
|
* If there is a static function in a trait (named boot[TraitName]),
|
|
* it will be executed as the boot() function would on an Eloquent model.
|
|
* Futhermore, event in the Model observer doesn't erase actual events in the model,
|
|
* but adds them.
|
|
*/
|
|
trait PlanificatedStatus
|
|
{
|
|
/**
|
|
* Adjusts model status using current status and publish dates.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function makeStatusSense(self $model): void
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $model->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $model->unpublish_at;
|
|
$wantedStatus = $model->published;
|
|
if ($publish_at or $unpublish_at) {
|
|
$model->published = $model->planificated_status;
|
|
}
|
|
if ($wantedStatus !== $model->published) {
|
|
$previousMessage = session()->get('bo.info', '');
|
|
session()->flash('bo.info', \trans(
|
|
'La publication est modifiée en concordance avec les dates enregistrées.
|
|
Pour modifier l\'état de publication, veuillez modifier ou enlever les dates correspondantes.'
|
|
) . " \n{$previousMessage}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getter the current status following planification dates planificated_status.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function getPlanificatedStatusAttribute(): bool
|
|
{
|
|
/** @var \Carbon\Carbon|null */
|
|
$publish_at = $this->publish_at;
|
|
/** @var \Carbon\Carbon|null */
|
|
$unpublish_at = $this->unpublish_at;
|
|
if ($publish_at and $unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(Carbon::now()) and $publish_at->lessThan(Carbon::now());
|
|
}
|
|
if ($publish_at) {
|
|
return $publish_at->lessThan(Carbon::now());
|
|
}
|
|
if ($unpublish_at) {
|
|
return $unpublish_at->greaterThanOrEqualTo(Carbon::now());
|
|
}
|
|
return false;
|
|
}
|
|
}
|