b2in/app/Livewire/Web/Components/Sections/ImmobilienContactForm.php

124 lines
3.7 KiB
PHP

<?php
namespace App\Livewire\Web\Components\Sections;
use Acme\ContactForm\ContactFormService;
use Acme\ContactForm\SpamDetector;
use Livewire\Attributes\On;
use Livewire\Component;
class ImmobilienContactForm extends Component
{
public string $projectSlug = '';
public string $projectTitle = '';
/** @var array<string, string> */
public array $interestOptions = [];
public string $interest = '';
public bool $showInterest = true;
public string $submitLabel = '';
public bool $syncFromAlpineProject = false;
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 = [], bool $showInterest = true, string $submitLabel = '', bool $syncFromAlpineProject = false): void
{
$this->projectSlug = $projectSlug;
$this->projectTitle = $projectTitle;
$this->interestOptions = $interestOptions;
$this->showInterest = $showInterest;
$this->submitLabel = $submitLabel;
$this->syncFromAlpineProject = $syncFromAlpineProject;
$this->formLoadedAt = time();
}
#[On('azizi-project-selected')]
public function setProject(string $slug = '', string $title = ''): void
{
if ($this->projectSlug === $slug && $this->projectTitle === $title) {
return;
}
$this->projectSlug = $slug;
$this->projectTitle = $title;
$this->success = false;
$this->interest = '';
$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');
}
}