Init PercoVitrine — app de gestion de fiches produits

This commit is contained in:
perco
2026-06-27 10:34:47 +02:00
commit a1b3f6da98
16 changed files with 1158 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
<?php
require __DIR__ . '/includes/db.php';
require __DIR__ . '/includes/helpers.php';
require __DIR__ . '/includes/layout.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['_action'] ?? '';
if ($action === 'add') {
$name = trim($_POST['name'] ?? '');
if (!$name) { $error = 'Le nom est obligatoire.'; }
else {
try { $pdo->prepare("INSERT INTO categories (name) VALUES(?)")->execute([$name]); }
catch (PDOException) { $error = 'Ce nom existe déjà.'; }
}
} elseif ($action === 'rename') {
$cid = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
if ($cid && $name) {
try { $pdo->prepare("UPDATE categories SET name=? WHERE id=?")->execute([$name,$cid]); }
catch (PDOException) { $error = 'Ce nom existe déjà.'; }
}
} elseif ($action === 'delete') {
$cid = (int)($_POST['id'] ?? 0);
if ($cid) {
$pdo->prepare("UPDATE products SET category_id=NULL WHERE category_id=?")->execute([$cid]);
$pdo->prepare("DELETE FROM categories WHERE id=?")->execute([$cid]);
}
}
if (!$error) { header('Location: /categories.php'); exit; }
}
$cats = $pdo->query("SELECT c.*, COUNT(p.id) as nb FROM categories c LEFT JOIN products p ON p.category_id=c.id GROUP BY c.id ORDER BY c.name")->fetchAll();
layout_head('Catégories');
layout_sidebar('categories');
layout_main_open();
?>
<div class="page-header">
<div class="page-title"><i class="fas fa-tags" style="color:var(--primary);margin-right:.4rem"></i>Catégories</div>
<button onclick="document.getElementById('addModal').style.display='flex'" class="btn btn-primary"><i class="fas fa-plus"></i> Ajouter</button>
</div>
<?php if ($error): ?><div style="background:rgba(239,68,68,.1);border:1px solid rgba(239,68,68,.3);padding:.75rem 1rem;border-radius:8px;margin-bottom:1rem;color:var(--red)"><?= h($error) ?></div><?php endif ?>
<?php if (!$cats): ?>
<div class="empty-state"><i class="fas fa-tags"></i><p>Aucune catégorie.<br>Crée-en une pour organiser tes produits.</p></div>
<?php else: ?>
<div class="card" style="padding:0">
<table class="table">
<thead><tr><th>Nom</th><th style="width:100px;text-align:center">Produits</th><th style="width:120px"></th></tr></thead>
<tbody>
<?php foreach ($cats as $c): ?>
<tr>
<td><strong><?= h($c['name']) ?></strong></td>
<td style="text-align:center"><span class="badge badge-muted"><?= $c['nb'] ?></span></td>
<td style="text-align:right">
<div style="display:flex;gap:.4rem;justify-content:flex-end">
<button onclick="openRename(<?= $c['id'] ?>,'<?= h(addslashes($c['name'])) ?>')" class="btn btn-secondary btn-sm" title="Renommer"><i class="fas fa-pen"></i></button>
<?php if ($c['nb'] == 0): ?>
<form method="post" style="display:inline" onsubmit="return confirm('Supprimer cette catégorie ?')">
<input type="hidden" name="_action" value="delete">
<input type="hidden" name="id" value="<?= $c['id'] ?>">
<button class="btn btn-danger btn-sm" title="Supprimer"><i class="fas fa-trash"></i></button>
</form>
<?php else: ?>
<button class="btn btn-danger btn-sm" disabled title="Catégorie utilisée" style="opacity:.35"><i class="fas fa-trash"></i></button>
<?php endif ?>
</div>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
<!-- Modal ajouter -->
<div id="addModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:999;align-items:center;justify-content:center">
<div class="card" style="width:360px">
<div style="font-weight:700;margin-bottom:1rem">Nouvelle catégorie</div>
<form method="post">
<input type="hidden" name="_action" value="add">
<div class="form-group"><label>Nom</label><input type="text" name="name" autofocus required></div>
<div style="display:flex;gap:.5rem">
<button type="submit" class="btn btn-primary" style="flex:1"><i class="fas fa-plus"></i> Ajouter</button>
<button type="button" onclick="document.getElementById('addModal').style.display='none'" class="btn btn-secondary">Annuler</button>
</div>
</form>
</div>
</div>
<!-- Modal renommer -->
<div id="renameModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:999;align-items:center;justify-content:center">
<div class="card" style="width:360px">
<div style="font-weight:700;margin-bottom:1rem">Renommer la catégorie</div>
<form method="post" id="renameForm">
<input type="hidden" name="_action" value="rename">
<input type="hidden" name="id" id="renameId">
<div class="form-group"><label>Nouveau nom</label><input type="text" name="name" id="renameName" required></div>
<div style="display:flex;gap:.5rem">
<button type="submit" class="btn btn-primary" style="flex:1"><i class="fas fa-check"></i> Enregistrer</button>
<button type="button" onclick="document.getElementById('renameModal').style.display='none'" class="btn btn-secondary">Annuler</button>
</div>
</form>
</div>
</div>
<script>
function openRename(id, name) {
document.getElementById('renameId').value = id;
document.getElementById('renameName').value = name;
document.getElementById('renameModal').style.display = 'flex';
setTimeout(()=>document.getElementById('renameName').focus(),50);
}
</script>
<?php layout_foot(); ?>