This commit is contained in:
KONE SOREL 2025-12-31 08:58:45 +00:00
parent abf98bb1e3
commit baa563b466

View File

@ -2,22 +2,25 @@
require_once 'Framework/Controleur.php'; require_once 'Framework/Controleur.php';
require_once 'Modele/Synthese.php'; require_once 'Modele/Synthese.php';
class ControleurAjaxgraphiquesinistres extends Controleur { class ControleurAjaxgraphiquesinistres extends Controleur
{
private $synthese; private $synthese;
public function __construct() { public function __construct()
{
$this->synthese = new Synthese(); $this->synthese = new Synthese();
} }
public function index() { public function index()
// Répartition sinistres {
// --- Répartition des sinistres ---
$claims = $this->synthese->getClaims(); $claims = $this->synthese->getClaims();
$tabclaims = [ $tabclaims = [
'claimsLabels' => array_column($claims, 'claimsLabels'), 'claimsLabels' => array_column($claims, 'claimsLabels'),
'claimsValues' => array_column($claims, 'claimsValues') 'claimsValues' => array_column($claims, 'claimsValues')
]; ];
// Evolution des sinistres // --- Évolution des sinistres ---
$claimsMonth = $this->synthese->getClaimsMonth(); $claimsMonth = $this->synthese->getClaimsMonth();
$claimsSingleMonth = $this->synthese->getClaimsSingleMonth(); $claimsSingleMonth = $this->synthese->getClaimsSingleMonth();
$tabclaimsMonth = [ $tabclaimsMonth = [
@ -26,31 +29,24 @@ class ControleurAjaxgraphiquesinistres extends Controleur {
'singleClaims' => array_column($claimsSingleMonth, 'singleClaims') 'singleClaims' => array_column($claimsSingleMonth, 'singleClaims')
]; ];
// Sinistralité // --- Sinistralité ---
$claimsLossRatio = $this->synthese->getClaimsLossRatio(); $claimsLossRatio = $this->synthese->getClaimsLossRatio();
$tabLossRatio = [ $tabLossRatio = [
'lossRatioLabels' => array_column($claimsLossRatio, 'months'), 'lossRatioLabels' => array_column($claimsLossRatio, 'months'),
'lossRatioValues' => array_column($claimsLossRatio, 'ratio') '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 --- // --- Mode API JSON brut ---
if (isset($_GET['api']) && $_GET['api'] == '1') { if ($this->isApiMode()) {
header('Content-Type: application/json'); $this->sendJsonResponse([
echo json_encode([
'claims' => $tabclaims, 'claims' => $tabclaims,
'claimsMonth' => $tabclaimsMonth, 'claimsMonth' => $tabclaimsMonth,
'lossRatio' => $tabLossRatio 'lossRatio' => $tabLossRatio
], JSON_NUMERIC_CHECK); ]);
exit; return;
} }
// --- Mode Vue Ajax HTML --- // --- 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; $optionsJson = JSON_NUMERIC_CHECK | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
$this->genererVueAjax([ $this->genererVueAjax([
@ -59,4 +55,22 @@ class ControleurAjaxgraphiquesinistres extends Controleur {
'dataLossRatio' => json_encode($tabLossRatio, $optionsJson) '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;
}
} }