1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Discuss; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
8
|
|
|
use Xetaravel\Models\DiscussConversation; |
9
|
|
|
use Xetaravel\Models\DiscussPost; |
10
|
|
|
use Xetaravel\Models\Repositories\DiscussPostRepository; |
11
|
|
|
use Xetaravel\Models\Repositories\DiscussUserRepository; |
12
|
|
|
use Xetaravel\Models\User; |
13
|
|
|
use Xetaravel\Models\Validators\DiscussPostValidator; |
14
|
|
|
|
15
|
|
|
class PostController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Create a post for a conversation. |
19
|
|
|
* |
20
|
|
|
* @param \Illuminate\Http\Request $request |
21
|
|
|
* |
22
|
|
|
* @return \Illuminate\Http\RedirectResponse |
23
|
|
|
*/ |
24
|
|
|
public function create(Request $request): RedirectResponse |
25
|
|
|
{ |
26
|
|
|
$conversation = DiscussConversation::findOrFail($request->conversation_id); |
|
|
|
|
27
|
|
|
|
28
|
|
|
if (DiscussPost::isFlooding('xetaravel.flood.discuss.post')) { |
29
|
|
|
return back() |
30
|
|
|
->withInput() |
31
|
|
|
->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
DiscussPostValidator::create($request->all())->validate(); |
35
|
|
|
$post = DiscussPostRepository::create($request->all()); |
36
|
|
|
$user = DiscussUserRepository::create($request->all()); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$parser = new MentionParser($post); |
39
|
|
|
$content = $parser->parse($post->content); |
40
|
|
|
|
41
|
|
|
$post->content = $content; |
42
|
|
|
$post->save(); |
43
|
|
|
|
44
|
|
|
return redirect() |
45
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
46
|
|
|
->with('success', 'Your reply has been posted successfully !'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Redirect an user to a conversation, page and post. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Http\Request $request |
53
|
|
|
* @param int $id The ID of the post. |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Http\RedirectResponse |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function show(Request $request, int $id): RedirectResponse |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$post = DiscussPost::findOrFail($id); |
60
|
|
|
|
61
|
|
|
$postsBefore = DiscussPost::where([ |
62
|
|
|
['conversation_id', $post->conversation_id], |
63
|
|
|
['created_at', '<', $post->created_at] |
64
|
|
|
])->count(); |
65
|
|
|
|
66
|
|
|
$postsPerPage = config('xetaravel.pagination.discuss.post_per_page'); |
67
|
|
|
|
68
|
|
|
$page = floor($postsBefore / $postsPerPage) + 1; |
69
|
|
|
$page = ($page > 1) ? $page : 1; |
70
|
|
|
|
71
|
|
|
$request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return redirect() |
74
|
|
|
->route( |
75
|
|
|
'discuss.conversation.show', |
76
|
|
|
[ |
77
|
|
|
'slug' => $post->conversation->slug, |
78
|
|
|
'id' => $post->conversation->id, |
79
|
|
|
'page' => $page, |
80
|
|
|
'#post-' . $post->getKey() |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Handle a delete action for the post. |
87
|
|
|
* |
88
|
|
|
* @param \Illuminate\Http\Request $request |
89
|
|
|
* @param int $id |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Http\RedirectResponse |
92
|
|
|
*/ |
93
|
|
|
public function delete(Request $request, int $id): RedirectResponse |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$post = DiscussPost::findOrFail($id); |
96
|
|
|
$conversation = $post->conversation; |
97
|
|
|
|
98
|
|
|
if ($conversation->first_post_id == $post->getKey()) { |
99
|
|
|
return redirect() |
100
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
101
|
|
|
->with('danger', 'You can not deete the first post of a conversation !'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($conversation->last_post_id == $post->getKey()) { |
105
|
|
|
$previousPost = DiscussPostRepository::findPreviousPost($post); |
106
|
|
|
|
107
|
|
|
$conversation->last_post_id = $previousPost->getKey(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($conversation->solved_post_id == $post->getKey()) { |
111
|
|
|
$conversation->solved_post_id = null; |
112
|
|
|
$conversation->is_solved = false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($post->delete()) { |
116
|
|
|
$conversation->save(); |
117
|
|
|
|
118
|
|
|
return redirect() |
119
|
|
|
->route('discuss.conversation.show', ['id' => $conversation->getKey(), 'slug' => $conversation->slug]) |
120
|
|
|
->with('success', 'This post has been deleted successfully !'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return redirect() |
124
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
125
|
|
|
->with('danger', 'An error occurred while deleting this post !'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Mark as solved. |
130
|
|
|
* |
131
|
|
|
* @param \Illuminate\Http\Request $request |
132
|
|
|
* @param int $id |
133
|
|
|
* |
134
|
|
|
* @return \Illuminate\Http\RedirectResponse |
135
|
|
|
*/ |
136
|
|
|
public function solved(Request $request, int $id): RedirectResponse |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$post = DiscussPost::findOrFail($id); |
139
|
|
|
|
140
|
|
|
if ($post->getKey() == $post->conversation->solved_post_id) { |
141
|
|
|
return back() |
142
|
|
|
->with('danger', 'This post is already the solved post !'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!is_null($post->conversation->solved_post_id)) { |
146
|
|
|
return back() |
147
|
|
|
->with('danger', 'This conversation has already a solved post !'); |
148
|
|
|
} |
149
|
|
|
$conversation = DiscussConversation::findOrFail($post->conversation_id); |
150
|
|
|
|
151
|
|
|
$conversation->solved_post_id = $post->getKey(); |
152
|
|
|
$conversation->is_solved = true; |
153
|
|
|
$conversation->save(); |
154
|
|
|
|
155
|
|
|
return redirect() |
156
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
157
|
|
|
->with('success', 'This reply as been marked as solved !'); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.