<?php
/* 
	Appointment: Подписки
	File: following.php 
 
*/
if(!defined('MOZG'))
	die('Hacking attempt!');

NoAjaxQuery();

if($logged){
	$act = $_GET['act'];
	$user_id = $user_info['user_id'];
	
	switch($act){
		
		//################### Добвление юзера в подписки ###################//
		case "add":
			$for_user_id = intval($_POST['for_user_id']);
			
			//Проверка на существование юзера в подписках
			$check = $db->super_query("SELECT user_id FROM `".PREFIX."_friends` WHERE user_id = '{$user_id}' AND friend_id = '{$for_user_id}' AND following = 1");
			
			//ЧС
			$CheckBlackList = CheckBlackList($check['user_id']);
				
			if(!$CheckBlackList AND !$check){
				$db->query("INSERT INTO `".PREFIX."_friends` SET user_id = '{$user_id}', friend_id = '{$for_user_id}', friends_date = NOW(), following = 1");
				$db->query("UPDATE `".PREFIX."_users` SET following_num = following_num+1 WHERE user_id = '{$user_id}'");
				$db->query("UPDATE `".PREFIX."_users` SET followers_num = followers_num+1 WHERE user_id = '{$for_user_id}'");
				
				//Чистим кеш
				mozg_clear_cache_file('user_'.$user_id.'/profile_'.$user_id);
				mozg_clear_cache_file('following_user_'.$user_id);
			}
		break;
		
		//################### Удаление юзера из подписок ###################//
		case "delete":
			$del_user_id = intval($_POST['del_user_id']);
			
			//Проверка на существование юзера в подписках
			$check = $db->super_query("SELECT user_id FROM `".PREFIX."_friends` WHERE user_id = '{$user_id}' AND friend_id = '{$del_user_id}' AND following = 1");
			if($check){
				$db->query("DELETE FROM `".PREFIX."_friends` WHERE user_id = '{$user_id}' AND friend_id = '{$del_user_id}' AND following = 1");
				$db->query("UPDATE `".PREFIX."_users` SET following_num = following_num-1 WHERE user_id = '{$user_id}'");
				$db->query("UPDATE `".PREFIX."_users` SET followers_num = followers_num-1 WHERE user_id = '{$del_user_id}'");
				
				//Чистим кеш
				mozg_clear_cache_file('user_'.$user_id.'/profile_'.$user_id);
				mozg_clear_cache_file('following_user_'.$user_id);
			}
		break;
		
		//################### Считываем подписчиков ###################//
		case "followers":
			$user_id = intval($_POST['uid']);
			$gcount = 24;
			$limit_page = ($page-1)*$count;
			$followers_num = intval($_POST['followers_num']);

			$sql_ = $db->super_query("SELECT SQL_CALC_FOUND_ROWS tb1.friend_id, tb2.user_id, name_lastname, avatar, country_city FROM `".PREFIX."_friends` tb1, `".PREFIX."_users` tb2 WHERE tb1.friend_id = '{$user_id}' AND tb1.user_id = tb2.user_id AND tb1.following = 1 ORDER by `friends_date` DESC LIMIT {$limit_page}, {$gcount}", 1);
			
			if($sql_){
						
				$tpl->load_template('profile_followers.tpl');
				foreach($sql_ as $row){
					$friend_info_online = explode(' ', $row['name_lastname']);
					if($row['avatar'])
						$tpl->set('{avatar}', $config['home_url'].'uploads/users/'.$row['user_id'].'/50_'.$row['avatar']);
					else
						$tpl->set('{avatar}', $config['home_url'].'templates/Default/style/img/no_ava_50.png');
					$friend_info_online = explode(' ', $row['name_lastname']);
					$country_city_exp = explode('|', $row['country_city']);
					$tpl->set('{user-id}', $row['user_id']);
					$tpl->set('{name}', $friend_info_online[0]);
					$tpl->set('{last-name}', $friend_info_online[1]);
					$tpl->set('{country}', $country_city_exp[0]);
						$tpl->set('{city}', $country_city_exp[1]);
					$tpl->compile('content');

				}
				box_navigation($gcount, $user_id, 'Followers.All', $followers_num);
			}
			AjaxTpl();

		break;
		
		default:
		
			//################### Показ всех подпискок юзера ###################//
			if($_POST['page'] > 0) $page = intval($_POST['page']); else $page = 1;
			$count = 24;
			$limit_page = ($page-1)*$count;
			$for_user_id = intval($_POST['for_user_id']);
			$following_num = intval($_POST['following_num']);
			
			$sql_ = $db->super_query("SELECT SQL_CALC_FOUND_ROWS tb1.friend_id, tb2.name_lastname, avatar, country_city FROM `".PREFIX."_friends` tb1, `".PREFIX."_users` tb2 WHERE tb1.user_id = '{$for_user_id}' AND tb1.friend_id = tb2.user_id AND tb1.following = 1 ORDER by `friends_date` DESC LIMIT {$limit_page}, {$count}", 1);
			
			if($sql_){
						
				$tpl->load_template('profile_following.tpl');
				foreach($sql_ as $row){
					if($row['avatar'])
						$tpl->set('{avatar}', $config['home_url'].'uploads/users/'.$row['friend_id'].'/50_'.$row['avatar']);
					else
						$tpl->set('{avatar}', $config['home_url'].'templates/Default/images/no_ava_50.png');
					$friend_info_online = explode(' ', $row['name_lastname']);
					$country_city_exp = explode('|', $row['country_city']);
					$tpl->set('{user-id}', $row['friend_id']);
					$tpl->set('{name}', $friend_info_online[0]);
					$tpl->set('{last-name}', $friend_info_online[1]);
					$tpl->set('{country}', $country_city_exp[0]);
					$tpl->set('{city}', $country_city_exp[1]);
					$tpl->compile('content');
				}
				box_navigation($gcount, $following_num, $for_user_id, 'Following.All', $following_num);
			}
			AjaxTpl();
	}
	$tpl->clear();
	$db->free();
} else 
	echo 'no_log';

die();

?>