Skip to content

Commit 083abc4

Browse files
committed
feat: add content blocks retrieval and post preview scope to Post model
1 parent 7fc49ac commit 083abc4

File tree

3 files changed

+102
-46
lines changed

3 files changed

+102
-46
lines changed

src/Http/Controllers/Admin/PostsController.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function index(PostType $type, Request $request, bool $trashed = false)
4747
$per_page = $this->getPerPage($request);
4848

4949
$posts = $type->posts()
50-
->orderBy($orderby, $order);
50+
->orderBy($orderby, $order);
5151

5252
$search = null;
5353
if ($search = $request->input('search')) {
@@ -92,7 +92,7 @@ public function index(PostType $type, Request $request, bool $trashed = false)
9292
}
9393

9494
$posts = $posts->paginate($per_page)
95-
->appends($request->except('page'));
95+
->appends($request->except('page'));
9696

9797
return view('cms::admin.posts.index', compact('posts', 'type', 'title', 'per_page', 'search', 'trashed'));
9898
}
@@ -131,7 +131,7 @@ public function store(PostType $type, PostsRequest $request)
131131

132132
$post->lang = $request->input('lang', app()->getLocale());
133133

134-
if ($request->has('department') && method_exists($post, 'department')) {
134+
if ($request->has('department') && method_exists($post, 'department') && \Illuminate\Support\Facades\Schema::hasColumn($post->getTable(), 'department_id')) {
135135
$post->department()->associate($request->input('department'));
136136
}
137137

@@ -199,7 +199,7 @@ public function update(PostsRequest $request, PostType $type, Post $post)
199199
$this->authorize('update', $post);
200200

201201
// If this is not a translation, set lang
202-
if ((! $request->input('is_translation')) && $request->input('lang')) {
202+
if ((!$request->input('is_translation')) && $request->input('lang')) {
203203
$post->lang = $request->input('lang');
204204
app()->setLocale($post->lang->value);
205205
}
@@ -226,7 +226,7 @@ public function update(PostsRequest $request, PostType $type, Post $post)
226226
$post->recently_updated = $request->input('recently_updated', false);
227227
}
228228

229-
if ($request->has('department') && method_exists($post, 'department')) {
229+
if ($request->has('department') && method_exists($post, 'department') && \Illuminate\Support\Facades\Schema::hasColumn($post->getTable(), 'department_id')) {
230230
$post->department()->associate($request->input('department'));
231231
}
232232

@@ -293,7 +293,7 @@ public function destroy(PostType $type, Post $post, Request $request)
293293
{
294294
$this->authorize('delete', $post);
295295

296-
if (! $post->delete()) {
296+
if (!$post->delete()) {
297297
if ($request->expectsJson()) {
298298
return response()->json(false, 500);
299299
}
@@ -327,14 +327,14 @@ public function forceDelete(PostType $postType, $id, Request $request)
327327
{
328328
//find the model
329329
$post = $postType->posts()
330-
->onlyTrashed()
331-
->where('id', $id)
332-
->firstOrFail();
330+
->onlyTrashed()
331+
->where('id', $id)
332+
->firstOrFail();
333333

334334
$this->authorize('forceDelete', $post);
335335

336336
// send error
337-
if (! $post->forceDelete()) {
337+
if (!$post->forceDelete()) {
338338
if ($request->expectsJson()) {
339339
return response()->json(false, 500);
340340
}
@@ -358,14 +358,14 @@ public function restore(PostType $postType, $id, Request $request)
358358
{
359359
//find the model
360360
$post = $postType->posts()
361-
->onlyTrashed()
362-
->where('id', $id)
363-
->firstOrFail();
361+
->onlyTrashed()
362+
->where('id', $id)
363+
->firstOrFail();
364364

365365
$this->authorize('restore', $post);
366366

367367
// send error
368-
if (! $post->restore()) {
368+
if (!$post->restore()) {
369369
if ($request->expectsJson()) {
370370
return response()->json(false, 500);
371371
}
@@ -390,8 +390,8 @@ public function bulk(PostType $type, Request $request)
390390
$this->authorize('viewAny', $type);
391391

392392
$this->validate($request, [
393-
'action' => 'required|in:delete,publish,draft,markAsPending',
394-
'posts' => 'required|array',
393+
'action' => 'required|in:delete,publish,draft,markAsPending',
394+
'posts' => 'required|array',
395395
'posts.*' => 'exists:posts,id,type,' . $type->slug,
396396
]);
397397

@@ -405,13 +405,13 @@ public function bulk(PostType $type, Request $request)
405405
$this->authorize('delete', $type);
406406

407407
$type->posts()
408-
->whereIn('id', $ids)
409-
->get()
410-
->each(function (Post $post) {
411-
if (auth()->user()->can('delete', $post)) {
412-
$post->delete();
413-
}
414-
});
408+
->whereIn('id', $ids)
409+
->get()
410+
->each(function (Post $post) {
411+
if (auth()->user()->can('delete', $post)) {
412+
$post->delete();
413+
}
414+
});
415415
break;
416416

417417
case 'reject':
@@ -427,18 +427,18 @@ public function bulk(PostType $type, Request $request)
427427
}
428428

429429
$posts->whereIn('id', $ids)
430-
->get()
431-
->each(function (Post $post) use ($action, $type) {
432-
if (method_exists($post, $action)) {
433-
if ($action == 'draft' && auth()->user()->can('update', $post)) {
434-
$post->{$action}();
435-
$post->save();
436-
} elseif ($action != 'draft' && auth()->user()->can('publish', $type)) {
437-
$post->{$action}();
438-
$post->save();
439-
}
440-
}
441-
});
430+
->get()
431+
->each(function (Post $post) use ($action, $type) {
432+
if (method_exists($post, $action)) {
433+
if ($action == 'draft' && auth()->user()->can('update', $post)) {
434+
$post->{$action}();
435+
$post->save();
436+
} elseif ($action != 'draft' && auth()->user()->can('publish', $type)) {
437+
$post->{$action}();
438+
$post->save();
439+
}
440+
}
441+
});
442442
break;
443443
}
444444

src/Models/Post.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ public function getRouteKeyName(): string
123123
return 'slug';
124124
}
125125

126+
/**
127+
* Get the content blocks
128+
* Default to one single raw block
129+
*
130+
* @return array
131+
*/
132+
public function getContentBlocksAttribute()
133+
{
134+
$blocks = json_decode($this->content, true);
135+
136+
if ($blocks) {
137+
return $blocks['blocks'];
138+
} else {
139+
return [
140+
[
141+
'type' => 'paragraph',
142+
'data' => [
143+
'text' => $this->content,
144+
],
145+
],
146+
];
147+
}
148+
}
149+
126150
/**
127151
* Post type relationship
128152
*
@@ -285,6 +309,21 @@ public function getTitleAttrAttribute(): string
285309
return Str::limit($this->title, 50);
286310
}
287311

312+
/**
313+
* Scope that provides ability to view post previews where has valid signature.
314+
*
315+
* @param $query
316+
* @return mixed
317+
*/
318+
public function scopePublishedOrPreview($query)
319+
{
320+
if (request()->hasValidSignature()) {
321+
return $query;
322+
}
323+
324+
return $query->published();
325+
}
326+
288327
/**
289328
* Get the permalink
290329
*
@@ -296,7 +335,7 @@ public function getPermalinkAttribute(): ?string
296335
$postTypeSlug = $this->postType->slug;
297336

298337
// Check if translations exist for the current locale
299-
if (! $this->hasTranslations($locale)) {
338+
if (!$this->hasTranslations($locale)) {
300339
$locale = \App\Helpers\Translation\Enums\Languages::getOppositeLocale($locale);
301340
}
302341

@@ -322,7 +361,7 @@ public function getPreviewLinkAttribute(): string
322361
$locale = app()->getLocale();
323362

324363
// Check if translations exist for the current locale
325-
if (! $this->hasTranslations($locale)) {
364+
if (!$this->hasTranslations($locale)) {
326365
$locale = Languages::getOppositeLocale($locale);
327366
}
328367

@@ -348,11 +387,11 @@ public function getPreviewLinkAttribute(): string
348387
*/
349388
public function translatedPermalink(string $action = 'show', string $locale = null): ?string
350389
{
351-
if (! $locale) {
390+
if (!$locale) {
352391
$locale = app()->getLocale();
353392
}
354393

355-
if ($this->lang->value != $locale && (is_null($this->translations) || $this->hide_translation)) {
394+
if ($this->lang->value != $locale && (is_null($this->translations) || $this->hide_translation)) {
356395
return null;
357396
}
358397

src/Support/Routes.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,25 @@ public static function admin(
4545
});
4646

4747
Route::bind('post', function ($value, $route) {
48-
$post_type = $route->parameter('post_type');
48+
$post_type = $route->parameter('post_type') ?: $route->parameter('postType');
4949
$post_type_slug = is_object($post_type) ? $post_type->slug : $post_type;
5050

5151
try {
52-
return Post::where('type', $post_type_slug ?: -1)
53-
->findOrFail($value);
52+
$query = Post::where('type', $post_type_slug ?: -1);
53+
54+
if (Str::startsWith($route->getName(), 'admin.')) {
55+
return $query->findOrFail($value);
56+
}
57+
58+
$language = $route->parameter('language');
59+
if ($language) {
60+
$query->notHiddenOfLocale($language);
61+
}
62+
63+
return $query->whereSlug($value)
64+
->publishedOrPreview()
65+
->firstOrFail();
66+
5467
} catch (ModelNotFoundException $e) {
5568
abort(404);
5669
}
@@ -152,13 +165,17 @@ public static function web(
152165

153166
Route::bind('post_slug', function ($value, $route) {
154167
$language = $route->parameter('language');
155-
$post_type = $route->parameter('web_post_type_slug');
168+
$post_type = $route->parameter('post_type') ?: $route->parameter('postType');
156169
$post_type_slug = is_object($post_type) ? $post_type->slug : $post_type;
157170

158171
try {
159-
return Post::where('type', $post_type_slug ?: -1)
160-
->publishedOrPreview()
161-
->notHiddenOfLocale($language)
172+
$query = Post::where('type', $post_type_slug ?: -1);
173+
174+
if ($language) {
175+
$query->notHiddenOfLocale($language);
176+
}
177+
178+
return $query->publishedOrPreview()
162179
->whereSlug($value)
163180
->firstOrFail();
164181

0 commit comments

Comments
 (0)