1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Forms; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use Livewire\Form; |
11
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
12
|
|
|
use Xetaravel\Events\Discuss\CategoryWasChangedEvent; |
13
|
|
|
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent; |
14
|
|
|
use Xetaravel\Events\Discuss\ConversationWasLockedEvent; |
15
|
|
|
use Xetaravel\Events\Discuss\ConversationWasPinnedEvent; |
16
|
|
|
use Xetaravel\Events\Discuss\TitleWasChangedEvent; |
17
|
|
|
use Xetaravel\Models\DiscussCategory; |
18
|
|
|
use Xetaravel\Models\DiscussConversation; |
19
|
|
|
use Xetaravel\Models\DiscussPost; |
20
|
|
|
use Xetaravel\Models\DiscussUser; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
class DiscussConversationForm extends Form |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The conversation to update. |
27
|
|
|
* |
28
|
|
|
* @var DiscussConversation|null |
29
|
|
|
*/ |
30
|
|
|
public ?DiscussConversation $discussConversation = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The category of the conversation |
34
|
|
|
* |
35
|
|
|
* @var int|null |
36
|
|
|
*/ |
37
|
|
|
public ?int $category_id = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The title of the conversation. |
41
|
|
|
* |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
public ?string $title = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Whatever the conversation is pinned |
48
|
|
|
* |
49
|
|
|
* @var bool|null |
50
|
|
|
*/ |
51
|
|
|
public ?bool $is_pinned = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Whatever the conversation is locked. |
55
|
|
|
* |
56
|
|
|
* @var bool|null |
57
|
|
|
*/ |
58
|
|
|
public ?bool $is_locked = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The content of the post, only when creating. |
62
|
|
|
* |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
public ?string $content = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The categories used in choice. |
69
|
|
|
* |
70
|
|
|
* @var Collection|array |
71
|
|
|
*/ |
72
|
|
|
public Collection|array $categoriesSearchable = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Function to store the model. |
76
|
|
|
* |
77
|
|
|
* @return DiscussConversation |
78
|
|
|
* |
79
|
|
|
* @throws Throwable |
80
|
|
|
*/ |
81
|
|
|
public function create(): DiscussConversation |
82
|
|
|
{ |
83
|
|
|
$properties = [ |
84
|
|
|
'category_id', |
85
|
|
|
'title', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if (Auth::user()->hasPermissionTo('pin discuss conversation')) { |
|
|
|
|
89
|
|
|
$properties[] = 'is_pinned'; |
90
|
|
|
} |
91
|
|
|
if (Auth::user()->hasPermissionTo('lock discuss conversation')) { |
92
|
|
|
$properties[] = 'is_locked'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return DB::transaction(function () use ($properties) { |
96
|
|
|
$discussConversation = DiscussConversation::create($this->only($properties)); |
97
|
|
|
|
98
|
|
|
$discussPost = DiscussPost::create([ |
99
|
|
|
'conversation_id' => $discussConversation->id, |
|
|
|
|
100
|
|
|
'content' => $this->content |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
DiscussUser::create([ |
104
|
|
|
'conversation_id' => $discussConversation->id, |
105
|
|
|
'is_read' => 1 |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$discussConversation->first_post_id = $discussPost->id; |
|
|
|
|
109
|
|
|
$discussConversation->last_post_id = $discussPost->id; |
|
|
|
|
110
|
|
|
$discussConversation->save(); |
111
|
|
|
|
112
|
|
|
$discussConversation->category->last_conversation_id = $discussConversation->getKey(); |
|
|
|
|
113
|
|
|
$discussConversation->category->save(); |
114
|
|
|
|
115
|
|
|
$parser = new MentionParser($discussPost, [ |
|
|
|
|
116
|
|
|
'regex' => config('mentions.regex') |
117
|
|
|
]); |
118
|
|
|
$content = $parser->parse($discussPost->content); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$discussPost->content = $content; |
121
|
|
|
$discussPost->save(); |
122
|
|
|
|
123
|
|
|
event(new ConversationWasCreatedEvent(Auth::user(), $discussConversation)); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $discussConversation; |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Function to update the conversation. |
131
|
|
|
* |
132
|
|
|
* @return DiscussConversation |
133
|
|
|
*/ |
134
|
|
|
public function update(): DiscussConversation |
135
|
|
|
{ |
136
|
|
|
// The title has changed |
137
|
|
|
if ($this->discussConversation->title !== $this->title) { |
138
|
|
|
event(new TitleWasChangedEvent($this->discussConversation, $this->title, $this->discussConversation->title)); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->discussConversation->title = $this->title; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// The category has changed |
144
|
|
|
if ($this->discussConversation->category_id !== $this->category_id) { |
145
|
|
|
event(new CategoryWasChangedEvent($this->discussConversation, $this->category_id, $this->discussConversation->category_id)); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$this->discussConversation->category_id = $this->category_id; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// The pinned status has changed |
151
|
|
|
if (Auth::user()->hasPermissionTo('pin discuss conversation')) { |
152
|
|
|
if ($this->discussConversation->is_pinned !== $this->is_pinned && $this->is_pinned === true) { |
153
|
|
|
event(new ConversationWasPinnedEvent($this->discussConversation)); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
$this->discussConversation->is_pinned = $this->is_pinned; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// The locked status has changed |
159
|
|
|
if (Auth::user()->hasPermissionTo('lock discuss conversation')) { |
160
|
|
|
if ($this->discussConversation->is_locked !== $this->is_locked && $this->is_locked === true) { |
161
|
|
|
event(new ConversationWasLockedEvent($this->discussConversation)); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
$this->discussConversation->is_locked = $this->is_locked; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->discussConversation->save(); |
|
|
|
|
167
|
|
|
|
168
|
|
|
return $this->discussConversation; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Function to search categories. |
173
|
|
|
* |
174
|
|
|
* @param string $value |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function searchCategories(string $value = ''): void |
179
|
|
|
{ |
180
|
|
|
$selectedOption = DiscussCategory::where('id', $this->category_id)->get(); |
181
|
|
|
|
182
|
|
|
$categories = DiscussCategory::query() |
183
|
|
|
->where('title', 'like', "%$value%"); |
184
|
|
|
|
185
|
|
|
$this->categoriesSearchable = $categories->take(10) |
186
|
|
|
->orderBy('title') |
|
|
|
|
187
|
|
|
->get() |
188
|
|
|
->merge($selectedOption); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|