1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Http\Controllers\Discuss; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\RedirectResponse; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Xetaravel\Events\Discuss\PostWasSolvedEvent; |
11
|
|
|
use Xetaravel\Models\DiscussConversation; |
12
|
|
|
use Xetaravel\Models\DiscussPost; |
13
|
|
|
|
14
|
|
|
class PostController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Redirect an user to a conversation, page and post. |
18
|
|
|
* |
19
|
|
|
* @param Request $request |
20
|
|
|
* @param int $id The ID of the post. |
21
|
|
|
* |
22
|
|
|
* @return RedirectResponse |
23
|
|
|
*/ |
24
|
|
|
public function show(Request $request, int $id): RedirectResponse |
25
|
|
|
{ |
26
|
|
|
$post = DiscussPost::findOrFail($id); |
27
|
|
|
|
28
|
|
|
$postsBefore = DiscussPost::where([ |
29
|
|
|
['conversation_id', $post->conversation_id], |
|
|
|
|
30
|
|
|
['created_at', '<', $post->created_at] |
|
|
|
|
31
|
|
|
])->count(); |
32
|
|
|
|
33
|
|
|
$postsPerPage = config('xetaravel.pagination.discuss.post_per_page'); |
34
|
|
|
|
35
|
|
|
$page = floor($postsBefore / $postsPerPage) + 1; |
36
|
|
|
$page = ($page > 1) ? $page : 1; |
37
|
|
|
|
38
|
|
|
$request->session()->keep(['primary', 'error', 'warning', 'success', 'info']); |
39
|
|
|
|
40
|
|
|
return redirect() |
41
|
|
|
->route( |
|
|
|
|
42
|
|
|
'discuss.conversation.show', |
43
|
|
|
[ |
44
|
|
|
'slug' => $post->conversation->slug, |
|
|
|
|
45
|
|
|
'id' => $post->conversation->id, |
46
|
|
|
'page' => $page, |
47
|
|
|
'#post-' . $post->getKey() |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Mark as solved. |
54
|
|
|
* |
55
|
|
|
* @param int $id |
56
|
|
|
* |
57
|
|
|
* @return RedirectResponse |
58
|
|
|
*/ |
59
|
|
|
public function solved(int $id): RedirectResponse |
60
|
|
|
{ |
61
|
|
|
$post = DiscussPost::findOrFail($id); |
62
|
|
|
|
63
|
|
|
$this->authorize('solved', $post->conversation); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if ($post->getKey() === $post->conversation->solved_post_id) { |
66
|
|
|
return back() |
67
|
|
|
->with('danger', 'This post is already the solved post !'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!is_null($post->conversation->solved_post_id)) { |
|
|
|
|
71
|
|
|
return back() |
72
|
|
|
->with('danger', 'This conversation has already a solved post !'); |
73
|
|
|
} |
74
|
|
|
$conversation = DiscussConversation::findOrFail($post->conversation_id); |
75
|
|
|
|
76
|
|
|
$conversation->solved_post_id = $post->getKey(); |
77
|
|
|
$conversation->is_solved = true; |
78
|
|
|
$conversation->save(); |
79
|
|
|
|
80
|
|
|
$post->is_solved = true; |
81
|
|
|
$post->save(); |
82
|
|
|
|
83
|
|
|
event(new PostWasSolvedEvent(Auth::user(), $post)); |
84
|
|
|
|
85
|
|
|
return redirect() |
86
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
87
|
|
|
->with('success', 'This reply as been marked as solved !'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|