Actualiser api.php
This commit is contained in:
@@ -193,16 +193,34 @@ switch ($action) {
|
||||
|
||||
if (($handle = fopen($file, "r")) !== FALSE) {
|
||||
$header = fgetcsv($handle, 0, ",");
|
||||
// Nettoyage des headers (suppression des espaces ou caractères cachés)
|
||||
$header = array_map('trim', $header);
|
||||
|
||||
while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
|
||||
if (count($row) !== count($header)) continue;
|
||||
$rowData = array_combine($header, $row);
|
||||
$id = !empty($rowData['id']) ? $rowData['id'] : makeStableId($rowData['title'] ?? '', $rowData['year'] ?? '0000');
|
||||
|
||||
// Récupération des données avec valeurs par défaut si colonnes manquantes
|
||||
$title = $rowData['Name'] ?? $rowData['title'] ?? 'Sans titre';
|
||||
$year = $rowData['Year'] ?? $rowData['year'] ?? '0000';
|
||||
$rating = isset($rowData['Rating']) ? (int)round($rowData['Rating'] * 1) : 3;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
$id = makeStableId($title, $year);
|
||||
|
||||
if ($type === 'critique') {
|
||||
$stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), poster=VALUES(poster), rating=VALUES(rating), review=VALUES(review), streaming=VALUES(streaming)");
|
||||
$stmt->execute([$id, $rowData['title']??'', $rowData['year']??'', $rowData['director']??'', $rowData['poster']??'', $rowData['rating']??3, $rowData['review']??'', $rowData['streaming']??'']);
|
||||
// On utilise les colonnes de la BDD.
|
||||
// Note : ON DUPLICATE KEY UPDATE ne mettra à jour que les champs fournis.
|
||||
$stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, rating, review)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
rating = VALUES(rating),
|
||||
review = IF(VALUES(review) != '', VALUES(review), review)");
|
||||
$stmt->execute([$id, $title, $year, $rating, $review]);
|
||||
} else {
|
||||
$stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description) 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)");
|
||||
$stmt->execute([$id, $rowData['title']??'', $rowData['year']??'', $rowData['director']??'', $rowData['poster']??'', $rowData['format']??'', $rowData['length']??'', $rowData['publisher']??'', $rowData['ean_isbn13']??'', $rowData['number_of_discs']??1, $rowData['aspect_ratio']??'', $rowData['description']??'']);
|
||||
// Pour la vidéothèque, on importe les champs disponibles
|
||||
$stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year) VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE title=VALUES(title)");
|
||||
$stmt->execute([$id, $title, $year]);
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
Reference in New Issue
Block a user