<?php 
_setView(__FILE__);

	$username = get_id(1);
	
_setTitle($username);	
	
	$usersClass = new users();
	
	$user = $usersClass->getByUsername($username);
	if(!is_array($user)) {
		header("HTTP/1.0 404 Not Found");
        header("Location: http://". DOMAIN ."/error");	
	}
	
	if(check_login_bool() && $_SESSION['user']['user_id'] != $user['user_id']) {
		$user['is_follow'] = $usersClass->isFollow($user['user_id']);
	}

	$user['profile_desc'] = replaceEmoticons($user['profile_desc']);
	
	abr('user', $user);
	
	
#LOAD CATEGORIES
		require_once ROOT_PATH.'/zipmarket/apps/categories/models/categories.class.php';
		$categoriesClass = new categories();
	
		$categories = $categoriesClass->getAll();
		abr('categories', $categories);	
	
#SEND EMAIL
	if(check_login_bool() && isset($_POST['send_email'])) {
		$s = $usersClass->sendEmail();
		if($s === true) {
			refresh('/'.$languageURL.'user/'.$user['username'], $langArray['complete_send_email'], 'complete');
		}
		else {
			addErrorMessage($s, '', 'error');
		}
	}
		
#FOLLOW USER
	if(check_login_bool() && isset($_GET['follow']) && $_SESSION['user']['user_id'] != $user['user_id']) {
		$usersClass->followUser($user['user_id']);
		if(isset($_POST)) {			
			if($user['is_follow']) {
				$text = $langArray['follow'];
			}
			else {
				$text = $langArray['unfollow'];
			}
			die('
				jQuery("#follow").html("'.$text.'");
			');
		}
		refresh('/'.$languageURL.'user/'.$user['username']);
	}	
	
#LOAD PUBLIC COLLECTIONS
	require_once ROOT_PATH.'/zipmarket/apps/collections/models/collections.class.php';
	$collectionsClass = new collections();

	$collections = $collectionsClass->getAll(0, 2, " `public` = 'true' AND `user_id` = '".intval($user['user_id'])."' ");
	abr('collections', $collections);
	
#GET FEATURE FILE
	if($user['featured_item_id'] != '0') {
		require_once ROOT_PATH.'/zipmarket/apps/items/models/items.class.php';
		$itemsClass = new items();
		
		$featureItem = $itemsClass->get($user['featured_item_id'], true);
		abr('featureItem', $featureItem);
	}

#GET FOLLOWERS
	$follow['to'] = $usersClass->getFollowers($user['user_id'], 0, 9, 'RAND()', true);
	$follow['to_count'] = $usersClass->foundRows;
	$follow['from'] = $usersClass->getFollowers($user['user_id'], 0, 9, 'RAND()');
	$follow['from_count'] = $usersClass->foundRows;	
	abr('follow', $follow);
	
	
	$follow['toto'] = $usersClass->getFollowers($user['user_id'], 0, 10000000, 'RAND()', true);
	$follow['toto_count'] = $usersClass->foundRows;
	$follow['fromfrom'] = $usersClass->getFollowers($user['user_id'], 0, 10000000, 'RAND()');
	$follow['fromfrom_count'] = $usersClass->foundRows;	
	abr('follow', $follow);
	
#BREADCRUMB	
	abr('breadcrumb', '<a href="/'.$languageURL.'" title="">'.$langArray['home'].'</a> \ <a href="/'.$languageURL.'users/'.$user['username'].'" title="">'.$user['username'].'</a>');		
	
	require_once ROOT_PATH.'/zipmarket/apps/system/models/badges.class.php';
	$badges = new badges();
	
	$badges_data = $badges->getAllFront();
	
	$other_badges = array_map('trim', explode(',', $user['badges']));
	
	$user_badges = array();
	
	if($user['exclusive_author'] == 'true' && isset($badges_data['system']['is_exclusive_author'])) {
		if($badges_data['system']['is_exclusive_author']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['is_exclusive_author']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['is_exclusive_author']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['is_exclusive_author']['photo']
			);
		}
	}
	
	if($user['featured_author'] == 'true' && isset($badges_data['system']['has_been_featured'])) {
		if($badges_data['system']['has_been_featured']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['has_been_featured']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['has_been_featured']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['has_been_featured']['photo']
			);
		}
	}
	
	if(isset($user['statuses']['freefile']) && $user['statuses']['freefile'] && isset($badges_data['system']['has_free_file_month'])) {
		if($badges_data['system']['has_free_file_month']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['has_free_file_month']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['has_free_file_month']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['has_free_file_month']['photo']
			);
		}
	}
	
	if(isset($user['statuses']['featured']) && $user['statuses']['featured'] && isset($badges_data['system']['has_had_item_featured'])) {
		if($badges_data['system']['has_free_file_month']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['has_had_item_featured']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['has_had_item_featured']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['has_had_item_featured']['photo']
			);
		}
	}
	
	if($user['buy'] && isset($badges_data['buyers']) && is_array($badges_data['buyers'])) {
		foreach($badges_data['buyers'] AS $k => $v) {
			list($from, $to) = explode('-', $k);
			if($from <= $user['buy'] && $to >= $user['buy']) {
				if($v['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $v['photo'])) {
					$user_badges[] = array(
						'name' => $v['name'],
						'photo' => '/uploads/badges/' . $v['photo']
					);
				}
				break;
			}
		}
	}
	
	if($user['sold'] && isset($badges_data['authors']) && is_array($badges_data['authors'])) {
		foreach($badges_data['authors'] AS $k => $v) {
			list($from, $to) = explode('-', $k);
			if($from <= $user['sold'] && $to >= $user['sold']) {
				if($v['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $v['photo'])) {
					$user_badges[] = array(
						'name' => $v['name'],
						'photo' => '/uploads/badges/' . $v['photo']
					);
				}
				break;
			}
		}
	}
	
	if($user['referals'] && isset($badges_data['referrals']) && is_array($badges_data['referrals'])) {
		foreach($badges_data['referrals'] AS $k => $v) {
			list($from, $to) = explode('-', $k);
			if($from <= $user['referals'] && $to >= $user['referals']) {
				if($v['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $v['photo'])) {
					$user_badges[] = array(
						'name' => $v['name'],
						'photo' => '/uploads/badges/' . $v['photo']
					);
				}
				break;
			}
		}
	}
	
	if(isset($badges_data['other']) && is_array($badges_data['other'])) {
		foreach($badges_data['other'] AS $k => $b) {
			if(in_array($k, $other_badges) && $b['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $b['photo'])) {
				$user_badges[] = array(
					'name' => $b['name'],
					'photo' => '/uploads/badges/' . $b['photo']
				);
			}
		}
	}
	
	if(isset($user['country']['photo']) && $user['country']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/countries/" . $user['country']['photo'])) {
		$user_badges[] = array(
			'name' => $user['country']['name'],
			'photo' => '/uploads/countries/' . $user['country']['photo']
		);
	} elseif(isset($badges_data['system']['location_global_community']) && $badges_data['system']['location_global_community']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['location_global_community']['photo'])) {
		$user_badges[] = array(
			'name' => $badges_data['system']['location_global_community']['name'],
			'photo' => '/uploads/badges/' . $badges_data['system']['location_global_community']['photo']
		);
	}

	if($user['power_elite_author'] == 'true' && isset($badges_data['system']['power_elite_author'])) {
		if($badges_data['system']['power_elite_author']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['has_been_featured']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['power_elite_author']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['power_elite_author']['photo']
			);
		}
	}
	
	if($user['elite_author'] == 'true' && isset($badges_data['system']['elite_author'])) {
		if($badges_data['system']['elite_author']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/badges/" . $badges_data['system']['has_been_featured']['photo'])) {
			$user_badges[] = array(
				'name' => $badges_data['system']['elite_author']['name'],
				'photo' => '/uploads/badges/' . $badges_data['system']['elite_author']['photo']
			);
		}
	}
	
	abr('user_badges', $user_badges);
	
	//echo '<pre>';
	//var_dump($user_badges); exit;
	//var_dump($badges_data['system'], $user_badges); exit;
	
	
?>