Actualiser api.php

This commit is contained in:
2026-06-24 09:09:56 +02:00
parent 2e6820cedc
commit 240c574e0b
+52 -29
View File
@@ -368,7 +368,7 @@ switch ($action) {
} else { } else {
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), poster=VALUES(poster), format=VALUES(format), length=VALUES(length), publisher=VALUES(publisher), ean_isbn13=VALUES(ean_isbn13), number_of_discs=VALUES(number_of_discs), aspect_ratio=VALUES(aspect_ratio), description=VALUES(description), actors=VALUES(actors)"; $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), poster=VALUES(poster), format=VALUES(format), length=VALUES(length), publisher=VALUES(publisher), ean_isbn13=VALUES(ean_isbn13), number_of_discs=VALUES(number_of_discs), aspect_ratio=VALUES(aspect_ratio), description=VALUES(description), actors=VALUES(actors)";
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['format'] ?? '', $data['length'] ?? '', $data['publisher'] ?? '', $data['ean_isbn13'] ?? '', !empty($data['number_of_discs']) ? (int)$data['number_of_discs'] : 1, $data['aspect_ratio'] ?? '', $data['description'] ?? '', $data['actors'] ?? '']); $stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['format'] ?? '', $data['length'] ?? '', $data['publisher'] ?? '', $data['ean_isbn13'] ?? '', $data['number_of_discs'] ?? 1, $data['aspect_ratio'] ?? '', $data['description'] ?? '', $data['actors'] ?? '']);
} }
echo json_encode(["success" => true]); echo json_encode(["success" => true]);
break; break;
@@ -389,6 +389,7 @@ switch ($action) {
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break; break;
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
$items = $data['items'] ?? []; $items = $data['items'] ?? [];
@@ -402,28 +403,44 @@ switch ($action) {
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
$year = extractYear($publishDate); $year = extractYear($publishDate);
// 🔥 ID isolé par type pour éviter les collisions
$id = makeStableId($type, $title, $year);
if ($type === 'videotheque') { if ($type === 'videotheque') {
$firstName = $rowData['first_name'] ?? ''; $firstName = $rowData['first_name'] ?? '';
$lastName = $rowData['last_name'] ?? ''; $lastName = $rowData['last_name'] ?? '';
$creators = $rowData['creators'] ?? ''; $creators = $rowData['creators'] ?? '';
$director = ''; $director = '';
if (!empty($firstName) && !empty($lastName)) { // Bug 4 : dans le format CSV vidéothèque (ex: CLZ/Collectorz), creators et
$director = trim("$firstName $lastName"); // first_name/last_name contiennent les ACTEURS, pas le réalisateur.
} elseif (!empty($creators)) { // On laisse $director vide pour laisser TMDB trouver le vrai réalisateur.
$director = $creators; // Fallback CSV (utile pour les coffrets) // Exception : si ensemble est vide, creators peut servir de fallback acteurs.
} unset($firstName, $lastName); // non utilisés volontairement
// Support de plusieurs nomenclatures de colonnes EAN $ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? $rowData['ean'] ?? ''; // Bug 1 : EAN peut arriver comme float "7321950374984.0", on normalise en entier string
if (!empty($ean)) {
$eanFloat = floatval($ean);
if ($eanFloat > 0) $ean = (string) round($eanFloat);
$ean = preg_replace('/[^0-9]/', '', $ean);
}
$description = $rowData['description'] ?? $rowData['Description'] ?? ''; $description = $rowData['description'] ?? $rowData['Description'] ?? '';
$publisher = $rowData['publisher'] ?? $rowData['Publisher'] ?? ''; $publisher = $rowData['publisher'] ?? '';
$length = $rowData['length'] ?? ''; // Bug 2 : length peut être un float "245.0", on le convertit en entier
$discs = !empty($rowData['number_of_discs']) ? (int)$rowData['number_of_discs'] : 1; $lengthRaw = $rowData['length'] ?? '';
$length = '';
if ($lengthRaw !== '' && $lengthRaw !== null) {
$lengthVal = floatval($lengthRaw);
if ($lengthVal > 0) $length = (string) round($lengthVal);
}
// Bug 3 : number_of_discs peut être NaN ou vide, on sécurise à 1
$discsRaw = $rowData['number_of_discs'] ?? '';
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
$aspect = $rowData['aspect_ratio'] ?? ''; $aspect = $rowData['aspect_ratio'] ?? '';
$format = $rowData['format'] ?? detectFormat($title, $description); $format = $rowData['format'] ?? detectFormat($title, $description);
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
// Extraction des acteurs depuis le CSV // 🔥 Extraction des acteurs depuis le CSV
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = ''; $actors = '';
if (!empty($csvActors)) { if (!empty($csvActors)) {
@@ -431,7 +448,7 @@ switch ($action) {
$actors = implode(', ', array_slice($actorsArray, 0, 4)); $actors = implode(', ', array_slice($actorsArray, 0, 4));
} }
// 1. UPCitemDB & DVDFr (Si un EAN est fourni) // 1. UPCitemDB
if (!empty($ean)) { if (!empty($ean)) {
$upcData = fetchUPCitemdb($ean, $pdo); $upcData = fetchUPCitemdb($ean, $pdo);
if ($upcData) { if ($upcData) {
@@ -441,25 +458,35 @@ switch ($action) {
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
} }
// 🔥 1.5 DVDFr (Récupère uniquement la vraie jaquette FR)
$dvdfrCover = fetchDVDFr($ean, $pdo); $dvdfrCover = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrCover)) { if (!empty($dvdfrCover)) {
$poster = $dvdfrCover; $poster = $dvdfrCover;
} }
}
// 2. TMDB (Sorti du bloc EAN pour fonctionner même avec juste un Titre) // 2. TMDB (Données Officielles & Synopsis)
if ($tmdbApiKey && !empty($title) && $title !== 'Sans titre') { if ($tmdbApiKey && !empty($title)) {
// Premier essai classique
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
// Fallback intelligent pour les coffrets si rien n'est trouvé // 🔥 NOUVEAU : Si TMDB ne trouve rien (cas des Coffrets ou titres multiples)
if (!$tmdbData || empty($tmdbData['overview'])) { if (!$tmdbData || empty($tmdbData['overview'])) {
$cleanTitle = $title; $cleanTitle = $title;
// A. Supprime les mots parasites
$cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle); $cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
// B. Sépare les listes de films (ex: "Alien / Aliens" ou "Batman + Batman Le Défi")
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0]; $cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
// C. Sépare les tirets longs (ex: "Le Parrain - L'intégrale")
$cleanTitle = explode(' - ', $cleanTitle)[0]; $cleanTitle = explode(' - ', $cleanTitle)[0];
$cleanTitle = trim($cleanTitle); $cleanTitle = trim($cleanTitle);
// Si le titre a été nettoyé, on relance TMDB avec le titre du 1er film
if (!empty($cleanTitle) && $cleanTitle !== $title) { if (!empty($cleanTitle) && $cleanTitle !== $title) {
// On omet volontairement l'année, car l'année du coffret n'est pas celle du 1er film
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo); $tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
if ($tmdbFallback && !empty($tmdbFallback['overview'])) { if ($tmdbFallback && !empty($tmdbFallback['overview'])) {
$tmdbData = $tmdbFallback; $tmdbData = $tmdbFallback;
@@ -479,30 +506,28 @@ switch ($action) {
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year']; if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length']; if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
// LIGNE DÉSACTIVÉE : TMDB ne touche jamais à l'affiche de la vidéothèque
// if (empty($poster) && !empty($tmdbData['poster'])) $poster = $tmdbData['poster'];
// 🔥 Récupération du Synopsis via TMDB (Même pour le 1er film d'un coffret)
if (!empty($tmdbData['overview'])) { if (!empty($tmdbData['overview'])) {
$description = $tmdbData['overview']; $description = $tmdbData['overview'];
} }
// Récupération des Acteurs officiels (Top 4)
if (!empty($tmdbData['cast'])) { if (!empty($tmdbData['cast'])) {
$actors = implode(', ', $tmdbData['cast']); $actors = implode(', ', $tmdbData['cast']);
} }
} }
} }
// 3. GÉNÉRATION DE L'ID (Calculé APRIORI avec les données fiables de l'API)
if ($title === 'Sans titre' && !empty($ean)) {
$id = makeStableId($type, $ean, '0000'); // Sécurité extrême si on n'a vraiment aucun titre
} else {
$id = makeStableId($type, $title, $year);
} }
// 🔥 L'INSERT SQL DOIT ÊTRE ICI (EN DEHORS DU IF TMDB)
// 4. INSERTION EN BDD
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) 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), actors=IF(VALUES(actors)!='',VALUES(actors),actors)"; $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) 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), actors=IF(VALUES(actors)!='',VALUES(actors),actors)";
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
} else { } else {
// Logique pour les Critiques // Pour les critiques
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? ''; $review = $rowData['Review'] ?? $rowData['review'] ?? '';
@@ -518,9 +543,7 @@ switch ($action) {
} }
} }
// 3. GÉNÉRATION DE L'ID (Déplacé ici pour les critiques aussi) // 🔥 Valeur par défaut pour le streaming
$id = makeStableId($type, $title, $year);
if (empty($streaming)) { if (empty($streaming)) {
$streaming = 'Support physique / Cinéma'; $streaming = 'Support physique / Cinéma';
} }