<?php
/************************
    VIP自定义模板文章列表页
************************/
require_once('./setting.php');
require_once(SYSTEM_ROOT.'include/smarty_setting.php');
require_once(SYSTEM_ROOT.'include/public.php');

$sql       = "select userid,oa_id,allow_copy_channel,used_mobile,web_show_mobile from user where username='".$user_name."' and (userlevel=3 or siteid>=7 or siteid=1) ";
$userInfo  = $DB_Ev123->get_row($sql);
if (Job_Wap::check_wap() && $userInfo['used_mobile'] && $userInfo['web_show_mobile']) {
    $url = 'http://'.WAP_DOMAIN.$_SERVER['REQUEST_URI'];
    $url = str_replace('/self_define/doc_list.php', '/wap/doc.php', $url);
    $url = str_replace('/vip_doc', '/wap_doc', $url);
    $url = str_replace('/doc_', '/wdoc_', $url);
    header('Location: '.$url);
    exit();
}

if ($user_tag->is_tj == 1) {
    $smarty->display('tjNew/index.tpl');
    exit();
} else {
    require_once(UPPER_ROOT.'/include/page_str.php');
}

$key_word   = $_REQUEST['keyWord']      ? trim($_REQUEST['keyWord'])        :'';
$channel_id = $_REQUEST['channel_id']   ? (int)$_REQUEST['channel_id']      :0;
$big_id     = $_REQUEST['big_id']       ? (int)$_REQUEST['big_id']          :0;
$sub_id     = $_REQUEST['sub_id']       ? (int)$_REQUEST['sub_id']          :0;
$is_search  = $_REQUEST['is_search']    ? (int)$_REQUEST['is_search']       :0;
$page       = $_REQUEST['page']         ? $_REQUEST['page']                 :1;
$list_list  = $_REQUEST['list_list']    ? (int)$_REQUEST['list_list']       :0;
$get_data   = isset($_GET)              ? stripslashes_string($_GET)        : array();
$param_arr  = array();

$sql="select id from ev_user_channel where id=".$channel_id." and is_del=0 and userid=".$user_id;
$channel_id=$DB_Ev123->get_var($sql);

//未指定栏目ID不允许操作
if (!$channel_id) {
    echo '<script lanaguage="javascript">alert("未找到相关信息！");location.href="/vip_'.$user_name.'.html";</script>';
    exit();
}
if (!$channel_id) {
    echo '<script lanaguage="javascript">alert("未找到相关信息！");location.href="/vip_'.$user_name.'.html";</script>';
    exit();
}


//获取参数条件
if (is_array($get_data) && !empty($get_data)) {
    foreach ($get_data as $key => $val) {
        if (false !== strpos($key, 'p_')) {
            $new_key = end(explode('_', $key));
            $param_arr[$new_key] = $val;
        }
    }
}

//页面显示条数处理
if ($user_id == 226414) {
    $pagerows =5;
} else {
    if ($user_id == 253911) {
        $pagerows = 9;
    } else {
        if (!$big_id && !$sub_id && $PAGE_TYPE==1 && $SCEEN_TYPE==2 && !$key_word) {
            $pagerows =12;
        } else {
            $pagerows =24;
        }
    }
}
$pagerows = $PAGE_SIZES ? $PAGE_SIZES : $pagerows;
//千琦科贸
if ($model_id==214) {
    if ($list_list==1) {
        $pagerows=($user_name=='qianqikemao' && $channel_id==1731)?4:5;
    } else {
        $pagerows=4;
    }
}
if ($model_id==321) {
    $pagerows =18;
}

//系统频道可实现综合查询只有43模板
$index_info=$class_arr=array();
if ($ch_info['init_dom_id'] && $dom_tmp_id==43) {
    list($index_info, $class_arr) = get_doc_index_info($channel_id, $param_arr);
}

//得到频道类型
$type          = $ch_info['type'];
$name          = strip_tags($ch_info['name']);
$big_name      = get_class_name($big_id);
$sub_name      = get_class_name($sub_id);
//获得类信息
$sub_tmp_arr   = get_class_arr($user_id, $channel_id);
$sub_class_arr = array();
$sub_name_arr  = array();
$sub_num_arr   = array();
if ($sub_tmp_arr) {
    $i = 1;
    foreach ($sub_tmp_arr as $sub) {
        $sub_class_arr[$sub['parentid']][$sub['id']] = cnsubstr($sub['name'], 50);
        $sub_num_arr[$sub['parentid']]['num']        = $i;
        $sub_name_arr[$sub['id']]                    = $sub['name'];
        $i++;
    }
}

