<?php
/**
 *
 * @authors zlc (youhuoye@126.com)
 * @date    2016-03-14 11:01:36
 */
/*
 * 新鲜事开关：index_last_news
 * 新鲜事版块id：index_block_id （1,2）
 * 热点新闻开关：index_hot_block
 * 热点新闻id：index_hot_news
*/
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
global $_G;
$setting = get_mocuz_setting(array('index_hot_block', 'index_hot_news', 'index_last_news', 'index_block_id', 'tel_register', 'nick_register', 'qq_login', 'wexin_login', 'index_header_name', 'index_block_name', 'community_header_name', 'community_block_name', 'color_switch', 'biu_switch','recomm_forum','biu_header_name', 'biu_block_name', 'groupbuy_switch','hot_community','hot_community_name'));

loadcache('plugin');
$focus = 0;

if ($_G['uid']) {
    $focus = DB::fetch_first('SELECT * FROM ' . DB::table('mocuz_member_focus') . "  WHERE uid = {$_G['uid']}");
    
    $focus = empty($focus) ? 0 : 1;
}

$setting_result = array(
    'findpwdbyphone' => isset($_G['cache']['plugin']['smstong']['mobilepwd']) && $_G['cache']['plugin']['smstong']['mobilepwd'] == 1 ? 1 : 0,
    'register_username' => !$_G['setting']['bbclosed'] ? $setting['nick_register'] : 0,
    'register_phone' => isset($_G['cache']['plugin']['smstong']) && !$_G['setting']['bbclosed'] ? $setting['tel_register'] : 0,
    'login_qq' => $setting['qq_login'],
    'login_wechat' => $setting['wexin_login'],
    'index_top_text' => $setting['index_header_name'],
    'index_bottom_text' => $setting['index_block_name'],
    'forum_top_text' => $setting['community_header_name'],
    'forum_bottom_text' => $setting['community_block_name'],
    'color' => $setting['color_switch'],
    'focus' => (string)$focus,
    'biu' => $setting['biu_switch'],
    "biu_top_text" => $setting['biu_header_name'],
    "biu_bottom_text" => $setting['biu_block_name'],
    'tuangou' => $setting['groupbuy_switch'],
    'hot_community'=>$setting['hot_community'],
    'hot_community_name'=>isset($setting['hot_community_name'])&&$setting['hot_community_name'] ? $setting['hot_community_name'] : "最新推荐",
);

$result['setting'] = $setting_result;



$result['newslist'] = $result['threadlist'] = array();
$pagesize = 20;

if($setting['index_last_news'] != 0){
    
    include 'includes/news/news_forum.php';
    $result['threadlist'] = $list;
    for ($i=0; $i <count($result['threadlist']) ; $i++) { 
        $result['threadlist'][$i]['usergroup']=usergruop_level($result['threadlist'][$i]['uid'],'0');
    }
    unset($list);

}


if ($setting['index_hot_block'] != 0) {
    $pagesize = $setting['index_last_news'] != 0 ? 6 : 20;
    include 'includes/news/news_pics.php';
    $result['newslist'] = $list;
    unset($list);
}


//推荐板块

if(ODZ_CHARSET == 'gbk')
{
    $recomm_forum = mb_unserialize($setting['recomm_forum']);
}
else{
    $recomm_forum = unserialize($setting['recomm_forum']);
}
//排序
$arr_nums=$arr=array();
foreach($recomm_forum as $k=>$v){
            $arr_nums[$k]=$v['bid'];
}
asort($arr_nums);
        
foreach($arr_nums as $k=>$v){
    $arr[$k]=$recomm_forum[$k];
}


$arr = array_values($arr);

$blocklist = array();
foreach ($arr as $k=>$v) {
      $blocklist[$k]['blockname'] = $v['blockname'];
      $blocklist[$k]['icon'] = $_G['baseurl'].'mocuz/manage/assets/images/channel_img/'.$v['icon'];
      $blocklist[$k]['target_id'] = (string)$v['target'];
}
$result['blocklist'] = $blocklist;

