<?php
//本地头条

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


//获取block_id+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------  ,
//总数
$odz_index_hot_news = get_mocuz_setting('index_hot_news');

$odz_index_hot_news = $odz_index_hot_news['index_hot_news'];

if(!empty($odz_index_hot_news)){
$sql = "select count(a.id) from ".DB::table('common_block_item_data')." as a left join ".DB::table('forum_thread')." as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id where a.idtype='tid' and a.bid = " . $odz_index_hot_news . " AND c.displayorder IN(0,1,2,3)" ;
$total =  DB::result_first($sql);
//查找删除的新闻总数
$sqlc ="select count(id) from mocuz_card_news where isdelete=1";
$isdelete =  DB::result_first($sqlc);
$total=$total-$isdelete;
$total_page = ceil($total/$pagesize);

//获取数据
$sqlb='select a.id,IFNULL(b.top,0) as top,IFNULL(b.isdelete,0) as isdelete,IFNULL(b.views_differ,0) as views_differ,c.views as discuz_views,a.picflag,IFNULL(b.title,a.title) as title,IFNULL(b.pic,a.pic) as pic,IFNULL(b.type,0) as pictype,IFNULL(b.type,1) as type ,c.dateline ,a.dateline as time from ' . DB::table('common_block_item_data') . " as a left join " . DB::table('forum_thread') . " as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id where a.idtype='tid' and a.bid = " . $odz_index_hot_news . " AND c.displayorder IN(0,1,2,3)";
$query = DB::query($sqlb);
$list = array();
$temp=array();
    //获取数据
    // $sql = 'SELECT c.id, c.title, c.pic,f.tid, c.picflag, c.makethumb, c.thumbpath, c.fields,f.views  FROM ' . DB::table('common_block_item') . " as c left join " . DB::table('forum_thread') . " as f on c.id = f.tid WHERE c.bid = " . $odz_index_hot_news . " AND f.displayorder IN(0,1,2,3)  ORDER BY c.displayorder ASC LIMIT $offset, $pagesize";

    if (strpos($_G['setting']['attachurl'], 'http') !== false) {
        $_G['setting']['attachurl'] = $_G['setting']['attachurl'];
    } else {
        $_G['setting']['attachurl'] = $_G['baseurl'] . $_G['setting']['attachurl'];
    }
    if ($_G['setting']['ftp']['on'] == 1) {
        $img_url = $_G['setting']['ftp']['attachurl'];
    } else {
        $img_url = $_G['setting']['attachurl'];
    }
    while ($row = DB::fetch($query)) {
        if($row['isdelete']==1){
            continue;
        }
        $views=$row["discuz_views"]+$row["views_differ"];
        $arr = array(
            'subject' => (string)odz_format_subject($row['title']),
            'date' => dgmdate($row['dateline'],'mu'),
            'views' => (string)$views,
            'tid' => (string)$row['id']
        ); // if ($row['makethumb']) {
        //     $imgpath = $row['thumbpath'];
        // } else {
        //     $imgpath = $row['pic'];
        // }
        $arr['pics'] = array();
        if($row['pictype']==0){
            $arr['pics']=array();
            if ($row['picflag'] == 0) {
            $arr['pic'] = $img_url . $row['pic'];
        } elseif ($row['picflag'] == 2) {
            $arr['pic'] = $_G['setting']['ftp']['attachurl'] . $row['pic'];
        } else {
            $arr['pic'] = $img_url . $row['pic'];
        }
        if (strpos($arr['pic'], 'nophoto.gif') !== false) {
            $arr['pic'] = '';
        }
        }elseif($row['pictype']==1){
                $pic=json_decode($row['pic'],true);
                $arr['pic']=$pic[0];
                $arr['pics'] =array();
            }else{
                $arr['pic']='';
                $arr['pics']= json_decode($row['pic'],true);  
            }
            if($row['top']){
                $temp[]=$arr;
                continue;
            }
        // $arr['pic'] = array_shift($arr['pic']);
         // $arr['pic'] = empty($arr['pic']) ? '' : $arr['pic'];
         // $arr['pics'] = empty($arr['pics']) ? array() : $arr['pics'];
        //$arr['pic'] = str_replace('/', '\/', $arr['pic']);
        $list[] = $arr;
        $sort[]=$row['time'];
    }
    if($temp){
        $total = $total-1;
        $pagesize=$pagesize-1;
        $offset = ($page - 1) * $pagesize;
        $total_page = ceil($total/$pagesize);
        array_multisort($sort,SORT_DESC,$list);
        $list=array_slice($list,$offset,$pagesize);
        if($page>$total_page){
            $temp=array();
        }
        $list=array_merge($temp,$list);
    }else{
      array_multisort($sort,SORT_DESC,$list);
      $list=array_slice($list,$offset,$pagesize);
        
    }     
}else{
    $total_page = 0;
    $list = array();
}
?>
