Actualiser api.php

This commit is contained in:
2026-07-02 09:06:30 +02:00
parent 02309fa133
commit d15a7ad28b
-90
View File
@@ -866,96 +866,6 @@ case 'get_films':
if (!empty($ids)) { $placeholders = implode(',', array_fill(0, count($ids), '?')); $stmt = $pdo->prepare("DELETE FROM $table WHERE id IN ($placeholders)"); $stmt->execute($ids); echo json_encode(["success" => true]); }
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break;
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"]);
exit;
}
error_log("--- DÉBUT RECHERCHE EAN : $ean ---");
// ✅ UTILISER fetchPhysicalByEan qui fonctionne (UPCitemdb → UPCMDB fallback)
$physicalData = fetchPhysicalByEan($ean, $pdo);
$title = $physicalData['title'] ?? '';
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);
// 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'] ?? '');
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, 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':
$data = json_decode(file_get_contents("php://input"), true);