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