Ajouter les statistiques par période
This commit is contained in:
parent
9ff26ad9be
commit
86642095cd
14 changed files with 315 additions and 125 deletions
|
|
@ -6,37 +6,95 @@ final class StatisticsController
|
|||
{
|
||||
public static function index(): void
|
||||
{
|
||||
$requested = (string) ($_GET['year'] ?? date('Y'));
|
||||
$all = $requested === 'all';
|
||||
$year = $all ? null : max(2000, min(2200, (int) $requested));
|
||||
$years = StatisticsService::years();
|
||||
$requestedYear = (string) ($_GET['year'] ?? date('Y'));
|
||||
$periodType = (string) ($_GET['period_type'] ?? ($requestedYear === 'all' ? 'all' : 'year'));
|
||||
if (!in_array($periodType, ['all','year','month','custom'], true)) {
|
||||
$periodType = 'year';
|
||||
}
|
||||
$year = max(2000, min(2200, (int) ($requestedYear === 'all' ? date('Y') : $requestedYear)));
|
||||
$month = preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', (string) ($_GET['month'] ?? ''))
|
||||
? (string) $_GET['month']
|
||||
: date('Y-m');
|
||||
$validDate = static function (string $value): bool {
|
||||
$date = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
|
||||
return $date !== false && $date->format('Y-m-d') === $value;
|
||||
};
|
||||
$dateFrom = $validDate((string) ($_GET['date_from'] ?? '')) ? (string) $_GET['date_from'] : date('Y-01-01');
|
||||
$dateTo = $validDate((string) ($_GET['date_to'] ?? '')) ? (string) $_GET['date_to'] : date('Y-m-d');
|
||||
if ($dateFrom > $dateTo) {
|
||||
[$dateFrom, $dateTo] = [$dateTo, $dateFrom];
|
||||
}
|
||||
$start = $end = null;
|
||||
if ($periodType === 'year') {
|
||||
$start = sprintf('%04d-01-01', $year);
|
||||
$end = sprintf('%04d-12-31', $year);
|
||||
} elseif ($periodType === 'month') {
|
||||
$start = $month . '-01';
|
||||
$end = (new DateTimeImmutable($start))->modify('last day of this month')->format('Y-m-d');
|
||||
$year = (int) substr($month, 0, 4);
|
||||
} elseif ($periodType === 'custom') {
|
||||
$start = $dateFrom;
|
||||
$end = $dateTo;
|
||||
$year = (int) substr($dateFrom, 0, 4);
|
||||
}
|
||||
$all = $periodType === 'all';
|
||||
$formatDate = static fn(string $date): string => (new DateTimeImmutable($date))->format('d/m/Y');
|
||||
$periodLabel = match ($periodType) {
|
||||
'all' => t('statistics.all_years'),
|
||||
'month' => substr($month, 5, 2) . '/' . substr($month, 0, 4),
|
||||
'custom' => $formatDate($dateFrom) . ' – ' . $formatDate($dateTo),
|
||||
default => (string) $year,
|
||||
};
|
||||
$periodQuery = ['period_type' => $periodType];
|
||||
if ($periodType === 'year') $periodQuery['year'] = $year;
|
||||
if ($periodType === 'month') $periodQuery['month'] = $month;
|
||||
if ($periodType === 'custom') $periodQuery += ['date_from' => $dateFrom,'date_to' => $dateTo];
|
||||
$municipalities = StatisticsService::municipalities();
|
||||
$municipality = trim((string) ($_GET['municipality'] ?? ''));
|
||||
if ($municipality !== '' && !isset($municipalities[$municipality])) {
|
||||
$municipality = '';
|
||||
}
|
||||
$ids = $municipality !== '' ? StatisticsService::animalIdsForMunicipality($municipality) : null;
|
||||
if ($all) {
|
||||
$comparison = [];
|
||||
$previousPeriodLabel = '';
|
||||
} elseif ($periodType === 'year') {
|
||||
$comparison = StatisticsService::comparison($year, $ids);
|
||||
$previousPeriodLabel = (string) ($year - 1);
|
||||
} else {
|
||||
$comparisonResult = StatisticsService::comparisonPeriod($start, $end, $ids);
|
||||
$comparison = $comparisonResult['rows'];
|
||||
$previousPeriodLabel = $formatDate($comparisonResult['previous_start']) . ' – ' . $formatDate($comparisonResult['previous_end']);
|
||||
}
|
||||
render('statistics.php', [
|
||||
'title' => t('statistics.title'),
|
||||
'pageDescription' => t('statistics.description'),
|
||||
'year' => $year,
|
||||
'allYears' => $all,
|
||||
'periodType' => $periodType,
|
||||
'month' => $month,
|
||||
'dateFrom' => $dateFrom,
|
||||
'dateTo' => $dateTo,
|
||||
'periodLabel' => $periodLabel,
|
||||
'previousPeriodLabel' => $previousPeriodLabel,
|
||||
'periodQuery' => $periodQuery,
|
||||
'years' => $years,
|
||||
'municipality' => $municipality,
|
||||
'municipalities' => $municipalities,
|
||||
'municipalityCoverage' => StatisticsService::municipalityCoverage(),
|
||||
'stats' => $all ? StatisticsService::all($ids) : StatisticsService::annual($year, $ids),
|
||||
'comparison' => $all ? [] : StatisticsService::comparison($year, $ids),
|
||||
'stats' => $all ? StatisticsService::all($ids) : StatisticsService::period($start, $end, $ids),
|
||||
'comparison' => $comparison,
|
||||
'timeline' => StatisticsService::timeline($years, $ids),
|
||||
'undatedHandled' => StatisticsService::undatedHandled($ids),
|
||||
'undatedDeaths' => StatisticsService::undatedDeaths($ids),
|
||||
'undatedBirths' => StatisticsService::undatedBirths($ids),
|
||||
'undatedExits' => StatisticsService::undatedExits($ids),
|
||||
'scopeCounts' => StatisticsService::scopeCounts($ids),
|
||||
'distributions' => StatisticsService::distributions($year, $ids, $municipality ?: null),
|
||||
'distributions' => StatisticsService::distributions($all ? null : $year, $ids, $municipality ?: null, $start, $end),
|
||||
'health' => StatisticsService::currentHealth($ids),
|
||||
'costs' => StatisticsService::costs($year, $ids),
|
||||
'municipalityCosts' => StatisticsService::costsByMunicipality($year),
|
||||
'costs' => StatisticsService::costs($all ? null : $year, $ids, $start, $end),
|
||||
'municipalityCosts' => StatisticsService::costsByMunicipality($all ? null : $year, $start, $end),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue