diff --git a/api.php b/api.php index 14033eb..0198f40 100644 --- a/api.php +++ b/api.php @@ -867,50 +867,94 @@ case 'get_films': else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } break; - // ── NOUVELLE ACTION : Recherche par EAN ── -case 'search_by_ean': - checkAuth($pdo); - $ean = $_GET['ean'] ?? ''; - $ean = preg_replace('/[^0-9]/', '', $ean); + case 'add_item_by_ean': + $data = json_decode(file_get_contents("php://input"), true); + $ean = preg_replace('/[^0-9]/', '', $data['ean'] ?? ''); if (strlen($ean) < 8) { echo json_encode(["success" => false, "error" => "EAN invalide"]); - break; + exit; } - // 1. Blu-ray.com - $blurayData = fetchFromBlurayCom($ean); + error_log("--- DÉBUT RECHERCHE EAN : $ean ---"); + + // ✅ UTILISER fetchPhysicalByEan qui fonctionne (UPCitemdb → UPCMDB fallback) + $physicalData = fetchPhysicalByEan($ean, $pdo); + $title = $physicalData['title'] ?? ''; - // 2. MovieCovers pour l'affiche HD - $mcData = []; - if (!empty($blurayData['title'])) { - $mcData = fetchFromMovieCovers($blurayData['title'], $blurayData['year'] ?? ''); + error_log("fetchPhysicalByEan a trouvé : " . ($title ?: 'RIEN')); + + if (empty($title)) { + echo json_encode(["success" => false, "error" => "EAN non trouvé dans les bases de données"]); + exit; } + + // ✅ 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); - // 3. Fallback TMDB - $tmdbData = []; - $title = !empty($blurayData['title']) ? $blurayData['title'] : ''; - if ($title) { - $tmdbData = fetchTmdbPosterAndSynopsis($title, $blurayData['year'] ?? '', $pdo); + // 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'] ?? ''); - // Fusion des données - $result = [ - 'success' => true, - 'title' => $blurayData['title'] ?? '', - 'year' => $blurayData['year'] ?? ($tmdbData['year'] ?? ''), - 'director' => $mcData['director'] ?? ($blurayData['director'] ?? ($tmdbData['director'] ?? '')), - 'poster' => $mcData['poster'] ?? ($blurayData['poster'] ?? ($tmdbData['poster'] ?? '')), - 'format' => $blurayData['format'] ?? 'Blu-ray', - 'length' => $blurayData['length'] ?? ($tmdbData['length'] ?? ''), - 'publisher' => $blurayData['publisher'] ?? '', - 'number_of_discs' => $blurayData['number_of_discs'] ?? 1, - 'aspect_ratio' => $blurayData['aspect_ratio'] ?? '', - 'actors' => $mcData['actors'] ?? ($blurayData['actors'] ?? ($tmdbData['actors'] ?? '')), - 'description' => $mcData['description'] ?? ($blurayData['description'] ?? ($tmdbData['description'] ?? '')) - ]; + error_log("Titre final : '$finalTitle' | Poster : " . ($poster ?: 'AUCUN')); + + // Insertion dans la base + $id = makeStableId('videotheque', $finalTitle, $tmdbData['year'] ?? $physicalData['year'] ?? date('Y')); - echo json_encode($result); + $stmt = $pdo->prepare("INSERT INTO videotheque + (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'] ?? $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, + "poster" => $poster, + "year" => $tmdbData['year'] ?? $physicalData['year'] ?? '' + ]); break; case 'add_item_by_ean':