|
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
|
|
|
* Mark as solved. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Illuminate\Http\Request $request |
|
89
|
|
|
* @param int $id |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function solved(Request $request, int $id): RedirectResponse |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$post = DiscussPost::findOrFail($id); |
|
96
|
|
|
|
|
97
|
|
|
if ($post->getKey() == $post->conversation->solved_post_id) { |
|
98
|
|
|
return back() |
|
99
|
|
|
->with('danger', 'This post is already the solved post !'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!is_null($post->conversation->solved_post_id)) { |
|
103
|
|
|
return back() |
|
104
|
|
|
->with('danger', 'This conversation has already a solved post !'); |
|
105
|
|
|
} |
|
106
|
|
|
$conversation = DiscussConversation::findOrFail($post->conversation_id); |
|
107
|
|
|
|
|
108
|
|
|
$conversation->solved_post_id = $post->getKey(); |
|
109
|
|
|
$conversation->is_solved = true; |
|
110
|
|
|
$conversation->save(); |
|
111
|
|
|
|
|
112
|
|
|
return redirect() |
|
113
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
|
114
|
|
|
->with('success', 'This reply as been marked as solved !'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.