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\PostWasDeletedEvent; |
11
|
|
|
use Xetaravel\Events\Discuss\PostWasSolvedEvent; |
12
|
|
|
use Xetaravel\Models\DiscussConversation; |
13
|
|
|
use Xetaravel\Models\DiscussPost; |
14
|
|
|
|
15
|
|
|
class PostController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Redirect an user to a conversation, page and post. |
19
|
|
|
* |
20
|
|
|
* @param Request $request |
21
|
|
|
* @param int $id The ID of the post. |
22
|
|
|
* |
23
|
|
|
* @return RedirectResponse |
24
|
|
|
*/ |
25
|
|
|
public function show(Request $request, int $id): RedirectResponse |
26
|
|
|
{ |
27
|
|
|
$post = DiscussPost::findOrFail($id); |
28
|
|
|
|
29
|
|
|
$postsBefore = DiscussPost::where([ |
30
|
|
|
['conversation_id', $post->conversation_id], |
31
|
|
|
['created_at', '<', $post->created_at] |
32
|
|
|
])->count(); |
33
|
|
|
|
34
|
|
|
$postsPerPage = config('xetaravel.pagination.discuss.post_per_page'); |
35
|
|
|
|
36
|
|
|
$page = floor($postsBefore / $postsPerPage) + 1; |
37
|
|
|
$page = ($page > 1) ? $page : 1; |
38
|
|
|
|
39
|
|
|
$request->session()->keep(['primary', 'error', 'warning', 'success', 'info']); |
40
|
|
|
|
41
|
|
|
return redirect() |
42
|
|
|
->route( |
|
|
|
|
43
|
|
|
'discuss.conversation.show', |
44
|
|
|
[ |
45
|
|
|
'slug' => $post->conversation->slug, |
46
|
|
|
'id' => $post->conversation->id, |
47
|
|
|
'page' => $page, |
48
|
|
|
'#post-' . $post->getKey() |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Mark as solved. |
55
|
|
|
* |
56
|
|
|
* @param int $id |
57
|
|
|
* |
58
|
|
|
* @return RedirectResponse |
59
|
|
|
*/ |
60
|
|
|
public function solved(int $id): RedirectResponse |
61
|
|
|
{ |
62
|
|
|
$post = DiscussPost::findOrFail($id); |
63
|
|
|
|
64
|
|
|
$this->authorize('solved', $post->conversation); |
65
|
|
|
|
66
|
|
|
if ($post->getKey() === $post->conversation->solved_post_id) { |
67
|
|
|
return back() |
68
|
|
|
->with('danger', 'This post is already the solved post !'); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!is_null($post->conversation->solved_post_id)) { |
72
|
|
|
return back() |
73
|
|
|
->with('danger', 'This conversation has already a solved post !'); |
74
|
|
|
} |
75
|
|
|
$conversation = DiscussConversation::findOrFail($post->conversation_id); |
76
|
|
|
|
77
|
|
|
$conversation->solved_post_id = $post->getKey(); |
78
|
|
|
$conversation->is_solved = true; |
79
|
|
|
$conversation->save(); |
80
|
|
|
|
81
|
|
|
$post->is_solved = true; |
82
|
|
|
$post->save(); |
83
|
|
|
|
84
|
|
|
event(new PostWasSolvedEvent(Auth::user(), $post)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return redirect() |
87
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
88
|
|
|
->with('success', 'This reply as been marked as solved !'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.