xssbook2/web/routes/home/controller.php

85 lines
1.7 KiB
PHP
Raw Normal View History

<?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');
}
}
?>