266 lines
9.5 KiB
PHP
266 lines
9.5 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
$form_index_status = null;
|
|
$form_index_message = '';
|
|
$form_index_errors = [];
|
|
$form_index_old = [
|
|
'name' => '',
|
|
'category' => '',
|
|
'situation' => '',
|
|
'contact_type' => 'email',
|
|
'email' => '',
|
|
'wa' => '',
|
|
];
|
|
|
|
if (! function_exists('t')) {
|
|
function t(array $translations, string $key, string $fallback = ''): string
|
|
{
|
|
$val = $translations[$key] ?? '';
|
|
return $val !== '' ? (string) $val : $fallback;
|
|
}
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Flash message après redirection
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
if (isset($_SESSION['form_index_flash']) && is_array($_SESSION['form_index_flash'])) {
|
|
$flash = $_SESSION['form_index_flash'];
|
|
|
|
$form_index_status = $flash['status'] ?? null;
|
|
$form_index_message = $flash['message'] ?? '';
|
|
$form_index_errors = $flash['errors'] ?? [];
|
|
$form_index_old = $flash['old'] ?? $form_index_old;
|
|
|
|
unset($_SESSION['form_index_flash']);
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Si ce n'est pas le bon POST, on ne fait rien
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || ! isset($_POST['form_index'])) {
|
|
return;
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Charger le mail helper
|
|
|--------------------------------------------------------------------------
|
|
| Adapte le chemin si ton fichier s'appelle autrement
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
require_once __DIR__ . '/includes/mailer.php';
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Récupération des données
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
$form_index_old = [
|
|
'name' => trim((string) ($_POST['name'] ?? '')),
|
|
'category' => trim((string) ($_POST['category'] ?? '')),
|
|
'situation' => trim((string) ($_POST['situation'] ?? '')),
|
|
'contact_type' => ($_POST['contact_type'] ?? 'email') === 'wa' ? 'wa' : 'email',
|
|
'email' => trim((string) ($_POST['email'] ?? '')),
|
|
'wa' => trim((string) ($_POST['wa'] ?? '')),
|
|
];
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Honeypot anti-spam
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
$honeypot = trim((string) ($_POST['company'] ?? ''));
|
|
if ($honeypot !== '') {
|
|
$_SESSION['form_index_flash'] = [
|
|
'status' => 'success',
|
|
'message' => t(
|
|
$translations,
|
|
'form_index_success_text',
|
|
'Мы изучим вашу ситуацию и свяжемся с вами в ближайшее время, чтобы предложить подходящего специалиста.'
|
|
),
|
|
'old' => [
|
|
'name' => '',
|
|
'category' => '',
|
|
'situation' => '',
|
|
'contact_type' => 'email',
|
|
'email' => '',
|
|
'wa' => '',
|
|
],
|
|
'errors' => [],
|
|
];
|
|
|
|
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?') . '#concierge-form');
|
|
exit;
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Validation
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
if ($form_index_old['name'] === '') {
|
|
$form_index_errors['name'] = true;
|
|
}
|
|
|
|
$allowedCategories = [
|
|
'lawyer',
|
|
'notary',
|
|
'real_estate',
|
|
'psychologist',
|
|
'dentist_ortho',
|
|
'financial_advisor',
|
|
'translator',
|
|
'residence_consultant',
|
|
'repair',
|
|
'funeral',
|
|
'other',
|
|
];
|
|
|
|
if ($form_index_old['category'] === '' || ! in_array($form_index_old['category'], $allowedCategories, true)) {
|
|
$form_index_errors['category'] = true;
|
|
}
|
|
|
|
if ($form_index_old['situation'] === '') {
|
|
$form_index_errors['situation'] = true;
|
|
}
|
|
|
|
if ($form_index_old['contact_type'] === 'email') {
|
|
if ($form_index_old['email'] === '' || ! filter_var($form_index_old['email'], FILTER_VALIDATE_EMAIL)) {
|
|
$form_index_errors['email'] = true;
|
|
}
|
|
} else {
|
|
if ($form_index_old['wa'] === '') {
|
|
$form_index_errors['wa'] = true;
|
|
}
|
|
}
|
|
|
|
if (! empty($form_index_errors)) {
|
|
$_SESSION['form_index_flash'] = [
|
|
'status' => 'error',
|
|
'message' => t(
|
|
$translations,
|
|
'form_index_error_required',
|
|
'Пожалуйста, заполните все обязательные поля.'
|
|
),
|
|
'old' => $form_index_old,
|
|
'errors' => $form_index_errors,
|
|
];
|
|
|
|
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?') . '#concierge-form');
|
|
exit;
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Labels catégories traduites
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
$categoryLabels = [
|
|
'lawyer' => t($translations, 'form_index_category_lawyer', 'Адвокат'),
|
|
'notary' => t($translations, 'form_index_category_notary', 'Нотариус'),
|
|
'real_estate' => t($translations, 'form_index_category_real_estate', 'Риелтор'),
|
|
'psychologist' => t($translations, 'form_index_category_psychologist', 'Психолог'),
|
|
'dentist_ortho' => t($translations, 'form_index_category_dentist_ortho', 'Стоматолог / Ортодонт'),
|
|
'financial_advisor' => t($translations, 'form_index_category_financial_advisor', 'Финансовый консультант'),
|
|
'translator' => t($translations, 'form_index_category_translator', 'Переводчик / Присяжный переводчик'),
|
|
'residence_consultant' => t($translations, 'form_index_category_residence_consultant', 'Консультант по ВНЖ'),
|
|
'repair' => t($translations, 'form_index_category_repair', 'Мастер по ремонту'),
|
|
'funeral' => t($translations, 'form_index_category_funeral', 'Похоронное бюро'),
|
|
'other' => t($translations, 'form_index_category_other', 'Другое'),
|
|
];
|
|
|
|
$contactValue = $form_index_old['contact_type'] === 'email'
|
|
? $form_index_old['email']
|
|
: $form_index_old['wa'];
|
|
|
|
$contactTypeLabel = $form_index_old['contact_type'] === 'email' ? 'Email' : 'WhatsApp';
|
|
|
|
$langCurrent = (string) ($lang ?? 'ru');
|
|
|
|
$subject = '[' . strtoupper($langCurrent) . '] Nouvelle demande index — ' . ($categoryLabels[$form_index_old['category']] ?? $form_index_old['category']);
|
|
|
|
$html = '
|
|
<h2>Nouvelle demande depuis le formulaire index</h2>
|
|
<p><strong>Langue :</strong> ' . htmlspecialchars($langCurrent, ENT_QUOTES, 'UTF-8') . '</p>
|
|
<p><strong>Nom :</strong> ' . nl2br(htmlspecialchars($form_index_old['name'], ENT_QUOTES, 'UTF-8')) . '</p>
|
|
<p><strong>Catégorie :</strong> ' . nl2br(htmlspecialchars($categoryLabels[$form_index_old['category']] ?? $form_index_old['category'], ENT_QUOTES, 'UTF-8')) . '</p>
|
|
<p><strong>Situation :</strong><br>' . nl2br(htmlspecialchars($form_index_old['situation'], ENT_QUOTES, 'UTF-8')) . '</p>
|
|
<p><strong>Type de contact :</strong> ' . htmlspecialchars($contactTypeLabel, ENT_QUOTES, 'UTF-8') . '</p>
|
|
<p><strong>Contact :</strong> ' . nl2br(htmlspecialchars($contactValue, ENT_QUOTES, 'UTF-8')) . '</p>
|
|
';
|
|
|
|
$text =
|
|
"Nouvelle demande depuis le formulaire index\n\n" .
|
|
"Langue : " . $langCurrent . "\n" .
|
|
"Nom : " . $form_index_old['name'] . "\n" .
|
|
"Catégorie : " . ($categoryLabels[$form_index_old['category']] ?? $form_index_old['category']) . "\n" .
|
|
"Situation : " . $form_index_old['situation'] . "\n" .
|
|
"Type de contact : " . $contactTypeLabel . "\n" .
|
|
"Contact : " . $contactValue . "\n";
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Reply-To seulement si email
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
$mailOptions = [];
|
|
if ($form_index_old['contact_type'] === 'email' && $form_index_old['email'] !== '') {
|
|
$mailOptions['reply_to'] = [$form_index_old['email'], $form_index_old['name']];
|
|
}
|
|
|
|
[$ok, $err] = uslugi_mail(
|
|
'contact@uslugi.fr',
|
|
$subject,
|
|
$html,
|
|
$text,
|
|
$mailOptions
|
|
);
|
|
|
|
if ($ok) {
|
|
$_SESSION['form_index_flash'] = [
|
|
'status' => 'success',
|
|
'message' => t(
|
|
$translations,
|
|
'form_index_success_text',
|
|
'Мы изучим вашу ситуацию и свяжемся с вами в ближайшее время, чтобы предложить подходящего специалиста.'
|
|
),
|
|
'old' => [
|
|
'name' => '',
|
|
'category' => '',
|
|
'situation' => '',
|
|
'contact_type' => 'email',
|
|
'email' => '',
|
|
'wa' => '',
|
|
],
|
|
'errors' => [],
|
|
];
|
|
|
|
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?') . '#concierge-form');
|
|
exit;
|
|
}
|
|
|
|
error_log('Form index send error: ' . (string) $err);
|
|
|
|
$_SESSION['form_index_flash'] = [
|
|
'status' => 'error',
|
|
'message' => t(
|
|
$translations,
|
|
'form_index_error_send',
|
|
'Произошла ошибка при отправке. Пожалуйста, попробуйте ещё раз.'
|
|
),
|
|
'old' => $form_index_old,
|
|
'errors' => [],
|
|
];
|
|
|
|
header('Location: ' . strtok($_SERVER['REQUEST_URI'], '?') . '#concierge-form');
|
|
exit;
|