1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Retosteffen\LaravelBlog\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Retosteffen\LaravelBlog\Models\Category; |
8
|
|
|
use Retosteffen\LaravelBlog\Models\LaravelBlog; |
9
|
|
|
use Spatie\Tags\Tag; |
10
|
|
|
|
11
|
|
|
class LaravelBlogController |
12
|
|
|
{ |
13
|
|
|
public function index() |
14
|
|
|
{ |
15
|
|
|
$posts = LaravelBlog::where('published', '=', true)->orderBy('published_at', 'desc')->get(); |
16
|
|
|
|
17
|
|
|
return view('laravel-blog::blog.index', compact('posts')); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function create(Request $request) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
//$this->authorize('create',[LaravelBlog::class]); |
23
|
|
|
|
24
|
|
|
$tags = Tag::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE); |
25
|
|
|
$categories = Category::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE); |
26
|
|
|
|
27
|
|
|
return view('laravel-blog::blog.create', compact('tags', 'categories')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function store(Request $request) |
31
|
|
|
{ |
32
|
|
|
//$this->authorize('create',[LaravelBlog::class]); |
33
|
|
|
$attributes = request()->validate([ |
34
|
|
|
'title'=>['required', 'regex:/^(?![0-9]*$)[a-zA-Z0-9 \-]+$/'], |
35
|
|
|
'content'=>['required'], |
36
|
|
|
'excerpt'=>['max:158'], |
37
|
|
|
'image' => ['image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], |
38
|
|
|
'alt_text'=>[], |
39
|
|
|
]); |
40
|
|
|
$attributes['published'] = request()->has('published'); |
41
|
|
|
|
42
|
|
|
if (request()->has('published')) { |
43
|
|
|
$attributes['published_at'] = date('Y-m-d H:i:s'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$blog_post = LaravelBlog::create($attributes); |
47
|
|
|
$blog_post->author()->associate(auth()->user()); |
|
|
|
|
48
|
|
|
$blog_post->category()->associate(request()->get('category')); |
49
|
|
|
$blog_post->save(); |
50
|
|
|
|
51
|
|
|
$image_path = null; |
52
|
|
View Code Duplication |
if ($request->file('image')) { |
|
|
|
|
53
|
|
|
$original_file_name = $request->file('image')->getClientOriginalName(); |
54
|
|
|
$image_path = $request->file('image')->storeAs('blog_posts/'.$blog_post->id, $original_file_name, 'public'); |
55
|
|
|
} |
56
|
|
|
$blog_post->image = $image_path; |
57
|
|
|
$blog_post->save(); |
58
|
|
|
|
59
|
|
|
$tags_array = $request->get('tags'); |
60
|
|
|
$tags_names_array = []; |
61
|
|
View Code Duplication |
if ($tags_array) { |
|
|
|
|
62
|
|
|
foreach ($tags_array as $tag_id) { |
63
|
|
|
$tag_object = Tag::find($tag_id); |
64
|
|
|
$tags_names_array[] = $tag_object->name; |
65
|
|
|
} |
66
|
|
|
$blog_post->syncTagsWithType($tags_names_array); |
67
|
|
|
} else { |
68
|
|
|
$blog_post->syncTags([]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return redirect()->route('blog_admin'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function showSlug(LaravelBlog $blogPost) |
75
|
|
|
{ |
76
|
|
|
if (! $blogPost->published && ! auth()->user()) { |
|
|
|
|
77
|
|
|
abort(404); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (config('laravel-blog.permalink') == 'year/month/slug') { |
81
|
|
|
return redirect()->route('showYearMonthSlug', ['year'=>\Carbon\Carbon::parse($blogPost->published_at)->year, 'month'=>\Carbon\Carbon::parse($blogPost->published_at)->month, 'blogPost' => $blogPost->slug] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
View Code Duplication |
if (config('laravel-blog.permalink') == 'year/month/day/slug') { |
|
|
|
|
85
|
|
|
return redirect()->route('showYearMonthDaySlug', ['year'=>\Carbon\Carbon::parse($blogPost->published_at)->year, 'month'=>\Carbon\Carbon::parse($blogPost->published_at)->month, 'day'=>\Carbon\Carbon::parse($blogPost->published_at)->day, 'blogPost' => $blogPost->slug] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
if (config('laravel-blog.permalink') == 'id') { |
89
|
|
|
return redirect()->route('showID', ['id' => $blogPost->id] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return view('laravel-blog::blog.show', compact('blogPost')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function showYearMonthSlug($year, $month, LaravelBlog $blogPost) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (! $blogPost->published && ! auth()->user()) { |
|
|
|
|
99
|
|
|
abort(404); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (config('laravel-blog.permalink') == 'slug') { |
103
|
|
|
return redirect()->route('showSlug', ['blogPost' => $blogPost->slug] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
if (config('laravel-blog.permalink') == 'id') { |
107
|
|
|
return redirect()->route('showID', ['id' => $blogPost->id] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
if (config('laravel-blog.permalink') == 'year/month/day/slug') { |
111
|
|
|
return redirect()->route('showYearMonthDaySlug', ['year'=>$year, 'month'=>$month, 'day'=>\Carbon\Carbon::parse($blogPost->published_at)->day, 'blogPost' => $blogPost->slug] |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return view('laravel-blog::blog.show', compact('blogPost')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function showYearMonthDaySlug($year, $month, $day, LaravelBlog $blogPost) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if (! $blogPost->published && ! auth()->user()) { |
|
|
|
|
121
|
|
|
abort(404); |
122
|
|
|
} |
123
|
|
|
if (config('laravel-blog.permalink') == 'slug') { |
124
|
|
|
return redirect()->route('showSlug', ['blogPost' => $blogPost->slug] |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
if (config('laravel-blog.permalink') == 'id') { |
128
|
|
|
return redirect()->route('showID', ['id' => $blogPost->id] |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
if (config('laravel-blog.permalink') == 'year/month/slug') { |
132
|
|
|
return redirect()->route('showYearMonthSlug', ['year'=>$year, 'month'=>$month, 'blogPost' => $blogPost->slug] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return view('laravel-blog::blog.show', compact('blogPost')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function showID($id) |
140
|
|
|
{ |
141
|
|
|
$blogPost = LaravelBlog::find($id); |
142
|
|
|
if ($blogPost && ! $blogPost->published && ! auth()->user()) { |
|
|
|
|
143
|
|
|
abort(404); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (config('laravel-blog.permalink') == 'slug') { |
147
|
|
|
return redirect()->route('showSlug', ['blogPost' => $blogPost->slug] |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
if (config('laravel-blog.permalink') == 'year/month/slug') { |
151
|
|
|
return redirect()->route('showYearMonthSlug', ['year'=>\Carbon\Carbon::parse($blogPost->published_at)->year, 'month'=>\Carbon\Carbon::parse($blogPost->published_at)->month, 'blogPost' => $blogPost->slug] |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
View Code Duplication |
if (config('laravel-blog.permalink') == 'year/month/day/slug') { |
|
|
|
|
155
|
|
|
return redirect()->route('showYearMonthDaySlug', ['year'=>\Carbon\Carbon::parse($blogPost->published_at)->year, 'month'=>\Carbon\Carbon::parse($blogPost->published_at)->month, 'day'=>\Carbon\Carbon::parse($blogPost->published_at)->day, 'blogPost' => $blogPost->slug] |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return view('laravel-blog::blog.show', compact('blogPost')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function edit(LaravelBlog $blogPost) |
163
|
|
|
{ |
164
|
|
|
//$this->authorize('update',$blogPost); |
165
|
|
|
|
166
|
|
|
$tags = Tag::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE); |
167
|
|
|
$categories = Category::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE); |
168
|
|
|
|
169
|
|
|
return view('laravel-blog::blog.edit', compact('blogPost', 'tags', 'categories')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function update(Request $request, LaravelBlog $blogPost) |
173
|
|
|
{ |
174
|
|
|
//$this->authorize('update',$blogPost); |
175
|
|
|
$attributes = request()->validate([ |
176
|
|
|
'title'=>['required', 'regex:/^(?![0-9]*$)[a-zA-Z0-9 \-]+$/'], |
177
|
|
|
'content'=>['required'], |
178
|
|
|
'excerpt'=>['max:158'], |
179
|
|
|
'image' => ['image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], |
180
|
|
|
'alt_text'=>[], |
181
|
|
|
]); |
182
|
|
|
$attributes['published'] = request()->has('published'); |
183
|
|
|
|
184
|
|
View Code Duplication |
if ($request->file('image')) { |
|
|
|
|
185
|
|
|
$original_file_name = $request->file('image')->getClientOriginalName(); |
186
|
|
|
$image_path = $request->file('image')->storeAs('blog_posts/'.$blogPost->id, $original_file_name, 'public'); |
187
|
|
|
} else { |
188
|
|
|
$image_path = $blogPost->image; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($blogPost->published == false && request()->has('published')) { |
192
|
|
|
$attributes['published_at'] = date('Y-m-d H:i:s'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$blogPost->category()->associate(request()->get('category')); |
196
|
|
|
|
197
|
|
|
$tags_array = $request->get('tags'); |
198
|
|
|
$tags_names_array = []; |
199
|
|
View Code Duplication |
if ($tags_array) { |
|
|
|
|
200
|
|
|
foreach ($tags_array as $tag_id) { |
201
|
|
|
$tag_object = Tag::find($tag_id); |
202
|
|
|
$tags_names_array[] = $tag_object->name; |
203
|
|
|
} |
204
|
|
|
$blogPost->syncTagsWithType($tags_names_array); |
205
|
|
|
} else { |
206
|
|
|
$blogPost->syncTags([]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$blogPost->update($attributes); |
210
|
|
|
|
211
|
|
|
$blogPost->image = $image_path; |
212
|
|
|
$blogPost->save(); |
213
|
|
|
|
214
|
|
|
return redirect()->route('blog_admin'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function destroy(LaravelBlog $blogPost) |
218
|
|
|
{ |
219
|
|
|
//$this->authorize('destroy',$blogPost); |
220
|
|
|
$blogPost->delete(); |
221
|
|
|
|
222
|
|
|
return redirect()->route('blog_admin'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function admin_page() |
226
|
|
|
{ |
227
|
|
|
$posts = LaravelBlog::orderBy('published_at', 'desc')->get(); |
228
|
|
|
|
229
|
|
|
return view('laravel-blog::blog_admin', compact('posts')); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
View Code Duplication |
public function indexAuthor($user_name) |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$item = User::where('name', '=', $user_name)->first(); |
235
|
|
|
$posts = LaravelBlog::where('published', '=', true)->where('user_id', '=', $item->id)->orderBy('published_at', 'desc')->get(); |
236
|
|
|
|
237
|
|
|
return view('laravel-blog::archive', compact('item', 'posts')); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.