1
|
|
|
<?php namespace jlourenco\blog\Controllers;
|
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request;
|
4
|
|
|
use App\Http\Requests;
|
5
|
|
|
use App\Http\Controllers\Controller;
|
6
|
|
|
use Blog;
|
7
|
|
|
use Sentinel;
|
8
|
|
|
use Searchy;
|
9
|
|
|
use Validator;
|
10
|
|
|
use Input;
|
11
|
|
|
use Base;
|
12
|
|
|
use Redirect;
|
13
|
|
|
use Lang;
|
14
|
|
|
|
15
|
|
|
class PostController extends Controller
|
16
|
|
|
{
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Declare the rules for the form validation
|
20
|
|
|
*
|
21
|
|
|
* @var array
|
22
|
|
|
*/
|
23
|
|
|
protected $validationRules = array(
|
24
|
|
|
'title' => 'required|min:3',
|
25
|
|
|
'slug' => 'required|min:3|unique:BlogPost,slug',
|
26
|
|
|
'category' => 'required|exists:BlogCategory,id',
|
27
|
|
|
'author' => 'required|exists:User,id',
|
28
|
|
|
'contents' => 'required|min:3',
|
29
|
|
|
);
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* Show list of posts.
|
33
|
|
|
*
|
34
|
|
|
* @return \Illuminate\Http\Response
|
35
|
|
|
*/
|
36
|
|
|
public function index()
|
37
|
|
|
{
|
38
|
|
|
$posts = Blog::getPostsRepository()->all();
|
39
|
|
|
|
40
|
|
|
return view('admin.posts.list', compact('posts'));
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Show details of a post,
|
45
|
|
|
*
|
46
|
|
|
* @param int $id
|
47
|
|
|
* @return View
|
48
|
|
|
*/
|
49
|
|
|
public function show($id)
|
50
|
|
|
{
|
51
|
|
|
$post = Blog::getPostsRepository()->findOrFail($id);
|
52
|
|
|
|
53
|
|
|
// Show the page
|
54
|
|
|
return View('admin.posts.show', compact('post'));
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* Post update.
|
59
|
|
|
*
|
60
|
|
|
* @param int $id
|
61
|
|
|
* @return View
|
62
|
|
|
*/
|
63
|
|
|
public function getEdit($id = null)
|
64
|
|
|
{
|
65
|
|
|
$post = Blog::getPostsRepository()->find($id);
|
66
|
|
|
|
67
|
|
|
// Get the post information
|
68
|
|
|
if($post == null)
|
69
|
|
|
{
|
70
|
|
|
// Prepare the error message
|
71
|
|
|
$error = Lang::get('blog.posts.not_found');
|
72
|
|
|
|
73
|
|
|
// Redirect to the post management page
|
74
|
|
|
return Redirect::route('posts')->with('error', $error);
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
$cats = null;
|
78
|
|
|
$users = null;
|
79
|
|
|
|
80
|
|
|
$categories = Blog::getCategoriesRepository()->all(['id', 'name']);
|
81
|
|
|
$authors = Sentinel::createModel()->all(['id', 'first_name', 'last_name']);
|
82
|
|
|
|
83
|
|
|
foreach ($categories as $cat)
|
84
|
|
|
$cats[$cat->id] = $cat->name;
|
85
|
|
|
|
86
|
|
|
foreach ($authors as $author)
|
87
|
|
|
$users[$author->id] = $author->first_name . ' ' . $author->last_name;
|
88
|
|
|
|
89
|
|
|
// Show the page
|
90
|
|
|
return View('admin.posts.edit', compact('post', 'cats', 'users'));
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* Post update form processing page.
|
95
|
|
|
*
|
96
|
|
|
* @param int $id
|
97
|
|
|
* @return Redirect
|
98
|
|
|
*/
|
99
|
|
|
public function postEdit($id = null)
|
100
|
|
|
{
|
101
|
|
|
// Get the post information
|
102
|
|
|
$post = Blog::getPostsRepository()->find($id);
|
103
|
|
|
|
104
|
|
|
if ($post == null)
|
105
|
|
|
{
|
106
|
|
|
// Prepare the error message
|
107
|
|
|
$error = Lang::get('blog.posts.not_found');
|
108
|
|
|
|
109
|
|
|
// Redirect to the post management page
|
110
|
|
|
return Redirect::route('admin.posts.show')->with('error', $error);
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
unset($this->validationRules['slug']);
|
114
|
|
|
$this->validationRules['slug'] = "required|min:3|unique:BlogPost,slug,{$post->slug},slug";
|
115
|
|
|
|
116
|
|
|
$slug = str_slug(Input::get('title'), '_');
|
117
|
|
|
|
118
|
|
|
$input = Input::all();
|
119
|
|
|
$input['slug'] = $slug;
|
120
|
|
|
|
121
|
|
|
// Create a new validator instance from our validation rules
|
122
|
|
|
$validator = Validator::make($input, $this->validationRules);
|
123
|
|
|
|
124
|
|
|
// If validation fails, we'll exit the operation now.
|
125
|
|
|
if ($validator->fails()) {
|
126
|
|
|
// Ooops.. something went wrong
|
127
|
|
|
return Redirect::back()->withInput()->withErrors($validator);
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
// Update the post
|
131
|
|
|
$post->title = Input::get('title');
|
132
|
|
|
$post->slug = $slug;
|
133
|
|
|
$post->author = Input::get('author');
|
134
|
|
|
$post->category = Input::get('category');
|
135
|
|
|
$post->contents = Input::get('contents');
|
136
|
|
|
|
137
|
|
|
// Was the post updated?
|
138
|
|
|
if ($post->save())
|
139
|
|
|
{
|
140
|
|
|
Base::Log('Post (' . $post->id . ') was edited.');
|
141
|
|
|
|
142
|
|
|
// Prepare the success message
|
143
|
|
|
$success = Lang::get('blog.posts.changed');
|
144
|
|
|
|
145
|
|
|
// Redirect to the user page
|
146
|
|
|
return Redirect::route('posts')->with('success', $success);
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
$error = Lang::get('blog.posts.error');
|
150
|
|
|
|
151
|
|
|
// Redirect to the post page
|
152
|
|
|
return Redirect::route('post.update', $id)->withInput()->with('error', $error);
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
/**
|
156
|
|
|
* Create new post
|
157
|
|
|
*
|
158
|
|
|
* @return View
|
159
|
|
|
*/
|
160
|
|
|
public function getCreate()
|
161
|
|
|
{
|
162
|
|
|
$cats = null;
|
163
|
|
|
$users = null;
|
164
|
|
|
|
165
|
|
|
$categories = Blog::getCategoriesRepository()->all(['id', 'name']);
|
166
|
|
|
$authors = Sentinel::createModel()->all(['id', 'first_name', 'last_name']);
|
167
|
|
|
|
168
|
|
|
foreach ($categories as $cat)
|
169
|
|
|
$cats[$cat->id] = $cat->name;
|
170
|
|
|
|
171
|
|
|
foreach ($authors as $author)
|
172
|
|
|
$users[$author->id] = $author->first_name . ' ' . $author->last_name;
|
173
|
|
|
|
174
|
|
|
// Show the page
|
175
|
|
|
return View('admin.posts.create', compact('cats', 'users'));
|
176
|
|
|
}
|
177
|
|
|
|
178
|
|
|
/**
|
179
|
|
|
* Post create form processing.
|
180
|
|
|
*
|
181
|
|
|
* @return Redirect
|
182
|
|
|
*/
|
183
|
|
|
public function postCreate()
|
184
|
|
|
{
|
185
|
|
|
$slug = str_slug(Input::get('title'), '_');
|
186
|
|
|
|
187
|
|
|
$input = Input::all();
|
188
|
|
|
$input['slug'] = $slug;
|
189
|
|
|
|
190
|
|
|
// Create a new validator instance from our validation rules
|
191
|
|
|
$validator = Validator::make($input, $this->validationRules);
|
192
|
|
|
|
193
|
|
|
// If validation fails, we'll exit the operation now.
|
194
|
|
|
if ($validator->fails()) {
|
195
|
|
|
// Ooops.. something went wrong
|
196
|
|
|
return Redirect::back()->withInput()->withErrors($validator);
|
197
|
|
|
}
|
198
|
|
|
|
199
|
|
|
$post = Blog::getPostsRepository()->findBySlug($slug);
|
200
|
|
|
|
201
|
|
|
if ($post != null)
|
202
|
|
|
return Redirect::route("posts")->with('error', Lang::get('blog.posts.already_exists'));
|
203
|
|
|
|
204
|
|
|
$post = Blog::getPostsRepository()->create([
|
205
|
|
|
'title' => Input::get('title'),
|
206
|
|
|
'slug' => $slug,
|
207
|
|
|
'author' => Input::get('author'),
|
208
|
|
|
'category' => Input::get('category'),
|
209
|
|
|
'keywords' => Input::get('keywords'),
|
210
|
|
|
'contents' => Input::get('contents'),
|
211
|
|
|
'likes' => 0,
|
212
|
|
|
'shares' => 0,
|
213
|
|
|
'views' => 0,
|
214
|
|
|
]);
|
215
|
|
|
|
216
|
|
|
$post->save();
|
217
|
|
|
|
218
|
|
|
Base::Log('A new post (' . $post->id . ') was created.');
|
219
|
|
|
|
220
|
|
|
// Redirect to the home page with success menu
|
221
|
|
|
return Redirect::route("posts")->with('success', Lang::get('blog.posts.created'));
|
222
|
|
|
}
|
223
|
|
|
|
224
|
|
|
/**
|
225
|
|
|
* Delete Confirm
|
226
|
|
|
*
|
227
|
|
|
* @param int $id
|
228
|
|
|
* @return View
|
229
|
|
|
*/
|
230
|
|
View Code Duplication |
public function getModalDelete($id = null)
|
|
|
|
|
231
|
|
|
{
|
232
|
|
|
$confirm_route = $error = null;
|
233
|
|
|
|
234
|
|
|
$title = 'Delete post';
|
235
|
|
|
$message = 'Are you sure to delete this post?';
|
236
|
|
|
|
237
|
|
|
// Get post information
|
238
|
|
|
$post = Blog::getPostsRepository()->findOrFail($id);
|
239
|
|
|
|
240
|
|
|
if ($post == null)
|
241
|
|
|
{
|
242
|
|
|
// Prepare the error message
|
243
|
|
|
$error = Lang::get('blog.posts.not_found');
|
244
|
|
|
return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
|
245
|
|
|
}
|
246
|
|
|
|
247
|
|
|
$confirm_route = route('delete/post', ['id' => $post->id]);
|
248
|
|
|
return View('layouts.modal_confirmation', compact('title', 'message', 'error', 'model', 'confirm_route'));
|
249
|
|
|
}
|
250
|
|
|
|
251
|
|
|
/**
|
252
|
|
|
* Delete the given post.
|
253
|
|
|
*
|
254
|
|
|
* @param int $id
|
255
|
|
|
* @return Redirect
|
256
|
|
|
*/
|
257
|
|
View Code Duplication |
public function getDelete($id = null)
|
|
|
|
|
258
|
|
|
{
|
259
|
|
|
// Get post information
|
260
|
|
|
$post = Blog::getPostsRepository()->find($id);
|
261
|
|
|
|
262
|
|
|
if ($post == null)
|
263
|
|
|
{
|
264
|
|
|
// Prepare the error message
|
265
|
|
|
$error = Lang::get('blog.posts.not_found');
|
266
|
|
|
|
267
|
|
|
// Redirect to the post management page
|
268
|
|
|
return Redirect::route('posts')->with('error', $error);
|
269
|
|
|
}
|
270
|
|
|
|
271
|
|
|
Base::Log('Post (' . $post->id . ') was deleted.');
|
272
|
|
|
|
273
|
|
|
// Delete the post
|
274
|
|
|
$post->delete();
|
275
|
|
|
|
276
|
|
|
// Prepare the success message
|
277
|
|
|
$success = Lang::get('blog.posts.deleted');
|
278
|
|
|
|
279
|
|
|
// Redirect to the post management page
|
280
|
|
|
return Redirect::route('posts')->with('success', $success);
|
281
|
|
|
}
|
282
|
|
|
|
283
|
|
|
/**
|
284
|
|
|
* Show a list of all the deleted posts.
|
285
|
|
|
*
|
286
|
|
|
* @return View
|
287
|
|
|
*/
|
288
|
|
|
public function getDeletedCategories()
|
289
|
|
|
{
|
290
|
|
|
// Grab deleted posts
|
291
|
|
|
$posts = Blog::getPostsRepository()->onlyTrashed()->get();
|
292
|
|
|
|
293
|
|
|
// Show the page
|
294
|
|
|
return View('admin.posts.deleted', compact('posts'));
|
295
|
|
|
}
|
296
|
|
|
|
297
|
|
|
/**
|
298
|
|
|
* Restore a deleted post.
|
299
|
|
|
*
|
300
|
|
|
* @param int $id
|
301
|
|
|
* @return Redirect
|
302
|
|
|
*/
|
303
|
|
View Code Duplication |
public function getRestore($id = null)
|
|
|
|
|
304
|
|
|
{
|
305
|
|
|
// Get post information
|
306
|
|
|
$post = Blog::getPostsRepository()->withTrashed()->find($id);
|
307
|
|
|
|
308
|
|
|
if ($post == null)
|
309
|
|
|
{
|
310
|
|
|
// Prepare the error message
|
311
|
|
|
$error = Lang::get('blog.posts.not_found');
|
312
|
|
|
|
313
|
|
|
// Redirect to the post management page
|
314
|
|
|
return Redirect::route('post.deleted')->with('error', $error);
|
315
|
|
|
}
|
316
|
|
|
|
317
|
|
|
Base::Log('Post (' . $post->id . ') was restored.');
|
318
|
|
|
|
319
|
|
|
// Restore the post
|
320
|
|
|
$post->restore();
|
321
|
|
|
|
322
|
|
|
// Prepare the success message
|
323
|
|
|
$success = Lang::get('blog.posts.restored');
|
324
|
|
|
|
325
|
|
|
// Redirect to the post management page
|
326
|
|
|
return Redirect::route('posts.deleted')->with('success', $success);
|
327
|
|
|
}
|
328
|
|
|
|
329
|
|
|
}
|
330
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.