<?php

//内容页
class contentMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();


    }



    //获取模型ID
    public function model_id()
    {
        $conditionmod['mark'] = "article";
        $model = $this->model->table('model')->where($conditionmod)->find();
        return $model['id'];
    }

    //首页
    public function index()
    {
        $id = intval($_GET[0]); //内容ID
        if (empty($id)) {
            $this->error404();
        }

        if ($this->model->table('content')->where('id=' . $id)->count()) {
        } else {
            $this->error404();
        }

        $info = $this->model->table('content')->where('id=' . $id)->find(); //读取内容信息

        if (!empty($info['url'])) //判断是否跳转
        {			
            $link=$this->tpl->compile($info['url']);
			header("HTTP/1.1 301 Moved Permanently");
			header("Location: ".$link."");
			exit;
        }

        $cont = $this->model->table('content_data')->where('aid=' . $id)->find(); //查询附加表
        $content = $cont['content']; //读入这个内容的附加数据

        $channel_info = $this->model->table('channel')->where('id=' . $info['cid'])->find(); //查询这个内容的栏目的信息

        /*模块地址自动纠正*/
        if ($channel_info['way_content'] == 1) {
            $model_info = $this->model->table('model')->where('id=' . $channel_info['mid'])->find(); //查询栏目对应的模型信息
            if (!empty($model_info['forepart_content_mod'])) {
                module($model_info['forepart_content_mod'])->index($id);
                exit;
            }
        }
        /*模块地址纠正结束*/
        
        //扩展模型输出
        if ($channel_info['ext_mid'] <> 0) { //判断是否扩展模型
        $ext_model=$this->model->table('plus_model')->where('id=' . $channel_info['ext_mid'])->find();//获取扩展模型信息
        
        $ext_info=$this->model->table($ext_model['table'])->where('my_aid=' . $id)->find();//获取扩展内容
        $this->assign('ext_info', $ext_info); //扩展模型内容    
        }
        //扩展结束
        
        $nav=$this->nav_cid($channel_info['id']); //位置导航

        $parent_info = $this->model->table('channel')->where('id=' . $channel_info['pid'])->find(); //查询这个内容栏目的上级栏目信息
        if (empty($parent_info)) {
            $parent_info = array(
                "id" => "0",
                "pid" => "0",
                "mid" => "0",
                "name" => "无上级栏目");
        }


        $data['views'] = $info['views'] + 1;
        $condition['id'] = $id;
        $this->model->table('content')->data($data)->where($condition)->update(); //更新点击率

        $replace = $this->model->field('*')->table('plus_replace')->select(); //读取内容替换
        if (!empty($replace)) {
            $str=$content;
            foreach ($replace as $key => $export) {
                $str = preg_replace('/(<a.*?>\s*)(.*'.$export[key].'.*)(\s*<\/a>)/isu', '${2}', $content,$export['num']);
				$content = preg_replace('/'.$export[key].'/isu',$export['content'], $str,$export['num']);
            }
        }

        if($this->config['URL_HTML_MODEL']==1){
            $content_url = __APP__ . '/'.$channel_info['pinyin'].'/'. $id . '-{page}.html'; //分页基准网址
        }
        if($this->config['URL_HTML_MODEL']==2){
            $content_url = module('label')->getcurl($channel_info['id']). '/'. $id . '-{page}.html'; //分页基准网址
        }
        
        $page = new Page();
        $content = $page->contentPage(html_out($content), $separator = "<hr style=\"page-break-after:always;\" class=\"ke-pagebreak\" />",
            $url = $content_url, $pageBarNum = 10, $mode = 4); //文章分页


        $mid = $this->model_id();
        $this->assign('prev', $this->model->table('content')->where('id<' . $id .' AND status=1 AND cid=' . $channel_info['id'] . ' AND mid=' . $mid)->order('id desc')->
            limit(1)->find()); //上一篇
        $this->assign('next', $this->model->table('content')->where('id>' . $id .' AND status=1 AND cid=' . $channel_info['id'] . ' AND mid=' . $mid)->order('id asc')->
            limit(1)->find()); //下一篇

        $top = array_reverse($nav); //获取顶级栏目信息
        $top_id = $top[0]['id'];
        $top_info = $this->model->table('channel')->where('id=' . $top_id)->find(); //顶级栏目信息

        $this->assign('top_id', $top_id); //顶级栏目ID
        $this->assign('top_info', $top_info); //顶级栏目信息
        $this->assign('nav', array_reverse($nav)); //  导航
        $this->assign('info', $info); //内容信息
        $this->assign('content', $content['content']); //内容数据
        $this->assign('page', $content['page']); //分页
        $this->assign('channel_info', $channel_info); //栏目信息
        $this->assign('parent_info', $parent_info); //上级栏目信息
        
        if($this->config['LANG_OPEN']==1){
        $this->display(__LANG__.'/'.$channel_info['tpl_content']);
        }else{
        $this->display($channel_info['tpl_content']);
        }
    }


}

?>