Actualiser api.php
This commit is contained in:
@@ -264,7 +264,72 @@ switch ($action) {
|
||||
echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'import_batch':
|
||||
checkAuth($pdo);
|
||||
$items = $data['items'] ?? [];
|
||||
$type = $data['type'] ?? 'critique';
|
||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||
$imported = 0;
|
||||
|
||||
foreach ($items as $rowData) {
|
||||
$title = $rowData['Name'] ?? $rowData['title'] ?? 'Sans titre';
|
||||
$year = $rowData['Year'] ?? $rowData['year'] ?? '0000';
|
||||
$director = $rowData['Director'] ?? $rowData['director'] ?? '';
|
||||
$poster = $rowData['Poster'] ?? $rowData['poster'] ?? $rowData['image'] ?? '';
|
||||
|
||||
// Appel TMDB pour combler les trous (Réal, Affiche, Streaming)
|
||||
$tmdbData = fetchTmdbData($title, $year, $tmdbApiKey);
|
||||
if ($tmdbData) {
|
||||
if (empty($director)) $director = $tmdbData['director'];
|
||||
if (empty($poster)) $poster = $tmdbData['poster'];
|
||||
}
|
||||
|
||||
$id = makeStableId($title, $year);
|
||||
|
||||
if ($type === 'critique') {
|
||||
$rating = isset($rowData['Rating']) && $rowData['Rating'] !== '' ? (float)$rowData['Rating'] : 3.0;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
$csvStreaming = $rowData['Streaming'] ?? $rowData['streaming'] ?? '';
|
||||
$streaming = !empty($csvStreaming) ? $csvStreaming : (!empty($tmdbData['streaming']) ? $tmdbData['streaming'] : 'Disponible en support physique ou Cinéma');
|
||||
|
||||
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
rating = VALUES(rating),
|
||||
review = IF(VALUES(review) != '', VALUES(review), review),
|
||||
director = IF(VALUES(director) != '', VALUES(director), director),
|
||||
poster = IF(VALUES(poster) != '', VALUES(poster), poster),
|
||||
streaming = IF(VALUES(streaming) != '', VALUES(streaming), streaming)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||
} else {
|
||||
$format = $rowData['format'] ?? $rowData['Format'] ?? '';
|
||||
$length = $rowData['length'] ?? $rowData['Length'] ?? '';
|
||||
$publisher = $rowData['publisher'] ?? $rowData['Publisher'] ?? '';
|
||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
||||
$discs = $rowData['number_of_discs'] ?? 1;
|
||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
||||
$desc = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||
|
||||
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description)
|
||||
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)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $desc]);
|
||||
}
|
||||
$imported++;
|
||||
}
|
||||
echo json_encode(["success" => true, "imported" => $imported]);
|
||||
break;
|
||||
case 'import_csv':
|
||||
checkAuth($pdo);
|
||||
if (isset($_FILES['csv_file'])) {
|
||||
|
||||
Reference in New Issue
Block a user