radiantrh/Controleur/ControleurAjaxgraphiquesinistres.php
2025-12-31 09:12:38 +00:00

111 lines
3.8 KiB
PHP
Executable File

<?php
require_once 'Framework/Controleur.php';
require_once 'Modele/Synthese.php';
class ControleurAjaxgraphiquesinistres extends Controleur
{
private $synthese;
public function __construct()
{
$this->synthese = new Synthese();
}
public function index()
{
// --- Répartition des sinistres ---
$claims = $this->synthese->getClaims();
$tabclaims = [
'claimsLabels' => array_column($claims, 'claimsLabels'),
'claimsValues' => array_column($claims, 'claimsValues')
];
// --- Évolution des sinistres ---
$claimsMonth = $this->synthese->getClaimsMonth();
$claimsSingleMonth = $this->synthese->getClaimsSingleMonth();
$tabclaimsMonth = [
'months' => array_column($claimsMonth, 'months'),
'monthlyClaims' => array_column($claimsMonth, 'monthlyClaims'),
'singleClaims' => array_column($claimsSingleMonth, 'singleClaims')
];
// --- Sinistralité ---
$claimsLossRatio = $this->synthese->getClaimsLossRatio();
$tabLossRatio = [
'lossRatioLabels' => array_column($claimsLossRatio, 'months'),
'lossRatioValues' => array_column($claimsLossRatio, 'ratio')
];
// --- Mode API JSON brut ---
if ($this->isApiMode()) {
$this->sendJsonResponse([
'claims' => $tabclaims,
'claimsMonth' => $tabclaimsMonth,
'lossRatio' => $tabLossRatio
]);
return;
}
// --- Mode Vue Ajax HTML ---
$optionsJson = JSON_NUMERIC_CHECK | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
$this->genererVueAjax([
'dataTabClaims' => json_encode($tabclaims, $optionsJson),
'dataTabClaimsMonth' => json_encode($tabclaimsMonth, $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;
}
public function api()
{
// Répartition sinistres
$claims = $this->synthese->getClaims();
$tabclaims = [
'claimsLabels' => array_column($claims, 'claimsLabels'),
'claimsValues' => array_column($claims, 'claimsValues')
];
// Évolution des sinistres
$claimsMonth = $this->synthese->getClaimsMonth();
$claimsSingleMonth = $this->synthese->getClaimsSingleMonth();
$tabclaimsMonth = [
'months' => array_column($claimsMonth, 'months'),
'monthlyClaims' => array_column($claimsMonth, 'monthlyClaims'),
'singleClaims' => array_column($claimsSingleMonth, 'singleClaims')
];
// Sinistralité
$claimsLossRatio = $this->synthese->getClaimsLossRatio();
$tabLossRatio = [
'lossRatioLabels' => array_column($claimsLossRatio, 'months'),
'lossRatioValues' => array_column($claimsLossRatio, 'ratio')
];
// Réponse JSON
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'claims' => $tabclaims,
'claimsMonth' => $tabclaimsMonth,
'lossRatio' => $tabLossRatio
], JSON_NUMERIC_CHECK);
exit;
}
}