credit-direct/app/controllers/old/credit-step1.php
2025-12-18 09:44:42 +01:00

271 lines
8.9 KiB
PHP

<?php
/*
*Template Name: credit-step1
*
*/
use models\CRED_credit_step1;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$is_from_simulator = false;
$is_from_back = false;
if(isset($_GET['credit-direct-token']) && !empty($_GET['credit-direct-token'])) {
$is_from_back = true;
}
if(isset($_POST['loan_type']) && !empty($_POST['loan_type'])) {
$is_from_simulator = true;
}
if (empty($_POST) && (!$is_from_simulator && !$is_from_back)) {
wp_redirect(home_url());
exit;
}
if(!class_exists('\models\CRED_credit')) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit.php');
}
if(!class_exists('\models\CRED_credit_step1')) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit-step1.php');
}
if(!class_exists('\libraries\TurnstileValidator')) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/libraries/TurnstileValidator.php');
}
get_header();
//try to load the model
$post = $_POST;
$one_step_form_send = false;
$one_step_credits = ['am','amr','cied','frais_notaire','cdp'];
$model = new CRED_credit_step1();
if(isset($_POST['one_step_form']))
$one_step_form_send = true;
//exemple credit : 8d0f45319ba2ebcfc708a7e6a19922c6a478b655
// Validation Turnstile si configuré
if (\libraries\TurnstileValidator::isConfigured() && !empty($_POST)) {
$turnstileToken = isset($_POST['cf-turnstile-response']) ? $_POST['cf-turnstile-response'] : '';
$turnstileValidator = new \libraries\TurnstileValidator();
$turnstileResult = $turnstileValidator->validateForDisplay($turnstileToken, $_SERVER['REMOTE_ADDR'] ?? null);
if (!$turnstileResult['valid']) {
$turnstile_error = $turnstileResult['message'];
}
}
// Ne traiter le formulaire que si Turnstile est valide (ou non configuré)
if (!isset($turnstile_error) && !$one_step_form_send && !isset($_GET['credit-direct-token'])) {
$token = $model->save_step_0($post);
} else if(isset($_POST['credit_token'])) {
$token = $_POST['credit_token'];
} else if(isset($_GET['credit-direct-token'])) {
$token = $_GET['credit-direct-token'];
} else {
wp_redirect(home_url());
}
$currentCredit = $model->getCredit($token);
if (!is_object($currentCredit)) {
wp_redirect(home_url());
}
/* echo '<pre>';
print_r($_POST);
echo '</pre>'; */
/* die(); */
// Débogage
/* error_log('POST one_step_form: ' . (isset($_POST['one_step_form']) ? $_POST['one_step_form'] : 'non défini'));
error_log('one_step_credits: ' . print_r($one_step_credits, true)); */
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit-step1.php') || file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit-one-step.php')) {
$in_one_step = false;
$civilStatus = $model->getCivilStatus();
$works = $model->getWorks();
$existingCreditTypes = $model->getExistingCreditTypes();
$contractTypes = $model->getContractTypes();
/* echo '<pre>';
print_r($currentCredit);
echo '</pre>'; */
if(in_array($currentCredit->type_credit, $one_step_credits))
$in_one_step = true;
if($one_step_form_send) {
$model->save_one_step($post);
/* echo '<pre>';
print_r($_FILES);
echo '</pre>'; */
}
//try to load the view
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-step1.php')) {
$agencies = $model->getAgencies();
$map_credit_type = $model->getCreditTypes();
$mapHouseCreditTypes = $model->getHouseCreditTypes();
/*re-hydrate current credit*/
$currentCredit = $model->getCredit($token);
$message = null;
$type_credit_selected = '';
if(isset($currentCredit->sel_credit) && !empty($currentCredit->sel_credit)) {
$type_credit_selected = $currentCredit->sel_credit;
}
if(isset($_POST['type_credit_selected']) && !empty($_POST['type_credit_selected']) || isset($_POST['sub_loan_type']) && !empty($_POST['sub_loan_type']))
$type_credit_selected = isset($_POST['sub_loan_type']) ? $_POST['sub_loan_type'] : $_POST['type_credit_selected'];
$creditOptionsLabels = !empty($type_credit_selected) ? $model->getCreditLabel($type_credit_selected) : $map_credit_type[$currentCredit->type_credit];
//save the credit options labels in a cookie for 2 months
/* $model->save_step($currentCredit); */
$attachments = [];
$upload_errors = [];
/*
// Exemple d'utilisation de la fonction handleUploads
$allowed_types = [
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
'application/msword', // doc
'image/jpeg',
'image/png',
'image/gif',
'image/bmp',
'image/webp'
];
$max_size = 2 * 1024 * 1024; // 2 Mo
$result = $model->handleUploads($_FILES, $allowed_types, $max_size, $token);
$attachments = $result['files'];
$upload_errors = $result['errors'];
$html_links = $result['html_links'];
*/
if(isset($_FILES)) {
if ($one_step_form_send) { // 4 = no file uploaded
$allowed_types = [
'application/pdf',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // docx
'application/msword', // doc
'image/jpeg',
'image/png',
'image/gif',
'image/bmp',
'image/webp'
];
//remove all the empty file from $_FILE
foreach($_FILES as $key => $value) {
if($value['error'] === 4) {
unset($_FILES[$key]);
}
}
$max_size = 4 * 1024 * 1024; // 4 Mo
$result = $model->handleUploads($_FILES, $allowed_types, $max_size, $token);
/* echo '<pre>';
print_r($result);
echo '</pre>';
die(); */
$attachments = $result['files'];
$upload_errors = $result['errors'];
}
}
$borrower = $model->getBorrower($currentCredit);
/* echo '<pre>';
print_r($borrower);
echo '</pre>';
die(); */
if($one_step_form_send) {
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/email/credit-one-step-mail.php')) {
$currentCredit = $model->getCredit($token);
$coBorrower = $model->getCoBorrower($currentCredit);
/* echo '<pre>';
print_r($currentCredit);
echo '</pre>';
echo '<pre>';
print_r($borrower);
echo '</pre>';
die(); */
ob_start();
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/email/credit-one-step-mail.php');
$message = ob_get_clean();
ob_start();
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/email/clients_emails/credit-one-step-mail-client.php');
$message_client = ob_get_clean();
// Ajout des en-têtes pour une meilleure compatibilité Outlook
/* $headers = array(
'Content-Type: text/html; charset=UTF-8',
'X-Mailer: PHP/' . phpversion(),
'MIME-Version: 1.0'
); */
/* echo '<pre>';
print_r($attachments);
echo '</pre>';
die(); */
// Exception : ne pas envoyer de mail si l'utilisateur connecté a l'ID 1
if (!is_user_logged_in() || get_current_user_id() != 1) {
// Envoi de l'email au client
$model->sendEmail('Demande de crédit', $message_client, $borrower, $currentCredit, [], true);
// Envoi de l'email à l'administrateur
$model->sendEmail('Demande de crédit', $message, $borrower, $currentCredit, [], false);
}
// Nettoyage des fichiers temporaires
/* foreach ($attachments as $file) {
if (file_exists($file)) @unlink($file);
} */
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-step5.php');
}
if (!empty($upload_errors)) {
foreach ($upload_errors as $err) {
echo '<div class="alert alert-danger">' . htmlspecialchars($err) . '</div>';
}
}
} else {
if($in_one_step) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-one-step.php');
} else {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-step1.php');
}
}
}
}
get_footer();