Actualiser api.php

This commit is contained in:
2026-06-19 08:29:45 +02:00
parent 224f779231
commit 2ed1e335b8
+43 -14
View File
@@ -193,34 +193,63 @@ switch ($action) {
if (($handle = fopen($file, "r")) !== FALSE) { if (($handle = fopen($file, "r")) !== FALSE) {
$header = fgetcsv($handle, 0, ","); $header = fgetcsv($handle, 0, ",");
// Nettoyage des headers (suppression des espaces ou caractères cachés)
$header = array_map('trim', $header); $header = array_map('trim', $header);
while (($row = fgetcsv($handle, 0, ",")) !== FALSE) { while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
if (count($row) !== count($header)) continue; if (count($row) !== count($header)) continue;
$rowData = array_combine($header, $row); $rowData = array_combine($header, $row);
// Récupération des données avec valeurs par défaut si colonnes manquantes // Récupération des champs communs avec flexibilité sur les noms de colonnes
$title = $rowData['Name'] ?? $rowData['title'] ?? 'Sans titre'; $title = $rowData['Name'] ?? $rowData['title'] ?? 'Sans titre';
$year = $rowData['Year'] ?? $rowData['year'] ?? '0000'; $year = $rowData['Year'] ?? $rowData['year'] ?? '0000';
$rating = isset($rowData['Rating']) ? (int)round($rowData['Rating'] * 1) : 3; $director = $rowData['Director'] ?? $rowData['director'] ?? '';
$review = $rowData['Review'] ?? $rowData['review'] ?? ''; // Accepte "Poster", "poster" ou "image"
$poster = $rowData['Poster'] ?? $rowData['poster'] ?? $rowData['image'] ?? '';
$id = makeStableId($title, $year); $id = makeStableId($title, $year);
if ($type === 'critique') { if ($type === 'critique') {
// On utilise les colonnes de la BDD. $rating = isset($rowData['Rating']) ? (int)round($rowData['Rating'] * 1) : 3;
// Note : ON DUPLICATE KEY UPDATE ne mettra à jour que les champs fournis. $review = $rowData['Review'] ?? $rowData['review'] ?? '';
$stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, rating, review) $streaming = $rowData['Streaming'] ?? $rowData['streaming'] ?? '';
VALUES (?, ?, ?, ?, ?)
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
rating = VALUES(rating), rating = VALUES(rating),
review = IF(VALUES(review) != '', VALUES(review), review)"); review = IF(VALUES(review) != '', VALUES(review), review),
$stmt->execute([$id, $title, $year, $rating, $review]); director = IF(VALUES(director) != '', VALUES(director), director),
poster = IF(VALUES(poster) != '', VALUES(poster), poster),
streaming = IF(VALUES(streaming) != '', VALUES(streaming), streaming)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
} else { } else {
// Pour la vidéothèque, on importe les champs disponibles // Vidéothèque
$stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year) VALUES (?, ?, ?) $format = $rowData['format'] ?? $rowData['Format'] ?? '';
ON DUPLICATE KEY UPDATE title=VALUES(title)"); $length = $rowData['length'] ?? $rowData['Length'] ?? '';
$stmt->execute([$id, $title, $year]); $publisher = $rowData['publisher'] ?? $rowData['Publisher'] ?? '';
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
$discs = $rowData['number_of_discs'] ?? 1;
$aspect = $rowData['aspect_ratio'] ?? '';
$desc = $rowData['description'] ?? $rowData['Description'] ?? '';
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
director = IF(VALUES(director) != '', VALUES(director), director),
poster = IF(VALUES(poster) != '', VALUES(poster), poster),
format = IF(VALUES(format) != '', VALUES(format), format),
length = IF(VALUES(length) != '', VALUES(length), length),
publisher = IF(VALUES(publisher) != '', VALUES(publisher), publisher),
ean_isbn13 = IF(VALUES(ean_isbn13) != '', VALUES(ean_isbn13), ean_isbn13),
number_of_discs = IF(VALUES(number_of_discs) != 1, VALUES(number_of_discs), number_of_discs),
aspect_ratio = IF(VALUES(aspect_ratio) != '', VALUES(aspect_ratio), aspect_ratio),
description = IF(VALUES(description) != '', VALUES(description), description)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $desc]);
} }
} }
fclose($handle); fclose($handle);