diff options
Diffstat (limited to 'src/web/_controller/_post.php')
-rw-r--r-- | src/web/_controller/_post.php | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/web/_controller/_post.php b/src/web/_controller/_post.php new file mode 100644 index 0000000..56c997f --- /dev/null +++ b/src/web/_controller/_post.php @@ -0,0 +1,201 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class _post_controller extends XSS_Controller { + + private $auth_model; + + function __construct() { + parent::__construct(); + $this->auth_model = $this->load_model('auth'); + $this->load_lang('post'); + } + + public function index(): void { + $this->view('_template/posts'); + } + + public function post(): void { + $pid = $this->get_int('id', 0); + + $post = $this->db() + ->select('p.*, l.id as like_id') + ->from('api.post p') + ->join('api.like l', 'p.id = l.post_id AND l.user_id') + ->eq($pid) + ->where('p.id') + ->eq($pid) + ->row(); + + if (!$post) + return; + + $users = $this->model->get_users([$post]); + $uid = $post['user_id']; + + if (!isset($users[$uid])) + return; + + $user = $users[$uid]; + + $data = $this->model->get_data(); + $data['user'] = $user; + $data['page_size'] = POST_PAGE_SIZE; + $data['post'] = $post; + $this->view('_template/post', $data); + } + + /** + * @return array<string,mixed> + */ + public function posts(): array { + $page = $this->get_int('page', 0); + $max = $this->get_int('max'); + $offset = $page * POST_PAGE_SIZE; + $filter_uid = $this->get_int('user_id', FALSE); + + $user = $this->auth_model->session(); + $uid = isset($user) ? $user['id'] : NULL; + + $query = $this->db() + ->select('p.*, l.id as like_id') + ->from('api.post p') + ->join('api.like l', 'p.id = l.post_id AND l.user_id') + ->eq($uid); + + if ($max) { + $query = $query + ->where('p.id')->le($max); + } + + if ($filter_uid) { + $query = $query + ->where('p.user_id')->eq($filter_uid); + } + + $posts = $query + ->order_by('p.id', 'DESC') + ->limit(POST_PAGE_SIZE) + ->offset($offset) + ->rows(); + + $users = $this->model->get_users($posts); + $max = 0; + + foreach ($posts as $post) { + $max = max($max, $post['id']); + $data = $this->model->get_data(); + $data['page_size'] = POST_PAGE_SIZE; + $data['user'] = $users[$post['user_id']]; + $data['post'] = $post; + $this->view('_template/post', $data); + } + + $query = $this->db() + ->select('COUNT(p.id) as pc') + ->from('api.post p'); + + if ($filter_uid) { + $query = $query + ->where('p.user_id')->eq($filter_uid); + } + + $pc = $query + ->row()['pc']; + + return array( + 'loaded' => count($posts), + 'total' => $pc, + 'page_size' => POST_PAGE_SIZE, + + 'max' => $max, + 'filter_uid' => $filter_uid + ); + } + + public function comment(): void { + $cid = $this->get_int('id', 0); + + $comment = $this->db() + ->select('*') + ->from('api.comment') + ->where('id') + ->eq($cid) + ->row(); + + if (!$comment) { + return; + } + + $users = $this->model->get_users([$comment]); + $uid = $comment['user_id']; + + if (!array_key_exists($uid, $users)) { + return; + } + + $user = $users[$uid]; + + $data = $this->model->get_data(); + $data['user'] = $user; + $data['comment'] = $comment; + $this->view('_template/comment', $data); + } + + /** + * @return array<string,mixed> + */ + public function comments(): array { + $page = $this->get_int('page', 0); + $max = $this->get_int('max'); + $id = $this->get_int('id', 0); + $offset = $page * COMMENT_PAGE_SIZE; + + $user = $this->auth_model->session(); + + $query = $this->db() + ->select('*') + ->from('api.comment') + ->where('post_id') + ->eq($id); + + if ($max) { + $query = $query + ->and() + ->where('id') + ->le($max); + } + + $comments = $query + ->order_by('id', 'ASC') + ->limit(COMMENT_PAGE_SIZE) + ->offset($offset) + ->rows(); + + $users = $this->model->get_users($comments); + $max = 0; + + // only add this hr when not logged in + // otherwise its added automatically by + // the like and comment buttons + if ( + count($comments) && + $page == 0 && + $user === NULL + ) { + echo '<hr>'; + } + + foreach ($comments as $comment) { + $max = max($max, $comment['id']); + $data = $this->model->get_data(); + $data['user'] = $users[$comment['user_id']]; + $data['comment'] = $comment; + $this->view('_template/comment', $data); + } + + return array( + 'loaded' => count($comments), + 'page_size' => COMMENT_PAGE_SIZE, + 'max' => $max, + ); + } +} |