feat: auto-refresh AJAX, bouton transfert entre comptes, webhook par compte
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
221
web/index.php
221
web/index.php
@@ -5,12 +5,10 @@ require_once __DIR__ . '/includes/layout.php';
|
||||
|
||||
auth_check();
|
||||
|
||||
$message = '';
|
||||
|
||||
// Suppression d'un email traité
|
||||
// Suppression
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_source'])) {
|
||||
$id = (int)$_POST['delete_source'];
|
||||
$st = db()->prepare('SELECT sf.title, CONCAT(sf.file_name) as fname, GROUP_CONCAT(f.path) as fpaths
|
||||
$st = db()->prepare('SELECT sf.title, GROUP_CONCAT(f.path) as fpaths
|
||||
FROM source_file sf LEFT JOIN files f ON sf.id = f.source_file_id
|
||||
WHERE sf.id = ? GROUP BY sf.id');
|
||||
$st->execute([$id]);
|
||||
@@ -22,21 +20,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_source'])) {
|
||||
}
|
||||
}
|
||||
db()->prepare('DELETE FROM source_file WHERE id = ?')->execute([$id]);
|
||||
flash('success', "Entrée « " . htmlspecialchars($row['title']) . " » supprimée.");
|
||||
flash('success', "Entrée supprimée.");
|
||||
}
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
|
||||
$rows = db()->query(
|
||||
'SELECT sf.id, sf.title, sf.sender, sf.date_processing, a.username,
|
||||
GROUP_CONCAT(f.id, ":", f.file_name, ":", f.path ORDER BY f.id SEPARATOR "|") as files
|
||||
FROM source_file sf
|
||||
LEFT JOIN accounts a ON sf.account_id = a.id
|
||||
LEFT JOIN files f ON sf.id = f.source_file_id
|
||||
GROUP BY sf.id
|
||||
ORDER BY sf.date_processing DESC'
|
||||
)->fetchAll();
|
||||
// Comptes pour le dropdown transfert
|
||||
$otherAccounts = db()->query('SELECT id, username, display_name FROM accounts WHERE active = 1 ORDER BY username')->fetchAll();
|
||||
|
||||
html_head('Courriers');
|
||||
html_navbar('mail');
|
||||
@@ -45,15 +36,13 @@ html_navbar('mail');
|
||||
<?php flash_render(); ?>
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h5 class="mb-0"><i class="bi bi-envelope-fill me-2"></i>Courriers traités</h5>
|
||||
<span class="badge bg-secondary"><?= count($rows) ?> entrée(s)</span>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<span id="mail-count" class="badge bg-secondary">—</span>
|
||||
<span class="text-muted small"><i class="bi bi-arrow-clockwise"></i> <span id="refresh-countdown">10</span>s</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (empty($rows)): ?>
|
||||
<div class="alert alert-info">
|
||||
<i class="bi bi-info-circle me-2"></i>Aucun courrier traité pour l'instant.
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="card shadow-sm">
|
||||
<div class="card shadow-sm" id="mail-card">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light">
|
||||
@@ -63,60 +52,158 @@ html_navbar('mail');
|
||||
<th>Sujet</th>
|
||||
<th>Expéditeur</th>
|
||||
<th>Pièces jointes</th>
|
||||
<th class="text-center">Action</th>
|
||||
<th class="text-center" style="width:110px">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($rows as $r): ?>
|
||||
<tr>
|
||||
<td class="text-nowrap text-muted small">
|
||||
<?= htmlspecialchars(date('d/m/Y H:i', strtotime($r['date_processing']))) ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php if ($r['username']): ?>
|
||||
<span class="badge bg-primary"><?= htmlspecialchars($r['username']) ?></span>
|
||||
<?php else: ?>
|
||||
<span class="text-muted">—</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?= htmlspecialchars($r['title'] ?? '(sans sujet)') ?></td>
|
||||
<td class="small text-muted"><?= htmlspecialchars($r['sender'] ?? '') ?></td>
|
||||
<td>
|
||||
<?php if ($r['files']): ?>
|
||||
<?php foreach (explode('|', $r['files']) as $f): ?>
|
||||
<?php [$fid, $fname, $fpath] = array_pad(explode(':', $f, 3), 3, ''); ?>
|
||||
<?php if (file_exists($fpath)): ?>
|
||||
<a href="/mail/download.php?id=<?= (int)$fid ?>" class="badge bg-info text-decoration-none me-1">
|
||||
<i class="bi bi-download me-1"></i><?= htmlspecialchars(substr($fname, 0, 20)) ?>
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<span class="badge bg-secondary me-1" title="Fichier introuvable">
|
||||
<?= htmlspecialchars(substr($fname, 0, 20)) ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<span class="text-muted small">Aucune pièce jointe</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<form method="post" onsubmit="return confirm('Supprimer cette entrée ?')">
|
||||
<input type="hidden" name="delete_source" value="<?= $r['id'] ?>">
|
||||
<button class="btn btn-sm btn-outline-danger">
|
||||
<i class="bi bi-trash"></i>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<tbody id="mail-tbody">
|
||||
<tr><td colspan="6" class="text-center text-muted py-4">Chargement…</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div id="empty-msg" class="alert alert-info mt-3" style="display:none">
|
||||
<i class="bi bi-info-circle me-2"></i>Aucun courrier traité pour l'instant.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Toast -->
|
||||
<div class="position-fixed bottom-0 end-0 p-3" style="z-index:9999">
|
||||
<div id="toast-el" class="toast align-items-center border-0" role="alert">
|
||||
<div class="d-flex">
|
||||
<div class="toast-body" id="toast-body"></div>
|
||||
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast"></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
setTimeout(() => location.reload(), 30000);
|
||||
const ALL_ACCOUNTS = <?= json_encode($otherAccounts) ?>;
|
||||
let knownIds = null;
|
||||
let countdown = 10;
|
||||
let countdownTimer = null;
|
||||
|
||||
function showToast(msg, type = 'success') {
|
||||
const el = document.getElementById('toast-el');
|
||||
el.className = `toast align-items-center text-bg-${type} border-0`;
|
||||
document.getElementById('toast-body').textContent = msg;
|
||||
bootstrap.Toast.getOrCreateInstance(el, {delay: 4000}).show();
|
||||
}
|
||||
|
||||
function buildTransferDropdown(sourceId, currentUsername) {
|
||||
const others = ALL_ACCOUNTS.filter(a => a.username !== currentUsername);
|
||||
if (!others.length) return '';
|
||||
const items = others.map(a =>
|
||||
`<li><a class="dropdown-item" href="#" onclick="doTransfer(event,${sourceId},${a.id},'${a.username}')">
|
||||
<i class="bi bi-arrow-right-circle me-1 text-primary"></i>${a.username}${a.display_name ? ' <span class=text-muted>— '+a.display_name+'</span>' : ''}
|
||||
</a></li>`
|
||||
).join('');
|
||||
return `<div class="btn-group">
|
||||
<button class="btn btn-sm btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" title="Transférer vers…">
|
||||
<i class="bi bi-share"></i>
|
||||
</button>
|
||||
<ul class="dropdown-menu dropdown-menu-end shadow">${items}</ul>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function doTransfer(e, sourceId, targetId, targetName) {
|
||||
e.preventDefault();
|
||||
const fd = new FormData();
|
||||
fd.append('source_id', sourceId);
|
||||
fd.append('target_id', targetId);
|
||||
fetch('/api/transfer.php', {method:'POST', body: fd})
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
showToast(`Transféré vers ${data.target} (${data.moved} fichier(s))`);
|
||||
loadMails();
|
||||
} else {
|
||||
showToast(data.error || 'Erreur lors du transfert', 'danger');
|
||||
}
|
||||
})
|
||||
.catch(() => showToast('Erreur réseau', 'danger'));
|
||||
}
|
||||
|
||||
function renderMails(rows) {
|
||||
const tbody = document.getElementById('mail-tbody');
|
||||
const card = document.getElementById('mail-card');
|
||||
const empty = document.getElementById('empty-msg');
|
||||
document.getElementById('mail-count').textContent = rows.length + ' entrée(s)';
|
||||
|
||||
// Détecte nouveaux mails
|
||||
const newIds = new Set(rows.map(r => r.id));
|
||||
if (knownIds !== null && [...newIds].some(id => !knownIds.has(id))) {
|
||||
showToast('Nouveau courrier reçu !');
|
||||
}
|
||||
knownIds = newIds;
|
||||
|
||||
if (!rows.length) {
|
||||
tbody.innerHTML = '';
|
||||
card.style.display = 'none';
|
||||
empty.style.display = '';
|
||||
return;
|
||||
}
|
||||
card.style.display = '';
|
||||
empty.style.display = 'none';
|
||||
|
||||
tbody.innerHTML = rows.map(r => {
|
||||
const date = new Date(r.date_processing).toLocaleString('fr-FR', {
|
||||
day:'2-digit', month:'2-digit', year:'numeric', hour:'2-digit', minute:'2-digit'
|
||||
});
|
||||
let filesHtml = '<span class="text-muted small">Aucune pièce jointe</span>';
|
||||
if (r.files_list && r.files_list.length) {
|
||||
filesHtml = r.files_list.map(f =>
|
||||
f.exists
|
||||
? `<a href="/mail/download.php?id=${f.id}" class="badge bg-info text-decoration-none me-1"><i class="bi bi-download me-1"></i>${escHtml(f.name.substring(0,22))}</a>`
|
||||
: `<span class="badge bg-secondary me-1" title="Fichier introuvable">${escHtml(f.name.substring(0,22))}</span>`
|
||||
).join('');
|
||||
}
|
||||
const transferBtn = buildTransferDropdown(r.id, r.username);
|
||||
return `<tr id="row-${r.id}">
|
||||
<td class="text-nowrap text-muted small">${date}</td>
|
||||
<td>${r.username ? `<span class="badge bg-primary">${escHtml(r.username)}</span>` : '<span class="text-muted">—</span>'}</td>
|
||||
<td>${escHtml(r.title || '(sans sujet)')}</td>
|
||||
<td class="small text-muted">${escHtml(r.sender || '')}</td>
|
||||
<td>${filesHtml}</td>
|
||||
<td class="text-center">
|
||||
<div class="d-flex gap-1 justify-content-center">
|
||||
${transferBtn}
|
||||
<form method="post" class="d-inline" onsubmit="return confirm('Supprimer cette entrée ?')">
|
||||
<input type="hidden" name="delete_source" value="${r.id}">
|
||||
<button class="btn btn-sm btn-outline-danger" title="Supprimer"><i class="bi bi-trash"></i></button>
|
||||
</form>
|
||||
</div>
|
||||
</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function escHtml(s) {
|
||||
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
||||
}
|
||||
|
||||
function loadMails() {
|
||||
fetch('/api/mails.php')
|
||||
.then(r => r.json())
|
||||
.then(renderMails)
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
function startCountdown() {
|
||||
if (countdownTimer) clearInterval(countdownTimer);
|
||||
countdown = 10;
|
||||
const el = document.getElementById('refresh-countdown');
|
||||
countdownTimer = setInterval(() => {
|
||||
countdown--;
|
||||
if (el) el.textContent = countdown;
|
||||
if (countdown <= 0) {
|
||||
clearInterval(countdownTimer);
|
||||
loadMails();
|
||||
startCountdown();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
loadMails();
|
||||
startCountdown();
|
||||
</script>
|
||||
<?php html_foot(); ?>
|
||||
|
||||
Reference in New Issue
Block a user