<?php
/**
 * @version $Id: user_friend.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

define('PB_CSCRIPT', 'user_friend');
require_once('include/common.inc.php');

$uid = (int)$uid;
$rssurl = 'rss.php?action=user&amp;uid='.$uid;
$userdata = $friends = $rs = $guestmsg = array();

$SQL = "SELECT m.uid, m.username, m.email, m.regdate, m.publicemail, 
	mx.qq, mx.msn, mx.site, mx.location, mx.birthday, mx.signature, mx.avatar, mx.gender 
	FROM {$db_prefix}members m 
	LEFT JOIN {$db_prefix}memberexp mx 
	USING (uid) 
	WHERE m.uid = $uid";
$userdata = $DB->db_fetch_one_array($SQL);
if (!$userdata)
{
	showMsg($u_message['user_id_noexist'], $_PBDEV['PB_REFERER']);
}
if (!$userdata['publicemail'])
{
	$userdata['email'] = $u_message['secrecy'];
}
if (!$userdata['avatar'])
{
	$userdata['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
}
elseif (substr($userdata['avatar'], 0, 7) != 'http://')
{
	$userdata['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$userdata['avatar'];
}
switch ($userdata['gender'])
{
	case '0':
		$userdata['gender_name'] = $u_message['male'];
	break;
	
	case '1':
		$userdata['gender_name'] = $u_message['female'];
	break;
	
	default:
		$userdata['gender_name'] = $u_message['secrecy'];
	break;
}

$pagesize = (int)$pb_aperpage;
$result = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}friends WHERE uid = $uid AND ifconfirm = 1");
$totalRecords = (int)$result['num'];
$page = !is_Page($page) ? 1 : $page;
$start_limit = ($page -1) * $pagesize;
$mult = multLink($page, $totalRecords, 'user_friend.php?uid='.$uid.'&amp;', $pagesize);

$SQL = "SELECT m.uid, m.username, mx.avatar 
		FROM {$db_prefix}friends f 
		LEFT JOIN {$db_prefix}members m 
		ON f.fuid = m.uid 
		LEFT JOIN {$db_prefix}memberexp mx 
		ON m.uid = mx.uid 
		WHERE f.uid = $uid 
		AND f.ifconfirm = 1 
		ORDER BY f.fid DESC 
		LIMIT $start_limit, $pagesize";
$DB->db_query($SQL);
while ($rs = $DB->db_fetch_array())
{
	if (!$rs['avatar'])
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
	}
	elseif (substr($rs['avatar'], 0, 7) != 'http://')
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$rs['avatar'];
	}
	$friends[] = $rs;
}

$SQL = "SELECT gm.authorid, gm.author, gm.content, gm.postdate, mx.avatar 
		FROM {$db_prefix}guestmsg gm 
		LEFT JOIN {$db_prefix}memberexp mx 
		ON gm.authorid = mx.uid 
		WHERE gm.uid = $uid 
		ORDER BY gm.gmid DESC 
		LIMIT 10";
$DB->db_query($SQL);

while ($rs = $DB->db_fetch_array())
{
	if (!$rs['author'])
	{
		$rs['author'] = $pb_sitename.$u_message['net_friend'];
	}
	$rs['content'] = PBSubstr(str_replace(array('[emote]', '[/emote]'), '', $rs['content']), $pb_indexcontent);
	if (!$rs['avatar'])
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
	}
	elseif (substr($rs['avatar'], 0, 7) != 'http://')
	{
		$rs['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$rs['avatar'];
	}
	$rs['postdate'] = formatPostTime($rs['postdate']);
	$guestmsg[]= $rs;
}

$seo['pagetitle'] = $seo['keywords'] = $seo['description'] = ($userdata['username'].' - '.$u_message['user_friends']);

$TPL->assign('mult_page', $mult);
$TPL->assign('userdata', $userdata);
$TPL->assign('friends', $friends);
$TPL->assign('guestmsg', $guestmsg);
$TPL->assign('customer', $customer);
$TPL->assign('seo', $seo);
$TPL->assign('rss_url', $_PBENV['PB_URL'].$rssurl);
unset($rs, $userdata, $mult, $friends, $customer, $guestmsg, $seo);
PBOutPut('user_friend.html');