<?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__);


	require_once ROOT_PATH.'/apps/users/models/users.class.php';
	$usersClass = new users();
			
	$users = $usersClass->getAll(0, 0, $itemsClass->usersWhere);
	abr('users', $users);


#推荐作品
	$weeklyItems = $itemsClass->getAll(0, 4, " `status` = 'active' AND `weekly_to` >= '".date('Y-m-d')."' ", "`datetime` DESC");
	abr('weeklyItems', $weeklyItems);
	
	if($itemsClass->foundRows > 10) {
		abr('haveWeekly', 'yes');
	}

#加载分类
		require_once ROOT_PATH.'/apps/categories/models/categories.class.php';
		$categoriesClass = new categories();
	
		$categories = $categoriesClass->getAll();
		abr('categories', $categories);	 
	
#近期作品
	$recentItems = $itemsClass->getAll(0, 8, " `status` = 'active' ", '`datetime` DESC');
	kshuffle($recentItems);
	abr('recentItems', $recentItems);

#最热
$hotItems = $itemsClass->getAll(0, 8, " `status` = 'active' ", '`sales` DESC');
kshuffle($hotItems);
abr('hotItems', $hotItems);
	
    //免费作品
	$freeItem = $itemsClass->getAll(0, 8, " `status` = 'active' AND `free_file` = 'true' ");

	abr('freeItem', $freeItem);
	
#推荐作者
	require_once ROOT_PATH.'/apps/users/models/users.class.php';
	$usersClass = new users();
    //获取用户信息及粉丝
	$featuredAuthor = $usersClass->getAll(0, 0, " `status` = 'activate' AND `featured_author` = 'true' ", 'RAND()');
	// if(is_array($featuredAuthor)) {
	// 	//作者的一些作品
	// 	$featuredItems = array();
    //      foreach($featuredAuthor as $itear){
	// 	    $featuredItems[$itear['user_id']] = $itemsClass->getAll(0, 2, " `status` = 'active' AND `user_id` = '".intval($itear['user_id'])."' ");
	// 	}
	// 	abr('featuredItems', $featuredItems);
		
	// 	abr('featuredAuthorInfo', langMessageReplace($langArray['featured_author_info'], array(
	// 															'USERNAME' => $featuredAuthor['username'],
	// 															'MONTH' => $langArray['monthArr'][date('n', strtotime($featuredAuthor['register_datetime']))],
	// 															'YEAR' => date('Y', strtotime($featuredAuthor['register_datetime'])),
	// 															'ITEMS' => $featuredAuthor['items'],
	// 															'SALES' => $featuredAuthor['sales']
	// 														)));
	// }
	abr('featuredAuthor', $featuredAuthor);


#推荐作者
	require_once ROOT_PATH.'/apps/users/models/users.class.php';
	$usersClass = new users();
	$newAuthor = $usersClass->getAll(0, 0, " `status` = 'activate' ", 'RAND()');
	abr('newAuthor', $newAuthor);

#用户关注的作者的最新作品
	if(check_login_bool()) {
		$following = $usersClass->getFollowersID($_SESSION['user']['user_id']);
		if(is_array($following)) {
			$whereQuery = '';
			foreach($following as $f) {
				if($whereQuery != '') {
					$whereQuery .= ' OR ';
				}
				$whereQuery .= " `user_id` = '".intval($f['follow_id'])."' ";
			}
			
			$followingItems = $itemsClass->getAll(0, 0, " `status` = 'active' AND ($whereQuery) ", "`datetime` DESC");
			abr('followingItems', $followingItems);
			
			abr('followingItemsCount', $itemsClass->foundRows);
			abr('emptyThumb', (10-$itemsClass->foundRows));
		}
	}
#王牌作者
	

		$topAuthors = $usersClass->getAll(0, 9, " `status` = 'activate' and `sales` > 0 ", "`sales` DESC");
		abr('topAuthors', $topAuthors);
		
		abr('topAuthorsCount', $usersClass->foundRows);
		abr('emptyThumb', (9-$usersClass->foundRows));
		
	

#随机分类
	$randCategories = array_rand($mainCategories, 5);
	abr('randCategories', $randCategories);	
	
#最低价格
	$lowPrice = $itemsClass->getAll(0, 1, " `status` = 'active' ", "`price` ASC");
	if(is_array($lowPrice)) {
		$lowPrice = array_shift($lowPrice);
		$lowPrice = $lowPrice['price'];
	}
	abr('lowPrice', $lowPrice);
	
#快讯
	
	require_once ROOT_PATH.'/apps/qnews/models/qnews.class.php';
	$qnews = new qnews();
	$data = array();
	foreach($qnews->getAll(0, 1, "`visible` = 'true'") AS $key => $value) {
		if($value['photo']) {
			$data[$key] = $value;
			$data[$key]['thumb'] = 'static/uploads/qnews/260x140/' . $value['photo'];
		}
	}
	
	abr('qnews_data', $data);
	
