80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Mail\UserNotification;
|
|
use App\Models\User;
|
|
use Filament\Actions\Concerns\InteractsWithActions;
|
|
use Filament\Actions\Contracts\HasActions;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Schemas\Concerns\InteractsWithSchemas;
|
|
use Filament\Schemas\Contracts\HasSchemas;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Livewire\Component;
|
|
|
|
class UserNotifications extends Component implements HasActions, HasSchemas, HasTable
|
|
{
|
|
use InteractsWithActions;
|
|
use InteractsWithSchemas;
|
|
use InteractsWithTable;
|
|
|
|
public ?array $data = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->form->fill();
|
|
}
|
|
|
|
public function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
Select::make('recipient')
|
|
->placeholder('Select an recipient')
|
|
->required()
|
|
->options(fn () => User::query()->where('client_id', auth()->user()->client_id)->orwhereIn('id', [1, 2])->pluck('name', 'id')),
|
|
Toggle::make('notify_by_email')->label('Send also as E-Mail'),
|
|
Textarea::make('message')->rows(4)
|
|
->required(),
|
|
])
|
|
->statePath('data');
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$data = $this->form->getState();
|
|
|
|
$recipient = User::find((int) $data['recipient']);
|
|
|
|
if ($recipient) {
|
|
if ($data['notify_by_email']) {
|
|
Mail::to($recipient)->send(new UserNotification($data['subject'], $data['message']));
|
|
}
|
|
Notification::make()
|
|
->title($data['message'])
|
|
->icon('phosphor-user-duotone')
|
|
->body('from: '.auth()->user()->name)
|
|
->sendToDatabase($recipient);
|
|
|
|
Notification::make()
|
|
->title('Message send successfully')
|
|
->success()
|
|
->send();
|
|
|
|
$this->dispatch('close-modal', id: 'send_message_modal');
|
|
}
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.user-notifications');
|
|
}
|
|
}
|