summaryrefslogtreecommitdiff
path: root/src/web/_controller/_post.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-12-23 11:13:27 -0500
committerFreya Murphy <freya@freyacat.org>2024-12-23 11:13:27 -0500
commit5a2ba9c2e7605bb788bc406184547d22c6436867 (patch)
treecbd988d534e8a8593a31d70571222443f80da0b3 /src/web/_controller/_post.php
parentfix about modal (diff)
downloadxssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.tar.gz
xssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.tar.bz2
xssbook2-5a2ba9c2e7605bb788bc406184547d22c6436867.zip
v2.1.0, refactor w/ crimson
Diffstat (limited to '')
-rw-r--r--src/web/_controller/_post.php (renamed from src/web/_controller/_util/post.php)113
1 files changed, 51 insertions, 62 deletions
diff --git a/src/web/_controller/_util/post.php b/src/web/_controller/_post.php
index 5346497..56c997f 100644
--- a/src/web/_controller/_util/post.php
+++ b/src/web/_controller/_post.php
@@ -1,30 +1,22 @@
<?php /* Copyright (c) 2024 Freya Murphy */
-class Post_controller extends Controller {
+class _post_controller extends XSS_Controller {
- // the request model
- private $request_model;
+ private $auth_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;
+ function __construct() {
+ parent::__construct();
+ $this->auth_model = $this->load_model('auth');
+ $this->load_lang('post');
}
public function index(): void {
- $this->view('template/posts');
+ $this->view('_template/posts');
}
public function post(): void {
- $pid = $this->request_model->get_int('id', 0);
+ $pid = $this->get_int('id', 0);
- $post = $this->db
+ $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')
@@ -33,42 +25,37 @@ class Post_controller extends Controller {
->eq($pid)
->row();
- if (!$post) {
+ if (!$post)
return;
- }
- $users = $this->cache_model->get_users([$post]);
+ $users = $this->model->get_users([$post]);
$uid = $post['user_id'];
- if (!array_key_exists($uid, $users)) {
+ if (!isset($users[$uid]))
return;
- }
$user = $users[$uid];
- $data = array(
- 'user' => $user,
- 'page_size' => $this->page_size,
- 'post' => $post
- );
- $this->view('template/post', $data);
+ $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->request_model->get_int('page', 0);
- $max = $this->request_model->get_int('max');
- $offset = $page * $this->page_size;
- $filter_uid = $this->request_model->get_int('user_id', FALSE);
+ $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->main->user();
+ $user = $this->auth_model->session();
$uid = isset($user) ? $user['id'] : NULL;
- $query = $this->db;
-
- $query = $this->db
+ $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')
@@ -86,23 +73,23 @@ class Post_controller extends Controller {
$posts = $query
->order_by('p.id', 'DESC')
- ->limit($this->page_size)
+ ->limit(POST_PAGE_SIZE)
->offset($offset)
->rows();
- $users = $this->cache_model->get_users($posts);
+ $users = $this->model->get_users($posts);
$max = 0;
foreach ($posts as $post) {
$max = max($max, $post['id']);
- $data = array();
- $data['page_size'] = $this->page_size;
+ $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);
+ $this->view('_template/post', $data);
}
- $query = $this->db
+ $query = $this->db()
->select('COUNT(p.id) as pc')
->from('api.post p');
@@ -117,16 +104,17 @@ class Post_controller extends Controller {
return array(
'loaded' => count($posts),
'total' => $pc,
- 'page_size' => $this->page_size,
+ 'page_size' => POST_PAGE_SIZE,
+
'max' => $max,
'filter_uid' => $filter_uid
);
}
public function comment(): void {
- $cid = $this->request_model->get_int('id', 0);
+ $cid = $this->get_int('id', 0);
- $comment = $this->db
+ $comment = $this->db()
->select('*')
->from('api.comment')
->where('id')
@@ -137,7 +125,7 @@ class Post_controller extends Controller {
return;
}
- $users = $this->cache_model->get_users([$comment]);
+ $users = $this->model->get_users([$comment]);
$uid = $comment['user_id'];
if (!array_key_exists($uid, $users)) {
@@ -146,23 +134,24 @@ class Post_controller extends Controller {
$user = $users[$uid];
- $data = array(
- 'user' => $user,
- 'comment' => $comment
- );
- $this->view('template/comment', $data);
+ $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->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;
+ $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
+ $query = $this->db()
->select('*')
->from('api.comment')
->where('post_id')
@@ -177,11 +166,11 @@ class Post_controller extends Controller {
$comments = $query
->order_by('id', 'ASC')
- ->limit($this->page_size)
+ ->limit(COMMENT_PAGE_SIZE)
->offset($offset)
->rows();
- $users = $this->cache_model->get_users($comments);
+ $users = $this->model->get_users($comments);
$max = 0;
// only add this hr when not logged in
@@ -190,22 +179,22 @@ class Post_controller extends Controller {
if (
count($comments) &&
$page == 0 &&
- $this->main->session === NULL
+ $user === NULL
) {
echo '<hr>';
}
foreach ($comments as $comment) {
$max = max($max, $comment['id']);
- $data = array();
+ $data = $this->model->get_data();
$data['user'] = $users[$comment['user_id']];
$data['comment'] = $comment;
- $this->view('template/comment', $data);
+ $this->view('_template/comment', $data);
}
return array(
'loaded' => count($comments),
- 'page_size' => $this->page_size,
+ 'page_size' => COMMENT_PAGE_SIZE,
'max' => $max,
);
}