100 lines
5 KiB
PHP
100 lines
5 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
final class StatisticsController
|
||
{
|
||
public static function index(): void
|
||
{
|
||
$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::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($all ? null : $year, $ids, $municipality ?: null, $start, $end),
|
||
'health' => StatisticsService::currentHealth($ids),
|
||
'costs' => StatisticsService::costs($all ? null : $year, $ids, $start, $end),
|
||
'municipalityCosts' => StatisticsService::costsByMunicipality($all ? null : $year, $start, $end),
|
||
]);
|
||
}
|
||
}
|