More robust
This commit is contained in:
@@ -4,16 +4,21 @@ declare(strict_types=1);
|
||||
|
||||
class PostController extends BaseController
|
||||
{
|
||||
public function create(): void
|
||||
private const MEDIA_PICKER_LIMIT = 60;
|
||||
|
||||
public function beforeRoute(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
$this->f3->expire(0);
|
||||
$this->disableCache();
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$this->renderForm('Nouvel article', $this->f3->alias('post_store'), Post::emptyForm());
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
$this->verifyCsrf();
|
||||
|
||||
$input = $this->postInput();
|
||||
@@ -22,18 +27,14 @@ class PostController extends BaseController
|
||||
(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'));
|
||||
$this->f3->reroute('@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.');
|
||||
@@ -45,7 +46,6 @@ class PostController extends BaseController
|
||||
|
||||
public function update(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
$this->verifyCsrf();
|
||||
|
||||
$id = (int) $this->f3->get('PARAMS.id');
|
||||
@@ -60,21 +60,19 @@ class PostController extends BaseController
|
||||
|
||||
Cache::instance()->reset('.url'); // invalide le cache des pages publiques
|
||||
$this->flash('success', 'Article mis à jour.');
|
||||
$this->f3->reroute($this->f3->alias('dashboard'));
|
||||
$this->f3->reroute('@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'));
|
||||
$this->f3->reroute('@dashboard');
|
||||
}
|
||||
|
||||
private function renderForm(string $pageTitle, string $formAction, array $post, ?string $error = null): void
|
||||
@@ -84,27 +82,33 @@ class PostController extends BaseController
|
||||
$coverPreview = (new Media($this->db))->findById((int) $post['cover_media_id']);
|
||||
}
|
||||
|
||||
$media = new Media($this->db);
|
||||
$mediaItems = $media->latest(self::MEDIA_PICKER_LIMIT);
|
||||
$mediaCount = $media->countAll();
|
||||
$flash = $error !== null ? ['type' => 'error', 'message' => $error] : null;
|
||||
|
||||
$this->render('admin/post_form.html', [
|
||||
'pageTitle' => $pageTitle,
|
||||
'formAction' => $formAction,
|
||||
'post' => $post,
|
||||
$this->renderSession('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,
|
||||
]);
|
||||
'mediaItems' => $mediaItems,
|
||||
'mediaCount' => $mediaCount,
|
||||
'mediaPickerLimit' => self::MEDIA_PICKER_LIMIT,
|
||||
'mediaPickerTruncated' => $mediaCount > count($mediaItems),
|
||||
'titleMax' => Post::TITLE_MAX_LENGTH,
|
||||
'excerptMax' => Post::EXCERPT_MAX_LENGTH,
|
||||
'flash' => $flash,
|
||||
], true);
|
||||
}
|
||||
|
||||
private function postInput(): array
|
||||
{
|
||||
return [
|
||||
'title' => trim((string) ($this->f3->get('POST.title') ?? '')),
|
||||
'excerpt' => trim((string) ($this->f3->get('POST.excerpt') ?? '')),
|
||||
'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') ?? '')),
|
||||
'body_markdown' => trim((string) ($this->f3->get('POST.body_markdown') ?? '')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user