Actualiser js/admin.js

This commit is contained in:
2026-06-24 09:10:10 +02:00
parent 240c574e0b
commit a4ef23c64d
+65 -17
View File
@@ -7,14 +7,20 @@ let selectedIds = new Set();
let pendingDeleteAction = null; let pendingDeleteAction = null;
function getStarsHTML(rating) { function getStarsHTML(rating) {
// Gère les virgules (ex: "3,5" -> "3.5") et force un nombre
let r = parseFloat(String(rating).replace(',', '.')) || 0; let r = parseFloat(String(rating).replace(',', '.')) || 0;
// Sécurise la note stricte entre 0 et 5
r = Math.min(Math.max(r, 0), 5); r = Math.min(Math.max(r, 0), 5);
const full = Math.floor(r); const full = Math.floor(r);
const hasHalf = (r - full) >= 0.5; const hasHalf = (r - full) >= 0.5;
const empty = Math.max(0, 5 - Math.ceil(r)); const empty = Math.max(0, 5 - Math.ceil(r)); // Empêche une valeur négative fatale
let html = '★'.repeat(full); let html = '★'.repeat(full);
if (hasHalf) html += '<span class="half-star">★</span>'; if (hasHalf) html += '<span class="half-star">★</span>';
html += `<span class="stars-muted">${'☆'.repeat(empty)}</span>`; html += `<span class="stars-muted">${'☆'.repeat(empty)}</span>`;
return html; return html;
} }
@@ -68,7 +74,6 @@ document.addEventListener('DOMContentLoaded', () => {
function initEventListeners() { function initEventListeners() {
const filmForm = document.getElementById('film-form'); const filmForm = document.getElementById('film-form');
if (filmForm) filmForm.addEventListener('submit', saveFilmForm); if (filmForm) filmForm.addEventListener('submit', saveFilmForm);
const csvInput = document.getElementById('csv-file'); const csvInput = document.getElementById('csv-file');
if (csvInput) { if (csvInput) {
csvInput.addEventListener('change', (e) => { csvInput.addEventListener('change', (e) => {
@@ -76,13 +81,10 @@ function initEventListeners() {
else handleVideothequeUpload(e.target); else handleVideothequeUpload(e.target);
}); });
} }
const searchInput = document.getElementById('search-input'); const searchInput = document.getElementById('search-input');
if (searchInput) searchInput.addEventListener('input', () => { currentPage = 1; renderAdminTable(); }); if (searchInput) searchInput.addEventListener('input', () => { currentPage = 1; renderAdminTable(); });
const selectAll = document.getElementById('select-all-checkbox'); const selectAll = document.getElementById('select-all-checkbox');
if (selectAll) selectAll.addEventListener('change', (e) => toggleSelectAll(e.target)); if (selectAll) selectAll.addEventListener('change', (e) => toggleSelectAll(e.target));
document.addEventListener('click', (e) => { document.addEventListener('click', (e) => {
if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) { if (e.target.classList.contains('modal-close') || e.target.closest('.modal-close')) {
const overlay = e.target.closest('.overlay'); const overlay = e.target.closest('.overlay');
@@ -90,7 +92,6 @@ function initEventListeners() {
} }
if (e.target.classList.contains('overlay')) e.target.classList.remove('open'); if (e.target.classList.contains('overlay')) e.target.classList.remove('open');
}); });
const physicalFilter = document.getElementById('admin-physical-checkbox'); const physicalFilter = document.getElementById('admin-physical-checkbox');
if (physicalFilter) physicalFilter.addEventListener('change', () => { currentPage = 1; renderAdminTable(); }); if (physicalFilter) physicalFilter.addEventListener('change', () => { currentPage = 1; renderAdminTable(); });
} }
@@ -111,14 +112,15 @@ function renderAdminTable() {
const tbody = document.getElementById('admin-table-body'); const tbody = document.getElementById('admin-table-body');
if (!tbody) return; if (!tbody) return;
tbody.innerHTML = ''; tbody.innerHTML = '';
const searchInput = document.getElementById('search-input'); const searchInput = document.getElementById('search-input');
const currentSearch = searchInput ? searchInput.value.toLowerCase() : ''; const currentSearch = searchInput ? searchInput.value.toLowerCase() : '';
const physicalFilter = document.getElementById('admin-physical-checkbox'); // ✅ 修复了 getElementByI d const physicalFilter = document.getElementById('admin-physical-checkbox');
let filtered = allItems.filter(item => item.type === currentAdminTab); // ✅ 修复了 = > let filtered = allItems.filter(item => item.type === currentAdminTab);
if (physicalFilter && physicalFilter.checked) { // ✅ 修复了 & & if (physicalFilter && physicalFilter.checked) {
filtered = filtered.filter(f => { // ✅ 修复了 = > filtered = filtered.filter(f => {
if (f.type === 'critique') { if (f.type === 'critique') {
return f.streaming && f.streaming.toLowerCase().includes('support physique'); return f.streaming && f.streaming.toLowerCase().includes('support physique');
} }
@@ -134,14 +136,14 @@ function renderAdminTable() {
} }
const countLabel = document.getElementById('admin-count-label'); const countLabel = document.getElementById('admin-count-label');
if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`; // ✅ 修复了 filtered. length if (countLabel) countLabel.textContent = `${filtered.length} élément(s)`;
const totalPages = Math.ceil(filtered.length / itemsPerPage) || 1; const totalPages = Math.ceil(filtered.length / itemsPerPage) || 1;
if (currentPage > totalPages) currentPage = totalPages; if (currentPage > totalPages) currentPage = totalPages;
const startIdx = (currentPage - 1) * itemsPerPage; const startIdx = (currentPage - 1) * itemsPerPage;
const pageItems = filtered.slice(startIdx, startIdx + itemsPerPage); const pageItems = filtered.slice(startIdx, startIdx + itemsPerPage);
pageItems.forEach(f => { // ✅ 修复了 f = > pageItems.forEach(f => {
const tr = document.createElement('tr'); const tr = document.createElement('tr');
const isChecked = selectedIds.has(String(f.id)) ? 'checked' : ''; const isChecked = selectedIds.has(String(f.id)) ? 'checked' : '';
tr.innerHTML = ` tr.innerHTML = `
@@ -163,7 +165,7 @@ function renderAdminTable() {
renderPagination(totalPages, filtered.length); renderPagination(totalPages, filtered.length);
const selectAll = document.getElementById('select-all-checkbox'); const selectAll = document.getElementById('select-all-checkbox');
if (selectAll) selectAll.checked = pageItems.length > 0 && pageItems.every(f => selectedIds.has(String(f.id))); // ✅ 修复了 check ed, & &, = > if (selectAll) selectAll.checked = pageItems.length > 0 && pageItems.every(f => selectedIds.has(String(f.id)));
} }
function toggleSingleSelect(id, checkbox) { function toggleSingleSelect(id, checkbox) {
@@ -197,6 +199,7 @@ function renderPagination(totalPages, totalItems) {
const container = document.getElementById('pagination-container'); const container = document.getElementById('pagination-container');
if (!container) return; if (!container) return;
container.innerHTML = ''; container.innerHTML = '';
if (totalItems === 0) { if (totalItems === 0) {
container.innerHTML = '<p style="color:var(--muted); text-align:center; width:100%;">Aucun élément trouvé.</p>'; container.innerHTML = '<p style="color:var(--muted); text-align:center; width:100%;">Aucun élément trouvé.</p>';
return; return;
@@ -206,7 +209,7 @@ function renderPagination(totalPages, totalItems) {
const info = document.createElement('span'); const info = document.createElement('span');
info.className = 'pagination-info'; info.className = 'pagination-info';
info.textContent = `Page ${currentPage} sur ${totalPages}`; info.textContent = `Page ${currentPage} sur ${totalPages}`;
container.appendChild(info); // ✅ 修复了 in fo container.appendChild(info);
const prevBtn = document.createElement('button'); const prevBtn = document.createElement('button');
prevBtn.innerHTML = '<i class="ti ti-chevron-left"></i>'; prevBtn.innerHTML = '<i class="ti ti-chevron-left"></i>';
@@ -214,7 +217,8 @@ function renderPagination(totalPages, totalItems) {
prevBtn.onclick = () => { currentPage--; renderAdminTable(); }; prevBtn.onclick = () => { currentPage--; renderAdminTable(); };
container.appendChild(prevBtn); container.appendChild(prevBtn);
let startPage = Math.max(1, currentPage - 2); // ✅ 修复了 currentP age // Fenêtre glissante : 2 pages avant, 2 pages après la page courante
let startPage = Math.max(1, currentPage - 2);
let endPage = Math.min(totalPages, currentPage + 2); let endPage = Math.min(totalPages, currentPage + 2);
if (startPage > 1) { if (startPage > 1) {
@@ -244,7 +248,7 @@ function renderPagination(totalPages, totalItems) {
dots.textContent = '...'; dots.textContent = '...';
container.appendChild(dots); container.appendChild(dots);
} }
const lastBtn = document.createElement('button'); // ✅ 修复了 c reateElement const lastBtn = document.createElement('button');
lastBtn.textContent = totalPages; lastBtn.textContent = totalPages;
lastBtn.onclick = () => { currentPage = totalPages; renderAdminTable(); }; lastBtn.onclick = () => { currentPage = totalPages; renderAdminTable(); };
container.appendChild(lastBtn); container.appendChild(lastBtn);
@@ -409,11 +413,55 @@ async function handleCritiqueUpload(input) {
loadDashboardData(); loadDashboardData();
} }
function normalizeVideothequeRow(row) {
// Bug 1 : EAN float (ex: 7321950374984.0) → chaîne entière sans décimale
let ean = row['ean_isbn13'] || row['EAN'] || '';
if (ean !== '') {
const eanNum = parseFloat(ean);
ean = isNaN(eanNum) ? '' : String(Math.round(eanNum));
}
// Bug 2 : length float (ex: 245.0) → entier en minutes
let length = row['length'] || '';
if (length !== '' && length !== null) {
const l = parseFloat(length);
length = isNaN(l) ? '' : String(Math.round(l));
}
// Bug 3 : number_of_discs NaN → 1
let discs = row['number_of_discs'] || '';
if (discs === '' || discs === null || isNaN(parseFloat(discs))) {
discs = 1;
} else {
discs = Math.round(parseFloat(discs));
}
// Bug 4 : creators contient les acteurs dans ce format CSV,
// first_name/last_name pointent vers le 1er acteur, pas le réalisateur.
// On laisse director vide pour que TMDB le remplisse correctement.
const creators = row['creators'] || '';
const ensemble = row['ensemble'] || '';
return Object.assign({}, row, {
ean_isbn13: ean,
length: length,
number_of_discs: discs,
// Ne pas construire de director depuis first_name/last_name (ce sont des acteurs)
first_name: '',
last_name: '',
// Conserver creators/ensemble pour extraction des acteurs côté PHP
creators: creators,
ensemble: ensemble || creators
});
}
async function handleVideothequeUpload(input) { async function handleVideothequeUpload(input) {
if (!input.files || input.files.length === 0) return; if (!input.files || input.files.length === 0) return;
let allData = []; let allData = [];
for (const file of input.files) { try { allData = allData.concat(parseCSV(await file.text())); } catch(e) {} } for (const file of input.files) { try { allData = allData.concat(parseCSV(await file.text())); } catch(e) {} }
if (allData.length === 0) { input.value = ''; return alert('❌ Fichier vide.'); } if (allData.length === 0) { input.value = ''; return alert('❌ Fichier vide.'); }
// Bug 5 : normaliser les données brutes avant envoi
allData = allData.map(normalizeVideothequeRow);
showImportModal(allData.length, 'videotheque'); showImportModal(allData.length, 'videotheque');
let processed = 0; let processed = 0;
for (let i = 0; i < allData.length; i += 10) { for (let i = 0; i < allData.length; i += 10) {
@@ -435,7 +483,7 @@ function showImportModal(total, type) {
document.getElementById('import-modal-desc').textContent = `Traitement de ${total} élément(s)...`; document.getElementById('import-modal-desc').textContent = `Traitement de ${total} élément(s)...`;
document.getElementById('import-progress-bar').style.width = '0%'; document.getElementById('import-progress-bar').style.width = '0%';
document.getElementById('import-modal-counter').textContent = '0%'; document.getElementById('import-modal-counter').textContent = '0%';
document.getElementById('import-progress-modal').classList.add('open'); // ✅ 修复了 mo dal document.getElementById('import-progress-modal').classList.add('open');
} }
function updateImportModal(current, total) { function updateImportModal(current, total) {