Database refactoring and improving blog comments, blog post history and more
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
|
||||
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 AND isDeleted = FALSE LIMIT 1', [$username])->result_array();
|
||||
$result = $this->db->query('SELECT u.ID, u.username, u.displayname, u.email, u.rank, u.dateCreated, s.profilePicture, s.headerImage, s.about, s.socialNetworks, s.showAds, s.gender, s.language, s.country, s.birthdate, s.birthyear, s.receiveEmails, s.receiveNewsletter FROM users u INNER JOIN user_settings s ON s.ID = u.ID WHERE username = ? AND activated = TRUE AND isDeleted = FALSE LIMIT 1', [strtolower($username)])->result_array();
|
||||
if (empty($result)) {
|
||||
return null;
|
||||
}
|
||||
@@ -25,11 +25,11 @@
|
||||
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'] = 'https://cdn.kinogofdog.eu' . '/' . $userList[$i]['displayname'];
|
||||
if ((isset($userList[$i]['headerImage']) && ($userList[$i]['headerImage'] == '' || $userList[$i]['headerImage'] == NULL)) || !isset($userList[$i]['headerImage'])) {
|
||||
$userList[$i]['headerImage'] = 'https://cdn.kinogofdog.eu' . '/' . $userList[$i]['displayname'];
|
||||
}
|
||||
if (isset($userList[$i]['profile_picture']) && $userList[$i]['profile_picture'] == '') {
|
||||
$userList[$i]['profile_picture'] = base_url('/f/8d204712d8132b36d765640ce775ce15');
|
||||
if (isset($userList[$i]['profilePicture']) && $userList[$i]['profilePicture'] == '') {
|
||||
$userList[$i]['profilePicture'] = base_url('/f/8d204712d8132b36d765640ce775ce15');
|
||||
}
|
||||
}
|
||||
return $userList;
|
||||
@@ -51,7 +51,18 @@
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function mergeFollowerCount($users) {
|
||||
public function getFollowers($id)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$followers = $this->db->query('SELECT u.ID, followedSince, username, displayname, profilePicture, headerImage FROM user_followers LEFT JOIN users u ON u.ID = followerUserID LEFT JOIN user_settings s ON s.ID = followerUserID WHERE followedUserID = ? AND u.activated = TRUE AND u.isDeleted = FALSE ORDER BY followedSince DESC', [$id])->result_array();
|
||||
$this->db->cache_on();
|
||||
$followers = $this->setDefaultImages($followers);
|
||||
$followers = $this->mergeFollowerCount($followers);
|
||||
return $followers;
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -61,19 +72,9 @@
|
||||
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 = ? AND is_activated = TRUE AND isDeleted = FALSE 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 AND isDeleted = FALSE LIMIT 1', [$id])->result_array();
|
||||
$result = $this->db->query('SELECT u.ID, originalName, username, displayname, email, rank, profilePicture, headerImage, activated, about, lastLogin, socialNetworks, showAds, dateCreated, gender, language, country, birthdate, birthyear, receiveEmails, receiveNewsletter FROM users u LEFT JOIN user_settings s ON s.ID = u.ID WHERE u.ID = ? AND activated = TRUE AND isDeleted = FALSE LIMIT 1', [$id])->result_array();
|
||||
if (empty($result)) {
|
||||
return null;
|
||||
}
|
||||
@@ -82,15 +83,16 @@
|
||||
return $result;
|
||||
}
|
||||
|
||||
function getUserEmailByID($id) {
|
||||
$result = $this->db->query('SELECT email FROM users WHERE ID = ? AND is_activated = TRUE AND isDeleted = TRUE', [$id])->result_array();
|
||||
function getUserEmailByID($id)
|
||||
{
|
||||
$result = $this->db->query('SELECT email FROM users WHERE ID = ? AND activated = TRUE AND isDeleted = TRUE', [$id])->result_array();
|
||||
return !empty($result) ? $result[0]['email'] : '';
|
||||
}
|
||||
|
||||
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 = ? AND isDeleted = FALSE ORDER BY followedSince DESC', [$id])->result_array();
|
||||
$following = $this->db->query('SELECT u.ID, followedSince, username, displayname, profilePicture, headerImage FROM user_followers LEFT JOIN users u ON u.ID = followedUserID LEFT JOIN user_settings s ON s.ID = followedUserID WHERE followerUserID = ? AND isDeleted = FALSE ORDER BY followedSince DESC', [$id])->result_array();
|
||||
$this->db->cache_on();
|
||||
$following = $this->setDefaultImages($following);
|
||||
$following = $this->mergeFollowerCount($following);
|
||||
@@ -99,15 +101,15 @@
|
||||
|
||||
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);
|
||||
$comments = $this->db->query('SELECT c.* FROM blog_post_comments c WHERE userID = ? ORDER BY date DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||||
$comments = $this->BlogModel->mergePostTranslation($comments, false, null, 'postID');
|
||||
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);
|
||||
$posts = $this->db->query('SELECT * FROM blog_post WHERE state = 1 AND authorID = ? ORDER BY initialRelease DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||||
$posts = $this->BlogModel->mergePostTranslation($posts);
|
||||
return $posts;
|
||||
}
|
||||
|
||||
@@ -125,11 +127,11 @@
|
||||
FROM user_followers
|
||||
WHERE followerUserID = ?) followedCount
|
||||
FROM user_posts
|
||||
WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
WHERE userID = ?', [$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];
|
||||
$blogResults = $this->db->query('SELECT COUNT(*) blogCount, (SELECT COUNT(*) FROM blog_post_comments WHERE userID = ?) commentCount FROM blog_post WHERE state = 1 AND authorID = ?', [$userID, $userID])->result_array()[0];
|
||||
$result['blogCount'] = $blogResults['blogCount'];
|
||||
$result['commentCount'] = $blogResults['commentCount'];
|
||||
$this->db->cache_on();
|
||||
@@ -162,19 +164,39 @@ WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
|
||||
function updateProfile($data, $id)
|
||||
{
|
||||
$this->db->where('ID', $id);
|
||||
$this->db->update('users', $data);
|
||||
function in_data1($val) {
|
||||
return in_array($val, ['username', 'displayname', 'email', 'password']);
|
||||
}
|
||||
|
||||
$data1 = array_filter($data, "in_data1");
|
||||
if(!empty($data1)) {
|
||||
$this->db->where('ID', $id);
|
||||
$this->db->update('users', $data1);
|
||||
}
|
||||
|
||||
$data2 = array_diff($data, $data1);
|
||||
if(!empty($data2)) {
|
||||
$this->db->where('ID', $id);
|
||||
$this->db->update('user_settings', $data2);
|
||||
}
|
||||
}
|
||||
|
||||
function insertIntoHistory($data)
|
||||
{
|
||||
unset($data['date_created']);
|
||||
$this->db->insert('users_history', $data);
|
||||
if(isset($data['password'])) {
|
||||
unset($data['password']);
|
||||
$data['passwordChanged'] = true;
|
||||
}
|
||||
|
||||
$this->db->insert('user_settings_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->db->query('SELECT u.ID, username, displayname, rank, profilePicture, headerImage, activated, showAds, receiveEmails, receiveNewsletter, dateCreated, isCurrentlyOnline, lastLogin, loginMethod, language, country, gender
|
||||
FROM users u
|
||||
LEFT JOIN user_settings s ON u.ID = s.ID
|
||||
LIMIT ? OFFSET ?', [$amount, $offset])->result_array();
|
||||
$data = $this->setDefaultImages($data);
|
||||
$data = $this->setRankname($data);
|
||||
return $data;
|
||||
@@ -187,34 +209,35 @@ WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
|
||||
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 AND is_activated = TRUE AND isDeleted = FALSE ORDER BY lastLogin DESC LIMIT ?', [$count])->result_array();
|
||||
$data = $this->db->query('SELECT username, displayname, profilePicture, lastLogin, (SELECT COUNT(*) FROM user_followers WHERE followedUserID = u.ID) follower_count FROM users u LEFT JOIN user_settings s ON s.ID = u.ID WHERE isCurrentlyOnline = TRUE AND activated = TRUE AND isDeleted = FALSE 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 WHERE is_activated = TRUE AND isDeleted = FALSE ORDER BY date_created DESC LIMIT ?', [$count])->result_array();
|
||||
$data = $this->db->query('SELECT username, displayname, profilePicture, dateCreated, (SELECT COUNT(*) FROM user_followers WHERE followedUserID = u.ID) follower_count FROM users u LEFT JOIN user_settings s ON s.ID = u.ID WHERE activated = TRUE AND isDeleted = FALSE ORDER BY dateCreated DESC LIMIT ?', [$count])->result_array();
|
||||
$data = $this->setDefaultImages($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function searchUsers($query, $rank = '', $region = '', $lang = '', $amount = 3, $offset = 0) {
|
||||
public function searchUsers($query, $rank = '', $region = '', $lang = '', $amount = 3, $offset = 0)
|
||||
{
|
||||
$conditions = [];
|
||||
$inputs = [];
|
||||
if($query !== '') {
|
||||
if ($query !== '') {
|
||||
$conditions[] = 'username RLIKE ?';
|
||||
$inputs[] = $query;
|
||||
}
|
||||
if($rank !== '') {
|
||||
if ($rank !== '') {
|
||||
$conditions[] = 'rank = ?';
|
||||
$inputs[] = $rank;
|
||||
}
|
||||
if($region !== '') {
|
||||
if ($region !== '') {
|
||||
$conditions[] = 'country = ?';
|
||||
$inputs[] = $region;
|
||||
}
|
||||
if($lang !== '') {
|
||||
if ($lang !== '') {
|
||||
$conditions[] = 'language = ?';
|
||||
$inputs[] = $lang;
|
||||
}
|
||||
@@ -222,7 +245,7 @@ WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
$dbClause = join(' AND ', $conditions);
|
||||
$inputs[] = $amount;
|
||||
$inputs[] = $offset;
|
||||
$data = $this->db->query('SELECT username, displayname, profile_picture, header_image, about, rank FROM users WHERE is_activated = TRUE AND isDeleted = FALSE AND ' . $dbClause . ' LIMIT ? OFFSET ?', $inputs)->result_array();
|
||||
$data = $this->db->query('SELECT username, displayname, profilePicture, headerImage, about, rank FROM users u LEFT JOIN user_settings s ON s.ID = u.ID WHERE activated = TRUE AND isDeleted = FALSE AND ' . $dbClause . ' LIMIT ? OFFSET ?', $inputs)->result_array();
|
||||
|
||||
$data = $this->setDefaultImages($data);
|
||||
$data = $this->setRankname($data);
|
||||
@@ -230,19 +253,23 @@ WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
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 getAvailableCountries()
|
||||
{
|
||||
return $this->db->query('SELECT country, count(*) countryUserCount FROM user_settings 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();
|
||||
public function getAvailableLanguages()
|
||||
{
|
||||
return $this->db->query('SELECT language, count(*) langUserCount FROM user_settings GROUP BY language ORDER BY language')->result_array();
|
||||
}
|
||||
|
||||
public function deleteUser($id) {
|
||||
public function deleteUser($id)
|
||||
{
|
||||
$this->db->query('UPDATE users SET isDeleted = TRUE, isCurrentlyOnline = FALSE, lastOnlineUpdate = NULL WHERE ID = ?', [$id])->result_array();
|
||||
}
|
||||
|
||||
public function getPermissions($userID) {
|
||||
public function getPermissions($userID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$result = $this->db->query('SELECT * FROM user_permissions WHERE userID = ?', [$userID])->result_array();
|
||||
$this->db->cache_on();
|
||||
@@ -256,18 +283,21 @@ WHERE user_id = ?', [$userID, $userID, $userID])->result_array()[0];
|
||||
return $perms;
|
||||
}
|
||||
|
||||
public function hasPermission($userID, $permType, $permName) {
|
||||
public function hasPermission($userID, $permType, $permName)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$result = $this->db->query('SELECT ID FROM user_permissions WHERE userID = ? AND permissionType = ? AND permissionName = ?', [$userID, $permType, $permName])->result_array();
|
||||
$this->db->cache_on();
|
||||
return !empty($result);
|
||||
}
|
||||
|
||||
public function addPermission($userID, $permissionGroup, $permissionName, $givenBy) {
|
||||
public function addPermission($userID, $permissionGroup, $permissionName, $givenBy)
|
||||
{
|
||||
$this->db->query('INSERT INTO user_permissions (userID, permissionType, permissionName, givenBy) VALUES (?, ?, ?, ?)', [$userID, $permissionGroup, $permissionName, $givenBy]);
|
||||
}
|
||||
|
||||
public function revokePermission($userID, $permissionGroup, $permissionName) {
|
||||
public function revokePermission($userID, $permissionGroup, $permissionName)
|
||||
{
|
||||
$this->db->query('DELETE FROM user_permissions WHERE userID = ? AND permissionType = ? AND permissionName = ?', [$userID, $permissionGroup, $permissionName]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user