diff --git a/public/index.php b/public/index.php index a864db4..308f76d 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,4 @@ itemsPerPage; - $offset = ($page - 1) * $limit; - $ticks = iterator_to_array(stream_ticks($limit, $offset)); - - $vars = [ - 'isLoggedIn' => $isLoggedIn, - 'config' => $config, - 'user' => $user, - 'ticks' => $ticks, - ]; - - echo render_template(TEMPLATES_DIR . "/home.php", $vars); +route('', function(){ + $hc = new HomeController(); + echo $hc->render(); }); diff --git a/src/Controller/Home/Home.php b/src/Controller/Home/Home.php new file mode 100644 index 0000000..6bcfe6d --- /dev/null +++ b/src/Controller/Home/Home.php @@ -0,0 +1,77 @@ + strcmp($b, $a)); // sort filenames in reverse chronological order + + $count = 0; + foreach ($tick_files as $file) { + // read all the ticks from the current file and reverse the order + // so the most recent ones are first + // + // each file is a single day, so we never hold more than + // one day's ticks in memory + $lines = array_reverse( + file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) + ); + + // split the path to the current file into the date components + $pathParts = explode('/', str_replace('\\', '/', $file)); + + // assign the different components to the appropriate part of the date + $year = $pathParts[count($pathParts) - 3]; + $month = $pathParts[count($pathParts) - 2]; + $day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME); + + foreach ($lines as $line) { + // just keep skipping ticks until we get to the starting point + if ($offset > 0) { + $offset--; + continue; + } + + // Ticks are pipe-delimited: timestamp|text + // But just in case a tick contains a pipe, only split on the first one that occurs + $tickParts = explode('|', $line, 2); + $time = $tickParts[0]; + $tick = $tickParts[1]; + + // Build the timestamp from the date and time + // Ticks are always stored in UTC + $timestampUTC = "$year-$month-$day $time"; + yield [ + 'timestamp' => $timestampUTC, + 'tick' => $tick, + ]; + + if (++$count >= $limit) { + return; + } + } + } + } + + public function render(){ + $page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; + $isLoggedIn = isset($_SESSION['user_id']); + $config = Config::load(); + $user = User::load(); + + $limit = $config->itemsPerPage; + $offset = ($page - 1) * $limit; + $ticks = iterator_to_array(stream_ticks($limit, $offset)); + + $view = new HomeView(); + $tickList = $view->renderTicksSection($config->siteDescription, $ticks, $page, $limit); + + $vars = [ + 'isLoggedIn' => $isLoggedIn, + 'config' => $config, + 'user' => $user, + 'tickList' => $tickList, + ]; + + echo render_template(TEMPLATES_DIR . "/home.php", $vars); + + } +} \ No newline at end of file diff --git a/src/Config/Config.php b/src/Model/Config/Config.php similarity index 81% rename from src/Config/Config.php rename to src/Model/Config/Config.php index 58a6dab..1b37166 100644 --- a/src/Config/Config.php +++ b/src/Model/Config/Config.php @@ -1,10 +1,4 @@ + +
+
+

+
+
+ +
+
+ +
+ +
+
+ 1): ?> + « Newer + + + Older » + +
+
+ + prepare("UPDATE user SET mood=? WHERE username=?"); - //$stmt->execute([$mood, $_SESSION['username']]); $user->mood = $mood; $user = $user->save(); diff --git a/src/lib/ticks.php b/src/lib/ticks.php index 86bd407..b66640c 100644 --- a/src/lib/ticks.php +++ b/src/lib/ticks.php @@ -1,5 +1,4 @@ strcmp($b, $a)); // sort filenames in reverse chronological order @@ -46,9 +47,6 @@ function stream_ticks(int $limit, int $offset = 0): Generator { $year = $pathParts[count($pathParts) - 3]; $month = $pathParts[count($pathParts) - 2]; $day = pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME); - // $date = $pathParts[count($pathParts) - 3] . '-' . - // $pathParts[count($pathParts) - 2] . '-' . - // pathinfo($pathParts[count($pathParts) - 1], PATHINFO_FILENAME); foreach ($lines as $line) { // just keep skipping ticks until we get to the starting point diff --git a/src/lib/util.php b/src/lib/util.php index 1c403a6..beffc38 100644 --- a/src/lib/util.php +++ b/src/lib/util.php @@ -38,6 +38,7 @@ function relative_time(string $tickTime): string { } return $diff->s . ' second' . ($diff->s != 1 ? 's' : '') . ' ago'; } + function verify_data_dir(string $dir, bool $allow_create = false): void { if (!is_dir($dir)) { if ($allow_create) { @@ -117,9 +118,9 @@ function get_db(): PDO { return $db; } +// TODO - Maybe this is the sort of thing that would be good +// in a Controller base class. function render_template(string $templateFile, array $vars = []): string { - #$templatePath = TEMPLATES_DIR . '/' . ltrim($templateFile, '/'); - if (!file_exists($templateFile)) { throw new RuntimeException("Template not found: $templatePath"); } diff --git a/templates/home.php b/templates/home.php index 341f926..064d7b3 100644 --- a/templates/home.php +++ b/templates/home.php @@ -1,7 +1,7 @@ - + @@ -48,27 +48,7 @@ -
-
-

siteDescription ?>

-
-
- -
-
- -
- -
-
- 1): ?> - « Newer - - - Older » - -
-
+