//发现频道
$sql = "SELECT * FROM mocuz_card_channel ORDER BY ordernum ASC";
$rs = DB::query($sql);
$i=0;
$info = array();
while($res = DB::fetch($rs))
{
    $info[$i]['id'] = $res['id'];
    $info[$i]['code'] = $res['cid'];
    $info[$i]['name'] = $res['title'];
    $info[$i]['data'] = !empty($res['goto_value'])?$res['goto_value']:'';
    $info[$i]['icon'] =  $_G['baseurl'].'mocuz/manage/assets/images/channel_img/'.$res['icon'];
    $requiredlogin = empty($res['requiredlogin'])?0:1;
    $info[$i]['requiredlogin'] = (string)$requiredlogin;
        $i++;
}
$result['channel'] = $info;

//首页入口
$sql = "SELECT * FROM mocuz_card_home_channel ORDER BY ordernum ASC";
$rs = DB::query($sql);
$i=0;
$home_channel = array();
while($res = DB::fetch($rs))
{
    $home_channel[$i]['id'] = $res['id'];
    $home_channel[$i]['code'] = $res['cid'];
    $home_channel[$i]['name'] = $res['title'];
    $home_channel[$i]['data'] = !empty($res['goto_value'])?$res['goto_value']:'';
    $home_channel[$i]['icon'] =  $_G['baseurl'].'mocuz/manage/assets/images/channel_img/'.$res['icon'];
    $requiredlogin = empty($res['requiredlogin'])?0:1;
    $home_channel[$i]['requiredlogin'] = (string)$requiredlogin;
    $home_channel[$i]['flag'] = $res['flag'];
    $i++;
}
$result['home_channel'] = $home_channel;

//发现楼层入口
$sql_floor = "SELECT * FROM mocuz_card_channel_floor ORDER BY ordernum ASC";
$rs = DB::query($sql_floor);
$floor = array();
while($res = DB::fetch($rs))
{
    $floor[$res['id']]['name'] = $res['name'];
}

$sql = "SELECT * FROM mocuz_card_discovery_channel ORDER BY ordernum ASC";
$rs = DB::query($sql);
$discovery = array();
$i = 0;
while($res = DB::fetch($rs))
{
    if (!isset($floor[$res['fid']]['channel'])) {
        $floor[$res['fid']]['channel'] = array();
        $i = 0;
    } else {
        $i = count($floor[$res['fid']]['channel']);
    }
    $floor[$res['fid']]['channel'][$i]['id'] = $res['id'];
    $floor[$res['fid']]['channel'][$i]['code'] = $res['cid'];
    $floor[$res['fid']]['channel'][$i]['fid'] = $res['fid'];
    $floor[$res['fid']]['channel'][$i]['name'] = $res['title'];
    $floor[$res['fid']]['channel'][$i]['data'] = !empty($res['goto_value'])?$res['goto_value']:'';
    $floor[$res['fid']]['channel'][$i]['icon'] =  $_G['baseurl'].'mocuz/manage/assets/images/channel_img/'.$res['icon'];
    $requiredlogin = empty($res['requiredlogin'])?0:1;
    $floor[$res['fid']]['channel'][$i]['requiredlogin'] = (string)$requiredlogin;
    $floor[$res['fid']]['channel'][$i]['flag'] = $res['flag'];
}
//将楼层数组$floor重新排序
$floor_sort = array();
foreach ($floor as $value) {
    $floor_sort[] = $value;
}
$result['floor_channel'] = $floor_sort;

odz_result($result);



/*如果论坛是gbk的则用这个方法*/
function mb_unserialize($serial_str) { 
  $serial_str= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str); 
  $serial_str= str_replace("\r", "", $serial_str); 
  return unserialize($serial_str);
}

