Less home code more F3
This commit is contained in:
@@ -20,14 +20,15 @@ class PostController extends BaseController
|
||||
{
|
||||
$this->verifyCsrf();
|
||||
|
||||
$media = new Media($this->db);
|
||||
$input = $this->postInput();
|
||||
|
||||
try {
|
||||
(new Post($this->db))->create($input);
|
||||
(new Post($this->db))->create($input, $media);
|
||||
$this->flash('success', 'Article créé.');
|
||||
$this->f3->reroute('@dashboard');
|
||||
} catch (RuntimeException $e) {
|
||||
$this->renderForm('Nouvel article', $this->f3->alias('post_store'), $input, $e->getMessage());
|
||||
$this->renderForm('Nouvel article', $this->f3->alias('post_store'), $input, $e->getMessage(), $media);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,11 +47,12 @@ class PostController extends BaseController
|
||||
{
|
||||
$this->verifyCsrf();
|
||||
|
||||
$media = new Media($this->db);
|
||||
$id = (int) $this->f3->get('PARAMS.id');
|
||||
$input = $this->postInput() + ['id' => $id];
|
||||
|
||||
try {
|
||||
$updated = (new Post($this->db))->updatePost($id, $input);
|
||||
$updated = (new Post($this->db))->updatePost($id, $input, $media);
|
||||
if (!$updated) {
|
||||
$this->f3->error(404, 'Article introuvable.');
|
||||
return;
|
||||
@@ -59,7 +61,7 @@ class PostController extends BaseController
|
||||
$this->flash('success', 'Article mis à jour.');
|
||||
$this->f3->reroute('@dashboard');
|
||||
} catch (RuntimeException $e) {
|
||||
$this->renderForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $id]), $input, $e->getMessage());
|
||||
$this->renderForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $id]), $input, $e->getMessage(), $media);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,14 +79,15 @@ class PostController extends BaseController
|
||||
$this->f3->reroute('@dashboard');
|
||||
}
|
||||
|
||||
private function renderForm(string $pageTitle, string $formAction, array $post, ?string $error = null): void
|
||||
private function renderForm(string $pageTitle, string $formAction, array $post, ?string $error = null, ?Media $media = null): void
|
||||
{
|
||||
$media ??= new Media($this->db);
|
||||
|
||||
$coverPreview = null;
|
||||
if (!empty($post['cover_media_id'])) {
|
||||
$coverPreview = (new Media($this->db))->findById((int) $post['cover_media_id']);
|
||||
$coverPreview = $media->findById((int) $post['cover_media_id']);
|
||||
}
|
||||
|
||||
$media = new Media($this->db);
|
||||
$mediaItems = $media->latest(self::MEDIA_PICKER_LIMIT);
|
||||
$mediaCount = $media->count();
|
||||
$flash = $error !== null ? ['type' => 'error', 'message' => $error] : null;
|
||||
|
||||
Reference in New Issue
Block a user