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(){ | ||||
|         $this->config = ConfigModel::load(); | ||||
|         $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"); | ||||
|     } | ||||
|  | ||||
| @ -10,10 +10,10 @@ class HomeController extends Controller { | ||||
|         $tickModel = new TickModel(); | ||||
|         $limit = $config->itemsPerPage; | ||||
|         $offset = ($page - 1) * $limit; | ||||
|         $ticks = iterator_to_array($tickModel->stream($limit, $offset)); | ||||
|         $ticks = $tickModel->getPage($limit, $offset); | ||||
| 
 | ||||
|         $view = new HomeView(); | ||||
|         $tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit); | ||||
|         $view = new TicksView($config, $ticks, $page); | ||||
|         $tickList = $view->getHtml(); | ||||
| 
 | ||||
|         $vars = [ | ||||
|             'config'     => $config, | ||||
|  | ||||
| @ -1,18 +1,12 @@ | ||||
| <?php | ||||
| class TickModel { | ||||
|     public function stream(int $limit, int $offset = 0): Generator { | ||||
|     public function getPage(int $limit, int $offset = 0): array { | ||||
|         global $db; | ||||
| 
 | ||||
|         $stmt = $db->prepare("SELECT id, timestamp, tick FROM tick ORDER BY timestamp DESC LIMIT ? OFFSET ?"); | ||||
|         $stmt->execute([$limit, $offset]); | ||||
| 
 | ||||
|         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { | ||||
|             yield [ | ||||
|                 'id' => $row['id'], | ||||
|                 'timestamp' => $row['timestamp'], | ||||
|                 'tick' => $row['tick'], | ||||
|             ]; | ||||
|         } | ||||
|         return $stmt->fetchAll(PDO::FETCH_ASSOC); | ||||
|     } | ||||
| 
 | ||||
|     public function insert(string $tick, ?DateTimeImmutable $datetime = null): void { | ||||
|  | ||||
| @ -1,7 +1,16 @@ | ||||
| <?php | ||||
| class HomeView { | ||||
|     public function renderTicksSection(string $siteDescription, array $ticks, int $page, int $limit){ | ||||
|         global $config; | ||||
| class TicksView { | ||||
|     private $html; | ||||
| 
 | ||||
|     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(); | ||||
|         ?>
 | ||||
| 
 | ||||
| @ -22,7 +31,7 @@ class HomeView { | ||||
|                 <a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?>
 | ||||
|                    href="?page=<?php echo $page - 1 ?>">« Newer</a> | ||||
|             <?php endif; ?>
 | ||||
|             <?php if (count($ticks) === $limit): ?>
 | ||||
|             <?php if (count($ticks) === $config->itemsPerPage): ?>
 | ||||
|                 <a <?php if($config->strictAccessibility): ?>tabindex="0"<?php endif; ?>
 | ||||
|                    href="?page=<?php echo $page + 1 ?>">Older »</a> | ||||
|             <?php endif; ?>
 | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user