#面包屑
	abr('breadcrumb', '<a href="/'.$languageURL.'" title="">'.$langArray['home'].'</a>');


//读取所有tags   require_once ROOT_PATH. "/apps/tags/models/tags.class.php";//引入tag的数据库模型类
//读取所有tags           $tags = new tags();
//读取所有tags           $getTags = $tags->getAll();//获取数据模型，结果为一个数组
//读取所有tags           abr('tags_new',$getTags);//在smarty模板中加入变量‘tags_new’
//读取所有tags {foreach from=$tags_new item=t}
//读取所有tags {$t.name}
//读取所有tags {/foreach}
	
//开始。。
//获取所有一级分类
	require_once ROOT_PATH . "/apps/categories/models/categories.class.php";
    $categoriesClass = new categories();
    $allCategories_0 = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`=0 ");
    $htmlmb = array();
    foreach ($allCategories_0 as $categories_data) {
    	//获取当前分类子分类
    	$categories_son = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`= ".intval($categories_data['id'])." ");
        //获取一级分类下得所有作品
        $whereQuery = " AND `id` IN (SELECT `item_id` FROM `items_to_category` WHERE `categories` LIKE '%,".intval($categories_data['id']).",%') ";
        $items = $itemsClass->getAll(0, 3, " `status` = 'active' ".$whereQuery, "RAND()");
        $htmlmb[$categories_data['id']][] = array(
            'categories' => $categories_data,
            'categories_son' => $categories_son,
            'items' => $items
        );
    }
    abr('htmlmb',$htmlmb);


$allCategories_1 = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`=0 ");
$chengxumb = array();
foreach ($allCategories_1 as $categories_data) {
	//获取当前分类子分类
	$categories_son = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`= ".intval($categories_data['id'])." ");
	//获取一级分类下得所有作品
	$whereQuery = " AND `id` IN (SELECT `item_id` FROM `items_to_category` WHERE `categories` LIKE '%,".intval($categories_data['id']).",%') ";
	$items = $itemsClass->getAll(0, 8, " `status` = 'active' ".$whereQuery);
	$chengxumb[$categories_data['id']][] = array(
		'categories' => $categories_data,
		'categories_son' => $categories_son,
		'items' => $items
	);
}
abr('chengxumb',$chengxumb);


$allCategories_1 = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`=0 ");
$code = array();
foreach ($allCategories_1 as $categories_data) {
	//获取当前分类子分类
	$categories_son = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`= ".intval($categories_data['id'])." ");
	//获取一级分类下得所有作品
	$whereQuery = " AND `id` IN (SELECT `item_id` FROM `items_to_category` WHERE `categories` LIKE '%,".intval($categories_data['id']).",%') ";
	$items = $itemsClass->getAll(0, 4, " `status` = 'active' ".$whereQuery);
	$code[$categories_data['id']][] = array(
		'categories' => $categories_data,
		'categories_son' => $categories_son,
		'items' => $items
	);
}
abr('code',$code);


$allCategories_1 = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`=0 ");
$gaoqingpic = array();
foreach ($allCategories_1 as $categories_data) {
	//获取当前分类子分类
	$categories_son = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`= ".intval($categories_data['id'])." ");
	//获取一级分类下得所有作品
	$whereQuery = " AND `id` IN (SELECT `item_id` FROM `items_to_category` WHERE `categories` LIKE '%,".intval($categories_data['id']).",%') ";
	$items = $itemsClass->getAll(0, 6, " `status` = 'active' ".$whereQuery);
	$gaoqingpic[$categories_data['id']][] = array(
		'categories' => $categories_data,
		'categories_son' => $categories_son,
		'items' => $items
	);
}
abr('gaoqingpic',$gaoqingpic);

$allCategories_1 = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`=0 ");
$ziti = array();
foreach ($allCategories_1 as $categories_data) {
	//获取当前分类子分类
	$categories_son = $categoriesClass->getAll(0, 0, " `visible` = 'true' AND `sub_of`= ".intval($categories_data['id'])." ");
	//获取一级分类下得所有作品
	$whereQuery = " AND `id` IN (SELECT `item_id` FROM `items_to_category` WHERE `categories` LIKE '%,".intval($categories_data['id']).",%') ";
	$items = $itemsClass->getAll(0, 6, " `status` = 'active' ".$whereQuery);
	$ziti[$categories_data['id']][] = array(
		'categories' => $categories_data,
		'categories_son' => $categories_son,
		'items' => $items
	);
}
abr('ziti',$ziti);

	
?>