<?php
/**
 * @author zlc
 * @author 用户关注的板块帖子列表，按最新发布时间排序
 */
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

//未登录跳到登录页面
if(!$_G['uid']){
    odz_error('token_expire');     
}


$pagesize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 20;
$page = intval($_GET['page']);
$page = $page <= 0 ? 1 : $page;

$start = ($page - 1) * $pagesize;


//取出用户关注的结果
$result = DB::fetch_all('SELECT fid FROM '.DB::table('mocuz_member_focus')."  WHERE uid = {$_G['uid']}");

if(empty($result)){
	odz_error('none_focus', -1004); 
}


foreach($result as $row){
	$fid[] = $row['fid'];
}
$fid_sql = implode(',', $fid);

//选择forumthread表
$threadtableids = !empty($_G['cache']['threadtableids']) ? $_G['cache']['threadtableids'] : array();

$threadtable = $_G['gp_archiveid'] && in_array($_G['gp_archiveid'], $threadtableids) ? "forum_thread_{$_G['gp_archiveid']}" : 'forum_thread';

loadcache('forums');


$total = DB::result_first("SELECT count(*) FROM ".DB::table($threadtable)." WHERE fid IN($fid_sql) AND displayorder IN (0,1,2, 3, 4)");
$total_page = ceil($total/$pagesize);

$query = DB::query("SELECT * FROM ".DB::table($threadtable)." 
		WHERE fid IN($fid_sql) AND displayorder IN (0,1,2, 3, 4)
		ORDER BY dateline DESC LIMIT $start,$pagesize");

require 'includes/class/thread_service.php';
$thread_sercie = new Thread_Service;

//获取图片
while($row = DB::fetch($query)){
	$thread_sercie->input_thread($row);
}


$threads = $thread_sercie->output_threads(array('subject', 'uid', 'author', 'views','forumname',  'avatar', 'likes', 'time', 'fid', 'replies', 'tid', 'pics'));


$focuslist['threadlist'] = $threads;
$focuslist['total'] = $total_page;
odz_result($focuslist);
//处理用户关注结果