190 lines
5.9 KiB
PHP
190 lines
5.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
private const MEDIA_PICKER_LIMIT = 60;
|
|
private const UPLOAD_MAX_BYTES = 10 * 1024 * 1024;
|
|
|
|
public function beforeRoute(): void
|
|
{
|
|
$this->requireAuth();
|
|
}
|
|
|
|
public function index(): void
|
|
{
|
|
$page = max(1, (int) ($this->f3->get('GET.page') ?: 1));
|
|
$result = (new Post())->page($page, 12);
|
|
|
|
$this->render('admin/dashboard.html', [
|
|
'pageTitle' => 'Tableau de bord',
|
|
'posts' => $result['items'],
|
|
'pagination' => $result['pagination'],
|
|
'paginationAlias' => 'dashboard',
|
|
'adminMode' => true,
|
|
]);
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$this->postForm('Nouvel article', $this->f3->alias('post_store'), Post::blank());
|
|
}
|
|
|
|
public function store(): void
|
|
{
|
|
$this->checkCsrf();
|
|
$input = $this->postInput();
|
|
|
|
try {
|
|
(new Post())->savePost($input);
|
|
$this->flash('success', 'Article créé.');
|
|
$this->f3->reroute('@dashboard');
|
|
} catch (RuntimeException $e) {
|
|
$this->postForm('Nouvel article', $this->f3->alias('post_store'), $input, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(): void
|
|
{
|
|
$post = (new Post())->findForForm((int) $this->f3->get('PARAMS.id'));
|
|
if (!$post) {
|
|
$this->f3->error(404);
|
|
return;
|
|
}
|
|
|
|
$this->postForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $post['id']]), $post);
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->checkCsrf();
|
|
$id = (int) $this->f3->get('PARAMS.id');
|
|
$input = $this->postInput() + ['id' => $id];
|
|
|
|
try {
|
|
(new Post())->savePost($input, $id);
|
|
$this->flash('success', 'Article mis à jour.');
|
|
$this->f3->reroute('@dashboard');
|
|
} catch (RuntimeException $e) {
|
|
$this->postForm('Modifier l\'article', $this->f3->alias('post_update', ['id' => $id]), $input, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete(): void
|
|
{
|
|
$this->checkCsrf();
|
|
|
|
try {
|
|
(new Post())->deleteById((int) $this->f3->get('PARAMS.id'));
|
|
$this->flash('success', 'Article supprimé.');
|
|
} catch (RuntimeException $e) {
|
|
$this->flash('error', $e->getMessage());
|
|
}
|
|
|
|
$this->f3->reroute('@dashboard');
|
|
}
|
|
|
|
public function media(): void
|
|
{
|
|
$page = max(1, (int) ($this->f3->get('GET.page') ?: 1));
|
|
$result = (new Media())->page($page, 24);
|
|
|
|
$this->render('admin/media.html', [
|
|
'pageTitle' => 'Médiathèque',
|
|
'items' => $result['items'],
|
|
'pagination' => $result['pagination'],
|
|
'paginationAlias' => 'media_index',
|
|
]);
|
|
}
|
|
|
|
public function mediaUpload(): void
|
|
{
|
|
$this->checkCsrf();
|
|
|
|
try {
|
|
$original = (string) ($this->f3->get('FILES.image.name') ?: '');
|
|
$received = Web::instance()->receive(
|
|
fn(array $file): bool => (int) ($file['size'] ?? 0) > 0 && (int) ($file['size'] ?? 0) <= self::UPLOAD_MAX_BYTES,
|
|
overwrite: false
|
|
);
|
|
$path = array_key_first(array_filter($received));
|
|
if (!$path) {
|
|
throw new RuntimeException('Choisis une image valide à envoyer.');
|
|
}
|
|
|
|
(new Media())->upload($path, $original);
|
|
$this->flash('success', 'Image ajoutée.');
|
|
} catch (RuntimeException $e) {
|
|
$this->flash('error', $e->getMessage());
|
|
}
|
|
|
|
$this->f3->reroute('@media_index');
|
|
}
|
|
|
|
public function mediaAlt(): void
|
|
{
|
|
$this->checkCsrf();
|
|
|
|
try {
|
|
(new Media())->updateAlt((int) $this->f3->get('PARAMS.id'), $this->f3->clean((string) ($this->f3->get('POST.alt') ?: '')));
|
|
$this->flash('success', 'Texte alternatif mis à jour.');
|
|
} catch (RuntimeException $e) {
|
|
$this->flash('error', $e->getMessage());
|
|
}
|
|
|
|
$this->f3->reroute('@media_index');
|
|
}
|
|
|
|
public function mediaDelete(): void
|
|
{
|
|
$this->checkCsrf();
|
|
|
|
try {
|
|
$media = new Media();
|
|
$item = $media->findById((int) $this->f3->get('PARAMS.id'));
|
|
if (!$item) {
|
|
throw new RuntimeException('Image introuvable.');
|
|
}
|
|
if ((new Post())->usesMedia($item['file_name'])) {
|
|
throw new RuntimeException('Cette image est encore utilisée par un article.');
|
|
}
|
|
$media->deleteById($item['id']);
|
|
$this->flash('success', 'Image supprimée.');
|
|
} catch (RuntimeException $e) {
|
|
$this->flash('error', $e->getMessage());
|
|
}
|
|
|
|
$this->f3->reroute('@media_index');
|
|
}
|
|
|
|
private function postForm(string $title, string $action, array $post, ?string $error = null): void
|
|
{
|
|
$media = new Media();
|
|
$items = $media->recent(self::MEDIA_PICKER_LIMIT);
|
|
$count = $media->count();
|
|
|
|
$this->render('admin/post_form.html', [
|
|
'pageTitle' => $title,
|
|
'formAction' => $action,
|
|
'post' => $post,
|
|
'mediaItems' => $items,
|
|
'mediaCount' => $count,
|
|
'mediaPickerLimit' => self::MEDIA_PICKER_LIMIT,
|
|
'mediaPickerTruncated' => $count > count($items),
|
|
'titleMax' => Post::TITLE_MAX_LENGTH,
|
|
'excerptMax' => Post::EXCERPT_MAX_LENGTH,
|
|
'flash' => $error ? [['type' => 'error', 'message' => $error]] : [],
|
|
]);
|
|
}
|
|
|
|
private function postInput(): array
|
|
{
|
|
return [
|
|
'title' => $this->f3->clean((string) ($this->f3->get('POST.title') ?: '')),
|
|
'excerpt' => $this->f3->clean((string) ($this->f3->get('POST.excerpt') ?: '')),
|
|
'body_markdown' => trim((string) ($this->f3->get('POST.body_markdown') ?: '')),
|
|
];
|
|
}
|
|
}
|