193 lines
6.4 KiB
PHP
193 lines
6.4 KiB
PHP
<?php
|
|
/*
|
|
*Template Name: credit-step3
|
|
*
|
|
*/
|
|
|
|
use models\CRED_credit_step3;
|
|
|
|
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 (empty($_POST) && (!$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('\libraries\FormValidator')) {
|
|
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/libraries/FormValidator.php');
|
|
}
|
|
|
|
if(!class_exists('\libraries\TurnstileValidator')) {
|
|
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/libraries/TurnstileValidator.php');
|
|
}
|
|
|
|
if(!class_exists('\models\CRED_credit_step3')) {
|
|
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit_step3.php');
|
|
}
|
|
|
|
$model = new CRED_credit_step3();
|
|
|
|
// Vérifier le token soit en POST soit en GET
|
|
$token = isset($_POST['credit-direct-token']) ? $_POST['credit-direct-token'] : (isset($_GET['credit-direct-token']) ? $_GET['credit-direct-token'] : null);
|
|
|
|
if(null === $token) {
|
|
$token = $model->get_ongoing_credit_token();
|
|
}
|
|
|
|
if (empty($token)) {
|
|
wp_redirect(home_url());
|
|
exit;
|
|
}
|
|
|
|
$currentCredit = $model->getCredit($token);
|
|
|
|
if(isset($_GET['debug'])) {
|
|
echo '<pre>';
|
|
print_r($token);
|
|
print_r($currentCredit);
|
|
echo '</pre>';
|
|
//die();
|
|
}
|
|
|
|
/* echo '<pre>';
|
|
print_r($currentCredit);
|
|
echo '</pre>';
|
|
die(); */
|
|
|
|
$is_credit_pat = $model->is_credit_pat($currentCredit);
|
|
|
|
$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'];
|
|
|
|
if(empty($type_credit_selected)) {
|
|
$type_credit_selected = $currentCredit->type_credit;
|
|
}
|
|
|
|
get_header();
|
|
|
|
//try to load the model
|
|
$post = $_POST;
|
|
if (empty($post)) {
|
|
$post = array('credit-direct-token' => $token);
|
|
}
|
|
|
|
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit_step3.php')) {
|
|
|
|
|
|
if (is_object($currentCredit)) {
|
|
// Sauvegarder l'étape 2 seulement si on vient du formulaire POST
|
|
if (!empty($_POST)) {
|
|
// Validation Turnstile si configuré
|
|
if (\libraries\TurnstileValidator::isConfigured()) {
|
|
$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)) {
|
|
$result = $model->save_step_2($post,$currentCredit);
|
|
|
|
// Vérifier s'il y a des erreurs de validation
|
|
if (is_array($result) && isset($result['success']) && !$result['success']) {
|
|
$validation_errors = $result['formatted_errors'];
|
|
}
|
|
}
|
|
|
|
// Gestion générique de l'upload de fichiers
|
|
$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'];
|
|
*/
|
|
$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 = 4 * 1024 * 1024; // 4 Mo
|
|
|
|
/* echo '<pre>';
|
|
print_r($_FILES);
|
|
echo '</pre>'; */
|
|
|
|
if(!empty($_FILES)) {
|
|
foreach ($_FILES as $field => $file) {
|
|
|
|
// Traiter seulement les champs de fichiers qui ne sont pas vides
|
|
if(empty($file['name']) || empty($file['name'][0]) || $file['error'] === 4) {
|
|
continue;
|
|
}
|
|
|
|
if (isset($file['error']) && $file['error'] !== 4) { // 4 = pas de fichier uploadé
|
|
$result = $model->handleUploads($file, $allowed_types, $max_size, $token);
|
|
$attachments = array_merge($attachments, $result['files']);
|
|
$upload_errors = array_merge($upload_errors, $result['errors']);
|
|
}
|
|
}
|
|
// Affichage des erreurs d'upload
|
|
if (!empty($upload_errors)) {
|
|
foreach ($upload_errors as $err) {
|
|
echo '<div class="alert alert-danger">' . htmlspecialchars($err) . '</div>';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//try to load the view
|
|
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-step3.php')) {
|
|
$coBorrower = $model->getCoBorrower($currentCredit);
|
|
$borrower = $model->getBorrower($currentCredit);
|
|
include(WP_PLUGIN_DIR . '/ESI_creditDirect/templates/front/credit-step3.php');
|
|
}
|
|
} else {
|
|
// Si le crédit n'existe pas, rediriger vers la page d'accueil
|
|
wp_redirect(home_url());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
get_footer(); |