<?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__);
_setTitle($langArray['top_authors']);

	$usersClass = new users();

	$limit = 60;
	$start = (PAGE-1)*$limit;

	abr('number', ($start+1));

	$users = $usersClass->getAll($start, $limit, " `items` > 0 AND `status` = 'activate' ", "`sales` DESC");
	abr('users', $users);

	abr('paging', paging('/'.$languageURL.'top_authors/?p=', '', PAGE, $limit, $usersClass->foundRows));



#面包屑
	abr('breadcrumb', '<a href="/'.$languageURL.'" title="">'.$langArray['home'].'</a> \ <a href="/'.$languageURL.'users/top/" title="">'.$langArray['top_authors'].'</a>');


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

$badges_data = $badges->getAllFront();

$other_badges = array_map('trim', explode(',', $users['badges']));

$user_badges = array();

if($users['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($users['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($users['statuses']['freefile']) && $users['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($users['statuses']['featured']) && $users['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($users['buy'] && isset($badges_data['buyers']) && is_array($badges_data['buyers'])) {
	foreach($badges_data['buyers'] AS $k => $v) {
		list($from, $to) = explode('-', $k);
		if($from <= $users['buy'] && $to >= $users['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($users['sold'] && isset($badges_data['authors']) && is_array($badges_data['authors'])) {
	foreach($badges_data['authors'] AS $k => $v) {
		list($from, $to) = explode('-', $k);
		if($from <= $users['sold'] && $to >= $users['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($users['referals'] && isset($badges_data['referrals']) && is_array($badges_data['referrals'])) {
	foreach($badges_data['referrals'] AS $k => $v) {
		list($from, $to) = explode('-', $k);
		if($from <= $users['referals'] && $to >= $users['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($users['country']['photo']) && $users['country']['photo'] && file_exists(DATA_SERVER_PATH . "/uploads/countries/" . $users['country']['photo'])) {
	$user_badges[] = array(
		'name' => $users['country']['name'],
		'photo' => '/uploads/countries/' . $users['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($users['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($users['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);

?>