Actualiser js/admin.js
This commit is contained in:
+87
-43
@@ -420,33 +420,41 @@ async function handleCritiqueUpload(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function normalizeVideothequeRow(row) {
|
function normalizeVideothequeRow(row) {
|
||||||
let ean = row['ean_isbn13'] || row['EAN'] || '';
|
let ean = row['ean_isbn13'] || row['EAN'] || row['ean'] || '';
|
||||||
if (ean !== '') {
|
if (ean !== '') {
|
||||||
ean = String(ean).replace(/[^0-9]/g, '');
|
ean = String(ean).replace(/[^0-9]/g, '');
|
||||||
ean = ean.replace(/^0+/, '');
|
ean = ean.replace(/^0+/, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
let length = row['length'] || '';
|
let length = row['length'] || row['Length'] || '';
|
||||||
if (length !== '' && length !== null) {
|
if (length !== '' && length !== null) {
|
||||||
const l = parseFloat(length);
|
const l = parseFloat(length);
|
||||||
length = isNaN(l) ? '' : String(Math.round(l));
|
length = isNaN(l) ? '' : String(Math.round(l));
|
||||||
}
|
}
|
||||||
|
|
||||||
let discs = row['number_of_discs'] || '';
|
let discs = row['number_of_discs'] || row['Number_of_discs'] || row['Discs'] || '';
|
||||||
if (discs === '' || discs === null || isNaN(parseFloat(discs))) {
|
if (discs === '' || discs === null || isNaN(parseFloat(discs))) {
|
||||||
discs = 1;
|
discs = 1;
|
||||||
} else {
|
} else {
|
||||||
discs = Math.round(parseFloat(discs));
|
discs = Math.round(parseFloat(discs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let aspect = row['aspect_ratio'] || row['Aspect_ratio'] || row['AspectRatio'] || '';
|
||||||
|
let actors = row['actors'] || row['Actors'] || row['creators'] || '';
|
||||||
|
let publisher = row['publisher'] || row['Publisher'] || '';
|
||||||
|
let director = row['director'] || row['Director'] || row['first_name'] + ' ' + row['last_name'] || '';
|
||||||
|
|
||||||
return Object.assign({}, row, {
|
return Object.assign({}, row, {
|
||||||
ean_isbn13: ean,
|
ean: ean,
|
||||||
length: length,
|
length: length,
|
||||||
number_of_discs: discs
|
number_of_discs: discs,
|
||||||
|
aspect_ratio: aspect,
|
||||||
|
actors: actors,
|
||||||
|
publisher: publisher,
|
||||||
|
director: director.trim()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── 2. Importation et Mapping du CSV Blu-ray.com ──
|
|
||||||
function handleVideothequeUpload(input) {
|
function handleVideothequeUpload(input) {
|
||||||
const file = input.files[0];
|
const file = input.files[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
@@ -454,61 +462,97 @@ function handleVideothequeUpload(input) {
|
|||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = async (e) => {
|
reader.onload = async (e) => {
|
||||||
const text = e.target.result;
|
const text = e.target.result;
|
||||||
|
|
||||||
// On suppose que ta fonction parseCSV retourne un tableau d'objets avec les en-têtes en clés
|
|
||||||
const parsedData = parseCSV(text);
|
const parsedData = parseCSV(text);
|
||||||
|
|
||||||
|
if (parsedData.length === 0) {
|
||||||
|
alert("❌ Fichier CSV vide ou invalide.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const items = parsedData.map(row => {
|
const items = parsedData.map(row => {
|
||||||
// Lecture des colonnes spécifiques du CSV de Blu-ray.com
|
const normalizedRow = normalizeVideothequeRow(row);
|
||||||
const title = row['title'] ? row['title'].trim() : '';
|
|
||||||
|
const title = normalizedRow['title'] || normalizedRow['Title'] || '';
|
||||||
if (!title) return null;
|
if (!title) return null;
|
||||||
|
|
||||||
// On priorise l'EAN, avec un fallback sur l'UPC si l'EAN est vide
|
const ean = normalizedRow['ean'] || '';
|
||||||
let ean = row['ean_isbn13'] ? row['ean_isbn13'].trim() : '';
|
const publishDate = normalizedRow['publish_date'] || normalizedRow['Publish_date'] || normalizedRow['publishdate'] || '';
|
||||||
if (!ean && row['upc_isbn10']) {
|
const year = publishDate ? publishDate.split('-')[0] : (normalizedRow['year'] || normalizedRow['Year'] || '');
|
||||||
ean = row['upc_isbn10'].trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extraction de l'année depuis la date complète
|
const description = normalizedRow['description'] || normalizedRow['Description'] || '';
|
||||||
const publishDate = row['publish_date'] ? row['publish_date'].trim() : '';
|
const length = normalizedRow['length'] || '';
|
||||||
const year = publishDate ? publishDate.split('-')[0] : '';
|
const discs = normalizedRow['number_of_discs'] || 1;
|
||||||
|
const aspect = normalizedRow['aspect_ratio'] || '';
|
||||||
|
const actors = normalizedRow['actors'] || '';
|
||||||
|
const publisher = normalizedRow['publisher'] || '';
|
||||||
|
const director = normalizedRow['director'] || '';
|
||||||
|
|
||||||
const description = row['description'] ? row['description'].trim() : '';
|
return {
|
||||||
|
title: title.trim(),
|
||||||
return { title, ean, year, description };
|
ean: ean,
|
||||||
|
year: year,
|
||||||
|
description: description.trim(),
|
||||||
|
length: length,
|
||||||
|
number_of_discs: discs,
|
||||||
|
aspect_ratio: aspect,
|
||||||
|
actors: actors,
|
||||||
|
publisher: publisher,
|
||||||
|
director: director
|
||||||
|
};
|
||||||
}).filter(item => item !== null);
|
}).filter(item => item !== null);
|
||||||
|
|
||||||
if (items.length === 0) {
|
if (items.length === 0) {
|
||||||
alert("Aucun film valide trouvé dans le fichier CSV.");
|
alert("❌ Aucun film valide trouvé dans le fichier CSV.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showImportModal(items.length, 'videotheque');
|
||||||
|
let processed = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_URL}?action=import_batch`, {
|
// Traiter par lots de 5 pour éviter la surcharge
|
||||||
method: 'POST',
|
for (let i = 0; i < items.length; i += 5) {
|
||||||
headers: {
|
const batch = items.slice(i, i + 5);
|
||||||
'Authorization': localStorage.getItem('token'),
|
|
||||||
'Content-Type': 'application/json'
|
const response = await fetch(`${API_URL}?action=import_batch`, {
|
||||||
},
|
method: 'POST',
|
||||||
body: JSON.stringify({ type: 'videotheque', items })
|
headers: {
|
||||||
});
|
'Authorization': localStorage.getItem('token'),
|
||||||
const data = await response.json();
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
if (data.success) {
|
body: JSON.stringify({ type: 'videotheque', items: batch })
|
||||||
// Utilisation de ta modale de succès
|
});
|
||||||
if (typeof showSuccessModal === 'function') {
|
|
||||||
showSuccessModal(`${data.imported} éditions physiques importées !`);
|
if (!response.ok) {
|
||||||
} else {
|
throw new Error(`Erreur serveur ${response.status}`);
|
||||||
alert(`${data.imported} éditions physiques importées !`);
|
|
||||||
}
|
}
|
||||||
loadDashboardData();
|
|
||||||
} else {
|
const data = await response.json();
|
||||||
alert("Erreur lors de l'importation côté serveur.");
|
|
||||||
|
if (!data.success) {
|
||||||
|
throw new Error(data.error || "Erreur inconnue");
|
||||||
|
}
|
||||||
|
|
||||||
|
processed += batch.length;
|
||||||
|
updateImportModal(processed, items.length);
|
||||||
|
|
||||||
|
// Petit délai pour éviter de surcharger le serveur
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input.value = '';
|
||||||
|
closeImportModal();
|
||||||
|
showSuccessModal(`${items.length} édition(s) physique(s) importée(s) avec succès !`);
|
||||||
|
loadDashboardData();
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Erreur d'importation :", err);
|
console.error("Erreur d'importation : ", err);
|
||||||
|
closeImportModal();
|
||||||
|
alert("❌ Échec de l'import : " + err.message);
|
||||||
|
input.value = '';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user