111 lines
3.7 KiB
PHP
111 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class PostController extends BaseController
|
|
{
|
|
public function create(): void
|
|
{
|
|
$this->requireAuth();
|
|
$this->f3->expire(0);
|
|
$this->renderForm('Nouvel article', $this->f3->alias('post_store'), Post::emptyForm());
|
|
}
|
|
|
|
public function store(): void
|
|
{
|
|
$this->requireAuth();
|
|
$this->verifyCsrf();
|
|
|
|
$input = $this->postInput();
|
|
|
|
try {
|
|
(new Post($this->db))->create($input);
|
|
Cache::instance()->reset('.url'); // invalide le cache des pages publiques
|
|
$this->flash('success', 'Article créé.');
|
|
$this->f3->reroute($this->f3->alias('dashboard'));
|
|
} catch (RuntimeException $e) {
|
|
$this->f3->expire(0);
|
|
$this->renderForm('Nouvel article', $this->f3->alias('post_store'), $input, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(): void
|
|
{
|
|
$this->requireAuth();
|
|
$this->f3->expire(0);
|
|
|
|
$post = (new Post($this->db))->findForEdit((int) $this->f3->get('PARAMS.id'));
|
|
if ($post === null) {
|
|
$this->f3->error(404, 'Article introuvable.');
|
|
return;
|
|
}
|
|
|
|
$this->renderForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $post['id']]), $post);
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->requireAuth();
|
|
$this->verifyCsrf();
|
|
|
|
$id = (int) $this->f3->get('PARAMS.id');
|
|
$input = $this->postInput() + ['id' => $id];
|
|
|
|
try {
|
|
$updated = (new Post($this->db))->updatePost($id, $input);
|
|
if (!$updated) {
|
|
$this->f3->error(404, 'Article introuvable.');
|
|
return;
|
|
}
|
|
|
|
Cache::instance()->reset('.url'); // invalide le cache des pages publiques
|
|
$this->flash('success', 'Article mis à jour.');
|
|
$this->f3->reroute($this->f3->alias('dashboard'));
|
|
} catch (RuntimeException $e) {
|
|
$this->f3->expire(0);
|
|
$this->renderForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $id]), $input, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
$this->requireAuth();
|
|
$this->verifyCsrf();
|
|
(new Post($this->db))->delete((int) $this->f3->get('PARAMS.id'));
|
|
Cache::instance()->reset('.url'); // invalide le cache des pages publiques
|
|
$this->flash('success', 'Article supprimé.');
|
|
$this->f3->reroute($this->f3->alias('dashboard'));
|
|
}
|
|
|
|
private function renderForm(string $pageTitle, string $formAction, array $post, ?string $error = null): void
|
|
{
|
|
$coverPreview = null;
|
|
if (!empty($post['cover_media_id'])) {
|
|
$coverPreview = (new Media($this->db))->findById((int) $post['cover_media_id']);
|
|
}
|
|
|
|
$flash = $error !== null ? ['type' => 'error', 'message' => $error] : null;
|
|
|
|
$this->render('admin/post_form.html', [
|
|
'pageTitle' => $pageTitle,
|
|
'formAction' => $formAction,
|
|
'post' => $post,
|
|
'coverPreview' => $coverPreview,
|
|
'mediaItems' => (new Media($this->db))->all(),
|
|
'titleMax' => Post::TITLE_MAX_LENGTH,
|
|
'excerptMax' => Post::EXCERPT_MAX_LENGTH,
|
|
'flash' => $flash,
|
|
]);
|
|
}
|
|
|
|
private function postInput(): array
|
|
{
|
|
return [
|
|
'title' => trim((string) ($this->f3->get('POST.title') ?? '')),
|
|
'excerpt' => trim((string) ($this->f3->get('POST.excerpt') ?? '')),
|
|
'cover_media_id' => (string) ($this->f3->get('POST.cover_media_id') ?? ''),
|
|
'body_markdown' => trim((string) ($this->f3->get('POST.body_markdown') ?? '')),
|
|
];
|
|
}
|
|
}
|