profile page

This commit is contained in:
Freya Murphy 2024-04-03 11:25:57 -04:00
parent 7e2553646c
commit 740df27069
Signed by: freya
GPG key ID: 744AB800E383AE52
19 changed files with 201 additions and 44 deletions

View file

@ -61,6 +61,7 @@ class Post_controller extends Controller {
$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);
$user = $this->main->user();
$uid = isset($user) ? $user['id'] : NULL;
@ -78,6 +79,11 @@ class Post_controller extends Controller {
->where('p.id')->le($max);
}
if ($uid) {
$query = $query
->where('p.user_id')->eq($uid);
}
$posts = $query
->order_by('p.id', 'DESC')
->limit($this->page_size)
@ -96,9 +102,16 @@ class Post_controller extends Controller {
$this->view('template/post', $data);
}
$pc = $this->db
$query = $this->db
->select('COUNT(p.id) as pc')
->from('api.post p')
->from('api.post p');
if ($uid) {
$query = $query
->where('p.user_id')->eq($uid);
}
$pc = $query
->row()['pc'];
return array(
@ -106,6 +119,7 @@ class Post_controller extends Controller {
'total' => $pc,
'page_size' => $this->page_size,
'max' => $max,
'filter_uid' => $filter_uid
);
}

View file

@ -0,0 +1,35 @@
<?php /* Copyright (c) 2024 Freya Murphy */
class Profile_controller extends Controller {
// the home model
private $profile_model;
// the format model
protected $format_model;
// the post model
protected $post_controller;
function __construct($load) {
parent::__construct($load);
$this->profile_model = $this->load->model('apps/profile');
$this->format_model = $this->load->model('format');
$this->post_controller = $this->load->controller('_util/post');
}
public function index(): void {
parent::index();
$data = $this->profile_model->get_data();
if (!$data) {
$this->error(404);
}
$this->view('header', $data);
$this->view('apps/profile/main', $data);
$this->view('footer', $data);
}
}
?>

View file

