<?php
//新鲜事

//处理页数和分页数量
$page = isset($page) ? $page : intval($_GET['page']);
$page = $page < 1 ? 1 : $page;
$pagesize =  isset($pagesize) ? $pagesize : (isset($_GET['pagesize']) ? $_GET['pagesize'] : 20);

//获取block_id
$fid = get_mocuz_setting('index_block_id');
$fid = $fid['index_block_id'];

if(empty($fid)){
    odz_error('fid_not_exists');
}

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

//获取总数
$total = DB::result_first('SELECT count(*) FROM ' . DB::table('forum_thread') . " AS T LEFT JOIN " . DB::table('forum_forum') . " AS F ON T.fid = F.fid WHERE T.fid IN ($fid) AND T.displayorder  IN(0,1,2,3) AND F.fid IN ($fid) ");
$total_page = ceil($total/$pagesize);

//获取数据

 $sql = 'SELECT authorid,author,tid,fid,authorid,subject,views, dateline,lastpost,replies,attachment, dateline FROM ' . DB::table('forum_thread') . " WHERE fid IN ($fid) AND displayorder  IN(0,1,2,3)  ORDER BY tid DESC LIMIT $offset, $pagesize";      


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

$query = DB::query($sql);
while ($thread = DB::fetch($query)) {
    $thread['usergroup']=usergruop_level($thread['authorid']);
    $thread_sercie->input_thread($thread);
}

$list = $thread_sercie->output_threads('full');
/*


$list = array();

while ($row = DB::fetch($query)) {

    $thumb = $temp = array();
    $temp['subject'] = (string)$row['subject']; 
    $temp['uid'] = (string)$row['authorid'];
    $temp['author'] = (string)$row['author'];
    $temp['avatar'] = avatar($row['authorid'], 'small', true);
    $temp['forumname'] = (string)strip_tags($row['name']);
    $temp['fid'] = (string)$row['fid'];
    $temp['likes'] = (string)intval(C::t('home_praise_total')->fetch_by_id_idtype($row['tid'],'tid'));
    $temp['replies'] = (string)$row['replies'];
    $temp['time'] = my_date_format($row['dateline'], 'm-d');
    $temp['tid'] = (string)$row['tid'];


    if ($row['attachment'] == 2) {
        foreach (DB::fetch_all('SELECT aid FROM ' . DB::table('forum_attachment') . " WHERE tid = $row[tid]") as $k => $v) {
            $thumb [] = mocuz_thumb($v['aid'], '200', '200');

        }
    }



    //帖子里面 外链图片
    $viewthread = DB::fetch_first('SELECT message FROM ' . DB::table('forum_post') . " WHERE tid={$row['tid']} AND first = 1");
 
    if (preg_match_all("/\[img.*\](.*)\[\/img\]/isU", $viewthread['message'], $out)) {
        //去除gif动态图片
        $threadpic = array();
        foreach ($out[1] as $k => $v) {
            if (!strpos($v, '.gif')) {
                $threadpic[] = $v;
            }
        }
        $thumb = array_merge($thumb, $threadpic);
    }
    $temp['pics'] = array_slice($thumb, 0, 3);
    $list[] = $temp;
    unset($thumb);
}
*/
?>