summaryrefslogtreecommitdiff
path: root/web/_controller/apps/home.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-03-30 12:14:42 -0400
committerFreya Murphy <freya@freyacat.org>2024-03-30 12:14:42 -0400
commit1f04b83be337cc91a3fabcf4e574e2306f3d2eaa (patch)
tree74d7d65a7047e60d1877384e3c7b0d70c7b0e49a /web/_controller/apps/home.php
parentstart database (user and post), and initial barebones home page (diff)
downloadxssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.gz
xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.bz2
xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.zip
refactor
Diffstat (limited to 'web/_controller/apps/home.php')
-rw-r--r--web/_controller/apps/home.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/web/_controller/apps/home.php b/web/_controller/apps/home.php
new file mode 100644
index 0000000..25c8c4e
--- /dev/null
+++ b/web/_controller/apps/home.php
@@ -0,0 +1,89 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class Home_controller extends Controller {
+
+ // the home model
+ private $home_model;
+
+ // the request model
+ private $request_model;
+
+ // the caceh model
+ private $cache_model;
+
+ function __construct($load) {
+ parent::__construct($load);
+ $this->home_model = $this->load->model('apps/home');
+ $this->request_model = $this->load->model('request');
+ $this->cache_model = $this->load->model('cache');
+ }
+
+ public function index(): void {
+ parent::index();
+ $data = $this->home_model->get_data();
+ $this->view('header', $data);
+ $this->view('apps/home/main', $data);
+ }
+
+ public function posts(): void {
+ $page = $this->request_model->get_int('page', 0);
+ $page_size = 20;
+ $offset = $page * $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')
+ ->where('l.user_id')->eq($user['id'])
+ ->or()->where('l.user_id IS NULL');
+ }
+
+ $posts = $query->limit($page_size)
+ ->offset($offset)
+ ->rows();
+
+ $users = $this->cache_model->get_users($posts);
+
+ foreach ($posts as $post) {
+ $data = array();
+ $data['user'] = $users[$post['user_id']];
+ $data['post'] = $post;
+ $this->view('template/post', $data);
+ }
+ }
+
+ public function comments(): void {
+ $page = $this->request_model->get_int('page', 0);
+ $id = $this->request_model->get_int('id');
+ $page_size = 20;
+ $offset = $page * $page_size;
+
+ $comments = $this->db
+ ->select('*')
+ ->from('admin.comment')
+ ->limit($page_size)
+ ->offset($offset)
+ ->rows();
+
+ $users = $this->cache_model->get_users($comments);
+
+ foreach ($comments as $comment) {
+ $data = array();
+ $data['user'] = $users[$comment['user_id']];
+ $data['comment'] = $comment;
+ $this->view('template/comment', $data);
+ }
+ }
+
+}
+
+?>