diff --git a/api.php b/api.php index ea2ff5e..da18ea0 100644 --- a/api.php +++ b/api.php @@ -480,40 +480,64 @@ case 'import_batch': $pdo->beginTransaction(); $imported = 0; $skipped = 0; try { - if ($type === 'videotheque') { - $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE - title=VALUES(title), year=VALUES(year), format=VALUES(format), poster=VALUES(poster), ean_isbn13=VALUES(ean_isbn13), description=VALUES(description), length=VALUES(length), number_of_discs=VALUES(number_of_discs), aspect_ratio=VALUES(aspect_ratio), actors=VALUES(actors), publisher=VALUES(publisher), director=VALUES(director)"); + if ($type === 'videotheque') { + // L'UPDATE est optimisé pour ne pas écraser les bonnes données avec des valeurs vides ou l'affiche par défaut + $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE + title=VALUES(title), year=VALUES(year), format=VALUES(format), + poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster), + ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13), + description=IF(VALUES(description)!='', VALUES(description), description), + length=IF(VALUES(length)!='', VALUES(length), length), + 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), + actors=IF(VALUES(actors)!='', VALUES(actors), actors), + publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher), + director=IF(VALUES(director)!='', VALUES(director), director)"); + + foreach ($items as $item) { + $ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); - foreach ($items as $item) { - $ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); - if (strlen($ean) < 8) { $skipped++; continue; } - - // 1. Données physiques via UPC (UPCitemdb → UPCMDB) + // 1. Données de base (CSV ou fallback UPC si le titre est absent) + $title = trim($item['title'] ?? ''); + $publisher = trim($item['publisher'] ?? ''); + $length = trim($item['length'] ?? ''); + $discs = (int)($item['number_of_discs'] ?? 1) ?: 1; + $aspect = trim($item['aspect_ratio'] ?? ''); + $desc = trim($item['description'] ?? ''); + $actors = trim($item['actors'] ?? ''); + $year = ''; + + // Fallback API UPC uniquement si le titre manque dans le CSV + if (empty($title) && strlen($ean) >= 8) { $phys = fetchPhysicalByEan($ean, $pdo); $title = $phys['title'] ?? ''; - if (empty($title)) { $skipped++; continue; } - + if (empty($publisher)) $publisher = $phys['publisher'] ?? ''; + if (empty($length)) $length = $phys['length'] ?? ''; + if ($discs == 1) $discs = $phys['number_of_discs'] ?? 1; + if (empty($aspect)) $aspect = $phys['aspect_ratio'] ?? ''; $year = $phys['year'] ?? ''; - $format = $phys['format'] ?: detectFormat($title); - $publisher = $phys['publisher'] ?? ''; - $discs = $phys['number_of_discs'] ?? 1; - $aspect = $phys['aspect_ratio'] ?? ''; - $length = $phys['length'] ?? ''; - - // 2. Données cinéma via TMDB (Affiche, Réalisateur, Acteurs, Synopsis, Durée) - $tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo); - $poster = $tmdb['poster']; - $director = $tmdb['director'] ?? ''; - $actors = $tmdb['actors'] ?? ''; - $desc = $tmdb['description'] ?? ''; - if (!empty($tmdb['length'])) $length = $tmdb['length']; - if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year']; - - $id = makeStableId('videotheque', $title, $year); - $stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]); - $imported++; } + + if (empty($title)) { $skipped++; continue; } + + $format = detectFormat($title); + + // 2. Données cinéma via TMDB (Affiche, Réalisateur, Acteurs, Synopsis, Année) + $tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo); + $poster = $tmdb['poster']; + $director = $tmdb['director'] ?? ''; + + // On complète avec TMDB si les données du CSV sont vides + if (empty($desc)) $desc = $tmdb['description'] ?? ''; + if (empty($actors)) $actors = $tmdb['actors'] ?? ''; + if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length']; + if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year']; + + $id = makeStableId('videotheque', $title, $year); + $stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]); + $imported++; + } } else { // ── IMPORTATION CRITIQUES ── $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?)