Skip to content

Commit

Permalink
Merge pull request #20 from SoipoServices/Fix-Schedule-for
Browse files Browse the repository at this point in the history
Fixing schedule for
  • Loading branch information
soipo committed Aug 9, 2023
2 parents edc8fb4 + fc736b1 commit 9c8db89
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateCmsPagesTableChangeScheduleFor extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{

Schema::table('pages', function (Blueprint $table) {
$table->timestamp('scheduled_at')->useCurrent()->nullable()->after('published');
$table->dropColumn('schedule_for');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('pages', function (Blueprint $table) {
$table->timestamp('scheduled_for')->useCurrent()->after('published');
$table->dropColumn('schedule_at');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateCmsPostsTableChangeScheduleFor extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->timestamp('scheduled_at')->useCurrent()->nullable()->after('featured');
$table->dropColumn('schedule_for');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->timestamp('scheduled_for')->after('featured')->useCurrent();
$table->dropColumn('schedule_at');
});
}
}
33 changes: 13 additions & 20 deletions src/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class BlogController extends Controller
*/
public function index(Request $request): View|Factory|Application
{
$posts = cache()->remember('cms.posts.page' . $request->get('page'), now()->addMinutes(config('cms.cache_minutes')), function () {
return static::getModelClassName(Resources::POST)::published()->orderByFeatured()->paginate(config('cms.paginate'));
$posts = cache()->remember('cms.posts.page'.$request->get('page'), now()->addMinutes(config('cms.cache_minutes')), function () {
return static::getModelClassName(Resources::POST)::with(Resources::CATEGORY)->published()->orderBy('scheduled_at', 'desc')->paginate(config('cms.paginate'));
});

$latest_posts = $posts->take(config('cms.take'));
Expand Down Expand Up @@ -52,11 +52,11 @@ public function single(string $slug, Request $request): View|Factory|Application
* @return View|Factory|Application
* @throws Exception
*/
public function date(string $year, string $month, string $day, string $slug, Request $request): View|Factory|Application
public function date(string $year,string $month,string $day,string $slug, Request $request): View|Factory|Application
{
$createdAt = Carbon::create($year, $month, $day);
$post = cache()->remember($slug . $createdAt->format('-Y-m-d'), now()->addMinutes(config('cms.cache_minutes')), function () use ($slug, $createdAt) {
return static::getModelClassName(Resources::POST)::where('slug', $slug)->whereDate('created_at', $createdAt)->published()->firstOrFail();
$post = cache()->remember($slug.$createdAt->format('-Y-m-d'), now()->addMinutes(config('cms.cache_minutes')), function () use($slug,$createdAt) {
return static::getModelClassName(Resources::POST)::where('slug', $slug)->whereDate('created_at',$createdAt)->published()->firstOrFail();
});

return view('cms::blog.single', compact(['post']));
Expand All @@ -70,37 +70,30 @@ public function date(string $year, string $month, string $day, string $slug, Req
*/
public function category(string $slug, Request $request): View|Factory|Application
{
$posts = cache()->remember('cms.posts.category.' . $slug . '.page' . $request->get('page'), now()->addMinutes(config('cms.cache_minutes')), function () use ($slug) {
return static::getModelClassName(Resources::POST)::published()->orderByFeatured()->whereHas('category', function ($q) use ($slug) {
$posts = cache()->remember('cms.posts.category.'.$slug.'.page'.$request->get('page'), now()->addMinutes(config('cms.cache_minutes')), function () use($slug) {
return static::getModelClassName(Resources::POST)::published()->orderBy('scheduled_at', 'desc')->whereHas('category', function ($q) use ($slug) {
$q->where('slug', $slug);
})->paginate(config('cms.paginate'));
});

if (count($posts)) {
if(count($posts)){
$category = $posts->first()->category;
} else {
$category = static::getModelClassName(Resources::CATEGORY)::where('slug', $slug)->firstOrFail();
}else{
$category = static::getModelClassName(Resources::CATEGORY)::where('slug',$slug)->firstOrFail();
}

return view('cms::blog.category', compact(['category', 'posts']));
}


/**
* @param Request $request
* @return View|Factory|Application
*/
public function search(Request $request): View|Factory|Application
{
$key = $request->input('query');
$posts = static::getModelClassName(Resources::POST)::where('title', 'like', "%$key%")
->where('summary', 'like', "%$key%")
->where('body', 'like', "%$key%")
->with(Resources::CATEGORY)
->published()
->orderBy('scheduled_for', 'desc')
->paginate(config('cms.paginate'));

return view('cms::blog.search', compact(['posts', 'key']));
$posts = static::getModelClassName(Resources::POST)::where('title','like', "%$key%")->with(Resources::CATEGORY)->published()->orderBy('scheduled_at', 'desc')->paginate(config('cms.paginate'));

return view('cms::pages.search', compact(['posts', 'key']));
}
}
6 changes: 3 additions & 3 deletions src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Page extends Model implements HasMedia
'title',
'summary',
'body',
'scheduled_for',
'scheduled_at',
'slug',
'is_home'
];
Expand All @@ -47,7 +47,7 @@ class Page extends Model implements HasMedia
* @var array<string>
*/
protected $casts = [
'scheduled_for' => 'datetime',
'scheduled_at' => 'datetime',
// 'summary' => 'json',
// 'title' => 'json',
// 'body' => 'json'
Expand Down Expand Up @@ -77,7 +77,7 @@ public function registerMediaCollections(): void

public function ScopePublished(Builder $builder)
{
$builder->whereDate('scheduled_for', '<=', Carbon::today()->toDateString());
$builder->whereDate('scheduled_at', '<=', Carbon::today()->toDateString());
}

public function ScopeOrderByFeatured(Builder $builder)
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Post extends Model implements HasMedia
'title',
'summary',
'body',
'scheduled_for',
'scheduled_at',
'featured',
'slug'
];
Expand All @@ -48,7 +48,7 @@ class Post extends Model implements HasMedia
*/
protected $casts = [
'featured' => 'boolean',
'scheduled_for' => 'datetime',
'scheduled_at' => 'datetime',
];

/**
Expand All @@ -57,7 +57,7 @@ class Post extends Model implements HasMedia
*/
public function getPublishedAttribute()
{
return now() > $this->attributes["scheduled_for"];
return now() > $this->attributes["scheduled_at"];
}

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public function registerMediaCollections() : void

public function ScopePublished(Builder $builder)
{
$builder->whereDate('scheduled_for', '<=', Carbon::today()->toDateString())->with(['tags','category']);
$builder->whereDate('scheduled_at', '<=', Carbon::today()->toDateString())->with(['tags','category']);
}

public function ScopeOrderByFeatured(Builder $builder)
Expand Down
2 changes: 1 addition & 1 deletion src/Nova/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function (Text $field, NovaRequest $request, FormData $formData) {

Trix::make(__('Body'), 'body')->withFiles('media')->rules(['required', 'string']),

DateTime::make(__('Scheduled For'), 'scheduled_for'),
DateTime::make(__('Scheduled For'), 'scheduled_at')->rules('nullable'),

Boolean::make(__('Published'), function () {
return $this->published;
Expand Down
4 changes: 2 additions & 2 deletions src/Nova/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ function (Text $field, NovaRequest $request, FormData $formData) {

BelongsTo::make(__('Author'), 'author', static::getNovaClassName(Resources::AUTHOR)),

DateTime::make(__('Scheduled For'), 'scheduled_for')
->rules('required'),
DateTime::make(__('Scheduled For'), 'scheduled_at')
->rules('nullable'),

Boolean::make(__('Published'), function () {
return $this->published;
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/Publishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ trait Publishable
*/
public function getPublishedAttribute()
{
return Carbon::now() > $this->attributes["scheduled_for"];
return Carbon::now() > $this->attributes["scheduled_at"];
}
}

0 comments on commit 9c8db89

Please sign in to comment.