-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathPostsController.php
executable file
·155 lines (130 loc) · 4.7 KB
/
PostsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace WebDevEtc\BlogEtc\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View;
use WebDevEtc\BlogEtc\Models\Post;
use WebDevEtc\BlogEtc\Requests\SearchRequest;
use WebDevEtc\BlogEtc\Services\CaptchaService;
use WebDevEtc\BlogEtc\Services\CategoriesService;
use WebDevEtc\BlogEtc\Services\PostsService;
/**
* Class BlogEtcReaderController
* All of the main public facing methods for viewing blog content (index, single posts).
*/
class PostsController extends Controller
{
/** @var PostsService */
private $postsService;
/** @var CategoriesService */
private $categoriesService;
/** @var CaptchaService */
private $captchaService;
/**
* BlogEtcReaderController constructor.
*/
public function __construct(
PostsService $postsService,
CategoriesService $categoriesService,
CaptchaService $captchaService
) {
$this->postsService = $postsService;
$this->categoriesService = $categoriesService;
$this->captchaService = $captchaService;
}
/**
* Show the search results.
*/
public function search(SearchRequest $request): \Illuminate\Contracts\View\View
{
// Laravel full text search (swisnl/laravel-fulltext) disabled due to poor Laravel 8 support.
// If you wish to add it, copy the code in this method that was in commit 9aff6c37d130.
// The LIKE query is not efficient. Search can be disabled in config.
$searchResults = Post::where('title', 'LIKE', '%'.$request->get('s').'%')->limit(100)->get();
// Map it so the post is actually accessible with ->indexable, for backwards compatibility in old view files.
$searchResultsMappedWithIndexable = $searchResults->map(function (Post $post) {
return new class($post) {
public $indexable;
public function __construct(Post $post)
{
$this->indexable = $post;
}
};
});
return view('blogetc::search', [
'title' => 'Search results for '.e($request->searchQuery()),
'query' => $request->searchQuery(),
'search_results' => $searchResultsMappedWithIndexable,
]);
}
/**
* @deprecated - use showCategory() instead
*/
public function view_category($categorySlug): \Illuminate\Contracts\View\View
{
return $this->showCategory($categorySlug);
}
/**
* View posts in a category.
*/
public function showCategory($categorySlug): \Illuminate\Contracts\View\View
{
return $this->index($categorySlug);
}
/**
* Show blog posts
* If $categorySlug is set, then only show from that category.
*
* @param string $categorySlug
*
* @return mixed
*/
public function index(string $categorySlug = null)
{
// the published_at + is_published are handled by BlogEtcPublishedScope, and don't take effect if the logged
// in user can manage log posts
$title = config('blogetc.blog_index_title', 'Viewing blog');
// default category ID
$categoryID = null;
if ($categorySlug) {
// get the category
$category = $this->categoriesService->findBySlug($categorySlug);
// get category ID to send to service
$categoryID = $category->id;
// TODO - make configurable
$title = config('blogetc.blog_index_category_title', 'Viewing blog posts in ').$category->category_name;
}
$posts = $this->postsService->indexPaginated(config('blogetc.per_page'), $categoryID);
return view('blogetc::index', [
'posts' => $posts,
'title' => $title,
'blogetc_category' => $category ?? null,
]);
}
/**
* @deprecated - use show()
*/
public function viewSinglePost(Request $request, string $blogPostSlug)
{
return $this->show($request, $blogPostSlug);
}
/**
* View a single post and (if enabled) comments.
*/
public function show(Request $request, string $postSlug): \Illuminate\View\View
{
$blogPost = $this->postsService->findBySlug($postSlug);
$usingCaptcha = $this->captchaService->getCaptchaObject();
if (null !== $usingCaptcha && method_exists($usingCaptcha, 'runCaptchaBeforeShowingPosts')) {
$usingCaptcha->runCaptchaBeforeShowingPosts($request, $blogPost);
}
return view(
'blogetc::single_post',
[
'post' => $blogPost,
'captcha' => $usingCaptcha,
'comments' => $blogPost->comments->load('user'),
]
);
}
}