<?php
if (!defined('ROOT'))
    exit('Can\'t Access !');
class index_act extends act {
    function init() {
        $this->_question = new question();
        $this->_question_reply = new questionreply();
        $this->_question_like = new questionlike();
        $this->_question_type = new questiontype();
        $this->_question_tag = new questiontag();
        $this->_question_config = new questionconfig();
        $this->_question_reward = new questionreward();
        $this->_question_message = new questionmessage();
        $this->view->form = $this->_question->get_form();
        $this->view->replyform = $this->_question_reply->get_form();
        $this->questionconfigdata = $this->_question_config->get_all_config(); //获取配置


        if (front::get('page'))
            $page = front::get('page');
        else
            $page = 1;
        $this->view->page = $page;
        $this->pagesize = $this->questionconfigdata['question_pagesize'];
        $this->reply_pagesize = $this->questionconfigdata['reply_pagesize'];

        if (cookie::get('login_username') && cookie::get('login_password')) {
            $user =user::getInstance()->getrow(array('username' => cookie::get('login_username')));
            if (cookie::get('login_password') != front::cookie_encode($user['password'])) {
                unset($user);
            }
        }
        $nologin_arr = array('questionlist','showquestion','index');
        if (!is_array($user) && !in_array(front::$act, $nologin_arr)) {
            front::redirect(url::create('user/login'));
        }else{
            $this->user=$user;
            $this->isadministrator=false;//当前用户是否管理员
            if (is_array($user))
                $groupisadmin=usergroup::getInstance()->getrow("groupid=".$this->user['groupid']);
            if (is_array($groupisadmin))if ($groupisadmin['isadministrator'])$this->isadministrator=true;
        }
        $this->view->isadministrator=$this->isadministrator;  //当前用户是否管理员
        $this->view->questionconfigdata=$this->questionconfigdata ;  //传配置

    }

    function index_action() {
        if ($this->questionconfigdata['question_index']==2){
            front::redirect(url::create('question/typelist'));
        }
        if ($this->questionconfigdata['question_index']==3){
            front::redirect(url::create('question/taglist'));
        }
    	$this->check_pw();
        if(config::get('list_index_php')==1){
            echo "<script>window.location.href = '/index-'+'".lang::getistemplate()."'+'.html';</script>";
            exit;
        }

        $where = 'state=1 and langid = "'.lang::getlangid(lang::getistemplate()).'"';
        //搜索
        if (front::get('keyword') && !front::post('keyword'))
            front::$post['keyword'] = front::get('keyword');
        front::$post['keyword'] = trim(front::post('keyword'));
        if (front::post('keyword')) {
            $arr=array("!","@","#","$","%","^","&","*","(",")","[","]","|",",",".","<",">","{","}","=","+","-","；","'","\"","www.","http:://","https:://");
            $a=0;
            foreach($arr as $key=>$value){
                if(strpos(front::post('keyword'),$value)){
                    $a=1;
                }
            }
            if($a==1){
                echo "<script>alert(\"不能包含特殊字符！\"); location.href='".url("question/questionlist")."';</script>";
                exit;
            }
            //过滤特殊字符
            $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\\+|\/|\;|\'|\`|\-|\=|\\\|\|/";
            front::$post['keyword']=preg_replace($regex,"",front::$post['keyword']);
            $this->view->keyword=front::$post['keyword'];
            $where .= " and title like '%" . $this->view->keyword . "%'";
        }

        //排序
        if(isset(front::$get['sorting'])){
            if(front::$get['sorting'] == '0'){   //最新
                $ordre = "adddate desc";
            }
            else if(front::$get['sorting'] == '1'){  //热门
                $ordre = "browsenum desc";
            }else if(front::$get['sorting'] == '2'){  //悬赏(未做！！！)
                $where.=" and rewardmenoy>0 ";
            }
        }
        else{
            $ordre = "adddate desc";
        }
        //筛选分类
        if (front::get('typeid')){
            $where.=" and typeid=".front::get('typeid');
        }
        //查询tag
        if(front::$get['tag']){
            $where.=" and FIND_IN_SET(".front::get('tag').",tag)";
        }

        $limit = (($this->view->page - 1) * $this->pagesize) . ',' . $this->pagesize;
        $questiondata=$this->_question->getrows($where,$limit,$ordre);
        foreach ($questiondata as $key=>$val){
            $userdata= user::getInstance()->getrow("userid='".$val['uid']."'");
            $questiondata[$key]['headimage']=$userdata['headimage'];
            $questiondata[$key]['username']=$userdata['username'];
            $questiondata[$key]['reply']=$this->_question_reply ->getrows("state=1 and  aid='".$val['id']."'", 0, 'adddate desc');
            $questiondata[$key]['reply_isadopt']=$this->_question_reply ->getrow("state=1 and  isadopt=1 and aid='".$val['id']."'");
            $questiondata[$key]['type']=$this->_question_type->getrow('id='.$val['typeid']);
            //校验未回答的
            if(front::$get['sorting'] == '3'){
                if (count($questiondata[$key]['reply'])!=0)
                    unset($questiondata[$key]);
            }
            //设置了仅管理员可见
            if($val['isadminsee'] && $val['uid']!=$this->user['userid']){
                unset($questiondata[$key]);
            }
            //全局 仅会员可见
            if($this->questionconfigdata['question_vip_see'] && $this->user['groupid']=='1000' && $val['uid']!=$this->user['userid']){
                unset($questiondata[$key]);
            }
            //显示tag
            $tagid= explode(',', $val['tag']);
            $question_tagarray=array();
            for($index=0;$index<count($tagid);$index++){
                $question_tagarray[$index]=$this->_question_tag->getrow("id=".$tagid[$index],0);
            }
            $questiondata[$key]['tagarray']=$question_tagarray;

        }

        $questiontype=questiontype::getInstance()->getrows("state=1",0);
        if (is_array($questiontype))
            foreach ($questiontype as $key=>$val){
                $questiontype[$key]['num']=$this->_question->rec_count("typeid=".$val['id']);
            }
        $this->view->questiontype =$questiontype;  //分类
        if (!front::get('typeid'))front::$get['typeid']=0;
        $this->view->typeid =front::$get['typeid'];  //分类id
        if (!front::get('sorting'))front::$get['sorting']=0;
        $this->view->sorting =front::$get['sorting'];  //最新最热
        $this->view->data =$questiondata;
        $this->view->record_count = $this->_question->rec_count($where);
        if($this->view->record_count<$this->pagesize){
            $allindex=1;
        }else{
            $allindex=ceil($this->view->record_count/$this->pagesize);
        }
        $this->view->allindex=$allindex; //总页数
        //获取tag
        $tagdara=$this->_question_tag->getrows(array("langid"=>lang::getlangid(lang::getistemplate())),0);
        foreach ($tagdara as $key=>$val){
            if ($this->isadministrator)
                $tagdara[$key]['tagnum']=$this->_question->rec_count(" state=1 and FIND_IN_SET(".$val['id'].',tag)');
            else
                $tagdara[$key]['tagnum']=$this->_question->rec_count(" state=1 and isadminsee=0 and FIND_IN_SET(".$val['id'].',tag)');
        }
        $tagdara=$this->arr_sort($tagdara,'tagnum');
        $this->view->tagdata=$tagdara;  //tag

        //热门问题
        $huoquestiondata=$this->_question->getrows('state=1 and langid = "'.lang::getlangid(lang::getistemplate()).'"',10,"browsenum desc");
        $this->view->huoquestiondata=$huoquestiondata;  //tag

    }
    //排序
    function arr_sort($array,$key,$order="desc"){ //asc是升序 desc是降序
        $arr_nums=$arr=array();
        foreach($array as $k=>$v){
            $arr_nums[$k]=$v[$key];
        }
        if($order=='asc'){
            asort($arr_nums);
        }else{
            arsort($arr_nums);
        }
        foreach($arr_nums as $k=>$v){
            $arr[$k]=$array[$k];
        }
        return $arr;
    }

    function end() {
            $this->render('question/questionlist.html');
    }
}