mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
migrate apropos data from config/apropos.php to SQLite
- Create apropos_contents table via migration 010 - Add Database methods: getAproposContent(), saveAproposContent(), getAllAproposContents() - Replace admin/pages.php with admin/contenus.php (renamed header from 'Pages statiques' to 'Contenus') - Replace admin/pages-edit.php with admin/contenus-edit.php (support editing pages + apropos contents) - Create admin/actions/apropos.php for saving apropos data (contacts, credits, erg_url) - Update public/apropos.php to read contacts/credits/erg_url from DB - Delete config/apropos.php
This commit is contained in:
232
public/admin/contenus-edit.php
Normal file
232
public/admin/contenus-edit.php
Normal file
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
require_once __DIR__ . "/../../config/bootstrap.php";
|
||||
require_once __DIR__ . '/../../src/AdminAuth.php';
|
||||
AdminAuth::requireLogin();
|
||||
|
||||
require_once __DIR__ . '/../../src/Database.php';
|
||||
|
||||
if (empty($_SESSION["csrf_token"])) {
|
||||
$_SESSION["csrf_token"] = bin2hex(random_bytes(32));
|
||||
}
|
||||
|
||||
$allowedPageSlugs = ["about", "licenses", "charte"];
|
||||
$allowedApropos = ["contacts", "credits", "erg_url"];
|
||||
|
||||
$pageSlug = $_GET["slug"] ?? "";
|
||||
$aproposKey = $_GET["apropos"] ?? "";
|
||||
|
||||
// Exactly one target must be specified
|
||||
if ($pageSlug && !in_array($pageSlug, $allowedPageSlugs)) {
|
||||
$pageSlug = "";
|
||||
}
|
||||
if ($aproposKey && !in_array($aproposKey, $allowedApropos)) {
|
||||
$aproposKey = "";
|
||||
}
|
||||
|
||||
if (!$pageSlug && !$aproposKey) {
|
||||
header("Location: /admin/contenus.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
try {
|
||||
$db = new Database();
|
||||
|
||||
if ($pageSlug) {
|
||||
$page = $db->getPage($pageSlug);
|
||||
if (!$page) {
|
||||
die("Page introuvable.");
|
||||
}
|
||||
$editTitle = $page["title"];
|
||||
$editType = "page";
|
||||
} else {
|
||||
$editType = "apropos";
|
||||
$value = $db->getAproposContent($aproposKey);
|
||||
$editTitle = match($aproposKey) {
|
||||
'contacts' => 'Contacts',
|
||||
'credits' => 'Crédits',
|
||||
'erg_url' => 'URL de l\'ERG',
|
||||
};
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
die("Erreur: " . htmlspecialchars($e->getMessage()));
|
||||
}
|
||||
|
||||
$pageTitle = "Éditer : " . $editTitle;
|
||||
$extraJs = ["/assets/js/overtype.min.js"];
|
||||
$extraJsInline = <<<'JS'
|
||||
var OT = window.OverType.default || window.OverType;
|
||||
var hidden = document.getElementById('content');
|
||||
var editor = new OT(document.getElementById('editor'), {
|
||||
value: hidden.value,
|
||||
minHeight: '400px',
|
||||
spellcheck: false,
|
||||
onChange: function(value) { hidden.value = value; }
|
||||
});
|
||||
JS;
|
||||
$aproposEditorJs = null;
|
||||
if ($editType === 'apropos' && in_array($aproposKey, ['contacts', 'credits'])) {
|
||||
// Rich textarea for JSON arrays rendered as structured form
|
||||
$aproposEditorJs = <<<'JS'
|
||||
// Auto-format JSON in the hidden field for display purposes
|
||||
JS;
|
||||
}
|
||||
|
||||
$initialContent = '';
|
||||
if ($editType === 'page') {
|
||||
$initialContent = $page["content"] ?? "";
|
||||
} else {
|
||||
// For apropos, show structured form
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
$isAdmin = true;
|
||||
$bodyClass = "admin-body";
|
||||
require_once APP_ROOT . "/templates/head.php";
|
||||
?>
|
||||
<?php include APP_ROOT . "/templates/header.php"; ?>
|
||||
|
||||
<main id="main-content">
|
||||
<h1>Éditer : <?= htmlspecialchars($editTitle) ?></h1>
|
||||
|
||||
<?php if ($editType === 'page'): ?>
|
||||
<form action="/admin/actions/page.php" method="post" class="admin-form">
|
||||
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION["csrf_token"]) ?>">
|
||||
<input type="hidden" name="slug" value="<?= htmlspecialchars($pageSlug) ?>">
|
||||
|
||||
<label for="editor">Contenu (Markdown) :</label>
|
||||
<input type="hidden" id="content" name="content"
|
||||
value="<?= htmlspecialchars($initialContent) ?>">
|
||||
<div id="editor"></div>
|
||||
|
||||
<div class="admin-form-footer">
|
||||
<button type="submit" class="admin-btn">Enregistrer</button>
|
||||
<a href="/admin/contenus.php" class="admin-btn-secondary admin-cancel-link">Annuler</a>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php elseif ($aproposKey === 'erg_url'): ?>
|
||||
<form action="/admin/actions/apropos.php" method="post" class="admin-form">
|
||||
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION["csrf_token"]) ?>">
|
||||
<input type="hidden" name="apropos_key" value="erg_url">
|
||||
|
||||
<label for="erg_url">URL du site de l'ERG :</label>
|
||||
<input type="url" id="erg_url" name="value"
|
||||
value="<?= htmlspecialchars($value) ?>" style="width:100%;max-width:600px;">
|
||||
|
||||
<div class="admin-form-footer">
|
||||
<button type="submit" class="admin-btn">Enregistrer</button>
|
||||
<a href="/admin/contenus.php" class="admin-btn-secondary admin-cancel-link">Annuler</a>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php elseif (in_array($aproposKey, ['contacts', 'credits'])): ?>
|
||||
<?php
|
||||
$items = is_array($value) ? $value : [];
|
||||
?>
|
||||
<form action="/admin/actions/apropos.php" method="post" class="admin-form" id="apropos-form">
|
||||
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION["csrf_token"]) ?>">
|
||||
<input type="hidden" name="apropos_key" value="<?= htmlspecialchars($aproposKey) ?>">
|
||||
|
||||
<?php if ($aproposKey === 'contacts'): ?>
|
||||
<?php foreach ($items as $i => $item): ?>
|
||||
<fieldset class="apropos-item">
|
||||
<legend>Contact <?= $i + 1 ?></legend>
|
||||
<label for="contact_<?= $i ?>_name">Nom :</label>
|
||||
<input type="text" id="contact_<?= $i ?>_name"
|
||||
name="items[<?= $i ?>][name]"
|
||||
value="<?= htmlspecialchars($item['name'] ?? '') ?>" required>
|
||||
|
||||
<label for="contact_<?= $i ?>_role">Rôle :</label>
|
||||
<input type="text" id="contact_<?= $i ?>_role"
|
||||
name="items[<?= $i ?>][role]"
|
||||
value="<?= htmlspecialchars($item['role'] ?? '') ?>">
|
||||
|
||||
<label for="contact_<?= $i ?>_email">Email :</label>
|
||||
<input type="email" id="contact_<?= $i ?>_email"
|
||||
name="items[<?= $i ?>][email]"
|
||||
value="<?= htmlspecialchars($item['email'] ?? '') ?>">
|
||||
|
||||
<label for="contact_<?= $i ?>_url">Lien (optionnel) :</label>
|
||||
<input type="url" id="contact_<?= $i ?>_url"
|
||||
name="items[<?= $i ?>][url]"
|
||||
value="<?= htmlspecialchars($item['url'] ?? '') ?>">
|
||||
</fieldset>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php else: ?>
|
||||
<?php foreach ($items as $i => $item): ?>
|
||||
<fieldset class="apropos-item">
|
||||
<legend>Crédit <?= $i + 1 ?></legend>
|
||||
<label for="credit_<?= $i ?>_label">Label :</label>
|
||||
<input type="text" id="credit_<?= $i ?>_label"
|
||||
name="items[<?= $i ?>][label]"
|
||||
value="<?= htmlspecialchars($item['label'] ?? '') ?>">
|
||||
|
||||
<label for="credit_<?= $i ?>_value">Valeur :</label>
|
||||
<input type="text" id="credit_<?= $i ?>_value"
|
||||
name="items[<?= $i ?>][value]"
|
||||
value="<?= htmlspecialchars($item['value'] ?? '') ?>">
|
||||
|
||||
<label for="credit_<?= $i ?>_url">Lien (optionnel) :</label>
|
||||
<input type="url" id="credit_<?= $i ?>_url"
|
||||
name="items[<?= $i ?>][url]"
|
||||
value="<?= htmlspecialchars($item['url'] ?? '') ?>">
|
||||
</fieldset>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
|
||||
<button type="button" class="admin-btn" id="add-item-btn" style="width:auto;">+ Ajouter un élément</button>
|
||||
|
||||
<div class="admin-form-footer">
|
||||
<button type="submit" class="admin-btn">Enregistrer</button>
|
||||
<a href="/admin/contenus.php" class="admin-btn-secondary admin-cancel-link">Annuler</a>
|
||||
</div>
|
||||
|
||||
<template id="row-template-<?= $aproposKey ?>">
|
||||
<?php if ($aproposKey === 'contacts'): ?>
|
||||
<fieldset class="apropos-item">
|
||||
<legend>Contact {{index}}</legend>
|
||||
<label for="contact_{{index}}_name">Nom :</label>
|
||||
<input type="text" id="contact_{{index}}_name"
|
||||
name="items[{{index}}][name]" required>
|
||||
<label for="contact_{{index}}_role">Rôle :</label>
|
||||
<input type="text" id="contact_{{index}}_role"
|
||||
name="items[{{index}}][role]">
|
||||
<label for="contact_{{index}}_email">Email :</label>
|
||||
<input type="email" id="contact_{{index}}_email"
|
||||
name="items[{{index}}][email]">
|
||||
<label for="contact_{{index}}_url">Lien (optionnel) :</label>
|
||||
<input type="url" id="contact_{{index}}_url"
|
||||
name="items[{{index}}][url]">
|
||||
</fieldset>
|
||||
<?php else: ?>
|
||||
<fieldset class="apropos-item">
|
||||
<legend>Crédit {{index}}</legend>
|
||||
<label for="credit_{{index}}_label">Label :</label>
|
||||
<input type="text" id="credit_{{index}}_label"
|
||||
name="items[{{index}}][label]">
|
||||
<label for="credit_{{index}}_value">Valeur :</label>
|
||||
<input type="text" id="credit_{{index}}_value"
|
||||
name="items[{{index}}][value]">
|
||||
<label for="credit_{{index}}_url">Lien (optionnel) :</label>
|
||||
<input type="url" id="credit_{{index}}_url"
|
||||
name="items[{{index}}][url]">
|
||||
</fieldset>
|
||||
<?php endif; ?>
|
||||
</template>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
let count = <?= count($items) ?>;
|
||||
const tpl = document.getElementById('row-template-<?= $aproposKey ?>').innerHTML;
|
||||
document.getElementById('add-item-btn').addEventListener('click', function() {
|
||||
count++;
|
||||
const html = tpl.replaceAll('{{index}}', count);
|
||||
this.insertAdjacentHTML('beforebegin', html);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
<?php endif; ?>
|
||||
</main>
|
||||
<?php require_once APP_ROOT . "/templates/admin/footer.php"; ?>
|
||||
Reference in New Issue
Block a user