Less home code more F3

This commit is contained in:
julien
2026-03-28 17:37:22 +01:00
parent d2e70af739
commit 16850386d3
19 changed files with 232 additions and 429 deletions

View File

@@ -39,7 +39,7 @@ class Post extends DB\SQL\Mapper
];
}
public function paginateList(int $page = 1, int $perPage = 12): array
public function paginateList(int $page, int $perPage, Media $media): array
{
$result = $this->paginate(
max(0, $page - 1),
@@ -49,11 +49,13 @@ class Post extends DB\SQL\Mapper
);
$posts = array_map(fn (self $p): array => $this->summaryRow($p->cast()), $result['subset']);
$covers = $this->loadCovers($posts);
$coverIds = array_filter(array_unique(array_column($posts, 'cover_media_id')));
$covers = $media->findByIds($coverIds);
foreach ($posts as &$post) {
$cover = $covers[$post['cover_media_id']] ?? null;
$post['cover_url'] = $cover ? app_media_url((string) $cover['file_name']) : '';
$post['cover_url'] = $cover['url'] ?? '';
$post['cover_alt'] = $cover['alt'] ?? '';
}
return [
@@ -63,7 +65,7 @@ class Post extends DB\SQL\Mapper
];
}
public function findBySlug(string $slug): ?array
public function findBySlug(string $slug, Media $media): ?array
{
$this->load(['slug = ?', $slug]);
if ($this->dry()) {
@@ -71,9 +73,9 @@ class Post extends DB\SQL\Mapper
}
$post = $this->summaryRow($this->cast());
$covers = $this->loadCovers([$post]);
$cover = $covers[$post['cover_media_id']] ?? null;
$post['cover_url'] = $cover ? app_media_url((string) $cover['file_name']) : '';
$cover = $post['cover_media_id'] > 0 ? $media->findById($post['cover_media_id']) : null;
$post['cover_url'] = $cover['url'] ?? '';
$post['cover_alt'] = $cover['alt'] ?? '';
$post['body_html'] = (string) $this->body_html;
return $post;
@@ -99,10 +101,10 @@ class Post extends DB\SQL\Mapper
];
}
public function create(array $input): int
public function create(array $input, Media $media): int
{
$payload = $this->payload($input);
$slug = app_unique_slug($payload['title'], fn (string $candidate): bool => $this->slugExists($candidate));
$payload = $this->payload($input, $media);
$slug = app_unique_slug($payload['title'], fn (string $candidate): bool => $this->count(['slug = ?', $candidate]) > 0);
$now = app_now();
$this->reset();
@@ -116,20 +118,31 @@ class Post extends DB\SQL\Mapper
return (int) $this->get('id');
}
public function updatePost(int $id, array $input): bool
public function updatePost(int $id, array $input, Media $media): bool
{
$this->load(['id = ?', $id]);
if ($this->dry()) {
return false;
}
$payload = $this->payload($input);
$payload = $this->payload($input, $media);
$this->copyfrom($payload + ['updated_at' => app_now()]);
$this->save();
return true;
}
// Vérifie les deux usages possibles : couverture (cover_media_id)
// et images insérées dans le corps (media:filename dans body_markdown).
public function isMediaUsed(int $mediaId, string $fileName): bool
{
return $this->count([
'cover_media_id = ? OR body_markdown LIKE ?',
$mediaId,
'%media:' . $fileName . '%',
]) > 0;
}
public function delete(int $id): void
{
$this->load(['id = ?', $id]);
@@ -140,7 +153,7 @@ class Post extends DB\SQL\Mapper
$this->erase();
}
private function payload(array $input): array
private function payload(array $input, Media $media): array
{
$title = trim((string) ($input['title'] ?? ''));
$excerpt = trim((string) ($input['excerpt'] ?? ''));
@@ -160,8 +173,6 @@ class Post extends DB\SQL\Mapper
throw new RuntimeException("L'extrait est trop long.");
}
$media = new Media($this->db);
$coverId = null;
if ($coverMediaId !== '') {
$coverId = (int) $coverMediaId;
@@ -181,11 +192,6 @@ class Post extends DB\SQL\Mapper
];
}
private function slugExists(string $slug): bool
{
return $this->count(['slug = ?', $slug]) > 0;
}
private function summaryRow(array $row): array
{
return [
@@ -199,24 +205,4 @@ class Post extends DB\SQL\Mapper
];
}
private function loadCovers(array $posts): array
{
$ids = array_filter(array_unique(array_column($posts, 'cover_media_id')));
if ($ids === []) {
return [];
}
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$rows = $this->db->exec(
"SELECT id, file_name FROM media WHERE id IN ($placeholders)",
array_values($ids)
);
$map = [];
foreach ($rows as $row) {
$map[(int) $row['id']] = $row;
}
return $map;
}
}