418 lines
17 KiB
PHP
418 lines
17 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
require_once(__DIR__ . '/class/mailer.php');
|
|
require_once(__DIR__ . '/class/NewsletterRenderer.php');
|
|
require_once(__DIR__ . '/class/NewsletterWp.php');
|
|
|
|
/**
|
|
* Elenco delle newsletter scritte in YogiBook.
|
|
* NB: protezione "solo staff" da aggiungere come per le altre pagine admin.
|
|
*/
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
function e($value): string
|
|
{
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
$drafts = $pdo->query("
|
|
SELECT d.iddraft, d.title, d.subject, d.updated_at, d.wp_email_id, d.wp_pushed_at,
|
|
TRIM(CONCAT(COALESCE(u.first_name, ''), ' ', COALESCE(u.last_name, ''))) AS author
|
|
FROM newsletter_drafts d
|
|
LEFT JOIN auth_users u ON u.id = d.updated_by
|
|
WHERE d.deleted_at IS NULL
|
|
ORDER BY d.updated_at DESC
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Stato in The Newsletter (una sola query)
|
|
$wpStatus = [];
|
|
$wpError = null;
|
|
if (NewsletterWp::isConfigured()) {
|
|
try {
|
|
$wpStatus = NewsletterWp::statuses(array_column($drafts, 'wp_email_id'));
|
|
} catch (Throwable $e) {
|
|
$wpError = $e->getMessage();
|
|
}
|
|
} else {
|
|
$wpError = 'Collegamento a WordPress non ancora configurato: puoi scrivere le newsletter, ma non pubblicarle.';
|
|
}
|
|
|
|
$statusLabel = [
|
|
'new' => ['Bozza in The Newsletter', 'st-new'],
|
|
'sending' => ['In invio', 'st-sending'],
|
|
'paused' => ['Invio in pausa', 'st-sending'],
|
|
'sent' => ['Inviata', 'st-sent'],
|
|
'error' => ['Errore di invio', 'st-error'],
|
|
'deleted' => ['Eliminata in WordPress', 'st-error'],
|
|
];
|
|
?>
|
|
<!doctype html>
|
|
<html lang="it">
|
|
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>YogiBook - Newsletter</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="shortcut icon" href="assets/images/favicon.ico">
|
|
<link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
|
|
|
<style>
|
|
.admin-card {
|
|
border: none;
|
|
border-radius: 14px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
|
|
}
|
|
|
|
.admin-card .card-body {
|
|
padding: 26px 30px;
|
|
}
|
|
|
|
.section-title {
|
|
font-size: 15px;
|
|
font-weight: 700;
|
|
letter-spacing: .04em;
|
|
text-transform: uppercase;
|
|
color: #6b7280;
|
|
margin-bottom: 4px;
|
|
}
|
|
|
|
.section-lead {
|
|
color: #9ca3af;
|
|
font-size: 14px;
|
|
margin-bottom: 22px;
|
|
}
|
|
|
|
.nl-table thead th {
|
|
font-size: 12px;
|
|
text-transform: uppercase;
|
|
letter-spacing: .03em;
|
|
color: #6b7280;
|
|
}
|
|
|
|
.nl-table td {
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.nl-title {
|
|
font-weight: 600;
|
|
color: #1f2937;
|
|
}
|
|
|
|
.nl-subject {
|
|
display: block;
|
|
font-size: 13px;
|
|
color: #6b7280;
|
|
}
|
|
|
|
.st {
|
|
display: inline-block;
|
|
border-radius: 20px;
|
|
padding: 3px 10px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.st-none {
|
|
background: #eef0f3;
|
|
color: #4b5563;
|
|
}
|
|
|
|
.st-new {
|
|
background: #c4e1fb;
|
|
color: #1a4f7a;
|
|
}
|
|
|
|
.st-sending {
|
|
background: #fbe4c4;
|
|
color: #8a5a1a;
|
|
}
|
|
|
|
.st-sent {
|
|
background: #cdfbc4;
|
|
color: #1a7f2e;
|
|
}
|
|
|
|
.st-error {
|
|
background: #fbc7c4;
|
|
color: #a12820;
|
|
}
|
|
|
|
.wp-list {
|
|
max-height: 380px;
|
|
overflow-y: auto;
|
|
text-align: left;
|
|
}
|
|
|
|
.wp-item {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 10px;
|
|
padding: 9px 4px;
|
|
border-bottom: 1px solid #f1f2f4;
|
|
}
|
|
|
|
.wp-item small {
|
|
color: #9ca3af;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div id="layout-wrapper">
|
|
<header id="page-topbar" class="isvertical-topbar">
|
|
<div class="navbar-header">
|
|
<div class="d-flex">
|
|
<?php include('include/logoarea.php'); ?>
|
|
<button type="button" class="btn btn-sm px-3 font-size-24 header-item waves-effect vertical-menu-btn">
|
|
<i class="bx bx-menu align-middle"></i>
|
|
</button>
|
|
<div class="page-title-box align-self-center d-none d-md-block">
|
|
<h4 class="page-title mb-0">Newsletter</h4>
|
|
</div>
|
|
</div>
|
|
<div class="d-flex">
|
|
<?php include('include/languageselection.php'); ?>
|
|
<?php include('include/profiletopbar.php'); ?>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<?php include('include/sidebar.php'); ?>
|
|
<header class="ishorizontal-topbar">
|
|
<div class="navbar-header">
|
|
<div class="d-flex"></div>
|
|
</div>
|
|
<div class="topnav">
|
|
<div class="container-fluid">
|
|
<nav class="navbar navbar-light navbar-expand-lg topnav-menu"></nav>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="main-content">
|
|
<div class="page-content">
|
|
<div class="container-fluid">
|
|
<?php if ($wpError) : ?>
|
|
<div class="alert alert-warning"><i class="fas fa-plug me-1"></i> <?php echo e($wpError); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card admin-card">
|
|
<div class="card-body">
|
|
<div class="d-flex justify-content-between align-items-start flex-wrap gap-2">
|
|
<div>
|
|
<div class="section-title">Newsletter</div>
|
|
<p class="section-lead mb-3">Scrivi qui, con salvataggio automatico. Poi pubblica su The Newsletter e invia da WordPress come sempre.</p>
|
|
</div>
|
|
<div class="d-flex gap-2">
|
|
<button class="btn btn-outline-secondary" id="btnImport" <?php echo NewsletterWp::isConfigured() ? '' : 'disabled'; ?>>
|
|
<i class="fas fa-file-import me-1"></i> Importa da The Newsletter
|
|
</button>
|
|
<button class="btn btn-primary" id="btnNew"><i class="fas fa-plus me-1"></i> Nuova newsletter</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table nl-table mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Newsletter</th>
|
|
<th>Ultima modifica</th>
|
|
<th>The Newsletter</th>
|
|
<th class="text-end">Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (!$drafts) : ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center text-muted py-4">Nessuna newsletter. Creane una nuova o importane una da The Newsletter.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($drafts as $d) :
|
|
$wpId = $d['wp_email_id'] !== null ? (int) $d['wp_email_id'] : null;
|
|
$st = $wpId ? ($wpStatus[$wpId] ?? null) : null;
|
|
[$stText, $stClass] = $wpId
|
|
? ($statusLabel[$st ?? ''] ?? ['Pubblicata (n. ' . $wpId . ')', 'st-new'])
|
|
: ['Non pubblicata', 'st-none'];
|
|
?>
|
|
<tr>
|
|
<td>
|
|
<a href="newsletter_editor.php?id=<?php echo (int) $d['iddraft']; ?>" class="nl-title"><?php echo e($d['title'] ?: '(senza titolo)'); ?></a>
|
|
<span class="nl-subject"><?php echo e($d['subject'] ?: 'Oggetto non ancora scritto'); ?></span>
|
|
</td>
|
|
<td>
|
|
<?php echo e(date('d/m/Y H:i', strtotime($d['updated_at']))); ?>
|
|
<?php if ($d['author']) : ?><span class="nl-subject"><?php echo e($d['author']); ?></span><?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<span class="st <?php echo $stClass; ?>"><?php echo e($stText); ?></span>
|
|
<?php if ($wpId) : ?><span class="nl-subject">n. <?php echo $wpId; ?></span><?php endif; ?>
|
|
</td>
|
|
<td class="text-end text-nowrap">
|
|
<a href="newsletter_editor.php?id=<?php echo (int) $d['iddraft']; ?>" class="btn btn-sm btn-primary"><i class="fas fa-pen"></i> Apri</a>
|
|
<button class="btn btn-sm btn-outline-secondary js-dup" data-id="<?php echo (int) $d['iddraft']; ?>" title="Duplica"><i class="fas fa-copy"></i></button>
|
|
<button class="btn btn-sm btn-outline-danger js-del" data-id="<?php echo (int) $d['iddraft']; ?>" data-title="<?php echo e($d['title']); ?>" title="Elimina"><i class="fas fa-trash"></i></button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/libs/metismenujs/metismenujs.min.js"></script>
|
|
<script src="assets/libs/simplebar/simplebar.min.js"></script>
|
|
<script src="assets/libs/eva-icons/eva.min.js"></script>
|
|
<script src="assets/js/app.js"></script>
|
|
|
|
<script>
|
|
const API = 'newsletter_api.php';
|
|
|
|
async function api(action, data) {
|
|
const body = new FormData();
|
|
body.append('action', action);
|
|
Object.entries(data || {}).forEach(([k, v]) => body.append(k, v));
|
|
const r = await fetch(API, {
|
|
method: 'POST',
|
|
body,
|
|
credentials: 'same-origin'
|
|
});
|
|
let j = null;
|
|
try {
|
|
j = await r.json();
|
|
} catch (e) {}
|
|
if (!j || !j.success) throw new Error((j && j.message) || 'Errore del server.');
|
|
return j;
|
|
}
|
|
|
|
function esc(s) {
|
|
return String(s ?? '').replace(/[&<>"']/g, c => ({
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": '''
|
|
} [c]));
|
|
}
|
|
|
|
document.getElementById('btnNew').addEventListener('click', async () => {
|
|
const r = await Swal.fire({
|
|
title: 'Nuova newsletter',
|
|
input: 'text',
|
|
inputLabel: 'Nome interno (lo vedi solo tu)',
|
|
inputValue: 'Newsletter del ' + new Date().toLocaleDateString('it-IT'),
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Crea',
|
|
cancelButtonText: 'Annulla'
|
|
});
|
|
if (!r.isConfirmed) return;
|
|
try {
|
|
const j = await api('create', {
|
|
title: r.value
|
|
});
|
|
location.href = 'newsletter_editor.php?id=' + j.id;
|
|
} catch (e) {
|
|
Swal.fire('Errore', e.message, 'error');
|
|
}
|
|
});
|
|
|
|
document.querySelectorAll('.js-dup').forEach(b => b.addEventListener('click', async () => {
|
|
try {
|
|
const j = await api('duplicate', {
|
|
id: b.dataset.id
|
|
});
|
|
location.href = 'newsletter_editor.php?id=' + j.id;
|
|
} catch (e) {
|
|
Swal.fire('Errore', e.message, 'error');
|
|
}
|
|
}));
|
|
|
|
document.querySelectorAll('.js-del').forEach(b => b.addEventListener('click', async () => {
|
|
const r = await Swal.fire({
|
|
title: 'Eliminare la newsletter?',
|
|
html: '<strong>' + esc(b.dataset.title) + '</strong><br><small>Quella eventualmente pubblicata in The Newsletter non viene toccata.</small>',
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonColor: '#d33',
|
|
confirmButtonText: 'Elimina',
|
|
cancelButtonText: 'Annulla'
|
|
});
|
|
if (!r.isConfirmed) return;
|
|
try {
|
|
await api('delete', {
|
|
id: b.dataset.id
|
|
});
|
|
location.reload();
|
|
} catch (e) {
|
|
Swal.fire('Errore', e.message, 'error');
|
|
}
|
|
}));
|
|
|
|
document.getElementById('btnImport').addEventListener('click', async () => {
|
|
Swal.fire({
|
|
title: 'Carico le newsletter…',
|
|
didOpen: () => Swal.showLoading()
|
|
});
|
|
let list;
|
|
try {
|
|
const r = await fetch(API + '?action=list_wp', {
|
|
credentials: 'same-origin'
|
|
});
|
|
const j = await r.json();
|
|
if (!j.success) throw new Error(j.message);
|
|
list = j.emails;
|
|
} catch (e) {
|
|
return Swal.fire('Errore', e.message || 'Impossibile leggere The Newsletter.', 'error');
|
|
}
|
|
|
|
const labels = {
|
|
new: 'bozza',
|
|
sending: 'in invio',
|
|
paused: 'in pausa',
|
|
sent: 'inviata',
|
|
error: 'errore'
|
|
};
|
|
const html = '<div class="wp-list">' + list.map(m =>
|
|
'<div class="wp-item"><div><strong>' + esc(m.subject || '(senza oggetto)') + '</strong><br>' +
|
|
'<small>n. ' + m.id + ' · ' + esc(labels[m.status] || m.status) + ' · ' + esc((m.created || '').slice(0, 10)) + '</small></div>' +
|
|
'<button class="btn btn-sm btn-primary js-imp" data-id="' + m.id + '">Importa</button></div>'
|
|
).join('') + '</div>' +
|
|
'<p class="mt-3 mb-0" style="font-size:13px;color:#6b7280;">Le bozze non ancora inviate restano collegate: "Pubblica" aggiornerà quella stessa newsletter.</p>';
|
|
|
|
Swal.fire({
|
|
title: 'Importa da The Newsletter',
|
|
html,
|
|
width: 640,
|
|
showConfirmButton: false,
|
|
showCloseButton: true,
|
|
didOpen: (el) => {
|
|
el.querySelectorAll('.js-imp').forEach(b => b.addEventListener('click', async () => {
|
|
b.disabled = true;
|
|
b.textContent = '…';
|
|
try {
|
|
const j = await api('import_wp', {
|
|
wp_id: b.dataset.id
|
|
});
|
|
location.href = 'newsletter_editor.php?id=' + j.id;
|
|
} catch (e) {
|
|
Swal.fire('Errore', e.message, 'error');
|
|
}
|
|
}));
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|