2025-10-22 17:09:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\Forms\ContactRequest;
|
2025-10-24 14:38:11 +02:00
|
|
|
use App\Services\ContactService;
|
2025-10-22 17:09:48 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Inertia\Inertia;
|
2026-04-07 18:06:20 +02:00
|
|
|
use Inertia\Response;
|
2025-10-22 17:09:48 +02:00
|
|
|
|
|
|
|
|
class ContactFormController extends Controller
|
|
|
|
|
{
|
2025-10-24 14:38:11 +02:00
|
|
|
public function __construct(protected ContactService $contactService) {}
|
2026-04-07 18:06:20 +02:00
|
|
|
|
|
|
|
|
public function create(): Response
|
2025-10-22 17:09:48 +02:00
|
|
|
{
|
2026-04-07 18:06:20 +02:00
|
|
|
return Inertia::render('forms/contact', [
|
|
|
|
|
'captcha_question' => $this->generateCaptcha(),
|
|
|
|
|
]);
|
2025-10-22 17:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
|
*/
|
|
|
|
|
public function store(ContactRequest $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validated();
|
2026-04-07 18:06:20 +02:00
|
|
|
|
2025-10-24 14:38:11 +02:00
|
|
|
try {
|
2025-10-26 00:16:25 +02:00
|
|
|
$this->contactService->registerNewContactRequest($validated);
|
2025-10-24 14:38:11 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
\Log::error('Erreur lors de la création d\'un contact', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString(),
|
2026-04-07 18:06:20 +02:00
|
|
|
'data' => $validated,
|
2025-10-24 14:38:11 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return to_route('contact')->with('error', __('contacts.responses.error'));
|
|
|
|
|
}
|
2025-10-22 17:09:48 +02:00
|
|
|
|
|
|
|
|
return to_route('contact')->with('success', __('contacts.responses.success'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 18:06:20 +02:00
|
|
|
private function generateCaptcha(): string
|
|
|
|
|
{
|
|
|
|
|
$a = random_int(1, 9);
|
|
|
|
|
$b = random_int(1, 9);
|
|
|
|
|
|
|
|
|
|
session(['captcha_contact' => (string) ($a + $b)]);
|
|
|
|
|
|
|
|
|
|
return "Combien font {$a} + {$b} ?";
|
|
|
|
|
}
|
2025-10-22 17:09:48 +02:00
|
|
|
}
|