public static function delete($user) {
try {
if(!is_array($user)) {
$user = self::getSingle($user, 'id', TRUE);
}
// Delete content user image folder
$user_images_path = CHV_PATH_CONTENT_IMAGES_USERS . $user['id_encoded'];
if(!@unlink($user_images_path)) {
$files = glob($user_images_path.'/{,.}*', GLOB_BRACE);
foreach($files as $file){
if(is_file($file)) {
@unlink($file);
}
}
}
// Delete images from disk
$db = DB::getInstance();
$db->query('SELECT image_id FROM '.DB::getTable('images').' WHERE image_user_id=:image_user_id');
$db->bind(':image_user_id', $user['id']);
$user_images = $db->fetchAll();
foreach($user_images as $user_image) {
Image::delete($user_image['image_id']);
}
// Remove related notifications tied to this user (follows)
Notification::delete([
'table' => 'users',
'user_id' => $user['id'],
]);
// Track stats
Stat::track([
'action' => 'delete',
'table' => 'users',
'value' => '-1',
'user_id' => $user['id'],
'date_gmt' => $user['date_gmt']
]);
// Update affected user_likes count
$sql = strtr('UPDATE `%table_users` SET user_likes = user_likes - COALESCE((SELECT COUNT(*) FROM `%table_likes` WHERE like_user_id = %user_id AND user_id = like_content_user_id AND like_user_id <> like_content_user_id GROUP BY like_content_user_id),"0") WHERE user_likes > 0;', [
'%table_users' => DB::getTable('users'),
'%table_likes' => DB::getTable('likes'),
'%user_id' => $user['id'],
]);
DB::queryExec($sql);
// Update affected user_liked count (users who liked content owner by this user)
// --> Should happen in Image::delete()
// Update affected user_followers count
$sql = strtr('UPDATE `%table_users` SET user_followers = user_followers - COALESCE((SELECT 1 FROM `%table_follows` WHERE follow_user_id = %user_id AND user_id = follow_followed_user_id AND follow_user_id <> follow_followed_user_id GROUP BY follow_followed_user_id),"0") WHERE user_followers > 0;', [
'%table_users' => DB::getTable('users'),
'%table_follows'=> DB::getTable('follows'),
'%user_id' => $user['id'],
]);
DB::queryExec($sql);
// Update affected user_following count
$sql = strtr('UPDATE `%table_users` SET user_following = user_following - COALESCE((SELECT 1 FROM `%table_follows` WHERE follow_followed_user_id = %user_id AND user_id = follow_user_id AND follow_user_id <> follow_followed_user_id GROUP BY follow_user_id),"0") WHERE user_following > 0;', [
'%table_users' => DB::getTable('users'),
'%table_follows'=> DB::getTable('follows'),
'%user_id' => $user['id'],
]);
DB::queryExec($sql);
DB::delete('albums', ['user_id' => $user['id']]); // Delete albums DB
DB::delete('images', ['user_id' => $user['id']]); // Delete images DB
DB::delete('logins', ['user_id' => $user['id']]); // Delete logins
DB::delete('likes', ['user_id' => $user['id']]); // Delete user likes
DB::delete('follows', ['user_id' => $user['id'], 'followed_user_id' => $user['id']], 'OR'); // Delete user's followers and follows
DB::delete('users', ['id' => $user['id']]); // Delete user DB
} catch(Exception $e) {
throw new UserException($e->getMessage(), $e->getCode());
}
}