summaryrefslogtreecommitdiff
path: root/web/_controller/_util/post.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-03-30 16:36:54 -0400
committerFreya Murphy <freya@freyacat.org>2024-03-30 16:36:54 -0400
commit1f647374a8cdf3bc5c2d29ff8be277b027925c8c (patch)
tree9fdf42d250edb941de13ecd1aab9185ba2b30b00 /web/_controller/_util/post.php
parentrename views to _views (diff)
downloadxssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.tar.gz
xssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.tar.bz2
xssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.zip
post comments, refactor post loading, hide load more btn
Diffstat (limited to 'web/_controller/_util/post.php')
-rw-r--r--web/_controller/_util/post.php129
1 files changed, 129 insertions, 0 deletions
diff --git a/web/_controller/_util/post.php b/web/_controller/_util/post.php
new file mode 100644
index 0000000..b128d67
--- /dev/null
+++ b/web/_controller/_util/post.php
@@ -0,0 +1,129 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Post_controller extends Controller {
+
+ // the request model
+ private $request_model;
+
+ // the caceh model
+ private $cache_model;
+
+ // page size
+ private $page_size;
+
+ function __construct($load) {
+ parent::__construct($load);
+ $this->request_model = $this->load->model('request');
+ $this->cache_model = $this->load->model('cache');
+ $this->page_size = 10;
+ }
+
+ public function index(): void {
+ $this->view('template/posts');
+ }
+
+ /**
+ * @return array<string,mixed>
+ */
+ public function posts(): array {
+ $page = $this->request_model->get_int('page', 0);
+ $max = $this->request_model->get_int('max');
+ $offset = $page * $this->page_size;
+
+ $user = $this->main->user();
+
+ $query = $this->db;
+
+ if ($user) {
+ $query = $query->select('p.*, l.post_id IS NOT NULL as liked');
+ } else {
+ $query = $query->select('p.*, FALSE as liked');
+ }
+
+ $query = $query->from('api.post p');
+
+ if ($user) {
+ $query = $query->join('admin.like l', 'p.id = l.post_id AND l.user_id')
+ ->eq($user['id']);
+ }
+
+ if ($max) {
+ $query = $query
+ ->where('id')->le($max);
+ }
+
+ $posts = $query
+ ->limit($this->page_size)
+ ->offset($offset)
+ ->rows();
+
+ $users = $this->cache_model->get_users($posts);
+ $max = 0;
+
+ foreach ($posts as $post) {
+ $max = max($max, $post['id']);
+ $data = array();
+ $data['page_size'] = $this->page_size;
+ $data['user'] = $users[$post['user_id']];
+ $data['post'] = $post;
+ $this->view('template/post', $data);
+ }
+
+ $pc = $this->db
+ ->select('COUNT(p.id) as pc')
+ ->from('api.post p')
+ ->row()['pc'];
+
+
+ return array(
+ 'loaded' => count($posts),
+ 'total' => $pc,
+ 'page_size' => $this->page_size,
+ 'max' => $max,
+ );
+ }
+
+ /**
+ * @return array<string,mixed>
+ */
+ public function comments(): array {
+ $page = $this->request_model->get_int('page', 0);
+ $max = $this->request_model->get_int('max');
+ $id = $this->request_model->get_int('id', 0);
+ $offset = $page * $this->page_size;
+
+ $query = $this->db
+ ->select('*')
+ ->from('api.comment')
+ ->where('post_id')
+ ->eq($id);
+
+ if ($max) {
+ $query = $query
+ ->and()
+ ->where('id')
+ ->le($max);
+ }
+
+ $comments = $query
+ ->limit($this->page_size)
+ ->offset($offset)
+ ->rows();
+
+ $users = $this->cache_model->get_users($comments);
+ $max = 0;
+
+ foreach ($comments as $comment) {
+ $max = max($max, $comment['id']);
+ $data = array();
+ $data['user'] = $users[$comment['user_id']];
+ $data['comment'] = $comment;
+ $this->view('template/comment', $data);
+ }
+
+ return array(
+ 'loaded' => count($comments),
+ 'page_size' => $this->page_size,
+ 'max' => $max,
+ );
+ }
+}