1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Blog; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Xetaravel\Events\Badges\CommentEvent; |
8
|
|
|
use Xetaravel\Http\Controllers\Controller; |
9
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
10
|
|
|
use Xetaravel\Models\Article; |
11
|
|
|
use Xetaravel\Models\Comment; |
12
|
|
|
use Xetaravel\Models\Repositories\CommentRepository; |
13
|
|
|
use Xetaravel\Models\User; |
14
|
|
|
use Xetaravel\Models\Validators\CommentValidator; |
15
|
|
|
|
16
|
|
|
class CommentController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Create a comment for an article. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Http\Request $request |
22
|
|
|
* |
23
|
|
|
* @return \Illuminate\Http\RedirectResponse |
24
|
|
|
*/ |
25
|
|
|
public function create(Request $request): RedirectResponse |
26
|
|
|
{ |
27
|
|
|
Article::findOrFail($request->article_id); |
28
|
|
|
|
29
|
|
|
if (Comment::isFlooding('xetaravel.flood.blog.comment')) { |
30
|
|
|
return back() |
31
|
|
|
->withInput() |
32
|
|
|
->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
CommentValidator::create($request->all())->validate(); |
36
|
|
|
$comment = CommentRepository::create($request->all()); |
37
|
|
|
|
38
|
|
|
$parser = new MentionParser($comment); |
39
|
|
|
$content = $parser->parse($comment->content); |
40
|
|
|
|
41
|
|
|
$comment->content = $content; |
42
|
|
|
$comment->save(); |
43
|
|
|
|
44
|
|
|
// We must find the user else we won't see the updated comment_count. |
45
|
|
|
event(new CommentEvent(User::find(Auth::id()))); |
|
|
|
|
46
|
|
|
|
47
|
|
|
return redirect() |
48
|
|
|
->route('blog.comment.show', ['id' => $comment->getKey()]) |
49
|
|
|
->with('success', 'Your comment has been posted successfully !'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Redirect an user to an article, page and comment. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Http\Request $request |
56
|
|
|
* @param int $id The ID of the comment. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Http\RedirectResponse |
59
|
|
|
*/ |
60
|
|
|
public function show(Request $request, int $id): RedirectResponse |
61
|
|
|
{ |
62
|
|
|
$comment = Comment::findOrFail($id); |
63
|
|
|
|
64
|
|
|
$commentsBefore = Comment::where([ |
65
|
|
|
['article_id', $comment->article_id], |
66
|
|
|
['created_at', '<', $comment->created_at] |
67
|
|
|
])->count(); |
68
|
|
|
|
69
|
|
|
$commentsPerPage = config('xetaravel.pagination.blog.comment_per_page'); |
70
|
|
|
|
71
|
|
|
$page = floor($commentsBefore / $commentsPerPage) + 1; |
72
|
|
|
$page = ($page > 1) ? $page : 1; |
73
|
|
|
|
74
|
|
|
$request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']); |
75
|
|
|
|
76
|
|
|
return redirect() |
77
|
|
|
->route( |
78
|
|
|
'blog.article.show', |
79
|
|
|
[ |
80
|
|
|
'slug' => $comment->article->slug, |
81
|
|
|
'id' => $comment->article->id, |
82
|
|
|
'page' => $page, |
83
|
|
|
'#comment-' . $comment->getKey() |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Handle a delete action for the comment. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Http\Request $request |
92
|
|
|
* @param int $id |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Http\RedirectResponse |
95
|
|
|
*/ |
96
|
|
|
public function delete(int $id): RedirectResponse |
97
|
|
|
{ |
98
|
|
|
$comment = Comment::findOrFail($id); |
99
|
|
|
|
100
|
|
|
$this->authorize('delete', $comment); |
101
|
|
|
|
102
|
|
|
if ($comment->delete()) { |
103
|
|
|
return redirect() |
104
|
|
|
->route( |
105
|
|
|
'blog.article.show', |
106
|
|
|
['id' => $comment->article->getKey(), 'slug' => $comment->article->slug] |
107
|
|
|
) |
108
|
|
|
->with('success', 'This comment has been deleted successfully !'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return redirect() |
112
|
|
|
->route('blog.comment.show', ['id' => $comment->getKey()]) |
113
|
|
|
->with('danger', 'An error occurred while deleting this comment !'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|