269 lines
12 KiB
PHP
269 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* BackOffice - ajout et edition de projet.
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/Security.php';
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
Security::bootstrap(true, ['no_store' => true]);
|
|
Security::requireAdmin();
|
|
|
|
$db = getDatabase();
|
|
$error = '';
|
|
$isEdit = false;
|
|
$project = [
|
|
'id' => '',
|
|
'title' => '',
|
|
'slug' => '',
|
|
'short_description' => '',
|
|
'full_description' => '',
|
|
'image' => '',
|
|
'status' => 'published',
|
|
'badge' => 'development',
|
|
'technologies' => '',
|
|
'project_url' => '',
|
|
'display_order' => 0,
|
|
];
|
|
|
|
if (!empty($_GET['id'])) {
|
|
$stmt = $db->prepare('SELECT * FROM projects WHERE id = :id LIMIT 1');
|
|
$stmt->execute(['id' => (int) $_GET['id']]);
|
|
$existing = $stmt->fetch();
|
|
|
|
if (!$existing) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
$project = $existing;
|
|
$isEdit = true;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
Security::verifyCsrf($_POST['csrf_token'] ?? '');
|
|
|
|
$project['title'] = trim((string) ($_POST['title'] ?? ''));
|
|
$project['slug'] = trim((string) ($_POST['slug'] ?? ''));
|
|
$project['short_description'] = trim((string) ($_POST['short_description'] ?? ''));
|
|
$project['full_description'] = trim((string) ($_POST['full_description'] ?? ''));
|
|
$project['status'] = in_array($_POST['status'] ?? '', ['draft', 'published'], true) ? $_POST['status'] : 'draft';
|
|
$project['badge'] = in_array($_POST['badge'] ?? '', ['development', 'production'], true) ? $_POST['badge'] : 'development';
|
|
$project['technologies'] = trim((string) ($_POST['technologies'] ?? ''));
|
|
$project['project_url'] = trim((string) ($_POST['project_url'] ?? ''));
|
|
$project['display_order'] = max(0, (int) ($_POST['display_order'] ?? 0));
|
|
|
|
$project['slug'] = Security::slugify($project['slug'] !== '' ? $project['slug'] : $project['title']);
|
|
|
|
if ($project['title'] === '') {
|
|
$error = 'Le titre est requis.';
|
|
} elseif ($project['short_description'] === '') {
|
|
$error = 'La description courte est requise.';
|
|
} elseif ($project['project_url'] !== '' && !preg_match('#^https://#i', $project['project_url'])) {
|
|
$error = 'Le lien du projet doit commencer par https://';
|
|
}
|
|
|
|
$imageFilename = (string) ($project['image'] ?? '');
|
|
if ($error === '' && !empty($_FILES['image_file']['name']) && (int) $_FILES['image_file']['error'] === UPLOAD_ERR_OK) {
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
$mime = $finfo->file($_FILES['image_file']['tmp_name']) ?: '';
|
|
$allowed = [
|
|
'image/jpeg' => 'jpg',
|
|
'image/png' => 'png',
|
|
'image/webp' => 'webp',
|
|
];
|
|
|
|
if (!isset($allowed[$mime])) {
|
|
$error = 'Format d\'image non autorise. Utilisez JPG, PNG ou WebP.';
|
|
} elseif ((int) $_FILES['image_file']['size'] > 5 * 1024 * 1024) {
|
|
$error = 'L\'image ne doit pas depasser 5 Mo.';
|
|
} elseif (!is_uploaded_file($_FILES['image_file']['tmp_name'])) {
|
|
$error = 'Upload invalide.';
|
|
} else {
|
|
$uploadDir = realpath(__DIR__ . '/../assets/img/projects');
|
|
if ($uploadDir === false) {
|
|
$error = 'Dossier d\'upload introuvable.';
|
|
} else {
|
|
$previousImage = $imageFilename;
|
|
$imageFilename = $project['slug'] . '-' . bin2hex(random_bytes(8)) . '.' . $allowed[$mime];
|
|
$uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $imageFilename;
|
|
|
|
if (!move_uploaded_file($_FILES['image_file']['tmp_name'], $uploadPath)) {
|
|
$error = 'Erreur lors de l\'upload de l\'image.';
|
|
$imageFilename = $previousImage;
|
|
} else {
|
|
@chmod($uploadPath, 0644);
|
|
|
|
if ($previousImage !== '' && $previousImage !== $imageFilename) {
|
|
$oldPath = $uploadDir . DIRECTORY_SEPARATOR . basename($previousImage);
|
|
if (is_file($oldPath)) {
|
|
@unlink($oldPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($error === '') {
|
|
try {
|
|
if ($isEdit) {
|
|
$stmt = $db->prepare(
|
|
'UPDATE projects SET
|
|
title = :title,
|
|
slug = :slug,
|
|
short_description = :short_description,
|
|
full_description = :full_description,
|
|
image = :image,
|
|
status = :status,
|
|
badge = :badge,
|
|
technologies = :technologies,
|
|
project_url = :project_url,
|
|
display_order = :display_order
|
|
WHERE id = :id'
|
|
);
|
|
$stmt->execute([
|
|
'title' => $project['title'],
|
|
'slug' => $project['slug'],
|
|
'short_description' => $project['short_description'],
|
|
'full_description' => $project['full_description'],
|
|
'image' => $imageFilename,
|
|
'status' => $project['status'],
|
|
'badge' => $project['badge'],
|
|
'technologies' => $project['technologies'],
|
|
'project_url' => $project['project_url'],
|
|
'display_order' => $project['display_order'],
|
|
'id' => (int) $_GET['id'],
|
|
]);
|
|
|
|
header('Location: index.php?success=updated');
|
|
exit;
|
|
}
|
|
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO projects (
|
|
title, slug, short_description, full_description, image, status, badge, technologies, project_url, display_order
|
|
) VALUES (
|
|
:title, :slug, :short_description, :full_description, :image, :status, :badge, :technologies, :project_url, :display_order
|
|
)'
|
|
);
|
|
$stmt->execute([
|
|
'title' => $project['title'],
|
|
'slug' => $project['slug'],
|
|
'short_description' => $project['short_description'],
|
|
'full_description' => $project['full_description'],
|
|
'image' => $imageFilename,
|
|
'status' => $project['status'],
|
|
'badge' => $project['badge'],
|
|
'technologies' => $project['technologies'],
|
|
'project_url' => $project['project_url'],
|
|
'display_order' => $project['display_order'],
|
|
]);
|
|
|
|
header('Location: index.php?success=created');
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
error_log('[ProjectForm] ' . $e->getMessage());
|
|
$error = 'Une erreur est survenue. Veuillez reessayer.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!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><?= $isEdit ? 'Modifier' : 'Nouveau' ?> projet - BackOffice</title>
|
|
<link rel="stylesheet" href="assets/admin.css">
|
|
</head>
|
|
<body>
|
|
<div class="admin-layout">
|
|
<header class="admin-header">
|
|
<h1 class="admin-header__title"><?= $isEdit ? 'Modifier' : 'Nouveau' ?> <span>projet</span></h1>
|
|
<div class="admin-header__actions">
|
|
<a href="index.php" class="admin-btn admin-btn--outline">Retour</a>
|
|
</div>
|
|
</header>
|
|
|
|
<?php if ($error !== ''): ?>
|
|
<div class="admin-alert admin-alert--error"><?= Security::e($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form class="admin-form" method="post" enctype="multipart/form-data">
|
|
<?= Security::csrfInput() ?>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="title" class="admin-form__label">Titre du projet</label>
|
|
<input type="text" id="title" name="title" class="admin-form__input" required value="<?= Security::e((string) $project['title']) ?>">
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="slug" class="admin-form__label">Slug <span class="admin-form__hint">(genere automatiquement si vide)</span></label>
|
|
<input type="text" id="slug" name="slug" class="admin-form__input" value="<?= Security::e((string) $project['slug']) ?>">
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="short_description" class="admin-form__label">Description courte</label>
|
|
<textarea id="short_description" name="short_description" class="admin-form__textarea" rows="3" required><?= Security::e((string) $project['short_description']) ?></textarea>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="full_description" class="admin-form__label">Description detaillee <span class="admin-form__hint">(optionnel)</span></label>
|
|
<textarea id="full_description" name="full_description" class="admin-form__textarea" rows="6"><?= Security::e((string) ($project['full_description'] ?? '')) ?></textarea>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="image_file" class="admin-form__label">Image <span class="admin-form__hint">(JPG, PNG ou WebP - max 5 Mo)</span></label>
|
|
<input type="file" id="image_file" name="image_file" class="admin-form__input" accept=".jpg,.jpeg,.png,.webp">
|
|
<?php if (!empty($project['image'])): ?>
|
|
<p style="margin-top:0.5rem;font-size:0.8125rem;color:var(--admin-text-secondary);">
|
|
Image actuelle : <?= Security::e((string) $project['image']) ?>
|
|
</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="admin-form__row">
|
|
<div class="admin-form__group">
|
|
<label for="status" class="admin-form__label">Statut de publication</label>
|
|
<select id="status" name="status" class="admin-form__select">
|
|
<option value="draft" <?= ($project['status'] === 'draft') ? 'selected' : '' ?>>Brouillon</option>
|
|
<option value="published" <?= ($project['status'] === 'published') ? 'selected' : '' ?>>Publie</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="badge" class="admin-form__label">Badge d'etat</label>
|
|
<select id="badge" name="badge" class="admin-form__select">
|
|
<option value="development" <?= ($project['badge'] === 'development') ? 'selected' : '' ?>>En developpement</option>
|
|
<option value="production" <?= ($project['badge'] === 'production') ? 'selected' : '' ?>>En production</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="technologies" class="admin-form__label">Technologies <span class="admin-form__hint">(separees par des virgules)</span></label>
|
|
<input type="text" id="technologies" name="technologies" class="admin-form__input" placeholder="HTML, CSS, PHP, JavaScript" value="<?= Security::e((string) ($project['technologies'] ?? '')) ?>">
|
|
</div>
|
|
|
|
<div class="admin-form__row">
|
|
<div class="admin-form__group">
|
|
<label for="project_url" class="admin-form__label">Lien du projet <span class="admin-form__hint">(https uniquement)</span></label>
|
|
<input type="url" id="project_url" name="project_url" class="admin-form__input" placeholder="https://..." value="<?= Security::e((string) ($project['project_url'] ?? '')) ?>">
|
|
</div>
|
|
|
|
<div class="admin-form__group">
|
|
<label for="display_order" class="admin-form__label">Ordre d'affichage</label>
|
|
<input type="number" id="display_order" name="display_order" class="admin-form__input" min="0" value="<?= (int) $project['display_order'] ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-form__actions">
|
|
<button type="submit" class="admin-btn admin-btn--primary"><?= $isEdit ? 'Mettre a jour' : 'Creer le projet' ?></button>
|
|
<a href="index.php" class="admin-btn admin-btn--outline">Annuler</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|