2024-03-30 02:29:56 +00:00
|
|
|
<?php /* Copyright (c) 2024 Freya Murphy */
|
2024-03-30 16:14:42 +00:00
|
|
|
class Home_controller extends Controller {
|
2024-03-30 02:29:56 +00:00
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
// the home model
|
|
|
|
private $home_model;
|
2024-03-30 02:29:56 +00:00
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
// 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');
|
2024-03-30 02:29:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
public function index(): void {
|
2024-03-30 02:29:56 +00:00
|
|
|
parent::index();
|
2024-03-30 16:14:42 +00:00
|
|
|
$data = $this->home_model->get_data();
|
2024-03-30 02:29:56 +00:00
|
|
|
$this->view('header', $data);
|
2024-03-30 16:14:42 +00:00
|
|
|
$this->view('apps/home/main', $data);
|
2024-03-30 02:29:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
public function posts(): void {
|
|
|
|
$page = $this->request_model->get_int('page', 0);
|
2024-03-30 02:29:56 +00:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
$query = $query->from('api.post p');
|
2024-03-30 02:29:56 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
$users = $this->cache_model->get_users($posts);
|
2024-03-30 02:29:56 +00:00
|
|
|
|
|
|
|
foreach ($posts as $post) {
|
|
|
|
$data = array();
|
|
|
|
$data['user'] = $users[$post['user_id']];
|
|
|
|
$data['post'] = $post;
|
|
|
|
$this->view('template/post', $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
public function comments(): void {
|
|
|
|
$page = $this->request_model->get_int('page', 0);
|
|
|
|
$id = $this->request_model->get_int('id');
|
2024-03-30 02:29:56 +00:00
|
|
|
$page_size = 20;
|
|
|
|
$offset = $page * $page_size;
|
|
|
|
|
|
|
|
$comments = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('admin.comment')
|
|
|
|
->limit($page_size)
|
|
|
|
->offset($offset)
|
|
|
|
->rows();
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
$users = $this->cache_model->get_users($comments);
|
2024-03-30 02:29:56 +00:00
|
|
|
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
$data = array();
|
|
|
|
$data['user'] = $users[$comment['user_id']];
|
|
|
|
$data['comment'] = $comment;
|
|
|
|
$this->view('template/comment', $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|