237 lines
11 KiB
PHP
237 lines
11 KiB
PHP
|
<?php
|
||
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
|
||
|
class UserModel extends CI_Model
|
||
|
{
|
||
|
|
||
|
function __construct()
|
||
|
{
|
||
|
parent::__construct();
|
||
|
$this->load->model('BlogModel', '', TRUE);
|
||
|
$this->load->model('NotificationModel', '', TRUE);
|
||
|
}
|
||
|
|
||
|
function getUser($username)
|
||
|
{
|
||
|
$result = $this->db->query('SELECT ID, username, displayname, email, rank, profile_picture, header_image, about, social_networks, showAds, date_created, gender, language, country, birthdate, birthyear, receiveEmails, receiveNewsletter FROM users WHERE username = ? AND is_activated = TRUE LIMIT 1', [$username])->result_array();
|
||
|
if (empty($result)) {
|
||
|
return null;
|
||
|
}
|
||
|
$result = $this->setDefaultImages($result);
|
||
|
$result = $this->setRankname($result);
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function setDefaultImages($userList)
|
||
|
{
|
||
|
for ($i = 0; $i < sizeof($userList); $i++) {
|
||
|
if ((isset($userList[$i]['header_image']) && ($userList[$i]['header_image'] == '' || $userList[$i]['header_image'] == NULL)) || !isset($userList[$i]['header_image'])) {
|
||
|
$userList[$i]['header_image'] = substr(base_url(), 0, base_url() - 1) . ':3000/' . $userList[$i]['displayname'];
|
||
|
}
|
||
|
if (isset($userList[$i]['profile_picture']) && $userList[$i]['profile_picture'] == '') {
|
||
|
$userList[$i]['profile_picture'] = base_url('/f/8d204712d8132b36d765640ce775ce15');
|
||
|
}
|
||
|
}
|
||
|
return $userList;
|
||
|
}
|
||
|
|
||
|
public function setRankname($userList)
|
||
|
{
|
||
|
for ($i = 0; $i < sizeof($userList); $i++) {
|
||
|
if (isset($userList[$i]['rank'])) {
|
||
|
$userList[$i]['rankName'] = $this->GeneralModel->getRankName($userList[$i]['rank']);
|
||
|
}
|
||
|
}
|
||
|
return $userList;
|
||
|
}
|
||
|
|
||
|
public function getUserIDByUsername($username)
|
||
|
{
|
||
|
$result = $this->db->query('SELECT ID FROM users WHERE username = ?', [$username])->result_array();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function mergeFollowerCount($users) {
|
||
|
foreach ($users as $i => $user) {
|
||
|
$this->db->cache_off();
|
||
|
$followerCount = $this->db->query('SELECT count(*) followerCount FROM user_followers WHERE followedUserID = ?', [$user['ID']])->result_array();
|
||
|
$this->db->cache_on();
|
||
|
$users[$i]['followerCount'] = $followerCount[0]['followerCount'];
|
||
|
}
|
||
|
return $users;
|
||
|
}
|
||
|
|
||
|
public function getFollowers($id)
|
||
|
{
|
||
|
$this->db->cache_off();
|
||
|
$followers = $this->db->query('SELECT ID, followedSince, username, displayname, profile_picture, header_image FROM user_followers LEFT JOIN users ON ID = followerUserID WHERE followedUserID = ? ORDER BY followedSince DESC', [$id])->result_array();
|
||
|
$this->db->cache_on();
|
||
|
$followers = $this->setDefaultImages($followers);
|
||
|
$followers = $this->mergeFollowerCount($followers);
|
||
|
return $followers;
|
||
|
}
|
||
|
|
||
|
function getUserByID($id)
|
||
|
{
|
||
|
$result = $this->db->query('SELECT ID, original_name, username, displayname, email, rank, profile_picture, header_image, is_activated, about, lastLogin, social_networks, showAds, date_created, gender, language, country, birthdate, birthyear, receiveEmails, receiveNewsletter FROM users WHERE ID = ? AND is_activated = TRUE LIMIT 1', [$id])->result_array();
|
||
|
if (empty($result)) {
|
||
|
return null;
|
||
|
}
|
||
|
$result = $this->setDefaultImages($result);
|
||
|
$result = $this->setRankname($result);
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function getFollowing($id)
|
||
|
{
|
||
|
$this->db->cache_off();
|
||
|
$following = $this->db->query('SELECT ID, followedSince, username, displayname, profile_picture, header_image FROM user_followers LEFT JOIN users ON ID = followedUserID WHERE followerUserID = ? ORDER BY followedSince DESC', [$id])->result_array();
|
||
|
$this->db->cache_on();
|
||
|
$following = $this->setDefaultImages($following);
|
||
|
$following = $this->mergeFollowerCount($following);
|
||
|
return $following;
|
||
|
}
|
||
|
|
||
|
function getUserComments($id, $count, $offset)
|
||
|
{
|
||
|
$comments = $this->db->query('SELECT *, p.postUrl FROM blog_comments c LEFT JOIN blog_posts p ON p.postID = c.post_id WHERE user_id = ? ORDER BY date_created DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||
|
$comments = $this->BlogModel->mergePostTitleDesc($comments);
|
||
|
return $comments;
|
||
|
}
|
||
|
|
||
|
function getUserBlogPosts($id, $count, $offset)
|
||
|
{
|
||
|
$posts = $this->db->query('SELECT * FROM blog_posts WHERE postIsDeleted = FALSE AND postState = 1 AND postAuthorID = ? ORDER BY postPublishDate DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||
|
$posts = $this->BlogModel->mergePostTitleDesc($posts);
|
||
|
return $posts;
|
||
|
}
|
||
|
|
||
|
|
||
|
function getUserStats($userID)
|
||
|
{
|
||
|
$result = [];
|
||
|
$this->db->cache_off();
|
||
|
$user = $this->db->query('SELECT
|
||
|
COUNT(*) postCount,
|
||
|
(SELECT COUNT(*)
|
||
|
FROM user_followers
|
||
|
WHERE followedUserID = ?) followerCount,
|
||
|
(SELECT COUNT(*)
|
||
|
FROM user_followers
|
||
|
WHERE followerUserID = ?) followedCount
|
||
|
FROM user_posts
|
||
|
WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||
|
$result['postCount'] = $user['postCount'];
|
||
|
$result['followerCount'] = $user['followerCount'];
|
||
|
$result['followedCount'] = $user['followedCount'];
|
||
|
$blogResults = $this->db->query('SELECT COUNT(*) blogCount, (SELECT COUNT(*) FROM blog_comments WHERE user_id = ?) commentCount FROM blog_posts WHERE postIsDeleted = FALSE AND postState = 1 AND postAuthorID = ?', [$userID, $userID])->result_array()[0];
|
||
|
$result['blogCount'] = $blogResults['blogCount'];
|
||
|
$result['commentCount'] = $blogResults['commentCount'];
|
||
|
$this->db->cache_on();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
function isFollowing($followerID, $followingID)
|
||
|
{
|
||
|
$this->db->cache_off();
|
||
|
$response = $this->db->query('SELECT followedUserID FROM user_followers WHERE followerUserID = ? AND followedUserID = ?', [$followerID, $followingID])->result_array();
|
||
|
$this->db->cache_off();
|
||
|
return !empty($response);
|
||
|
}
|
||
|
|
||
|
function unfollow($followerID, $followedID)
|
||
|
{
|
||
|
$this->db->query('DELETE FROM user_followers WHERE followerUserID = ? AND followedUserID = ?', [$followerID, $followedID]);
|
||
|
|
||
|
// Remove notification
|
||
|
$this->NotificationModel->removeNotification($followerID, $followedID, $followedID, 'users.newFollower');
|
||
|
}
|
||
|
|
||
|
function follow($followerID, $followedID)
|
||
|
{
|
||
|
$this->db->query('INSERT INTO user_followers (followerUserID, followedUserID) VALUES (?, ?)', [$followerID, $followedID]);
|
||
|
|
||
|
// Send notification to followed user
|
||
|
$this->NotificationModel->userNotificationNewFollower($followerID, $followedID);
|
||
|
}
|
||
|
|
||
|
function updateProfile($data, $id)
|
||
|
{
|
||
|
$this->db->where('ID', $id);
|
||
|
$this->db->update('users', $data);
|
||
|
}
|
||
|
|
||
|
function insertIntoHistory($data)
|
||
|
{
|
||
|
unset($data['date_created']);
|
||
|
$this->db->insert('users_history', $data);
|
||
|
}
|
||
|
|
||
|
function getUserList($amount, $offset)
|
||
|
{
|
||
|
$data = $this->db->query('SELECT ID, username, displayname, rank, profile_picture, header_image, is_activated, showAds, receiveEmails, receiveNewsletter, date_created, isCurrentlyOnline, lastLogin, login_method, language, country, gender FROM users LIMIT ? OFFSET ?', [$amount, $offset])->result_array();
|
||
|
$data = $this->setDefaultImages($data);
|
||
|
$data = $this->setRankname($data);
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function updateOnline($id)
|
||
|
{
|
||
|
$this->db->query('UPDATE users SET isCurrentlyOnline = 1, lastOnlineUpdate = NOW() WHERE ID = ? LIMIT 1', [$id]);
|
||
|
}
|
||
|
|
||
|
function getActiveUsers($count)
|
||
|
{
|
||
|
$data = $this->db->query('SELECT username, displayname, profile_picture, lastLogin, (SELECT COUNT(*) FROM user_followers WHERE followedUserID = users.ID) follower_count FROM users WHERE isCurrentlyOnline = TRUE ORDER BY lastLogin DESC LIMIT ?', [$count])->result_array();
|
||
|
$data = $this->setDefaultImages($data);
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function getNewestUsers($count)
|
||
|
{
|
||
|
$data = $this->db->query('SELECT username, displayname, profile_picture, date_created, (SELECT COUNT(*) FROM user_followers WHERE followedUserID = users.ID) follower_count FROM users ORDER BY date_created DESC LIMIT ?', [$count])->result_array();
|
||
|
$data = $this->setDefaultImages($data);
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function searchUsers($query, $rank = '', $region = '', $lang = '', $amount = 3, $offset = 0) {
|
||
|
$conditions = [];
|
||
|
$inputs = [];
|
||
|
if($query !== '') {
|
||
|
$conditions[] = 'username RLIKE ?';
|
||
|
$inputs[] = $query;
|
||
|
}
|
||
|
if($rank !== '') {
|
||
|
$conditions[] = 'rank = ?';
|
||
|
$inputs[] = $rank;
|
||
|
}
|
||
|
if($region !== '') {
|
||
|
$conditions[] = 'country = ?';
|
||
|
$inputs[] = $region;
|
||
|
}
|
||
|
if($lang !== '') {
|
||
|
$conditions[] = 'language = ?';
|
||
|
$inputs[] = $lang;
|
||
|
}
|
||
|
|
||
|
$dbClause = join(' AND ', $conditions);
|
||
|
$inputs[] = $amount;
|
||
|
$inputs[] = $offset;
|
||
|
$data = $this->db->query('SELECT username, displayname, profile_picture, header_image, about, rank FROM users WHERE ' . $dbClause . ' LIMIT ? OFFSET ?', $inputs)->result_array();
|
||
|
|
||
|
$data = $this->setDefaultImages($data);
|
||
|
$data = $this->setRankname($data);
|
||
|
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function getAvailableCountries() {
|
||
|
return $this->db->query('SELECT country, count(*) countryUserCount FROM users WHERE country IS NOT NULL AND country != "" GROUP BY country ORDER BY country')->result_array();
|
||
|
}
|
||
|
|
||
|
public function getAvailableLanguages() {
|
||
|
return $this->db->query('SELECT language, count(*) langUserCount FROM users GROUP BY language ORDER BY language')->result_array();
|
||
|
}
|
||
|
|
||
|
}
|