@ -5,7 +5,7 @@ class Auth_model extends Model {
parent::__construct($load);
}
public function get_data(): array {
public function get_data(): ?array {
$data = parent::get_data();
$data['title'] = lang('login');
return $data;

View file

@ -22,7 +22,7 @@ class Error_model extends Model {
}
}
public function get_data(): array {
public function get_data(): ?array {
$data = parent::get_data();
$this->get_msg($data);
return $data;

View file

@ -13,7 +13,7 @@ class Home_model extends Model {
->rows();
}
public function get_data(): array {
public function get_data(): ?array {
$data = parent::get_data();
$data['title'] = lang('title');
$data['posts'] = $this->get_posts();

View file

@ -80,7 +80,7 @@ class People_model extends Model {
);
}
public function get_data(): array {
public function get_data(): ?array {
$data = parent::get_data();
$data['title'] = lang('title');
return $data;

View file

@ -0,0 +1,37 @@
<?php /* Copyright (c) 2024 Freya Murphy */
class Profile_model extends Model {
private $request_model;
function __construct($load) {
parent::__construct($load);
$this->request_model = $this->load->model('request');
}
public function get_data(): ?array {
$uid = $this->request_model->get_int('id', FALSE);
if ($uid === FALSE) {
if ($this->main->session) {
$uid = $this->main->user()['id'];
} else {
return NULL;
}
}
$user = $this->db
->select('*')
->from('api.user u')
->where('u.id')
->eq($uid)
->row();
if (!$user) {
return NULL;
}
$data = parent::get_data();
$data['user'] = $user;
$data['title'] = lang('title', sub: [$user['first_name']]);
return $data;
}
}

View file

@ -49,7 +49,7 @@
<script>
var onLogin = function(data) {
let jwt = data.token;
var jwt = data.token;
$.ajax({
url: '/auth/update',
@ -66,13 +66,13 @@
$('#action-login').on('submit', function(e) {
e.preventDefault();
let username = $('#login-username').val();
let password = $('#login-password').val();
var username = $('#login-username').val();
var password = $('#login-password').val();
$.ajax({
url: '/api/rpc/login',
method: 'POST',
data: JSON.stringify({ username, password }),
data: JSON.stringify({ username: username, password: password }),
success: onLogin
});
});

View file

@ -18,7 +18,7 @@
<?php if ($loaded >= $page_size && $page_size < $total): ?>
<?=ilang('action_load_users',
id: 'action-load-users',
class: 'btn btn-line btn-wide mt mb',
class: 'btn btn-line btn-wide mb',
attrs: array(
'loaded' => $loaded,
'pageSize' => $page_size,
@ -31,27 +31,27 @@
var urlParams = new URLSearchParams(window.location.search).toString();
$('#action-load-users').on('click', function() {
let me = $(this);
let page = me.attr('page');
var me = $(this);
var page = me.attr('page');
if (!page) {
page = '1';
}
let newPage = Number(page) + 1;
var newPage = Number(page) + 1;
me.attr('page', newPage + '');
let loaded = Number(me.attr('loaded'));
let pageSize = Number(me.attr('pageSize'));
let userCount = Number(me.attr('userCount'));
let userMax = Number(me.attr('userMax'));
var loaded = Number(me.attr('loaded'));
var pageSize = Number(me.attr('pageSize'));
var userCount = Number(me.attr('userCount'));
var userMax = Number(me.attr('userMax'));
let url = '/people/people?page=' + page + '&max=' + userMax + '&' + urlParams;
var url = '/people/people?page=' + page + '&max=' + userMax + '&' + urlParams;
$.get(url, function (data) {
if (data === '') {
me.remove();
return;
}
let container = $('#people-container');
var container = $('#people-container');
container.append(data);
loaded += pageSize;

View file

@ -0,0 +1,37 @@
<div id="main-content">
<div id="profile-header" class="col">
<div class="banner image-loading">
<img src="/api/rpc/profile_banner?user_id=<?=$user['id']?>">
</div>
<div class="info row">
<div class="pfp-wrapper">
<?php $this->view('template/pfp', array('user' => $user)); ?>
</div>
<div class="col content">
<strong class="name"><?=$this->format_model->name($user)?></strong>
<span class="dim"><?=lang('joined') . $this->format_model->date($user['created'])?></span>
</div>
</div>
<hr>
<div class="row options">
<?=ilang('action_posts',
sub: [$user['first_name']],
class: 'btn'
)?>
<?=ilang('action_about',
sub: [$user['first_name']],
class: 'btn'
)?>
<?=ilang('action_friends',
sub: [$user['first_name']],
class: 'btn'
)?>
</div>
</div>
<div id="#tab-posts">
<?php
$_GET['user_id'] = $user['id'];
$this->post_controller->index();
?>
</div>
</div>

View file

@ -55,7 +55,7 @@
</div>
<script>
$('#action-hamburger').on('click', function() {
let menu = $('.nav-center');
var menu = $('.nav-center');
menu.toggleClass('visible');
});
</script>

View file

@ -31,18 +31,18 @@
<script>
$('#new-post-form').submit(function(e) {
e.preventDefault();
let content = $('#new-post-content').val();
let me = $(this);
var content = $('#new-post-content').val();
var me = $(this);
const getPost = function(data) {
var getPost = function(data) {
if (data) {
$('#post-container').prepend(data);
}
me.closest('.modal-container').remove();
}
const onPost = function(data) {
let id = data[0].id;
var onPost = function(data) {
var id = data[0].id;
$.get({
url: '/_util/post/post?id=' + id,
success: getPost
@ -52,7 +52,7 @@
$.ajax({
url: '/api/post',
method: 'POST',
data: JSON.stringify({ content }),
data: JSON.stringify({ content: content }),
success: onPost
});
});

View file

@ -133,28 +133,28 @@
$('#register-form').submit(function(e) {
e.preventDefault();
const form = event.target;
const formFields = form.elements;
var form = event.target;
var formFields = form.elements;
let first_name = formFields.first_name.value.trim();
let last_name = formFields.last_name.value.trim();
let username = formFields.username.value.trim();
let password = formFields.password.value.trim();
let email = formFields.email.value.trim();
let birth_date = formFields.birth_date.value.trim();
let gender = formFields.gender.value.trim();
var first_name = formFields.first_name.value.trim();
var last_name = formFields.last_name.value.trim();
var username = formFields.username.value.trim();
var password = formFields.password.value.trim();
var email = formFields.email.value.trim();
var birth_date = formFields.birth_date.value.trim();
var gender = formFields.gender.value.trim();
if(birth_date === '') {
errorToast('toast_date_empty');
return;
}
const onSuccess = () => {
var onSuccess = function() {
$.ajax({
url: '/api/rpc/login',
method: 'POST',
data: JSON.stringify({
username, password
username: username, password: password
}),
success: onLogin
});
@ -164,8 +164,13 @@
url: '/api/user',
method: 'POST',
data: JSON.stringify({
first_name, last_name, username, password,
email, birth_date, gender
first_name: first_name,
last_name: last_name,
username: username,
password: password,
email: email,
birth_date: birth_date,
gender: gender
}),
success: onSuccess
});

View file

@ -6,16 +6,18 @@
$page_size = $pdata['page_size'];
$total = $pdata['total'];
$max = $pdata['max'];
$filterUid = $pdata['filter_uid'];
if ($loaded >= $page_size && $page_size < $total) {
ilang('action_load_posts',
id: 'action-load-posts',
class: 'btn btn-line btn-wide mb',
class: 'btn btn-line btn-wide mb mt',
attrs: array(
'loaded' => $loaded,
'pageSize' => $page_size,
'postCount' => $total,
'postMax' => $max,
'userId' => $filterUid
)
);
}

View file

@ -22,7 +22,6 @@ class Aesthetic {
),
'home' => array(
'js' => [
'js/routes/home.js',
'js/post.js',
],
'css' => [
@ -40,6 +39,15 @@ class Aesthetic {
'css/people.css'
],
),
'profile' => array(
'js' => [
'js/post.js',
],
'css' => [
'css/profile.css',
'css/post.css'
],
),
);
}
/**

View file

@ -5,5 +5,6 @@ $routes['home'] = 'apps/home';
$routes['error'] = 'apps/error';
$routes['auth'] = 'apps/auth';
$routes['people'] = 'apps/people';
$routes['profile'] = 'apps/profile';
$routes[''] = '_index';

View file

@ -24,7 +24,7 @@ abstract class Model {
/**
* @returns the base model data
*/
public function get_data(): array {
public function get_data(): ?array {
$data = array();
$data['self'] = $this->main->user();

View file

@ -26,7 +26,7 @@ function ilang($key,
$button = FALSE,
) {
$text = lang($key . "_text", FALSE, sub: $sub);
$tip = lang($key . "_tip", FALSE);
$tip = lang($key . "_tip", FALSE, sub: $sub);
$icon = lang($key . "_icon", FALSE);
$content = lang($key . "_content", FALSE);

View file

@ -0,0 +1,18 @@
<?php
$lang['title'] = '%s\'s profile';
$lang['joined'] = 'Joined: ';
$lang['seen'] = 'Seen: ';
$lang['action_posts_text'] = 'Posts';
$lang['action_posts_tip'] = 'View %s\'s posts';
$lang['action_about_text'] = 'About';
$lang['action_about_tip'] = 'View %s\'s information';
$lang['action_friends_text'] = 'Friends';
$lang['action_friends_tip'] = 'View %s\'s friends';
$lang['action_load_posts_text'] = 'Load more posts';
$lang['action_load_posts_tip'] = 'Load more posts';
?>