|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Helpers\Helper; |
|
6
|
|
|
use App\Http\Requests\PostCategoryStoreRequest; |
|
7
|
|
|
use App\Http\Requests\PostSearchRequest; |
|
8
|
|
|
use App\Http\Requests\PostStoreRequest; |
|
9
|
|
|
use App\Models\Post; |
|
10
|
|
|
use App\Services\PostCategoryService; |
|
11
|
|
|
use App\Services\PostService; |
|
12
|
|
|
use App\Services\TagService; |
|
13
|
|
|
use App\Traits\CheckPermission; |
|
14
|
|
|
use Illuminate\Http\RedirectResponse; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\View\View; |
|
17
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
18
|
|
|
|
|
19
|
|
|
class PostController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
use CheckPermission; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private PostService $postService; |
|
24
|
|
|
private PostCategoryService $postCategoryService; |
|
25
|
|
|
private TagService $tagService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* PostController constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \App\Services\PostService $postService |
|
31
|
|
|
* @param \App\Services\PostCategoryService $postCategoryService |
|
32
|
|
|
* @param \App\Services\TagService $tagService |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
PostService $postService, |
|
36
|
|
|
PostCategoryService $postCategoryService, |
|
37
|
|
|
TagService $tagService |
|
38
|
|
|
) { |
|
39
|
|
|
$this->postService = $postService; |
|
40
|
|
|
$this->postCategoryService = $postCategoryService; |
|
41
|
|
|
$this->tagService = $tagService; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Display a listing of the resource. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \App\Http\Requests\PostSearchRequest $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View |
|
50
|
|
|
*/ |
|
51
|
|
|
public function index(PostSearchRequest $request): View |
|
52
|
|
|
{ |
|
53
|
|
|
$this->checkPermission('posts.view'); |
|
54
|
|
|
|
|
55
|
|
|
$searchParameters = Helper::getSearchParameters($request, Post::SEARCH_PARAMETERS); |
|
56
|
|
|
$posts = $this->postService->getPosts(20, $searchParameters); |
|
57
|
|
|
$categories = $this->postCategoryService->getPostCategories(); |
|
58
|
|
|
$statuses = Post::PUBLISHING_STATUS; |
|
59
|
|
|
|
|
60
|
|
|
return view('posts.index', [ |
|
61
|
|
|
'posts' => $posts, |
|
62
|
|
|
'categories' => $categories, |
|
63
|
|
|
'searchParameters' => $searchParameters, |
|
64
|
|
|
'statuses' => $statuses, |
|
65
|
|
|
]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Show the form for creating a new resource. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View |
|
72
|
|
|
*/ |
|
73
|
|
|
public function create(): View |
|
74
|
|
|
{ |
|
75
|
|
|
$this->checkPermission('posts.create'); |
|
76
|
|
|
|
|
77
|
|
|
$categories = $this->postCategoryService->getPostCategories(); |
|
78
|
|
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
79
|
|
|
$tags = $this->tagService->getTags(); |
|
80
|
|
|
|
|
81
|
|
|
return view('posts.create', [ |
|
82
|
|
|
'categories' => $categories, |
|
83
|
|
|
'countriesAvailableForTranslations' => $countriesAvailableForTranslations, |
|
84
|
|
|
'tags' => $tags, |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Store a newly created resource in storage. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \App\Http\Requests\PostStoreRequest $request |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
94
|
|
|
* @throws \Spatie\ModelStatus\Exceptions\InvalidStatus |
|
95
|
|
|
*/ |
|
96
|
|
|
public function store(PostStoreRequest $request): RedirectResponse |
|
97
|
|
|
{ |
|
98
|
|
|
$this->checkPermission('posts.create'); |
|
99
|
|
|
|
|
100
|
|
|
$this->postService->createPost($request); |
|
101
|
|
|
|
|
102
|
|
|
return redirect()->route('posts.index') |
|
103
|
|
|
->with('success', 'Post updated successfully'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Display the specified resource. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $postSlug |
|
110
|
|
|
* |
|
111
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View |
|
112
|
|
|
*/ |
|
113
|
|
|
public function show(string $postSlug) |
|
114
|
|
|
{ |
|
115
|
|
|
//$post = $this->postService->getById($postId); |
|
116
|
|
|
$post = $this->postService->getBySlug($postSlug); |
|
117
|
|
|
|
|
118
|
|
|
$post['body'] = $this->postService->getPostBody($post); |
|
119
|
|
|
|
|
120
|
|
|
//dd($post->getMedia('gallery')); |
|
121
|
|
|
|
|
122
|
|
|
return view('posts.show', compact('post')); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Show the form for editing the specified resource. |
|
127
|
|
|
* |
|
128
|
|
|
* @param int $postId |
|
129
|
|
|
* |
|
130
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View |
|
131
|
|
|
*/ |
|
132
|
|
|
public function edit(int $postId) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->checkPermission('posts.edit'); |
|
135
|
|
|
|
|
136
|
|
|
$post = $this->postService->getById($postId); |
|
137
|
|
|
$categories = $this->postCategoryService->getPostCategories(); |
|
138
|
|
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
139
|
|
|
$tags = $this->tagService->getTags(); |
|
140
|
|
|
|
|
141
|
|
|
return view('posts.edit', [ |
|
142
|
|
|
'post' => $post, |
|
143
|
|
|
'categories' => $categories, |
|
144
|
|
|
'countriesAvailableForTranslations' => $countriesAvailableForTranslations, |
|
145
|
|
|
'tags' => $tags, |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Update the specified resource in storage. |
|
151
|
|
|
* |
|
152
|
|
|
* @param \App\Http\Requests\PostStoreRequest $request |
|
153
|
|
|
* @param int $postId |
|
154
|
|
|
* |
|
155
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
156
|
|
|
*/ |
|
157
|
|
|
public function update(PostStoreRequest $request, int $postId): RedirectResponse |
|
158
|
|
|
{ |
|
159
|
|
|
$this->checkPermission('posts.edit'); |
|
160
|
|
|
|
|
161
|
|
|
$this->postService->updatePost($request, $postId); |
|
162
|
|
|
|
|
163
|
|
|
return redirect()->route('posts.index') |
|
164
|
|
|
->with('success', 'Post updated successfully'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Remove the specified resource from storage. |
|
169
|
|
|
* |
|
170
|
|
|
* @param int $postId |
|
171
|
|
|
* |
|
172
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
173
|
|
|
*/ |
|
174
|
|
|
public function destroy(int $postId): RedirectResponse |
|
175
|
|
|
{ |
|
176
|
|
|
$this->checkPermission('posts.delete'); |
|
177
|
|
|
|
|
178
|
|
|
$this->postService->deletePost($postId); |
|
179
|
|
|
|
|
180
|
|
|
return redirect()->route('posts.index') |
|
181
|
|
|
->with('success', 'Post deleted successfully'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Display a listing of the resource. |
|
186
|
|
|
* |
|
187
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View |
|
188
|
|
|
*/ |
|
189
|
|
|
public function blog() |
|
190
|
|
|
{ |
|
191
|
|
|
$posts = $this->postService->getPosts(5, ['status' => 'published']); |
|
192
|
|
|
|
|
193
|
|
|
return view('posts.blog', [ |
|
194
|
|
|
'posts' => $posts, |
|
195
|
|
|
]); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|