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:
parent
dc63d70944
commit
a9f610fc60
@ -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");
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
@ -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 {
|
||||||
|
@ -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 ?>">« Newer</a>
|
href="?page=<?php echo $page - 1 ?>">« 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 »</a>
|
href="?page=<?php echo $page + 1 ?>">Older »</a>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
Loading…
x
Reference in New Issue
Block a user