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

159 lines
5.4 KiB
PHP

<?php
/*
*Template Name: credit-step2
*
*/
/* ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); */
use models\CRED_credit_step2;
$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 (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit_step2.php')) {
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('\models\CRED_credit_step2')) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect/app/models/credit_step2.php');
}
$model = new CRED_credit_step2();
$ongoing_credit = $model->checkOngoingCreditRequest();
if(!$ongoing_credit && empty($_POST) && !$is_from_back) {
wp_redirect(home_url());
exit;
}
// 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;
}
get_header();
//try to load the model
$post = $_POST;
if (empty($post)) {
$post = array('credit-direct-token' => $token);
}
$currentCredit = $model->getCredit($token);
$borrower = $model->getBorrower($currentCredit);
$coBorrower = $model->getCoBorrower($currentCredit);
$hasCoBorrower = $model->hasCoBorrower($currentCredit);
$is_credit_auto = $model->is_credit_auto($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;
}
if (is_object($currentCredit)) {
// Sauvegarder l'étape 2 seulement si on vient du formulaire POST complet de step2
if (!empty($_POST)) {
// Vérifier si c'est une soumission complète du formulaire step2
// (présence de champs clés de step2) ou champ 'step' explicite
$is_step2_submission = isset($_POST['step']) && $_POST['step'] === '2';
if (!$is_step2_submission) {
// Vérifier la présence de champs clés de step2 pour détecter automatiquement
$step2_key_fields = ['civilstatus', 'job', 'salary', 'habitation_type'];
$has_step2_fields = false;
foreach ($step2_key_fields as $field) {
if (isset($_POST[$field]) && !empty($_POST[$field])) {
$has_step2_fields = true;
break;
}
}
$is_step2_submission = $has_step2_fields;
}
if ($is_step2_submission) {
// Validation complète du formulaire step2
$result = $model->save_step_1($post, true); // Avec validation
// Vérifier s'il y a des erreurs de validation
if (is_array($result) && isset($result['success']) && !$result['success']) {
$validation_errors = $result['formatted_errors'];
} else {
$model->save_step($currentCredit);
}
} else {
// Simple transition de step1 : sauvegarder le borrower sans validation
$model->save_step_1($post, false); // Sans validation
$model->save_step($currentCredit); // Mettre à jour l'étape
}
}
$contractTypes = $model->getContractTypes();
$mapHouseCreditTypes = $model->getHouseCreditTypes();
// Utilisation du mode debug pour charger les templates
$template_a = CRED::getTemplatePath('/templates/front/credit-step2-a.php');
$template_b = CRED::getTemplatePath('/templates/front/credit-step2-b.php');
$template_c = CRED::getTemplatePath('/templates/front/credit-step2-c.php');
/* echo '<pre>';
print_r($_COOKIE);
echo '</pre>'; */
//try to load the view
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_a)) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_a);
}
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_b)) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_b);
}
if (file_exists(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_c)) {
include(WP_PLUGIN_DIR . '/ESI_creditDirect' . $template_c);
}
} else {
// Si le crédit n'existe pas, rediriger vers la page d'accueil
wp_redirect(home_url());
exit;
}
}
get_footer();