diff --git a/api.php b/api.php index 7af67c2..536401c 100644 --- a/api.php +++ b/api.php @@ -867,66 +867,94 @@ case 'get_films': else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } break; -case 'add_item_by_ean': + case 'add_item_by_ean': $data = json_decode(file_get_contents("php://input"), true); - $ean = $data['ean'] ?? ''; + $ean = preg_replace('/[^0-9]/', '', $data['ean'] ?? ''); + + if (strlen($ean) < 8) { + echo json_encode(["success" => false, "error" => "EAN invalide"]); + exit; + } - // LOG : EAN traité error_log("--- DÉBUT RECHERCHE EAN : $ean ---"); - // 1. Test UpcIndex - $titleUpc = fetchFromUpcIndex($ean); - error_log("UpcIndex a trouvé : " . ($titleUpc ?? 'RIEN')); - - // 2. Test Blu-ray.com - $blurayData = fetchFromBlurayCom($ean); - error_log("Blu-ray.com a trouvé : " . ($blurayData['title'] ?? 'RIEN')); - $ean = $data['ean'] ?? ''; + // ✅ UTILISER fetchPhysicalByEan qui fonctionne (UPCitemdb → UPCMDB fallback) + $physicalData = fetchPhysicalByEan($ean, $pdo); + $title = $physicalData['title'] ?? ''; - // 1. Récupération via UpcIndex (Priorité 1) - $title = fetchFromUpcIndex($ean); - - // Fallback si UpcIndex échoue - if (!$title) { - $blurayData = fetchFromBlurayCom($ean); - $title = $blurayData['title'] ?? ''; - } + error_log("fetchPhysicalByEan a trouvé : " . ($title ?: 'RIEN')); if (empty($title)) { - echo json_encode(["success" => false, "error" => "EAN non trouvé sur UpcIndex ni Blu-ray.com"]); + echo json_encode(["success" => false, "error" => "EAN non trouvé dans les bases de données"]); exit; } - // 2. Recherche sur TMDB (Source de vérité pour le titre FR et infos) - // On utilise fetchTmdbPosterAndSynopsis pour récupérer le titre français, poster, etc. - $tmdbData = fetchTmdbPosterAndSynopsis($title, '', $pdo); + // ✅ Nettoyage amélioré du titre pour TMDB + $cleanTitle = cleanUpcTitle($title); + error_log("Titre nettoyé pour TMDB : '$cleanTitle'"); + + // Recherche TMDB avec le titre nettoyé + $tmdbData = fetchTmdbPosterAndSynopsis($cleanTitle, $physicalData['year'] ?? '', $pdo); - // Si TMDB ne trouve rien avec le titre trouvé, on utilise le titre original - $finalTitle = (!empty($tmdbData['title']) && $tmdbData['title'] !== 'assets/img/default_physical_media.jpg') - ? $tmdbData['title'] - : $title; - - $poster = ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') ? $tmdbData['poster'] : ''; + // Si TMDB ne trouve rien, essayer avec le titre original nettoyé différemment + if (empty($tmdbData['title']) || $tmdbData['poster'] === 'assets/img/default_physical_media.jpg') { + // Tentative alternative : extraire juste le nom du film sans "DVD", "Blu-ray", etc. + $altTitle = preg_replace('/^(DVD|Blu-ray|Bluray|4K|Ultra HD|VHS)\s*/i', '', $title); + $altTitle = preg_replace('/\s*(New Blister|Import|Edition|Édition|Spéciale|Special|Collector).*$/i', '', $altTitle); + $altTitle = trim(preg_replace('/\s{2,}/', ' ', $altTitle)); + + if ($altTitle !== $cleanTitle) { + error_log("Tentative alternative TMDB avec : '$altTitle'"); + $tmdbDataAlt = fetchTmdbPosterAndSynopsis($altTitle, $physicalData['year'] ?? '', $pdo); + if (!empty($tmdbDataAlt['title']) && $tmdbDataAlt['poster'] !== 'assets/img/default_physical_media.jpg') { + $tmdbData = $tmdbDataAlt; + } + } + } + + // Déterminer le titre final + $finalTitle = !empty($tmdbData['title']) ? $tmdbData['title'] : $cleanTitle; + $poster = ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') ? $tmdbData['poster'] : ($physicalData['poster'] ?? ''); - // 3. Insertion - $id = makeStableId('videotheque', $finalTitle, date('Y')); + error_log("Titre final : '$finalTitle' | Poster : " . ($poster ?: 'AUCUN')); + + // Insertion dans la base + $id = makeStableId('videotheque', $finalTitle, $tmdbData['year'] ?? $physicalData['year'] ?? date('Y')); $stmt = $pdo->prepare("INSERT INTO videotheque - (id, title, poster, description, director, actors, ean_isbn13) - VALUES (?, ?, ?, ?, ?, ?, ?) - ON DUPLICATE KEY UPDATE title=VALUES(title)"); - + (id, title, year, poster, description, director, actors, ean_isbn13, format, length, publisher, number_of_discs, aspect_ratio) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON DUPLICATE KEY UPDATE + title=VALUES(title), year=VALUES(year), poster=IF(VALUES(poster)!='', VALUES(poster), poster), + description=IF(VALUES(description)!='', VALUES(description), description), + director=IF(VALUES(director)!='', VALUES(director), director), + actors=IF(VALUES(actors)!='', VALUES(actors), actors), + format=IF(VALUES(format)!='', VALUES(format), format), + length=IF(VALUES(length)!='', VALUES(length), length), + publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher)"); + $stmt->execute([ $id, $finalTitle, + $tmdbData['year'] ?? $physicalData['year'] ?? '', $poster, - $tmdbData['description'] ?? '', - $tmdbData['director'] ?? '', - $tmdbData['actors'] ?? '', - $ean + $tmdbData['description'] ?? $physicalData['description'] ?? '', + $tmdbData['director'] ?? $physicalData['director'] ?? '', + $tmdbData['actors'] ?? $physicalData['actors'] ?? '', + $ean, + $physicalData['format'] ?? detectFormat($title), + $tmdbData['length'] ?? $physicalData['length'] ?? '', + $physicalData['publisher'] ?? '', + $physicalData['number_of_discs'] ?? 1, + $physicalData['aspect_ratio'] ?? '' ]); - echo json_encode(["success" => true, "title" => $finalTitle]); + echo json_encode([ + "success" => true, + "title" => $finalTitle, + "poster" => $poster, + "year" => $tmdbData['year'] ?? $physicalData['year'] ?? '' + ]); break; case 'import_batch':