Portfolio_pero/admin/content.php
2026-09-03 13:27:06 +02:00

179 lines
7.7 KiB
PHP

<?php
/**
* BackOffice — Contenu & SEO
* Portfolio Tony AVERTY
*/
require_once __DIR__ . '/../includes/Security.php';
Security::bootstrap(true, ['no_store' => true]);
Security::requireAdmin();
require_once __DIR__ . '/../config/database.php';
$db = getDatabase();
$feedback = '';
$feedbackType = '';
// ── Sauvegarde ────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Security::verifyCsrf($_POST['csrf_token'] ?? '');
$saved = 0;
foreach ($_POST as $key => $value) {
if (str_starts_with($key, 'content_')) {
$contentKey = substr($key, 8); // retire "content_"
$stmt = $db->prepare(
'UPDATE site_content SET content_value = :val WHERE content_key = :key'
);
$stmt->execute([
'val' => trim($value),
'key' => $contentKey,
]);
$saved++;
}
}
$feedback = "{$saved} champ(s) enregistré(s) avec succès.";
$feedbackType = 'success';
}
// ── Chargement du contenu ─────────────────────────────────
$rows = $db->query('SELECT * FROM site_content ORDER BY section ASC, id ASC')->fetchAll();
$content = [];
$sections = [];
foreach ($rows as $row) {
$content[$row['content_key']] = $row;
$sections[$row['section']][] = $row;
}
$sectionLabels = [
'seo' => '🔍 SEO & Métadonnées',
'hero' => '🚀 Hero — Section principale',
'about' => '👤 À propos',
'contact' => '📬 Contact',
'footer' => '🔻 Footer',
'general' => '⚙️ Général',
];
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<title>Contenu & SEO — BackOffice Tony AVERTY</title>
<link rel="stylesheet" href="assets/admin.css">
</head>
<body>
<div class="admin-page">
<?php require __DIR__ . '/includes/nav.php'; ?>
<main class="admin-content">
<div class="admin-topbar">
<h1 class="admin-page-title">Contenu & SEO</h1>
<span class="admin-topbar-sub">Tous les textes visibles sur le site</span>
</div>
<?php if ($feedback): ?>
<div class="admin-alert admin-alert--<?= $feedbackType ?>"><?= htmlspecialchars($feedback, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<form method="post">
<?= Security::csrfInput() ?>
<!-- Tabs sections -->
<div class="content-tabs">
<?php foreach ($sectionLabels as $secKey => $secLabel): ?>
<?php if (!isset($sections[$secKey])) continue; ?>
<button type="button" class="content-tab <?= $secKey === 'seo' ? 'is-active' : '' ?>"
data-section="section-<?= $secKey ?>">
<?= $secLabel ?>
</button>
<?php endforeach; ?>
</div>
<?php foreach ($sectionLabels as $secKey => $secLabel): ?>
<?php if (!isset($sections[$secKey])) continue; ?>
<div id="section-<?= $secKey ?>" class="content-section <?= $secKey === 'seo' ? '' : 'is-hidden' ?>">
<div class="admin-form" style="margin-bottom:1.5rem;">
<h2 style="font-size:1rem;font-weight:700;margin-bottom:1.5rem;"><?= $secLabel ?></h2>
<?php foreach ($sections[$secKey] as $field): ?>
<div class="admin-form__group">
<label class="admin-form__label" for="field_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>">
<?= htmlspecialchars($field['label'], ENT_QUOTES, 'UTF-8') ?>
<span class="admin-form__hint">· <?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?></span>
</label>
<?php if ($field['content_type'] === 'textarea'): ?>
<textarea
id="field_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
name="content_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
class="admin-form__textarea"
rows="4"><?= htmlspecialchars($field['content_value'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
<?php elseif ($field['content_type'] === 'html'): ?>
<textarea
id="field_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
name="content_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
class="admin-form__textarea admin-form__textarea--code"
rows="6"><?= htmlspecialchars($field['content_value'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
<?php else: ?>
<input
type="text"
id="field_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
name="content_<?= htmlspecialchars($field['content_key'], ENT_QUOTES, 'UTF-8') ?>"
class="admin-form__input"
value="<?= htmlspecialchars($field['content_value'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
<?php endif; ?>
<?php if ($field['content_key'] === 'seo_description'): ?>
<p class="admin-form__counter" id="counter_seo_description">
<span id="count_seo_description"><?= mb_strlen($field['content_value'] ?? '') ?></span> / 160 caractères
</p>
<?php endif; ?>
<?php if ($field['content_key'] === 'seo_title'): ?>
<p class="admin-form__counter" id="counter_seo_title">
<span id="count_seo_title"><?= mb_strlen($field['content_value'] ?? '') ?></span> / 65 caractères
</p>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<div style="position:sticky;bottom:1.5rem;z-index:10;text-align:right;padding:0.5rem 0;">
<button type="submit" class="admin-btn admin-btn--primary" style="box-shadow:0 4px 20px rgba(99,102,241,0.4);">
💾 Enregistrer toutes les modifications
</button>
</div>
</form>
</main>
</div>
<script nonce="<?= Security::cspNonce() ?>">
// Tabs
document.querySelectorAll('.content-tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelectorAll('.content-tab').forEach(t => t.classList.remove('is-active'));
document.querySelectorAll('.content-section').forEach(s => s.classList.add('is-hidden'));
tab.classList.add('is-active');
document.getElementById(tab.dataset.section).classList.remove('is-hidden');
});
});
// Compteurs SEO
function attachCounter(fieldKey, maxLen) {
const input = document.querySelector(`[name="content_${fieldKey}"]`);
const span = document.getElementById(`count_${fieldKey}`);
if (!input || !span) return;
input.addEventListener('input', () => {
const len = input.value.length;
span.textContent = len;
span.style.color = len > maxLen ? '#ef4444' : '#8b8b9e';
});
}
attachCounter('seo_description', 160);
attachCounter('seo_title', 65);
</script>
</body>
</html>