1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Discuss; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
8
|
|
|
use Xetaravel\Models\DiscussCategory; |
9
|
|
|
use Xetaravel\Models\DiscussThread; |
10
|
|
|
use Xetaravel\Models\DiscussLog; |
11
|
|
|
use Xetaravel\Models\Repositories\DiscussThreadRepository; |
12
|
|
|
use Xetaravel\Models\Validators\DiscussThreadValidator; |
13
|
|
|
|
14
|
|
|
class ThreadController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Show the thread by its id. |
18
|
|
|
* |
19
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function show(Request $request, string $slug, int $id) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$thread = DiscussThread::findOrFail($id); |
24
|
|
|
|
25
|
|
|
$comments = $thread->comments() |
26
|
|
|
->where('id', '!=', $thread->solved_comment_id) |
27
|
|
|
->paginate(config('xetaravel.pagination.discuss.comment_per_page')); |
28
|
|
|
$comments->load('user'); |
29
|
|
|
|
30
|
|
|
$commentsWithLogs = $thread->getCommentWithLogs( |
31
|
|
|
collect($comments->items()), |
32
|
|
|
$this->getCurrentPage($request) |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->breadcrumbs->setListElementClasses('breadcrumbs'); |
36
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb(e($thread->title), $thread->thread_url); |
37
|
|
|
|
38
|
|
|
return view('Discuss::thread.show', compact('thread', 'comments', 'commentsWithLogs', 'breadcrumbs')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Show the create form. |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\View\View |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function showCreateForm(): View |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$categories = DiscussCategory::pluck('title', 'id'); |
49
|
|
|
|
50
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb('Start a discussion', route('discuss.thread.create')); |
51
|
|
|
|
52
|
|
|
return view('Discuss::thread.create', compact('breadcrumbs', 'categories')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Handle a thread create request for the application. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Http\Request $request |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Http\RedirectResponse |
61
|
|
|
*/ |
62
|
|
|
public function create(Request $request) |
63
|
|
|
{ |
64
|
|
|
DiscussThreadValidator::create($request->all())->validate(); |
65
|
|
|
|
66
|
|
|
if (DiscussThread::isFlooding(Auth::user(), 'xetaravel.flood.discuss.thread')) { |
67
|
|
|
return back() |
68
|
|
|
->withInput() |
69
|
|
|
->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
70
|
|
|
} |
71
|
|
|
$thread = DiscussThreadRepository::create($request->all()); |
72
|
|
|
|
73
|
|
|
$parser = new MentionParser($thread); |
74
|
|
|
$content = $parser->parse($thread->content); |
75
|
|
|
|
76
|
|
|
$thread->content = $content; |
77
|
|
|
$thread->save(); |
78
|
|
|
|
79
|
|
|
return redirect() |
80
|
|
|
->route('discuss.thread.show', ['slug' => $thread->slug, 'id' => $thread->getKey()]) |
81
|
|
|
->with('success', 'Your discussion has been created successfully !'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the current page for the thread. |
86
|
|
|
* |
87
|
|
|
* @param \Illuminate\Http\Request $request |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
protected function getCurrentPage(Request $request) |
92
|
|
|
{ |
93
|
|
|
return !is_null($request->get('page')) ? (int)$request->get('page') : 1; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.