Make home page similar to feeds. Simplify tick retrieval. (#37)

Reviewed-on: https://gitea.subcultureofone.org/greg/tkr/pulls/37
Co-authored-by: Greg Sarjeant <greg@subcultureofone.org>
Co-committed-by: Greg Sarjeant <greg@subcultureofone.org>
This commit is contained in:
Greg Sarjeant 2025-07-31 01:30:25 +00:00 committed by greg
parent dc63d70944
commit a9f610fc60
4 changed files with 19 additions and 16 deletions

View File

@ -6,7 +6,7 @@ class FeedController extends Controller {
public function __construct(){ public function __construct(){
$this->config = ConfigModel::load(); $this->config = ConfigModel::load();
$tickModel = new TickModel(); $tickModel = new TickModel();
$this->ticks = iterator_to_array($tickModel->stream($this->config->itemsPerPage)); $this->ticks = $tickModel->getPage($this->config->itemsPerPage);
Log::debug("Loaded " . count($this->ticks) . " ticks for feeds"); Log::debug("Loaded " . count($this->ticks) . " ticks for feeds");
} }

View File

@ -10,10 +10,10 @@ class HomeController extends Controller {
$tickModel = new TickModel(); $tickModel = new TickModel();
$limit = $config->itemsPerPage; $limit = $config->itemsPerPage;
$offset = ($page - 1) * $limit; $offset = ($page - 1) * $limit;
$ticks = iterator_to_array($tickModel->stream($limit, $offset)); $ticks = $tickModel->getPage($limit, $offset);
$view = new HomeView(); $view = new TicksView($config, $ticks, $page);
$tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit); $tickList = $view->getHtml();
$vars = [ $vars = [
'config' => $config, 'config' => $config,

View File

@ -1,18 +1,12 @@
<?php <?php
class TickModel { class TickModel {
public function stream(int $limit, int $offset = 0): Generator { public function getPage(int $limit, int $offset = 0): array {
global $db; global $db;
$stmt = $db->prepare("SELECT id, timestamp, tick FROM tick ORDER BY timestamp DESC LIMIT ? OFFSET ?"); $stmt = $db->prepare("SELECT id, timestamp, tick FROM tick ORDER BY timestamp DESC LIMIT ? OFFSET ?");
$stmt->execute([$limit, $offset]); $stmt->execute([$limit, $offset]);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { return $stmt->fetchAll(PDO::FETCH_ASSOC);
yield [
'id' => $row['id'],
'timestamp' => $row['timestamp'],
'tick' => $row['tick'],
];
}
} }
public function insert(string $tick, ?DateTimeImmutable $datetime = null): void { public function insert(string $tick, ?DateTimeImmutable $datetime = null): void {

View File

@ -1,7 +1,16 @@
<?php <?php
class HomeView { class TicksView {
public function renderTicksSection(string $siteDescription, array $ticks, int $page, int $limit){ private $html;
global $config;
public function __construct(ConfigModel $config, array $ticks, int $page){
$this->html = $this->render($config, $ticks, $page);
}
public function getHtml(): string {
return $this->html;
}
private function render(ConfigModel $config, array $ticks, int $page): string{
ob_start(); ob_start();
?> ?>
@ -22,7 +31,7 @@ class HomeView {
<a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?> <a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?>
href="?page=<?php echo $page - 1 ?>">&laquo; Newer</a> href="?page=<?php echo $page - 1 ?>">&laquo; Newer</a>
<?php endif; ?> <?php endif; ?>
<?php if (count($ticks) === $limit): ?> <?php if (count($ticks) === $config->itemsPerPage): ?>
<a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?> <a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?>
href="?page=<?php echo $page + 1 ?>">Older &raquo;</a> href="?page=<?php echo $page + 1 ?>">Older &raquo;</a>
<?php endif; ?> <?php endif; ?>