anime-backlog-list/php/fetch_data.php
2025-01-07 12:23:54 +05:00

91 lines
2.6 KiB
PHP

<?php
include_once 'db_connect.php';
$year = $_GET['year'] ?? 'pre-2009';
// Map special tabs to year values
$yearMap = [
'pre-2009' => -1,
're-watch' => -2,
'manga' => -3
];
$yearValue = $yearMap[$year] ?? (int)$year;
// Seasons array to organize records
$seasons = ['winter', 'spring', 'summer', 'fall', 'omake'];
// Fetch records from DB based on mode
if ($yearValue == -1 || $yearValue == -2 || $yearValue == -3) {
// For pre-2009, re-watch, or manga, we order by name (or differently if you prefer)
$stmt = $conn->prepare("SELECT * FROM anime_list WHERE year = :year ORDER BY name ASC");
} else {
// Normal year mode
$stmt = $conn->prepare("SELECT * FROM anime_list WHERE year = :year ORDER BY
CASE
WHEN season = 'winter' THEN 1
WHEN season = 'spring' THEN 2
WHEN season = 'summer' THEN 3
WHEN season = 'fall' THEN 4
WHEN season = 'omake' THEN 5
ELSE 6
END, name ASC");
}
$stmt->bindParam(':year', $yearValue, PDO::PARAM_INT);
$stmt->execute();
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($yearValue == -1 || $yearValue == -2) {
$mode = 'non-season-table';
} else if ($yearValue == -3) {
$mode = 'manga';
} else {
$mode = 'normal';
}
// If normal mode, group records by season
$seasonRecords = [];
if ($mode === 'normal') {
// Initialize season arrays
foreach ($seasons as $s) {
$seasonRecords[$s] = [];
}
// Place records into their respective season
foreach ($records as $record) {
$recordSeason = strtolower($record['season']);
if (in_array($recordSeason, $seasons)) {
$seasonRecords[$recordSeason][] = $record;
} else {
// If season is unknown, treat as 'omake'
$seasonRecords['omake'][] = $record;
}
}
}
$yearTotal = count($records);
$yearCompleted = 0;
foreach ($records as $r) {
if ($r['is_completed']) {
$yearCompleted++;
}
}
$yearPercent = $yearTotal > 0 ? round(($yearCompleted / $yearTotal) * 100) : 0;
// Prepare data for the view
$viewData = [
'year' => $year,
'yearValue' => $yearValue,
'mode' => $mode, // either 'non-season-table' or 'normal'
'records' => $records,
'seasonRecords' => $seasonRecords ?? [],
'seasons' => $seasons,
'showSuggestButton' => ($mode === 'normal'),
'yearTotal' => $yearTotal,
'yearCompleted' => $yearCompleted,
'yearPercent' => $yearPercent
];
// Include the view
include '../tmpl/fetch_data_view.php';