//类别---可以上传类别图片的时候
$class_arr=get_doc_class($user_id, $channel_id, '', '');


$big_class_arr=$class_arr[0];
$sub_class_arr2=$class_arr[1];


$tmp_sql = ' and ((check_status=2 and user_user_id!=0) or user_user_id=0)';

if (Model_Doc::checkOAID()) {
    if ($channel_id) {
        $tmp_sql .= " and channel_id in(".$channel_id.",".PARENT_CHANNEL_ID.") ";
    }
} else {
    if ($channel_id) {
        $tmp_sql .= " and channel_id=".$channel_id;
    }
}

if (empty($param_arr)) {
    if ($big_id) {
        $tmp_sql.= " and big_id=".$big_id ;
    }
    if ($sub_id) {
        $tmp_sql.=" and sub_id=".$sub_id ;
    }
}

//关键词查询
if ($key_word) {
    $tmp_sql.= " and (title like BINARY '%".$key_word."%' or key_word like BINARY '%".$key_word."%'  or brief like BINARY '%".$key_word."%')";
}


//综合搜索
if (!empty($param_arr) && ($dom_tmp_id==43 || $is_search)) {
    $index_table  = get_doc_index_table($channel_id);
    $tmp_sql = " and b.channel_id=".$channel_id ;
    if ($big_id) {
        $tmp_sql.= " and b.big_id=".$big_id ;
    }
    if ($sub_id) {
        $tmp_sql.=" and b.sub_id=".$sub_id ;
    }
    foreach ($param_arr as $key => $val) {
        $sql="select type from ev_power_param where id=".$key;
        $tmp_param_type=$DB_User_Doc->get_var($sql);
        if ($tmp_param_type==5 || $tmp_param_type==11 || $tmp_param_type ==12) {
            if ($tmp_param_type==11 || $tmp_param_type ==12) {
                $val = str_replace('-', '#*#', $val);
            }
            $tmp_sql .= " and a.param_{$key} like '%{$val}%'";
        } elseif ($tmp_param_type == 23) {
            $nodeLastInfo = Model_Doc::getNodeInfo(
                array(
                    'id'  => 'id',
                    'lvl' => 'lvl',
                ),
                "t.channel_id={$channel_id} && t.id in({$val})",
                array('t.lvl', 'desc')
            );

            if (!$nodeLastInfo) {
                continue;
            }

            $nodeLength    = 9 - $nodeLastInfo->lvl;
            $nodeParentIds = $nodeLastInfo->id;

            for ($i=0; $i < $nodeLength; $i++) {
                $nodeList = Model_Doc::getNodeList(
                    array(
                        'id'  => 'id',
                        'lvl' => 'lvl'
                    ),
                    "t.channel_id={$channel_id} && t.pid in ({$nodeParentIds})"
                );

                if (!$nodeList) {
                    $i = 100;
                }

                $nodeParentIds = '';

                foreach ($nodeList as $k => $v) {
                    $nodeParentIds ? $nodeParentIds .= ",{$v->id}" : $nodeParentIds = $v->id;
                }

                $val .= ",{$nodeParentIds}";
            }

            $tmp_sql .= " and a.param_{$key} in ({$val})";
        } else {
            $tmp_sql .= " and a.param_{$key}='{$val}'";
        }
    }
    //关键词查询
    if ($key_word) {
        $tmp_sql.= " and (b.title like BINARY '%".$key_word."%' or b.key_word like BINARY '%".$key_word."%'  or b.brief like BINARY '%".$key_word."%')";
    }
    //总数
    $sql = "select count(b.id) from {$index_table} as a inner join ev_user_doc_content as b on a.doc_id=b.id where b.userid={$user_id} and b.is_del=0 and b.infostate='publish' {$tmp_sql}";
    $count_num = $DB_User_Doc->get_var($sql);
    $page_num=ceil($count_num/$pagerows);
    if ($count_num) {

        //分页显示
        if (!$page || 0 ==(int)$page) {
            $page   = 1;
            $offset = 0;
        } elseif ($page<=0) {
            $page   = 1;
            $offset = 0;
        } elseif ($page>$page_num) {
            $page = $page_num;
            $offset=($page-1)*$pagerows;
        } else {
            $page=$page;
            $offset=($page-1)*$pagerows;
        }

        $sql = "select a.*,b.id as id,b.title,b.pic,b.brief,b.input_time,b.price,b.market_price,b.href,b.target,b.content
                from {$index_table} as a inner join ev_user_doc_content as b on a.doc_id=b.id
                where b.userid={$user_id} and b.is_del=0 and b.infostate='publish' {$tmp_sql}
                order by b.order_sort desc,b.publish_date desc,b.id desc
                limit {$offset},{$pagerows}";

        $info_list_arr = $DB_User_Doc->get_results($sql);
        $info_arr      = get_new_doc_info($info_list_arr, $model_id, $key, $user_name);
        $url           = preg_replace("/&page=\d+/i", '', $_SERVER['REQUEST_URI']);
        $page_str      = page_str8($count_num, $pagerows, $page, $url.'&page=');
    }
} else {
    //未指定条件显示数据
    if (!$big_id && !$sub_id && $PAGE_TYPE==1 && $SCEEN_TYPE==2 && !$key_word) {
        foreach ($big_class_arr as $key => $big_info) {
            if ($dom_tmp_id == 31) {
                $info_list_arr_tmp = get_doc_info($user_id, $key, 1);
                if ($info_list_arr_tmp [0]['pic']) {
                    $tmp_num = 6;
                    $info_arr2[$key]['id'] = $info_list_arr_tmp[0]['id'];
                } else {
                    $tmp_num = 10;
                }
            } else {
                $tmp_num=($dom_tmp_id == 32)?9:$tmp_num=8;
            }
            $info_list_arr = get_doc_info($user_id, $key, $tmp_num);
            $info_arr2[$key]['list']=get_new_doc_info($info_list_arr, $model_id, $key, $user_name);
            $info_arr2[$key]['name'] = $big_info['classname'];
            $info_arr2[$key]['name_20'] = cnsubstr($big_info['classname'], 20);
            $info_arr2[$key]['link'] = $big_info['link'];
            //print_r($info_arr2);
        }
        $list_style = 1;
    } else {
        //总数
        // if (Model_Doc::checkOAID()) {

        if (Model_Doc::checkOAID()) {
            Model_Doc::$uid = PARENT_USER_ID.', '.USER_ID;
            // Model_Doc::$chid = Model_Doc::check_pid(array('channel_id' => $channel_id));
            Model_Doc::$flag = true;
            // Model_Doc::$chid = PARENT_CHANNEL_ID;
        } else {
            Model_Doc::$chid = $channel_id;
            Model_Doc::$uid = USER_ID;
        }

        $whereSql = "is_del=0 and infostate='publish' ".$tmp_sql;
        $count_num = Model_Doc::getAllDocData(false, array('id' => 'id'), $whereSql, array(), array(), 2);
        $page_num=ceil($count_num/$pagerows);
        //分页显示
        if (!$page || 0 ==(int)$page) {
            $page   = 1;
            $offset = 0;
        } elseif ($page<=0) {
            $page   = 1;
            $offset = 0;
        } elseif ($page>$page_num) {
            $page = $page_num;
            $offset=($page-1)*$pagerows;
        } else {
            $page=$page;
            $offset=($page-1)*$pagerows;
        }

        if (Model_Doc::checkOAID()) {
            Model_Doc::$uid = PARENT_USER_ID.', '.USER_ID;
            // Model_Doc::$chid = Model_Doc::check_pid(array('method' => true, 'channel_id' => $channel_id));
            Model_Doc::$flag = true;
        } else {
            Model_Doc::$chid = $channel_id;
            Model_Doc::$uid = USER_ID;
        }
        if (211 == $model_id) {
            if ($count_num == 1) {
                if (defined('PARENT_CHANNEL_ID') && defined('PARENT_USER_ID')) {
                    $sql = "select * from ev_user_doc_content where userid in(".$user_id.",".PARENT_USER_ID.") and is_del=0 and infostate='publish' ".$tmp_sql." order by order_sort desc,publish_date desc,id desc limit ".$offset.",".$pagerows ;
                } else {
                    $sql = "select * from ev_user_doc_content where userid=".$user_id." and is_del=0 and infostate='publish' ".$tmp_sql." order by order_sort desc,publish_date desc,id desc limit ".$offset.",".$pagerows ;
                }
                $info_doc = $DB_User_Doc->get_row($sql);
                //info_doc
                // $info_doc = Model_Public::newObjToArray(Model_Doc::getAllDocData(true, array(), $whereSql, array(), array($offset, $pagerows), 2));
            } else {
                $info_list_arr = Model_Public::newObjToArray(Model_Doc::getAllDocData(true, array(), $whereSql, array(), array($offset, $pagerows), 2));
            }
        } else {
            $info_list_arr = Model_Public::newObjToArray(Model_Doc::getAllDocData(true, array(), $whereSql, array(), array($offset, $pagerows), 2));
        }

        $info_arr = get_new_doc_info($info_list_arr, $model_id, $key, $user_name);
        // 特殊处理：如果只有一条信息，自动调整到最终页
        /*if (($count_num == 1) && empty($key_word)) {
            $doc_ids = array_keys($info_arr);
            header("location:/". $user_name ."/vip_doc/". $doc_ids[0] .".html");
            exit();
        }*/
        if ($model_id==323) {
            $info_arr_num=count($info_arr);
        }

        if ($model_css) {
            if (($ch_info['init_dom_id'] && $dom_tmp_id==43 && !empty($param_arr)) || $key_word) {
                $url = preg_replace("/&page=\d+/i", '', $_SERVER['REQUEST_URI']);
                $page_str = page_str8($count_num, $pagerows, $page, $url.'&page=');
            } else {
                $page_str = page_str8($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            }
        } else {
            if ($model_id==213) {
                $page_str = page_str5($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            } elseif ($model_id==323) {
                $page_str = page_str7($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            } elseif ($model_id==55 || $model_id==69 || $model_id==245 || $model_id==250 || $model_id==285) {
                $page_str = page_str6($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            } elseif ($model_id>=302 || $is_define_temp) {
                $page_str = page_str6($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            } else {
                $page_str = page_str2($count_num, $pagerows, $page, '/'.$user_name.'/vip_doc/'.$channel_id.'_'.$big_id.'_'.$sub_id.'_', '.html');
            }
            if ($model_id==214 && $list_list==1) {
                $page_str = page_str($count_num, $pagerows, $page, '/self_define/server.php?username='.$user_name.'&channel_id='.$channel_id.'&list_list=1&page=');
            }
        }

        $ser_id=0;
        if ($big_id) {
            $ser_id=$big_id;
        }
        if ($sub_id) {
            $ser_id=$sub_id;
        }
    }
}

//广告
$ad_arr = get_ad_info($user_id, $model_id, $channel_id, $ser_id);
if ($ad_arr) {
    foreach ($ad_arr as $key => $val) {
        $smarty->assign('ad_'.$key, $val);
    }
}

if ($model_id==218) {
    require_once(SYSTEM_ROOT.'dom/other218_doc.php');
}

$paramType23 = 0;
if ($index_info) {
    foreach ($index_info as $k => $v) {
        if ($v['type'] == 23) {
            $paramType23 = 1;
            break;
        }
    }
}

$smarty->assign('paramType23', $paramType23);
$smarty->assign('info_arr2', $info_arr2);
$smarty->assign('key_word', $key_word);
$smarty->assign('info_arr_num', $info_arr_num);
$smarty->assign('info_doc', $info_doc);
$smarty->assign('sub_name_arr', $sub_name_arr);
$smarty->assign('system_arr', $system_arr);
$smarty->assign('sub_class_arr', $sub_class_arr);
$smarty->assign('info_arr', $info_arr);
$smarty->assign('channel_info_arr', $channel_info);
$smarty->assign('channel_info_arr2', $channel_info11);
$smarty->assign('page_str', $page_str);
$smarty->assign('page', $page);
$smarty->assign('sub_num_arr', $sub_num_arr);
$smarty->assign('name', $name);
$smarty->assign('channel_id', $channel_id);
$smarty->assign('bottom_info_arr', $bottom_info_arr);
$smarty->assign('bottom_item_arr', $bottom_item_arr);
$smarty->assign('channel_id', $channel_id);
$smarty->assign('big_id', $big_id);
$smarty->assign('sub_id', $sub_id);
$smarty->assign('user_name', $user_name);
$smarty->assign('guest_chan_id', $guest_chan_id);
$smarty->assign('line_jobs', $line_jobs);
$smarty->assign('ad_type1', $ad_type1);
$smarty->assign('big_name', $big_name);
$smarty->assign('sub_name', $sub_name);
$smarty->assign('count_num', $count_num);
$smarty->assign('big_class_arr', $big_class_arr);
$smarty->assign('sub_class_arr2', $sub_class_arr2);
$smarty->assign('ad_arr', $ad_arr);
$smarty->assign('list_style', $list_style);
$smarty->assign('index_info', $index_info);
$smarty->assign('username', $user_name);
//$smarty->assign('current_url', $current_url);

if ($model_id==214) {
    if ($list_list==1) {
        if ($channel_id==1731) {
            $smarty->display($dec[$model_id].'doc_list_style2.tpl');
        } else {
            $smarty->display($dec[$model_id].'doc_list2.tpl');
        }
    } else {
        if ($channel_id==1731) {
            $smarty->display($dec[$model_id].'doc_list_style.tpl');
        } else {
            $smarty->display($dec[$model_id].'doc_list.tpl');
        }
    }
} else {
    $smarty->display($dec[$model_id].'doc_list.tpl');
}
