<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class follow_class extends ASK_MODEL
{
	public function user_follow_add($fans_uid, $friend_uid)
	{		
		if ($fans_uid == $friend_uid)
		{
			return false;
		}
		
		if (! $this->model('account')->check_uid($fans_uid) || ! $this->model('account')->check_uid($friend_uid))
		{
			return false;
		}
		
		if ($this->user_follow_check($fans_uid, $friend_uid))
		{
			return false;
		}
		else
		{
			$insert_data['fans_uid'] = intval($fans_uid);
			$insert_data['friend_uid'] = intval($friend_uid);
			$insert_data['add_time'] = time();
			
			$result = $this->insert('user_follow', $insert_data);
			
			$this->update_user_count($friend_uid);
			$this->update_user_count($fans_uid);
			
			return $result;
		}
	
	}

	public function user_follow_check($fans_uid, $friend_uid)
	{
		if (! $fans_uid || ! $friend_uid)
		{
			return false;
		}
		
		if ($fans_uid == $friend_uid)
		{
			return false;
		}
		
		return $this->count('user_follow', "fans_uid = " . intval($fans_uid) . " AND friend_uid = " . intval($friend_uid));	
	}
	
	public function users_follow_check($fans_uid, $friend_uids)
	{		
		if (! $fans_uid OR ! is_array($friend_uids))
		{
			return false;
		}
		
		$user_follow = $this->fetch_all('user_follow', "fans_uid = " . intval($fans_uid) . " AND friend_uid IN (" . implode(',', $friend_uids) . ")");
		
		foreach ($user_follow AS $key => $val)
		{
			$result[$val['friend_uid']] = TRUE;
		}
		
		return $result;
	}
	
	public function user_follow_del($fans_uid, $friend_uid)
	{
		if (! $fans_uid || ! $friend_uid)
		{
			return false;
		}
		
		if (! $this->user_follow_check($fans_uid, $friend_uid))
		{
			return false;
		}
		else
		{
			$result = $this->delete("user_follow", "fans_uid = " . intval($fans_uid) . " AND friend_uid = " . intval($friend_uid));
			
			$this->update_user_count($friend_uid);
			$this->update_user_count($fans_uid);
			
			return $result;
		}
	}

	/**
	 * Get a list of individual users Fans
	 * 
	 * @param  $friend_uid		
	 * @param  $limit
	 */
	public function get_user_fans($friend_uid, $limit = 20)
	{		
		if (!$user_fans = $this->fetch_all('user_follow', 'friend_uid = ' . intval($friend_uid), 'add_time DESC', $limit))
		{
			return false;
		}
		
		foreach ($user_fans AS $key => $val)
		{
			$fans_uids[] = $val['fans_uid'];
		}
		
		return $this->model('account')->get_user_info_by_uids($fans_uids, true);
	}

	/**
	 * Get a single user Watchlist (People I have followed)
	 * 
	 * @param  $friend_uid
	 * @param  $limit
	 */
	public function get_user_friends($fans_uid, $limit = 20)
	{
		if (!$user_follow = $this->fetch_all('user_follow', 'fans_uid = ' . intval($fans_uid), 'add_time DESC', $limit))
		{
			return false;
		}
		
		foreach ($user_follow AS $key => $val)
		{
			$friend_uids[] = $val['friend_uid'];
		}
		
		return $this->model('account')->get_user_info_by_uids($friend_uids, true);
	}
	
	public function get_fans_count($friend_uid)
	{
		return $this->count('user_follow', 'friend_uid = ' . intval($friend_uid));
	}

	public function get_friends_count($fans_uid)
	{
		return $this->count('user_follow', 'fans_uid = ' . intval($fans_uid));
	}
	
	public function update_user_count($uid)
	{	
		return $this->update('users', array(
			'fans_count' => $this->get_fans_count($uid),
			'friend_count' => $this->get_friends_count($uid)
		), 'uid = ' . intval($uid));
	}
}