From: Christian Weiske Date: Mon, 26 Feb 2018 21:39:09 +0000 (+0100) Subject: Simple latest comment list X-Git-Tag: v1.0.0~9 X-Git-Url: https://git.cweiske.de/anoweco.git/commitdiff_plain/506949fa5639b8810a2437a9bffbe40165e4d8ae Simple latest comment list --- diff --git a/data/templates/latest.htm b/data/templates/latest.htm new file mode 100644 index 0000000..aa6e884 --- /dev/null +++ b/data/templates/latest.htm @@ -0,0 +1,30 @@ + + + + Latest comments + + + +

Latest comments

+ + + + + + + + + + + {% for comment in comments %} + + + + + + + {% endfor %} + +
DateTypeUserFor domain
{{comment.comment_published}}{{comment.comment_type}}{{comment.user.user_name}}{{comment.domain}}
+ + diff --git a/src/anoweco/Storage.php b/src/anoweco/Storage.php index 4508f10..6a7e204 100644 --- a/src/anoweco/Storage.php +++ b/src/anoweco/Storage.php @@ -92,6 +92,49 @@ class Storage return $json; } + /** + * @return null|object NULL if not found, JSON comment object otherwise + * - "Xrow" property contains the database row object + * - "user" property contains the user db row object + */ + public function listLatest() + { + $stmt = $this->db->prepare( + 'SELECT comment_id, comment_user_id, comment_published' + . ', comment_of_url, comment_type' + . ' FROM comments' + . ' ORDER BY comment_published DESC' + . ' LIMIT 20' + ); + $stmt->execute(); + $rows = $stmt->fetchAll(\PDO::FETCH_OBJ); + if (!count($rows)) { + return []; + } + $userIds = array_values( + array_unique(array_column($rows, 'comment_user_id')) + ); + + + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + $stmt = $this->db->prepare( + 'SELECT * FROM users WHERE user_id IN (' . $placeholders . ')' + ); + $stmt->execute($userIds); + + $users = $stmt->fetchAll(\PDO::FETCH_OBJ); + $users = array_combine( + array_column($users, 'user_id'), + $users + ); + + foreach ($rows as $row) { + $row->user = $users[$row->comment_user_id]; + } + + return $rows; + } + /** * @return null|object NULL if not found, user database row otherwise */ diff --git a/www/css/latest.css b/www/css/latest.css new file mode 100644 index 0000000..6c8c651 --- /dev/null +++ b/www/css/latest.css @@ -0,0 +1,8 @@ +table { + border: 1px solid grey; + border-collapse: collapse; +} +td, th { + border: 1px solid grey; + padding: 0.5ex 1ex; +} diff --git a/www/latest.php b/www/latest.php new file mode 100644 index 0000000..12105d9 --- /dev/null +++ b/www/latest.php @@ -0,0 +1,17 @@ +listLatest(); + +foreach ($comments as $comment) { + $comment->url = Urls::comment($comment->comment_id); + $comment->domain = parse_url($comment->comment_of_url, PHP_URL_HOST); +} + +$vars = [ + 'comments' => $comments, +]; +render('latest', $vars); +?>