|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Discuss; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
8
|
|
|
use Illuminate\Http\RedirectResponse; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use Livewire\Attributes\Url; |
|
12
|
|
|
use Livewire\Component; |
|
13
|
|
|
use Masmerise\Toaster\Toastable; |
|
14
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
|
15
|
|
|
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent; |
|
16
|
|
|
use Xetaravel\Livewire\Forms\DiscussConversationForm; |
|
17
|
|
|
use Xetaravel\Models\DiscussCategory; |
|
18
|
|
|
use Xetaravel\Models\DiscussConversation; |
|
19
|
|
|
|
|
20
|
|
|
class CreateConversation extends Component |
|
21
|
|
|
{ |
|
22
|
|
|
use AuthorizesRequests; |
|
23
|
|
|
use Toastable; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The form used to create/update a model. |
|
27
|
|
|
* |
|
28
|
|
|
* @var DiscussConversationForm |
|
29
|
|
|
*/ |
|
30
|
|
|
public DiscussConversationForm $form; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Used to show the Edit/Create modal. |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public bool $showModal = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whatever the creating url param is set or not. |
|
41
|
|
|
* |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
#[Url(except: false)] |
|
45
|
|
|
public bool $creating = false; |
|
46
|
|
|
|
|
47
|
|
|
public function mount(Request $request): void |
|
48
|
|
|
{ |
|
49
|
|
|
if ($request->boolean('creating') === true) { |
|
50
|
|
|
$this->create(); |
|
51
|
|
|
|
|
52
|
|
|
$this->creating = false; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function render() |
|
57
|
|
|
{ |
|
58
|
|
|
return view('livewire.discuss.create-conversation'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create a blank model and assign it to the model. (Used in create modal) |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function create(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->authorize('create', DiscussConversation::class); |
|
69
|
|
|
|
|
70
|
|
|
$this->form->reset(); |
|
71
|
|
|
$this->searchCategories(); |
|
72
|
|
|
|
|
73
|
|
|
$this->showModal = true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Validate and save the model. |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function save(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->authorize('create', DiscussConversation::class); |
|
84
|
|
|
|
|
85
|
|
|
$this->validate(); |
|
86
|
|
|
|
|
87
|
|
|
if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation') && |
|
88
|
|
|
!Auth::user()->hasPermissionTo('manage discuss conversation')) { |
|
|
|
|
|
|
89
|
|
|
$this->error('Wow, keep calm bro, and try to not flood !'); |
|
90
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
$discussConversation = $this->form->store(); |
|
94
|
|
|
|
|
95
|
|
|
event(new ConversationWasCreatedEvent(Auth::user(), $discussConversation)); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
$this->showModal = false; |
|
98
|
|
|
|
|
99
|
|
|
redirect() |
|
100
|
|
|
->route('discuss.conversation.show', ['slug' => $discussConversation->slug, 'id' => $discussConversation->getKey()]) |
|
|
|
|
|
|
101
|
|
|
->success('Your discussion has been created successfully !'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Function to search suppliers in form. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $value |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function searchCategories(string $value = ''): void |
|
112
|
|
|
{ |
|
113
|
|
|
$selectedOption = DiscussCategory::where('id', $this->form->category_id)->get(); |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$categories = DiscussCategory::query() |
|
117
|
|
|
->where('title', 'like', "%$value%"); |
|
118
|
|
|
|
|
119
|
|
|
$this->form->categoriesSearchable = $categories->take(10) |
|
120
|
|
|
->orderBy('title') |
|
|
|
|
|
|
121
|
|
|
->get() |
|
122
|
|
|
->merge($selectedOption); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|