100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Web\Components\Sections;
|
|
|
|
use Acme\ContactForm\ContactFormService;
|
|
use Acme\ContactForm\SpamDetector;
|
|
use Livewire\Component;
|
|
|
|
class ImmobilienContactForm extends Component
|
|
{
|
|
public string $projectSlug = '';
|
|
|
|
public string $projectTitle = '';
|
|
|
|
/** @var array<string, string> */
|
|
public array $interestOptions = [];
|
|
|
|
public string $interest = '';
|
|
|
|
public string $firstName = '';
|
|
|
|
public string $lastName = '';
|
|
|
|
public string $email = '';
|
|
|
|
public string $phone = '';
|
|
|
|
public string $message = '';
|
|
|
|
public bool $privacy = false;
|
|
|
|
public bool $success = false;
|
|
|
|
/** @var string Hidden honeypot field */
|
|
public string $website = '';
|
|
|
|
public ?int $formLoadedAt = null;
|
|
|
|
/**
|
|
* @param array<string, string> $interestOptions
|
|
*/
|
|
public function mount(string $projectSlug = '', string $projectTitle = '', array $interestOptions = []): void
|
|
{
|
|
$this->projectSlug = $projectSlug;
|
|
$this->projectTitle = $projectTitle;
|
|
$this->interestOptions = $interestOptions;
|
|
$this->formLoadedAt = time();
|
|
}
|
|
|
|
public function submit(ContactFormService $service): void
|
|
{
|
|
$this->validate([
|
|
'firstName' => ['required', 'string', 'max:255'],
|
|
'lastName' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'email:rfc', 'max:255'],
|
|
'phone' => ['nullable', 'string', 'max:80'],
|
|
'interest' => ['nullable', 'string', 'max:255'],
|
|
'message' => ['nullable', 'string', 'max:2000'],
|
|
'privacy' => ['accepted'],
|
|
'website' => ['nullable', 'string', 'max:0'],
|
|
]);
|
|
|
|
$spamDetector = SpamDetector::fromConfig();
|
|
$payload = [
|
|
'project' => $this->projectSlug,
|
|
'project_title' => $this->projectTitle,
|
|
'interest' => $this->interest,
|
|
'first_name' => $this->firstName,
|
|
'last_name' => $this->lastName,
|
|
'email' => $this->email,
|
|
'phone' => $this->phone,
|
|
'message' => $this->message,
|
|
'website' => $this->website,
|
|
'ip' => request()->ip(),
|
|
'user_agent' => request()->userAgent(),
|
|
'is_spam' => false,
|
|
];
|
|
|
|
$payload['is_spam'] = $spamDetector->detect($payload, $this->formLoadedAt);
|
|
|
|
$subject = 'Immobilien-Anfrage: '.($this->projectTitle ?: $this->projectSlug);
|
|
$service->handle($payload, $subject, 'immobilien-contact-form');
|
|
|
|
$this->success = true;
|
|
$this->interest = '';
|
|
$this->firstName = '';
|
|
$this->lastName = '';
|
|
$this->email = '';
|
|
$this->phone = '';
|
|
$this->message = '';
|
|
$this->privacy = false;
|
|
$this->website = '';
|
|
$this->formLoadedAt = time();
|
|
}
|
|
|
|
public function render(): \Illuminate\View\View
|
|
{
|
|
return view('livewire.web.components.sections.immobilien-contact-form');
|
|
}
|
|
}
|