|
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 Illuminate\View\View; |
|
11
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
|
12
|
|
|
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent; |
|
13
|
|
|
use Xetaravel\Models\DiscussCategory; |
|
14
|
|
|
use Xetaravel\Models\DiscussConversation; |
|
15
|
|
|
use Xetaravel\Models\Repositories\DiscussConversationRepository; |
|
16
|
|
|
use Xetaravel\Models\Validators\DiscussConversationValidator; |
|
17
|
|
|
|
|
18
|
|
|
class ConversationController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Get the current page for the conversation. |
|
22
|
|
|
* |
|
23
|
|
|
* @param Request $request |
|
24
|
|
|
* |
|
25
|
|
|
* @return int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getCurrentPage(Request $request): int |
|
28
|
|
|
{ |
|
29
|
|
|
return !is_null($request->get('page')) ? (int) $request->get('page') : 1; |
|
30
|
|
|
} |
|
31
|
|
|
/** |
|
32
|
|
|
* Show the conversation by its id. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Http\Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function show(Request $request, string $slug, int $id) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$conversation = DiscussConversation::findOrFail($id); |
|
39
|
|
|
$categories = DiscussCategory::pluckLocked('title', 'id'); |
|
40
|
|
|
|
|
41
|
|
|
$posts = $conversation->posts() |
|
42
|
|
|
->where('id', '!=', $conversation->solved_post_id) |
|
|
|
|
|
|
43
|
|
|
->where('id', '!=', $conversation->first_post_id) |
|
|
|
|
|
|
44
|
|
|
->paginate(config('xetaravel.pagination.discuss.post_per_page')); |
|
45
|
|
|
|
|
46
|
|
|
$postsWithLogs = $conversation->getPostsWithLogs( |
|
47
|
|
|
collect($posts->items()), |
|
48
|
|
|
$this->getCurrentPage($request) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$this->breadcrumbs->setListElementClasses('breadcrumbs'); |
|
52
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb(e($conversation->title), $conversation->conversation_url); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
return view( |
|
55
|
|
|
'Discuss::conversation.show', |
|
56
|
|
|
compact('conversation', 'posts', 'postsWithLogs', 'breadcrumbs', 'categories') |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Handle a conversation create request for the application. |
|
62
|
|
|
* |
|
63
|
|
|
* @param Request $request |
|
64
|
|
|
* |
|
65
|
|
|
* @return RedirectResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
public function create(Request $request) |
|
68
|
|
|
{ |
|
69
|
|
|
DiscussConversationValidator::create($request->all())->validate(); |
|
70
|
|
|
|
|
71
|
|
|
// Users that have the permission "manage.discuss" can bypass this rule. (Default to Administrator) |
|
72
|
|
|
if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation') && |
|
73
|
|
|
!Auth::user()->hasPermission('manage.discuss') |
|
|
|
|
|
|
74
|
|
|
) { |
|
75
|
|
|
return back() |
|
76
|
|
|
->withInput() |
|
|
|
|
|
|
77
|
|
|
->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
|
78
|
|
|
} |
|
79
|
|
|
$conversation = DiscussConversationRepository::create($request->all()); |
|
80
|
|
|
$post = $conversation->firstPost; |
|
81
|
|
|
|
|
82
|
|
|
$parser = new MentionParser($post, [ |
|
83
|
|
|
'regex' => config('mentions.regex') |
|
84
|
|
|
]); |
|
85
|
|
|
$content = $parser->parse($post->content); |
|
86
|
|
|
|
|
87
|
|
|
$post->content = $content; |
|
88
|
|
|
$post->save(); |
|
89
|
|
|
|
|
90
|
|
|
event(new ConversationWasCreatedEvent($conversation, Auth::user())); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
return redirect() |
|
93
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
|
|
|
|
|
|
94
|
|
|
->with('success', 'Your discussion has been created successfully !'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Handle a conversation update request for the application. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Request $request |
|
101
|
|
|
* @param string $slug The slug of the conversation to update. |
|
102
|
|
|
* @param int $id The id of the conversation to update. |
|
103
|
|
|
* |
|
104
|
|
|
* @return RedirectResponse |
|
105
|
|
|
*/ |
|
106
|
|
|
public function update(Request $request, string $slug, int $id) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$conversation = DiscussConversation::findOrFail($id); |
|
109
|
|
|
|
|
110
|
|
|
$this->authorize('update', $conversation); |
|
111
|
|
|
|
|
112
|
|
|
DiscussConversationValidator::update($request->all(), $id)->validate(); |
|
113
|
|
|
$conversation = DiscussConversationRepository::update($request->all(), $conversation); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
return redirect() |
|
116
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
|
117
|
|
|
->with('success', 'Your discussion has been updated successfully !'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Handle the delete request for a conversation. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $slug The slug of the conversation to delete. |
|
124
|
|
|
* @param int $id The id of the conversation to delete. |
|
125
|
|
|
* |
|
126
|
|
|
* @return RedirectResponse |
|
127
|
|
|
*/ |
|
128
|
|
|
public function delete(string $slug, int $id): RedirectResponse |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
$conversation = DiscussConversation::findOrFail($id); |
|
131
|
|
|
|
|
132
|
|
|
$this->authorize('delete', $conversation); |
|
133
|
|
|
|
|
134
|
|
|
if ($conversation->delete()) { |
|
135
|
|
|
return redirect() |
|
136
|
|
|
->route('discuss.index') |
|
137
|
|
|
->with('success', 'This discussion has been deleted successfully !'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return back() |
|
141
|
|
|
->with('danger', 'An error occurred while deleting this discussion !'); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.