Add publish and un publish times to sliders
This commit is contained in:
parent
5b8c3f2e10
commit
b431508e98
6 changed files with 201 additions and 4 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\SlidersStatusUpdate;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ class Kernel extends ConsoleKernel
|
|||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
$schedule->job(SlidersStatusUpdate::class)->everyMinute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -27,7 +27,9 @@ class StoreSliderRequest extends FormRequest
|
|||
{
|
||||
$this->merge([
|
||||
'link_new_tab' => $this->link_new_tab ? true : false,
|
||||
'status' => $this->status ? true : false
|
||||
'status' => $this->status ? true : false,
|
||||
'publish_at' => $this->publish_at ? \str_replace('T', ' ', $this->publish_at) : null,
|
||||
'unpublish_at' => $this->unpublish_at ? \str_replace('T', ' ', $this->unpublish_at) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +51,9 @@ class StoreSliderRequest extends FormRequest
|
|||
'link_color' => 'required|color_hex',
|
||||
'link_color_bg' => 'required|color_hex',
|
||||
'link_new_tab' => 'required|boolean',
|
||||
'status' => 'required|boolean'
|
||||
'status' => 'required|boolean',
|
||||
'publish_at' => 'nullable|date_format:Y-m-d H:i',
|
||||
'unpublish_at' => 'nullable|date_format:Y-m-d H:i',
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
41
app/Jobs/SlidersStatusUpdate.php
Normal file
41
app/Jobs/SlidersStatusUpdate.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Slider;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Update published and visibility attributes reading their
|
||||
* publish_at and unpublish_at attributes.
|
||||
*/
|
||||
class SlidersStatusUpdate
|
||||
{
|
||||
/**
|
||||
* Called when the job will be executed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$slidersToPublish = Slider::where('publish_at', '<=', Carbon::now()->toDateTime())
|
||||
->whereNotNull('publish_at')
|
||||
->whereStatus(false)->get();
|
||||
Log::debug(__CLASS__ . " sliders to publish " . $slidersToPublish->count());
|
||||
foreach ($slidersToPublish as $slider) {
|
||||
$slider->publish_at = null;
|
||||
$slider->status = true;
|
||||
$slider->saveOrFail();
|
||||
}
|
||||
$slidersToUnPublish = Slider::where('unpublish_at', '<=', Carbon::now()->toDateTime())
|
||||
->whereNotNull('unpublish_at')
|
||||
->whereStatus(true)->get();
|
||||
Log::debug(__CLASS__ . " sliders to unpublish " . $slidersToPublish->count());
|
||||
foreach ($slidersToUnPublish as $slider) {
|
||||
$slider->unpublish_at = null;
|
||||
$slider->status = false;
|
||||
$slider->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ use App\Enums\Sliders\SliderType;
|
|||
use App\Lib\ToolboxHelper;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||||
|
||||
class Slider extends Model
|
||||
|
|
@ -37,7 +38,9 @@ class Slider extends Model
|
|||
'link_color_bg',
|
||||
'link_new_tab',
|
||||
'status',
|
||||
'order'
|
||||
'order',
|
||||
'publish_at',
|
||||
'unpublish_at'
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -63,7 +66,9 @@ class Slider extends Model
|
|||
'link_color' => HtmlColor::class,
|
||||
'link_color_bg' => HtmlColor::class,
|
||||
'link_new_tab' => 'bool',
|
||||
'status' => 'bool'
|
||||
'status' => 'bool',
|
||||
'publish_at' => 'datetime',
|
||||
'unpublish_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -77,12 +82,27 @@ class Slider extends Model
|
|||
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
|
||||
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
|
||||
$model->order = self::whereType($model->type)->max('order') ?? 0;
|
||||
if (!$model->status) {
|
||||
$model->publish_at = null;
|
||||
} else {
|
||||
$model->unpublish_at = null;
|
||||
}
|
||||
});
|
||||
static::updating(function (self $model) {
|
||||
ToolboxHelper::assertNoInternalBoUrls('link', $model->link);
|
||||
if ($model->picture != $model->getOriginal('picture')) {
|
||||
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
|
||||
}
|
||||
// * If status changed
|
||||
if (\boolval($model->getOriginal('status')) !== $model->status) {
|
||||
if ($model->status) {
|
||||
$model->publish_at = null;
|
||||
Session::flash('info', trans('Le slider est à présent publié'));
|
||||
} else {
|
||||
$model->unpublish_at = null;
|
||||
Session::flash('warning', trans('Le slider est à présent dépublié'));
|
||||
}
|
||||
}
|
||||
});
|
||||
static::updated(function (self $model) {
|
||||
if ($model->picture != $model->getOriginal('picture')) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddPublishTimestampsToSliders extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('sliders', function (Blueprint $table) {
|
||||
$table->timestamp('publish_at')->after('order')->nullable()
|
||||
->comment('The date where the post published state should be changed to published.');
|
||||
$table->timestamp('unpublish_at')->after('order')->nullable()
|
||||
->comment('The date where the post published state should be changed to archived.');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('sliders', function (Blueprint $table) {
|
||||
$table->dropColumn(['publish_at', 'unpublish_at']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -66,6 +66,39 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<fieldset class="border p-3 mb-3">
|
||||
<legend>{{ __('Publication') }}</legend>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<span class="h6 text-info">{{ __('La zone suivante à modifier la visibilité.') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="sliderPublishedAt"><b>{{ __('Date de publication (Europe/Paris)') }}</b></label>
|
||||
<input id="sliderPublishedAt" name="publish_at" class="form-control publishTime" type="datetime-local" data-form-type="date" aria-describedby="sliderPublishAtHelp" value="{{ old('publish_at', ((isset($slider) and $slider->publish_at) ? $slider->publish_at->format('Y-m-d\TH:i') : '')) }}">
|
||||
@include('back.modules.input-error', [
|
||||
'inputName' => 'publish_at', 'helperId' => 'sliderPublishAtHelp' , 'helper' => __('Uniquement pour planifier la publication, le slider deviendra visible.')
|
||||
])
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="sliderUnPublishedAt"><b>{{ __('Date de fin de publication (Europe/Paris)') }}</b></label>
|
||||
<input id="sliderUnPublishedAt" name="unpublish_at" class="form-control publishTime" type="datetime-local" data-form-type="date" aria-describedby="sliderUnPublishAtHelp" value="{{ old('unpublish_at', ((isset($slider) and $slider->unpublish_at) ? $slider->unpublish_at->format('Y-m-d\TH:i') : '')) }}">
|
||||
@include('back.modules.input-error', [
|
||||
'inputName' => 'unpublish_at', 'helperId' => 'sliderUnPublishAtHelp' , 'helper' => __('Uniquement pour planifier la fin de la publication, le slider deviendra invisible.')
|
||||
])
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<fieldset class="border p-3 mb-3">
|
||||
|
|
@ -177,3 +210,65 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
|
||||
// Publish times
|
||||
let publishTimes = document.getElementsByClassName('publishTime');
|
||||
for (let input of publishTimes) {
|
||||
let parent = input.parentNode,
|
||||
dates = {
|
||||
start: 0,
|
||||
end: 0
|
||||
};
|
||||
input.originalValue = (new Date(input.value)).valueOf();
|
||||
if(input.originalValue === NaN) {
|
||||
input.originalValue = 0
|
||||
}
|
||||
if(input.id === 'sliderPublishedAt') {
|
||||
dates.start = input.originalValue;
|
||||
} else {
|
||||
dates.end = input.originalValue;
|
||||
}
|
||||
let getEuropeParisTime = () => {
|
||||
return new Date((new Date()).getTime() + (60000*((new Date()).getTimezoneOffset()+60)));
|
||||
};
|
||||
let setDateHelpers = (input, parent) => {
|
||||
let small = parent.children.postPublishedHelp ? parent.children.postPublishedHelp : parent.children.postUnPublishAtHelp;
|
||||
let isPlanned = false;
|
||||
if(input.id === 'sliderPublishedAt') {
|
||||
isPlanned = dates.start > dates.end && dates.end != 0;
|
||||
} else {
|
||||
isPlanned = dates.start < dates.end && dates.start != 0;
|
||||
}
|
||||
if (isPlanned || getEuropeParisTime().valueOf() < (new Date(input.value)).valueOf()) {
|
||||
let date = new Date(input.value);
|
||||
small.textContent = "{{ __('Planifié pour le ') }}" + date.toLocaleString();
|
||||
} else {
|
||||
small.textContent = input.id === 'sliderPublishedAt' ?
|
||||
"{{ __('Uniquement pour planifier la publication') }}" :
|
||||
"{{ __('Uniquement pour planifier la fin de la publication') }}";
|
||||
}
|
||||
};
|
||||
setDateHelpers(input, parent);
|
||||
input.addEventListener('change', (event) => {
|
||||
let parent = event.target.parentNode,
|
||||
input = event.target, value;
|
||||
value = (new Date(input.value)).valueOf();
|
||||
if(input.originalValue === NaN) {
|
||||
value = 0
|
||||
}
|
||||
if(input.id === 'sliderPublishedAt') {
|
||||
dates.start = value;
|
||||
} else {
|
||||
dates.end = value;
|
||||
}
|
||||
setDateHelpers(input, parent);
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue