1
|
|
|
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest; |
4
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Posts\UpdatePostRequest; |
5
|
|
|
use Arcanesoft\Blog\Models\Category; |
6
|
|
|
use Arcanesoft\Blog\Models\Post; |
7
|
|
|
use Arcanesoft\Blog\Models\Tag; |
8
|
|
|
use Arcanesoft\Blog\Policies\PostsPolicy; |
9
|
|
|
use Illuminate\Support\Facades\Log; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PostsController |
13
|
|
|
* |
14
|
|
|
* @package Arcanesoft\Blog\Http\Controllers\Admin |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PostsController extends Controller |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Properties |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The post model. |
26
|
|
|
* |
27
|
|
|
* @var \Arcanesoft\Blog\Models\Post |
28
|
|
|
*/ |
29
|
|
|
private $post; |
30
|
|
|
|
31
|
|
|
/* ----------------------------------------------------------------- |
32
|
|
|
| Constructor |
33
|
|
|
| ----------------------------------------------------------------- |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* PostsController constructor. |
38
|
|
|
* |
39
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Post $post) |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
|
45
|
|
|
$this->post = $post; |
46
|
|
|
|
47
|
|
|
$this->setCurrentPage('blog-posts'); |
48
|
|
|
$this->addBreadcrumbRoute(trans('blog::posts.titles.posts'), 'admin::blog.posts.index'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* ----------------------------------------------------------------- |
52
|
|
|
| Main Methods |
53
|
|
|
| ----------------------------------------------------------------- |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* List the posts. |
58
|
|
|
* |
59
|
|
|
* @param bool $trashed |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\View\View |
62
|
|
|
*/ |
63
|
|
|
public function index($trashed = false) |
64
|
|
|
{ |
65
|
|
|
$this->authorize(PostsPolicy::PERMISSION_LIST); |
66
|
|
|
|
67
|
|
|
$posts = $this->post->with(['author', 'category'])->when($trashed, function ($query) { |
|
|
|
|
68
|
|
|
return $query->onlyTrashed(); |
69
|
|
|
})->paginate(30); |
70
|
|
|
|
71
|
|
|
$title = trans('blog::posts.titles.posts-list'); |
72
|
|
|
$this->setTitle($title . ($trashed ? ' - '.trans('core::generals.trashed') : '')); |
73
|
|
|
$this->addBreadcrumb($title); |
74
|
|
|
|
75
|
|
|
return $this->view('admin.posts.index', compact('trashed', 'posts')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* List the trashed posts. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\View\View |
82
|
|
|
*/ |
83
|
|
|
public function trash() |
84
|
|
|
{ |
85
|
|
|
return $this->index(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a post. |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\View\View |
92
|
|
|
*/ |
93
|
|
|
public function create() |
94
|
|
|
{ |
95
|
|
|
$this->authorize(PostsPolicy::PERMISSION_CREATE); |
96
|
|
|
|
97
|
|
|
$categories = Category::getSelectData(); |
98
|
|
|
$tags = Tag::getSelectData(); |
99
|
|
|
$statuses = Post::getStatuses(); |
100
|
|
|
|
101
|
|
|
$this->setTitle($title = trans('blog::posts.titles.create-post')); |
102
|
|
|
$this->addBreadcrumb($title); |
103
|
|
|
|
104
|
|
|
return $this->view('admin.posts.create', compact('categories', 'tags', 'statuses')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Store the post. |
109
|
|
|
* |
110
|
|
|
* @param \Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest $request |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Http\RedirectResponse |
113
|
|
|
*/ |
114
|
|
|
public function store(CreatePostRequest $request) |
115
|
|
|
{ |
116
|
|
|
$this->authorize(PostsPolicy::PERMISSION_CREATE); |
117
|
|
|
|
118
|
|
|
$post = Post::createOne($request->getValidatedData()); |
119
|
|
|
|
120
|
|
|
$this->transNotification('created', ['title' => $post->title], $post->toArray()); |
121
|
|
|
|
122
|
|
|
return redirect()->route('admin::blog.posts.show', [$post]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Show a post. |
127
|
|
|
* |
128
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\View\View |
131
|
|
|
*/ |
132
|
|
|
public function show(Post $post) |
133
|
|
|
{ |
134
|
|
|
$this->authorize(PostsPolicy::PERMISSION_SHOW); |
135
|
|
|
|
136
|
|
|
$post = $post->load(['author', 'category', 'tags', 'seo']); |
137
|
|
|
|
138
|
|
|
$this->setTitle(trans('blog::posts.titles.post-details')); |
139
|
|
|
$this->addBreadcrumb($post->title); |
140
|
|
|
|
141
|
|
|
return $this->view('admin.posts.show', compact('post')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Edit a post. |
146
|
|
|
* |
147
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
148
|
|
|
* |
149
|
|
|
* @return \Illuminate\View\View |
150
|
|
|
*/ |
151
|
|
|
public function edit(Post $post) |
152
|
|
|
{ |
153
|
|
|
$this->authorize(PostsPolicy::PERMISSION_UPDATE); |
154
|
|
|
|
155
|
|
|
$this->setTitle($title = trans('blog::posts.titles.edit-post')); |
156
|
|
|
$this->addBreadcrumb($title); |
157
|
|
|
|
158
|
|
|
$categories = Category::getSelectData(); |
159
|
|
|
$tags = Tag::getSelectData(); |
160
|
|
|
$statuses = Post::getStatuses(); |
161
|
|
|
|
162
|
|
|
return $this->view('admin.posts.edit', compact('post', 'categories', 'tags', 'statuses')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Update the post. |
167
|
|
|
* |
168
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
169
|
|
|
* @param \Arcanesoft\Blog\Http\Requests\Admin\Posts\UpdatePostRequest $request |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Http\RedirectResponse |
172
|
|
|
*/ |
173
|
|
|
public function update(Post $post, UpdatePostRequest $request) |
174
|
|
|
{ |
175
|
|
|
$this->authorize(PostsPolicy::PERMISSION_UPDATE); |
176
|
|
|
|
177
|
|
|
$post->updateOne($request->getValidatedData()); |
178
|
|
|
|
179
|
|
|
$this->transNotification('updated', ['title' => $post->title], $post->toArray()); |
180
|
|
|
|
181
|
|
|
return redirect()->route('admin::blog.posts.show', [$post]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Publish/Unpublish a post. |
186
|
|
|
* |
187
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
188
|
|
|
* |
189
|
|
|
* @return \Illuminate\Http\JsonResponse |
190
|
|
|
*/ |
191
|
|
|
public function publish(Post $post) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$this->authorize(PostsPolicy::PERMISSION_UPDATE); |
194
|
|
|
|
195
|
|
|
// TODO: Complete the implementation |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Restore a trashed post. |
200
|
|
|
* |
201
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
202
|
|
|
* |
203
|
|
|
* @return \Illuminate\Http\JsonResponse |
204
|
|
|
*/ |
205
|
|
|
public function restore(Post $post) |
206
|
|
|
{ |
207
|
|
|
$this->authorize(PostsPolicy::PERMISSION_UPDATE); |
208
|
|
|
|
209
|
|
|
try { |
210
|
|
|
$post->restore(); |
211
|
|
|
|
212
|
|
|
return $this->jsonResponseSuccess([ |
213
|
|
|
'message' => $this->transNotification('restored', ['title' => $post->title], $post->toArray()) |
214
|
|
|
]); |
215
|
|
|
} |
216
|
|
|
catch (\Exception $e) { |
217
|
|
|
return $this->jsonResponseError(['message' => $e->getMessage()], 500); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Delete a post. |
223
|
|
|
* |
224
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
225
|
|
|
* |
226
|
|
|
* @return \Illuminate\Http\JsonResponse |
227
|
|
|
*/ |
228
|
|
|
public function delete(Post $post) |
229
|
|
|
{ |
230
|
|
|
$this->authorize(PostsPolicy::PERMISSION_DELETE); |
231
|
|
|
|
232
|
|
|
try { |
233
|
|
|
$post->trashed() ? $post->forceDelete() : $post->delete(); |
234
|
|
|
|
235
|
|
|
return $this->jsonResponseSuccess([ |
236
|
|
|
'message' => $this->transNotification('deleted', ['title' => $post->title], $post->toArray()) |
237
|
|
|
]); |
238
|
|
|
} |
239
|
|
|
catch(\Exception $e) { |
240
|
|
|
return $this->jsonResponseError(['message' => $e->getMessage()], 500); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/* ----------------------------------------------------------------- |
245
|
|
|
| Other Methods |
246
|
|
|
| ----------------------------------------------------------------- |
247
|
|
|
*/ |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Notify with translation. |
251
|
|
|
* |
252
|
|
|
* @todo: Refactor this methods to the core package ? |
253
|
|
|
* |
254
|
|
|
* @param string $action |
255
|
|
|
* @param array $replace |
256
|
|
|
* @param array $context |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
protected function transNotification($action, array $replace = [], array $context = []) |
261
|
|
|
{ |
262
|
|
|
$title = trans("blog::posts.messages.{$action}.title"); |
263
|
|
|
$message = trans("blog::posts.messages.{$action}.message", $replace); |
264
|
|
|
|
265
|
|
|
Log::info($message, $context); |
266
|
|
|
$this->notifySuccess($message, $title); |
267
|
|
|
|
268
|
|
|
return $message; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: