<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=users.details.tags
[END_COT_EXT]
==================== */

defined('COT_CODE') or die('Wrong URL');

require_once cot_incfile('cotsocial', 'plug');

$action = cot_import('action','G','TXT');
$wid = cot_import('wid','G','INT');
$parent = cot_import('parent','P','INT');
$message = cot_import('message','P','TXT');


if($action == 'sent')
{

	$message .= $L['cotsocial_sent'] . ' ' . $urr['user_name'];

}
if($action == 'send')
{

	cot_social_post($urr['user_id'], $usr['id'], $parent, $sys['now'], $message);

}
if($action == 'remove')
{

	cot_social_remove($wid);

}
if($action == 'removefriend')
{

	cot_social_remove_friend($urr['user_id'], $usr['id']);

}
if($action == 'accept')
{

	cot_social_accept_request($urr['user_id'], $usr['id']);

}
if($action == 'decline')
{

	cot_social_remove_friend($urr['user_id'], $usr['id']);
	
}
if($action == 'friendrequest')
{

	if(!cot_social_check_friend($urr['user_id'], $usr['id']))
	{
	
		$text .= $usr['name'] .' ';
		$text .= $L['cotsocial_pm_text'];
		$accept_link = cot_url('users', 'm=details&id=' . $usr['id'] . '&action=accept', '', true);
		$decline_link = cot_url('users', 'm=details&id=' . $usr['id'] . '&action=decline', '', true);
		if($cfg['parser']=='bbcode')
		{
			$text .= ' [url='.$accept_link.']'.$L['cotsocial_accept_request'].'[/url] [url='.$decline_link.']'.$L['cotsocial_decline_request'].'[/url]';
		}
		else 
		{
			$text .= ' <a href="'.$accept_link.'">'.$L['cotsocial_accept_request'].'</a> <a href="'.$decline_link.'">'.$L['cotsocial_decline_request'].'</a>';
		}
	
		$pm['pm_title'] = $L['cotsocial_pm_title'];
		$pm['pm_date'] = (int)$sys['now'];
		$pm['pm_text'] = $text;
		$pm['pm_fromstate'] = 0;
		$pm['pm_fromuserid'] = 1;
		$pm['pm_fromuser'] = 'System';
		$pm['pm_touserid'] = $urr['user_id'];
		$pm['pm_tostate'] = 0;
		$pmsql = $db->insert($db_pm, $pm);
		$pmsql = $db->update($db_users, array('user_newpm' => '1'), "user_id = ".$urr['user_id']."");

		cot_social_add_friend($urr['user_id'], $usr['id'], $sys['now']);
		
	}
	else
	{
	
		$message .= $L['cotsocial_waiting'] . ' ' . $urr['user_name'];
		
	}

}

$parent_sql = $db->query("SELECT * FROM ".$db_cot_social_userwall." WHERE onuser = ".$urr['user_id']." AND parent = 0 ORDER by id DESC LIMIT 15");
while($parent = $parent_sql->fetch())
{

	$sql = $db->query("SELECT * FROM ".$db_users." WHERE user_id = ".$parent['fromuser']." LIMIT 1");
	$data = $sql->fetch();

	$t-> assign(array(
		'USERWALL_PARENT_ID' => (int)$parent['id'],
		'USERWALL_PARENT_ONUSER' => (int)$parent['onuser'],
		'USERWALL_PARENT_FROMUSER' => (int)$parent['fromuser'],
		'USERWALL_PARENT_FROMUSERNAME' => $data['user_name'],
		'USERWALL_PARENT_AVATAR' => $data['user_avatar'],
		'USERWALL_PARENT_PARENT' => (int)$parent['parent'],
		'USERWALL_PARENT_DATE' => cot_date('datetime_medium',$parent['date']),
		'USERWALL_PARENT_MESSAGE' => $parent['message'],
		'POST_FORM' => cot_url('users', 'm=details&id=' . $urr['user_id'] . '&action=send', '', true),
		'PARENT_REMOVE_URL' => cot_url('users', 'm=details&id=' . $urr['user_id'] . '&action=remove&wid=' . $parent['id'] . '', '', true)
	));
	
	$sibling_sql = $db->query("SELECT * FROM ".$db_cot_social_userwall." WHERE onuser = ".$urr['user_id']." AND parent = ".$parent['id']." ORDER by id ASC LIMIT 5");
	while($sibling = $sibling_sql->fetch())
	{
	
		$sql = $db->query("SELECT * FROM ".$db_users." WHERE user_id = ".$sibling['fromuser']." LIMIT 1");
		$data = $sql->fetch();
	
		$t-> assign(array(
		'USERWALL_SIBLING_ID' => (int)$sibling['id'],
		'USERWALL_SIBLING_ONUSER' => (int)$sibling['onuser'],
		'USERWALL_SIBLING_FROMUSER' => (int)$sibling['fromuser'],
		'USERWALL_SIBLING_FROMUSERNAME' => $data['user_name'],
		'USERWALL_SIBLING_AVATAR' => $data['user_avatar'],
		'USERWALL_SIBLING_PARENT' => (int)$sibling['parent'],
		'USERWALL_SIBLING_DATE' => cot_date('datetime_medium',$sibling['date']),
		'USERWALL_SIBLING_MESSAGE' => $sibling['message']
		));

		$t->parse('MAIN.USER_WALL.USER_WALL_PARENT.USER_WALL_SIBLING');
		
	}
	
	$t->parse('MAIN.USER_WALL.USER_WALL_PARENT');
	
}
$t->parse('MAIN.USER_WALL');

$totalfriends = $db->query("SELECT COUNT(*) FROM ".$db_cot_social_friendslist." WHERE (friendid = ".$urr['user_id']." OR userid = ".$urr['user_id'].")  AND status='1'")->fetchColumn();
$t-> assign(array('COTSOCIAL_TOTAL_FRIENDS' => (int)$totalfriends));

$friends_sql = $db->query("SELECT * FROM ".$db_cot_social_friendslist." WHERE (friendid = ".$urr['user_id']." OR userid = ".$urr['user_id'].") AND status='1' ORDER BY RAND() LIMIT 10");
while($data = $friends_sql->fetch())
{

	if($data['userid']==$urr['user_id']){$checkid = $data['friendid'];}else{$checkid = $data['userid'];}

	$sql = $db->query("SELECT * FROM ".$db_users." WHERE user_id = ".$checkid." LIMIT 1");
	$row = $sql->fetch();

	$t-> assign(array(
	'FRIENS_LIST_ID' => $data['id'],
	'FRIENS_LIST_FRIENDNAME' => $row['user_name'],
	'FRIENS_LIST_AVATAR' => $row['user_avatar'],
	'FRIENS_LIST_FRIENDID' => $checkid,
	'FRIENS_LIST_DATE' => cot_date('datetime_medium',$data['date']),
	));

	$t->parse('MAIN.FRIENDS_LIST');
	
}

if((cot_social_check_friend_status($usr['id'], $urr['user_id']) || $urr['user_id'] == $usr['id']) && $usr['id'] > 0)
{
$t-> assign(array(
	'REMOVE_FRIEND_LINK' => cot_url('users', 'm=details&id=' . $urr['user_id'] . '&action=removefriend', '', true),
	'WALL_FORM' => cot_url('users', 'm=details&id=' . $urr['user_id'] . '&action=send', '', true)
));
$t->parse('MAIN.ARE_FRIENDS');
}
else
{
	if($usr['id'] > 0)
	{
	$t-> assign(array('ADD_FRIEND_LINK' => cot_url('users', 'm=details&id=' . $urr['user_id'] . '&action=friendrequest', '', true)));
	$t->parse('MAIN.NOT_FRIENDS');
	}
}

if(!empty($message))
{
$t-> assign(array('MESSAGE' => $message));
$t->parse('MAIN.MESSAGES');
}

?>