<?php

/**
 * @filename news.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-11-25  10:35:40
 * @version 1.0
 * @Description
 * 新闻频道
 */
class News extends CI_Controller {

    var $data = array();

    function __construct() {

        parent::__construct();

        $this->data['channel'] = strtolower(__CLASS__);

        $this->load->model('model_cmf');
        $this->load->add_package_path(APPPATH . '../package_news', false);
        $this->load->model('model_news');
        //    $this->data['channel'] = strtolower(__CLASS__);
        $this->data['频道名称'] = "新闻频道";
    }

    public function index() {
        $data = lz_tag();

        $data = array_merge($data, $this->data);

        $data['首页幻灯'] = $this->model_cmf->photo("menu_id = 119", "order by  id desc  limit 0,7");

        $data['文章1'] = $this->model_cmf->article("menu_id = 134", "order by id desc limit 0,1");
        $data['文章1'] = $this->model_cmf->summary($data['文章1'], 65);

        $data['文章2'] = $this->model_cmf->article("menu_id = 134", "order by id desc limit 1,5");

        $data['文章3'] = $this->model_cmf->article("menu_id = 134", "order by id desc limit 6,5");

        $data['文章4'] = $this->model_cmf->article("menu_id = 134", "order by id desc limit 11,7");

        $data['国内新闻'] = $this->model_cmf->article("menu_id = 117", "order by id desc limit 0,8");

        $data['国际新闻'] = $this->model_cmf->article("menu_id = 118", "order by id desc limit 0,8");

        $data['24小时排行_1'] = $this->model_news->hot_24(0, 3);

        $data['24小时排行_2'] = $this->model_news->hot_24(4, 6);

        $data['周排行_1'] = $this->model_news->hot_week(0, 3);

        $data['周排行_2'] = $this->model_news->hot_week(4, 6);

        $data['月排行_1'] = $this->model_news->hot_month(0, 3);

        $data['月排行_2'] = $this->model_news->hot_month(4, 6);

        $sql = "select * from cms_photo where menu_id =226 order by id desc limit 0,8";
        $data['图片新闻'] = $this->db->query($sql)->result();

        $sql = "select * from cms_photo where menu_id =230 order by id desc limit 0,5";
        $data['广告1'] = $this->db->query($sql)->result();

        $sql = "select * from cms_photo where menu_id =231 order by id desc limit 0,5";
        $data['广告2'] = $this->db->query($sql)->result();

        $sql = "select * from cms_photo where menu_id =232 order by id desc limit 0,1";
        $data['广告3'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 129 order by id desc limit 0,1";
        $data['财经摘要'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 129 order by id desc limit 1,4";
        $data['财经频道'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 132 order by id desc limit 0,1";
        $data['医疗摘要'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 132 order by id desc limit 1,4";
        $data['医疗卫生'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 134 order by id desc limit 0,1";
        $data['新闻摘要'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 134 order by id desc limit 1,4";
        $data['新闻热点'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 127 order by id desc limit 0,5";
        $data['本地新闻1'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 127 order by id desc limit 5,5";
        $data['本地新闻2'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 127 order by id desc limit 10,5";
        $data['本地新闻3'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 131 order by id desc limit 0,14";
        $data['市井生活'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 130 order by id desc limit 0,14";
        $data['都市生活'] = $this->db->query($sql)->result();

        $sql = "select * from cms_article where menu_id = 133 order by id desc limit 0,1";
        $data['科技摘要'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 253 order by id desc limit 1,5";
        $data['科技教育1'] = $this->db->query($sql)->result();

        $sql = "select * from  cms_article where menu_id  = 253 order by id desc limit 6,5";
        $data['科技教育2'] = $this->db->query($sql)->result();

        $date = date("Y-m-d", time());
        $sql = "select * from hits where menu_id=5 and date = '" . $date . "'";
        $hitss = $this->db->query($sql)->row();
        if ($hitss == null) {
            $sql = "INSERT INTO `hits` (`menu_id`, `click`, `date`) VALUES ('5', '0', '" . $date . "'); ";
            $this->db->query($sql);
        } else {
            $sql = "update hits set click = click+1 where menu_id=5 and date = '" . $date . "'";
            $this->db->query($sql);
        }

        $this->parser->parse(__FUNCTION__, $data);
    }

    /*
     * 文章列表
     * $type_id 文章类别
     * $start   开始数
     */

    public function article_list($type_id, $start = 0) {


        $config['per_page'] = 20;

        $data = lz_tag();

        $data = array_merge($data, $this->data);
        $this->load->model('model_news');

        //新闻频道 页面的公共部分的数据
        $data = $this->model_news->common($data);

        $sql = "select id,title,left(publish_time,10) as date  ";
        $from_where = "from cms_article " .
                " where menu_id = {$type_id}" .
                " and state = '正常' ";

        $limit = " order by id desc limit {$start},{$config['per_page']}";
        $data['列表'] = $this->db->query($sql . $from_where . $limit)->result();

//        $config['base_url'] = __CLASS__ . "/" . __FUNCTION__ . "/" . $type_id . "/";
        lz_log($config);

        $data['分页'] = $this->model_cmf->pages($from_where, $config);

        $menu = $this->model_cmf->find_by_id("cms_menu", $type_id);
        $data['面包屑'] = $menu->name;

        $data['24小时排行_1'] = $this->model_news->hot_24(0, 3);

        $data['24小时排行_2'] = $this->model_news->hot_24(4, 6);

        $data['周排行_1'] = $this->model_news->hot_week(0, 3);

        $data['周排行_2'] = $this->model_news->hot_week(4, 6);

        $data['月排行_1'] = $this->model_news->hot_month(0, 3);

        $data['月排行_2'] = $this->model_news->hot_month(4, 6);

        $sql = "SELECT * FROM cms_photo WHERE menu_id =232 ORDER BY rand( ) LIMIT 1";
        $data['广告'] = $this->db->query($sql)->result();
//        lz_log($data);
        $date = date("Y-m-d", time());
        $sql = "select * from hits where menu_id=5 and date = '" . $date . "'";
        $hitss = $this->db->query($sql)->row();
        if ($hitss == null) {
            $sql = "INSERT INTO `hits` (`menu_id`, `click`, `date`) VALUES ('5', '0', '" . $date . "'); ";
            $this->db->query($sql);
        } else {
            $sql = "update hits set click = click+1 where menu_id=5 and date = '" . $date . "'";
            $this->db->query($sql);
        }
        
        $this->parser->parse(__FUNCTION__, $data);
    }

    public function article($id) {

        $data = lz_tag();

        $data = array_merge($data, $this->data);

        $sql = "update cms_article set click = click + 1 where id = " . $id;
        $this->db->query($sql);

        $sql = "select * from cms_article where id = " . $id;
        $data['文章'] = $this->db->query($sql)->result();
        $data['commentary_id'] = $id;

        $menu = $this->model_cmf->find_by_id("cms_menu", $data['文章'][0]->menu_id);
        $data['面包屑'] = $menu->name;

        $data['24小时排行_1'] = $this->model_news->hot_24(0, 3);

        $data['24小时排行_2'] = $this->model_news->hot_24(4, 6);

        $data['周排行_1'] = $this->model_news->hot_week(0, 3);

        $data['周排行_2'] = $this->model_news->hot_week(4, 6);

        $data['月排行_1'] = $this->model_news->hot_month(0, 3);

        $data['月排行_2'] = $this->model_news->hot_month(4, 6);
        $sql = "select * from commentary where acticle_id  = 0 order by id desc limit 0,5 ;";
        $data['评论信息'] = $this->db->query($sql)->result();

        $sql = "SELECT * FROM cms_photo WHERE menu_id =232 ORDER BY rand( ) LIMIT 1";
        $data['广告'] = $this->db->query($sql)->result();

        $sql = "select * from commentary as c inner join users as u on c.user_id = u.id  where acticle_id  = " . $id . " order by c.publish_time desc limit 0,2 ;";
        $data['评论信息'] = $this->db->query($sql)->result();

        $sql = "select count(*) from commentary where acticle_id = " . $id . ";";
        $data['评论条数'] = $this->db->query($sql)->result();

        $sql = "select count(DISTINCT user_id) AS num_user from commentary where acticle_id = " . $id . ";";
        $data['评论人数'] = $this->db->query($sql)->result();

        $date = date("Y-m-d", time());
        $sql = "select * from hits where menu_id=5 and date = '" . $date . "'";
        $hitss = $this->db->query($sql)->row();
        if ($hitss == null) {
            $sql = "INSERT INTO `hits` (`menu_id`, `click`, `date`) VALUES ('5', '0', '" . $date . "'); ";
            $this->db->query($sql);
        } else {
            $sql = "update hits set click = click+1 where menu_id=5 and date = '" . $date . "'";
            $this->db->query($sql);
        }
        
        $this->parser->parse(__FUNCTION__, $data);
    }

    public function get_by_commentary($acticle_id, $star) {

        $sql = "select * from commentary as c inner join users as u on c.user_id = u.id  where acticle_id  = " . $acticle_id . " order by c.publish_time desc limit " . $star . ",5 ;";

        $commentarys = $this->db->query($sql)->result();

        $commentarys = json_encode($commentarys);
        echo $commentarys;
    }

    public function refresh($acticle_id) {
        $sql = "select * from commentary as c inner join users as u on c.user_id = u.id  where acticle_id  = " . $acticle_id . " order by c.publish_time desc limit 0,3 ;";
        $commentarys = $this->db->query($sql)->result();

        $commentarys = json_encode($commentarys);
        echo $commentarys;
    }

    //  //新发布评论
    public function add_commentary() {
        $this->load->library('session');

        $star = 0;

        if ($this->session->userdata('current_user') == null) {
            //  print_r($this->session->userdata('current_user'));
            echo "请先登录，再发布评论！";
            return;
        } else {
            $acticle_id = $this->input->post('acticle_id');
            //楼层  
            $sql = "select remark,publish_time from commentary where acticle_id = " . $acticle_id . " ORDER BY publish_time DESC ;";
            $remarks = $this->db->query($sql)->result();

            foreach ($remarks as $remark) {
                if ($star == 0) {
                    $star = $remark->remark;
                }
            }
            if ($star == 0) {
                $star = 1;
            } else {
                $star = $star + 1;
            }
            $current_user = $this->session->userdata('current_user');
            // $this->session->get_userdata("current_user");

            $content = $this->input->post('saytext');
            $content = str_replace("国军", "*", $content);

            for ($i = 1; $i <= 75; $i++) {
                $qq_face = "[em_" . $i . "]";

                $qq_face_1 = "<img src='http://www.lloonn.cn/assets/uploads/arclist/" . $i . ".gif'>";

                $content = str_replace($qq_face, $qq_face_1, $content);
            }
            //  $content = str_replace($photo, $img, $content);
            $data = array(
                'user_id' => $current_user->id,
                'acticle_id' => $this->input->post('acticle_id'),
                'content' => $content,
                'remark' => $star,
            );
            $this->db->insert('commentary', $data);
            echo "ok";
        }
    }

}

/* End of file : news.php */
