diff --git a/Controleur/ControleurAjaxgraphiquesinistres.php b/Controleur/ControleurAjaxgraphiquesinistres.php index f1a96d1..18b7bf7 100755 --- a/Controleur/ControleurAjaxgraphiquesinistres.php +++ b/Controleur/ControleurAjaxgraphiquesinistres.php @@ -2,22 +2,25 @@ require_once 'Framework/Controleur.php'; require_once 'Modele/Synthese.php'; -class ControleurAjaxgraphiquesinistres extends Controleur { +class ControleurAjaxgraphiquesinistres extends Controleur +{ private $synthese; - - public function __construct() { + + public function __construct() + { $this->synthese = new Synthese(); } - public function index() { - // Répartition sinistres + public function index() + { + // --- Répartition des sinistres --- $claims = $this->synthese->getClaims(); $tabclaims = [ 'claimsLabels' => array_column($claims, 'claimsLabels'), 'claimsValues' => array_column($claims, 'claimsValues') ]; - // Evolution des sinistres + // --- Évolution des sinistres --- $claimsMonth = $this->synthese->getClaimsMonth(); $claimsSingleMonth = $this->synthese->getClaimsSingleMonth(); $tabclaimsMonth = [ @@ -26,31 +29,24 @@ class ControleurAjaxgraphiquesinistres extends Controleur { 'singleClaims' => array_column($claimsSingleMonth, 'singleClaims') ]; - // Sinistralité + // --- Sinistralité --- $claimsLossRatio = $this->synthese->getClaimsLossRatio(); $tabLossRatio = [ 'lossRatioLabels' => array_column($claimsLossRatio, 'months'), 'lossRatioValues' => array_column($claimsLossRatio, 'ratio') ]; - // Debug : vérifier si le paramètre 'api' est bien reçu - die("API mode: " . ($_GET['api'] ?? 'non défini')); - - // --- Mode API JSON brut --- - if (isset($_GET['api']) && $_GET['api'] == '1') { - header('Content-Type: application/json'); - echo json_encode([ + if ($this->isApiMode()) { + $this->sendJsonResponse([ 'claims' => $tabclaims, 'claimsMonth' => $tabclaimsMonth, 'lossRatio' => $tabLossRatio - ], JSON_NUMERIC_CHECK); - exit; + ]); + return; } // --- Mode Vue Ajax HTML --- - // Utilisation de constantes JSON pour sécuriser l'insertion dans le JavaScript - // JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP empêchent le bris du HTML/JS $optionsJson = JSON_NUMERIC_CHECK | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP; $this->genererVueAjax([ @@ -59,4 +55,22 @@ class ControleurAjaxgraphiquesinistres extends Controleur { 'dataLossRatio' => json_encode($tabLossRatio, $optionsJson) ]); } + + /** + * Vérifie si on est en mode API (paramètre GET api=1) + */ + private function isApiMode(): bool + { + return isset($_GET['api']) && $_GET['api'] === '1'; + } + + /** + * Envoie une réponse JSON et termine le script + */ + private function sendJsonResponse(array $data): void + { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($data, JSON_NUMERIC_CHECK); + exit; + } } \ No newline at end of file