summaryrefslogtreecommitdiff
path: root/web/routes/home/controller.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-03-29 22:29:56 -0400
committerFreya Murphy <freya@freyacat.org>2024-03-29 22:29:56 -0400
commit944b6b0526032ad8c1b4a2612d6723bec75e0e4c (patch)
treed3da5584df33a7878c087622b4fc2ec2883cf880 /web/routes/home/controller.php
downloadxssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.tar.gz
xssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.tar.bz2
xssbook2-944b6b0526032ad8c1b4a2612d6723bec75e0e4c.zip
start database (user and post), and initial barebones home page
Diffstat (limited to '')
-rw-r--r--web/routes/home/controller.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/web/routes/home/controller.php b/web/routes/home/controller.php
new file mode 100644
index 0000000..775e43a
--- /dev/null
+++ b/web/routes/home/controller.php
@@ -0,0 +1,84 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+class HomeController extends Controller {
+
+ private $model;
+
+ function __construct($model) {
+ parent::__construct();
+ $this->model = $model;
+ }
+
+ public function index() {
+ parent::index();
+ $data = $this->model->get_data();
+ $this->view('header', $data);
+ $this->app_view('main', $data);
+ }
+
+ public function posts() {
+ $page = $this->main->get_num('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('admin.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->main->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() {
+ $page = $this->main->get_num('page', 0);
+ $id = $this->main->get_num('id');
+ $page_size = 20;
+ $offset = $page * $page_size;
+
+ $comments = $this->db
+ ->select('*')
+ ->from('admin.comment')
+ ->limit($page_size)
+ ->offset($offset)
+ ->rows();
+
+ $users = $this->main->get_users($comments);
+
+ foreach ($comments as $comment) {
+ $data = array();
+ $data['user'] = $users[$comment['user_id']];
+ $data['comment'] = $comment;
+ $this->view('template/comment', $data);
+ }
+ }
+
+ public function new_post_modal() {
+ $this->modal(lang('new_post_modal_title'), 'new-post');
+ }
+
+}
+
+?>