    <?php
// +----------------------------------------------------------------------
// | mobanbox [ Beautiful Digital Content Trading System ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://mobanbox.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Email author@mobanbox.net
// +----------------------------------------------------------------------
_setView(__FILE__);
if(!check_login_bool()) {
		$_SESSION['temp']['golink'] = '/'.$languageURL.'upload/index/';
		refresh('/'.$languageURL.'sign_in/');
}

	if($_SESSION['user']['quiz'] != 'true') {
		refresh('/'.$languageURL.'quiz/');
	} 	

	if(!isset($_GET['category']) || !is_numeric($_GET['category']) || $_GET['category'] == '0') {
            $_GET['category'] = '';
	}

$allCategories = $categoriesClass->getAllWithChilds(0, " `visible` = 'true' ");
	if(!array_key_exists($_GET['category'], $allCategories)) {
		addErrorMessage($langArray['error_wrong_category'], '', 'error');
		abr('hideForm', 'true');
	}
	else {
		
		if(!isset($_POST['category'])) {
			$_POST['category'] = 0;
		}
		
		$categoriesSelect = $categoriesClass->generateSelect($allCategories, $_POST['category'], $_GET['category']);
		abr('categoriesSelect', $categoriesSelect);







#头部会员信息

require_once ROOT_PATH.'/apps/users/models/users.class.php';
$usersClass = new users();


if(check_login_bool() && $_SESSION['user']['user_id'] != $users['user_id']) {
		$users['is_follow'] = $usersClass->isFollow($users['user_id']);
	}

	$users['profile_desc'] = replaceEmoticons($users['profile_desc']);

	abr('user', $users);

	abr('checkItemsType', 'yes');

	if(check_login_bool() && ($username == '' || $username == $_SESSION['user']['username'])) {
		$username = $_SESSION['user']['username'];
		$whereQuery = " AND (`status` = 'active' OR `status` = 'unapproved' ) ";
	}
	else {
		$whereQuery = " AND `status` = 'active' ";
	}
	$user = $usersClass->getByUsername($username);

	abr('user', $user);


	$userss = $usersClass->getAll(0, 0, $itemsClass->usersWhere);
  	abr('userss', $userss);






//勋章
    require_once ROOT_PATH.'/apps/system/models/badges.class.php';
	$badges = new badges();

	$badges_data = $badges->getAllFront();

	$user = $_SESSION['user'];

	$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);


#获取粉丝
	$follow['to'] = $usersClass->getFollowers($users['user_id'], 0, 9, 'RAND()', true);
	$follow['to_count'] = $usersClass->foundRows;
	$follow['from'] = $usersClass->getFollowers($users['user_id'], 0, 9, 'RAND()');
	$follow['from_count'] = $usersClass->foundRows;
	abr('follow', $follow);


	$follow['toto'] = $usersClass->getFollowers($users['user_id'], 0, 10000000, 'RAND()', true);
	$follow['toto_count'] = $usersClass->foundRows;
	$follow['fromfrom'] = $usersClass->getFollowers($users['user_id'], 0, 10000000, 'RAND()');
	$follow['fromfrom_count'] = $usersClass->foundRows;
	abr('follow', $follow);


#关注用户
	if(check_login_bool() && isset($_GET['follow']) && $_SESSION['user']['user_id'] != $users['user_id']) {
		$usersClass->followUser($users['user_id']);
		if(isset($_POST)) {
			if($users['is_follow']) {
				$text = $langArray['follow'];
			}
			else {
				$text = $langArray['unfollow'];
			}
			die('
				jQuery("#follow").html("'.$text.'");
			');
		}
		refresh('/'.$languageURL.'user/'.$users['username']);
	}




#加载属性
		require_once ROOT_PATH.'/apps/attributes/models/attributes.class.php';
		$attributesClass = new attributes();
		
		$attributes = $attributesClass->getAllWithCategories(" `visible` = 'true' AND `categories` LIKE '%,".(int)$_GET['category'].",%' ");
		abr('attributes', $attributes);
		
#保存作品
		if(isset($_POST['upload'])) {
			require_once ROOT_PATH.'/apps/items/models/items.class.php';
			$itemsClass = new items();
			
			$s = $itemsClass->add();
			if($s === true) {
				refresh('/'.$languageURL.'author_dashboard/', $langArray['complete_upload_item'], 'complete');
			}
			else {
				$message = '<ul>';
				foreach($s as $e) {
					$message .= '<li>'.$e.'</li>';
				}
				$message .= '</ul>';
				addErrorMessage($message, '', 'error');
			}
		}		
		
		$fileTypes = '';
	  foreach($config['upload_ext'] as $ext) {
	  	if($fileTypes != '') {	  		
	  		$fileTypes .= ';';
	  	}
	  	$fileTypes .= '*.'.$ext;	  	
	  }
	  abr('fileTypes', $fileTypes);
	  
	  abr('sessID', session_id());
		
	}


    //获取所有推荐标签
    require_once ROOT_PATH.'/apps/tags/models/tags.class.php';
    $tagsClass = new tags();
    $tags_all = $tagsClass->getAll();
    $all_tags = json_encode($tags_all);

    abr('all_tags',$all_tags);

#面包屑	
	abr('breadcrumb', '<a href="/'.$languageURL.'" title="">'.$langArray['home'].'</a> \ <a href="/'.$languageURL.'upload/form/?category='.$_GET['category'].'" title="">'.$langArray['upload_theme'].'</a>');		
	

?>