|
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 Livewire\Attributes\Validate; |
|
10
|
|
|
use Livewire\Form; |
|
11
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
|
12
|
|
|
use Xetaravel\Models\BlogComment; |
|
13
|
|
|
use Xetaravel\Models\DiscussConversation; |
|
14
|
|
|
use Xetaravel\Models\DiscussPost; |
|
15
|
|
|
use Xetaravel\Models\DiscussUser; |
|
16
|
|
|
|
|
17
|
|
|
class DiscussConversationForm extends Form |
|
18
|
|
|
{ |
|
19
|
|
|
public ?DiscussConversation $discussConversation = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The category of the conversation |
|
23
|
|
|
* |
|
24
|
|
|
* @var int|null |
|
25
|
|
|
*/ |
|
26
|
|
|
#[Validate('required|integer|exists:discuss_categories,id')] |
|
27
|
|
|
public ?int $category_id = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The title of the conversation. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string|null |
|
33
|
|
|
*/ |
|
34
|
|
|
#[Validate('required|min:5')] |
|
35
|
|
|
public ?string $title = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Whatever the conversation is pinned |
|
39
|
|
|
* |
|
40
|
|
|
* @var bool|null |
|
41
|
|
|
*/ |
|
42
|
|
|
#[Validate('boolean')] |
|
43
|
|
|
public ?bool $is_pinned = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Whatever the conversation is locked |
|
47
|
|
|
* |
|
48
|
|
|
* @var bool|null |
|
49
|
|
|
*/ |
|
50
|
|
|
#[Validate('boolean')] |
|
51
|
|
|
public ?bool $is_locked = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The categories used in choice. |
|
55
|
|
|
* |
|
56
|
|
|
* @var Collection|array |
|
57
|
|
|
*/ |
|
58
|
|
|
public Collection|array $categoriesSearchable = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The content of the post. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string|null |
|
64
|
|
|
*/ |
|
65
|
|
|
#[Validate('required|min:10')] |
|
66
|
|
|
public ?string $content = null; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Function to store the model. |
|
70
|
|
|
* |
|
71
|
|
|
* @return DiscussConversation |
|
72
|
|
|
*/ |
|
73
|
|
|
public function store(): DiscussConversation |
|
74
|
|
|
{ |
|
75
|
|
|
$properties = [ |
|
76
|
|
|
'category_id', |
|
77
|
|
|
'title', |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
if (Auth::user()->hasPermissionTo('manage discuss conversation')) { |
|
|
|
|
|
|
81
|
|
|
$properties += [ |
|
82
|
|
|
'is_locked', |
|
83
|
|
|
'is_pinned', |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$discussConversation = DiscussConversation::create($this->only($properties)); |
|
88
|
|
|
|
|
89
|
|
|
$post = DiscussPost::create([ |
|
90
|
|
|
'conversation_id' => $discussConversation->id, |
|
|
|
|
|
|
91
|
|
|
'content' => $this->content |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
DiscussUser::create([ |
|
95
|
|
|
'conversation_id' => $discussConversation->id, |
|
96
|
|
|
'is_read' => 1 |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
$discussConversation->first_post_id = $post->id; |
|
|
|
|
|
|
100
|
|
|
$discussConversation->last_post_id = $post->id; |
|
|
|
|
|
|
101
|
|
|
$discussConversation->save(); |
|
102
|
|
|
|
|
103
|
|
|
$discussConversation->category->last_conversation_id = $discussConversation->getKey(); |
|
|
|
|
|
|
104
|
|
|
$discussConversation->category->save(); |
|
105
|
|
|
|
|
106
|
|
|
$parser = new MentionParser($post, [ |
|
|
|
|
|
|
107
|
|
|
'regex' => config('mentions.regex') |
|
108
|
|
|
]); |
|
109
|
|
|
$content = $parser->parse($post->content); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$post->content = $content; |
|
112
|
|
|
$post->save(); |
|
113
|
|
|
|
|
114
|
|
|
return $discussConversation; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|