|
1
|
|
|
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Entities\PostStatus; |
|
4
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest; |
|
5
|
|
|
use Arcanesoft\Blog\Http\Requests\Admin\Posts\UpdatePostRequest; |
|
6
|
|
|
use Arcanesoft\Blog\Models\Category; |
|
7
|
|
|
use Arcanesoft\Blog\Models\Post; |
|
8
|
|
|
use Arcanesoft\Blog\Models\Tag; |
|
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
|
|
|
* The post model. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Arcanesoft\Blog\Models\Post |
|
27
|
|
|
*/ |
|
28
|
|
|
private $post; |
|
29
|
|
|
|
|
30
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
31
|
|
|
| Constructor |
|
32
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
33
|
|
|
*/ |
|
34
|
|
|
/** |
|
35
|
|
|
* Instantiate the controller. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Post $post |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Post $post) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
$this->post = $post; |
|
44
|
|
|
|
|
45
|
|
|
$this->setCurrentPage('blog-posts'); |
|
46
|
|
|
$this->addBreadcrumbRoute('Posts', 'admin::blog.posts.index'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
50
|
|
|
| Main Functions |
|
51
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
52
|
|
|
*/ |
|
53
|
|
|
/** |
|
54
|
|
|
* List the posts. |
|
55
|
|
|
* |
|
56
|
|
|
* @param bool $trashed |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\View\View |
|
59
|
|
|
*/ |
|
60
|
|
|
public function index($trashed = false) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->authorize('blog.posts.list'); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$posts = $this->post->with(['author', 'category']); |
|
65
|
|
|
$posts = $trashed |
|
66
|
|
|
? $posts->onlyTrashed()->paginate(30) |
|
67
|
|
|
: $posts->paginate(30); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$this->setTitle($title = 'List of posts' . ($trashed ? ' - Trashed' : '')); |
|
70
|
|
|
$this->addBreadcrumb($title); |
|
71
|
|
|
|
|
72
|
|
|
return $this->view('admin.posts.list', compact('trashed', 'posts')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* List the trashed posts. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Illuminate\View\View |
|
79
|
|
|
*/ |
|
80
|
|
|
public function trash() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->index(true); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create a post. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Illuminate\View\View |
|
89
|
|
|
*/ |
|
90
|
|
|
public function create() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->authorize('blog.posts.create'); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$this->setTitle('Blog - Posts'); |
|
95
|
|
|
$this->addBreadcrumb('Create post'); |
|
96
|
|
|
|
|
97
|
|
|
$categories = Category::getSelectOptions(); |
|
98
|
|
|
$tags = Tag::getSelectOptions(); |
|
99
|
|
|
$statuses = PostStatus::all(); |
|
100
|
|
|
|
|
101
|
|
|
return $this->view('admin.posts.create', compact('categories', 'tags', 'statuses')); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Store the post. |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest $request |
|
108
|
|
|
* @param \Arcanesoft\Blog\Models\Post $post |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
111
|
|
|
*/ |
|
112
|
|
|
public function store(CreatePostRequest $request, Post $post) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->authorize('blog.posts.create'); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$post->createOne($request->all()); |
|
117
|
|
|
|
|
118
|
|
|
$message = "The post {$post->title} was created successfully !"; |
|
119
|
|
|
Log::info($message, $post->toArray()); |
|
120
|
|
|
$this->notifySuccess($message, 'Post created !'); |
|
121
|
|
|
|
|
122
|
|
|
return redirect()->route('admin::blog.posts.index'); |
|
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('blog.posts.show'); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$post = $post->load(['author', 'category', 'tags']); |
|
137
|
|
|
|
|
138
|
|
|
$this->setTitle('Blog - Posts'); |
|
139
|
|
|
$this->addBreadcrumb("Post - {$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('blog.posts.update'); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$this->setTitle('Blog - Posts'); |
|
156
|
|
|
$this->addBreadcrumb('Edit post'); |
|
157
|
|
|
|
|
158
|
|
|
$categories = Category::getSelectOptions(); |
|
159
|
|
|
$tags = Tag::getSelectOptions(); |
|
160
|
|
|
$statuses = PostStatus::all(); |
|
161
|
|
|
|
|
162
|
|
|
return $this->view('admin.posts.edit', compact('post', 'categories', 'tags', 'statuses')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function update(UpdatePostRequest $request, Post $post) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->authorize('blog.posts.update'); |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
$post->updateOne($request->all()); |
|
170
|
|
|
|
|
171
|
|
|
$message = "The post {$post->title} was updated successfully !"; |
|
172
|
|
|
Log::info($message, $post->toArray()); |
|
173
|
|
|
$this->notifySuccess($message, 'Post updated !'); |
|
174
|
|
|
|
|
175
|
|
|
return redirect()->route('admin::blog.posts.show', [$post->id]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function publish(Post $post) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$this->authorize('blog.posts.update'); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function restore(Post $post) |
|
|
|
|
|
|
184
|
|
|
{ |
|
185
|
|
|
$this->authorize('blog.posts.update'); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function delete(Post $post) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$this->authorize('blog.posts.delete'); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: