<?php

class labelMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();
        
    }


    //列表调用
    public function getlist($html)
    {

        $html = stripslashes($html);

        preg_match_all('/(\S+)=\((.*)\)/iU', $html, $matches);

        $get = array_combine($matches[1], $matches[2]);

        //print_r($get);
        //extract($get);
        //echo "$table";
        $table = $get['table'];
        $limit = $get['limit'];
        $desc = $get['order'];
        $condition = $get['where']; //条件


        //exit;
        if ($table == "channel") {

            if (!empty($get['mid'])) {
                $mid = ' AND mid=' . $get['mid']; //调用模型ID
            }
            if (!empty($get['pid'])) {
                $pid = ' AND pid in(' . $get['pid']. ')'; //上级栏目ID
            }
            if ($get['type'] == 'top') {
                $pid = ' AND pid=0'; //调用顶级栏目
            }
            if ($get['type'] == 'sub') {
                $pid = ' AND pid in (' . $this->getcat($get['pid']) . ')'; //调用所有次级栏目
            }
            if ($get['att'] == 'class') {
                $att = ' AND type=0'; //调用所有栏目页
            }
            if ($get['att'] == 'list') {
                $att = ' AND type=1'; //调用所有列表页
            }

            if (!empty($get['where'])) {
                $where = $get['where']; //调用附加条件
            }

            if (!empty($get['rand'])) {
                unset($desc);
                $desc = 'rand()'; //调用附加条件
            }

            $langid=$this->getlangid();
            if(!empty($langid)){
            $langwhere=' AND lang='.$langid; //当前语言数据
            }
            
            if ($this->model->table('channel')->where('hide=0' . $pid)->count()) {
            } else {
               return;
            }

            return $this->model->field('*')->table('channel')->where('hide=0' .$langwhere. $mid . $pid .
                $att . $where)->limit($limit)->order('sorter desc,' . $desc)->select();
        }


        if ($table == "content") {


            if (!empty($get['mid'])) {
                $mid = ' AND A.mid=' . $get['mid']; //调用模型ID
            }

            if (!empty($get['cid'])) {
                $cid = ' AND A.cid in(' . $get['cid'] . ')'; //调用栏目ID
            }

            if (!empty($get['sub'])) {
                $cid = '';
                $cid = " AND A.cid in (" . $this->getcat($get['cid']) . ")"; //调用下级栏目所有内容

            }

            if (!empty($get['pic'])) {
                if($get['pic']=='true')
                {
                $pic = ' AND A.pic<>"" '; //调用缩略图内容
                }
                if($get['pic']=='false')
                {
                $pic = ' AND A.pic="" '; //调用非缩略图内容
                }
            }

            if (!empty($get['good'])) {
                $good = " AND A.good=1"; //调用推荐内容
            }
            if (!empty($get['hits'])) {
                $hits = " AND A.hits=1"; //调用置顶内容
            }
            if (!empty($get['where'])) {
                $where = ' AND '.$get['where']; //调用附加条件
            }

            if (!empty($get['rand'])) {
                unset($desc);
                $desc = 'rand()'; //调用附加条件
            }

            $langid=$this->getlangid();
            if(!empty($langid)){
            $langwhere=' AND A.lang='.$langid; //当前语言数据
            }

            $order = $desc;

            if (empty($get['cid'])) {
                $sql = "SELECT A.*,B.pid,B.type as ctype,B.name as cname,B.pic as cpic,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id WHERE A.status=1 {$langwhere}{$cid}{$mid}{$pic}{$good}{$hits}{$where} ORDER BY {$desc} LIMIT {$limit}";
            } else {
                $channel_info = $this->model->table('channel')->field('id,ext_mid')->where('id in(' .$get['cid'].')')->find(); //获取栏目信息
                if ($channel_info['ext_mid'] == 0) {
                    $sql = "SELECT A.*,B.pid,B.type as ctype,B.name as cname,B.pic as cpic,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id WHERE A.status=1 {$langwhere}{$cid}{$mid}{$pic}{$good}{$hits}{$where} ORDER BY {$desc} LIMIT {$limit}";
                } else {
                    $model_info=$this->model->table('plus_model')->where('id=' . $channel_info['ext_mid'])->find();//获取扩展模型信息
                    $sql = "SELECT A.*,C.*,B.pid,B.type as ctype,B.name as cname,B.pic as cpic,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id LEFT JOIN {$this->model->pre}{$model_info['table']} C ON A.id = C.my_aid WHERE A.status=1 {$langwhere}{$cid}{$mid}{$pic}{$good}{$hits}{$where} ORDER BY {$desc} LIMIT {$limit}";
                }
            }

            return $menu = $this->model->query($sql);
        }

        if ($table == "content_tag") {

            return $this->model->field('*')->table('content_tag')->limit($limit)->order($desc)->select();
        }

        if ($table == "related") {

            $id = intval($get['id']); //文章ID

            if (!empty($get['cid'])) {
                $cid = ' AND cid in(' . $get['cid'] . ')'; //栏目ID
            }

            if (!empty($get['sub'])) {
                $cid = '';
                $cid = " AND cid in (" . $this->getcat($get['cid']) . ")"; //调用下级栏目所有内容
            }

            if($get['pic']=='true') {
                $pic = ' AND pic<>"" '; //调用缩略图内容
            }

            if($get['pic']=='false') {
                $pic = ' AND pic="" '; //调用非缩略图内容
            }

            if (!empty($get['mid'])) {
                $mid = ' AND mid=' . $get['mid']; //调用模型ID
            }
            if (!empty($get['where'])) {
                $where = ' AND '.$get['where']; //调用附加条件
            }
        
            $condition['id']=$id;
            $info = $this->model->field('id,keywords')->table('content')->where($condition)->find();

            if(empty($info['keywords'])){
            return;
            }else{
            $keyword=explode(",", $info['keywords']);
            foreach ($keyword as $key) {
               $related.=" keywords like '%" . $key . "%' or";
            }
            $related='('.substr($related, 0,-2).')';

            $langid=$this->getlangid();
            if(!empty($langid)){
            $langwhere=' AND lang='.$langid; //当前语言数据
            }
            
            $sql = "SELECT * FROM {$this->model->pre}content WHERE {$related} AND id<>{$id} {$langwhere} {$cid}{$pic}{$mid}{$where} ORDER BY {$desc} LIMIT {$limit}";
            return $loop = $this->model->query($sql);
            }

        }

        return $list = $this->model->field('*')->table($table)->where($condition)->limit($limit)->order($desc)->select(); //通用
    }


    //统计调用

    public function getcount($table, $condition)
    {
        return $count = $this->model->table($table)->where($condition)->count();
    }


    //菜单循环超链接调用
    public function getcurl($id)
    {
        
        if($this->config['URL_HTML_MODEL']==1){ 
        $condition['id']=$id;
        $info = $this->model->field('id,pinyin')->table('channel')->where($condition)->find();
        return ROOTAPP . '/'.$info['pinyin'];
        }
        if($this->config['URL_HTML_MODEL']==2){     
        $data = $this->model->field('id,pid,name,pinyin')->table('channel')->select();
        $cat = new Category(array(
            'id',
            'pid',
            'name',
            'pinyin',
            'cname'));
        $channel=$cat->getPath($data, $id); 
        $cpinyin='';
        foreach ($channel as $pinyin) {
        $cpinyin.=$pinyin['pinyin'].'/';
         }
        $cpinyin=substr($cpinyin,0,strlen($cpinyin)-1);
        return ROOTAPP . '/'.$cpinyin;
        }

    }

    //内容循环超链接调用
    public function getaurl($id)
    {

        $condition['id']=$id;
        $info = $this->model->field('id,cid')->table('content')->where($condition)->find();
        
        if($this->config['URL_HTML_MODEL']==1){
        $condition2['id']=$info['cid'];
        $channel_info = $this->model->field('id,pinyin')->table('channel')->where($condition2)->find();
        return ROOTAPP . '/'.$channel_info['pinyin'].'/'.$id.'.html';   
        }
        if($this->config['URL_HTML_MODEL']==2){
        $data = $this->model->field('id,pid,name,pinyin')->table('channel')->select();
        $cat = new Category(array(
            'id',
            'pid',
            'name',
            'pinyin',
            'cname'));
        $channel=$cat->getPath($data, $info['cid']); 
        $cpinyin='';
        foreach ($channel as $pinyin) {
        $cpinyin.=$pinyin['pinyin'].'/';
         }
        $cpinyin=substr($cpinyin,0,strlen($cpinyin)-1);
        return ROOTAPP . '/'.$cpinyin.'/'.$id.'.html';
        }

    }
    


    //关键词分词处理
    public function getkeywords($keyword, $partition)
    {
        $strArray = explode(",", $keyword);
        foreach ($strArray as $list) {
            $key .= "<a href=\"" . ROOTAPP . "/tags/index/" . $list . ".html\">" . $list .
                "</a>" . $partition;
        }
        $key = substr($key, 0, strlen($key) - 1);
        return $key;

    }




}

?>