<?php

/**
 * @filename life.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2014-4-10  15:36:09
 * @version 1.0
 * @Description
 * 生活信息
 */
class Life extends CI_Controller {

    var $data = array();

    function __construct() {

        parent::__construct();
        $this->load->model('model_cmf');

        $this->load->add_package_path(APPPATH . '../package_life', false);

        $this->data['channel'] = strtolower(__CLASS__);
        $this->data['频道名称'] = "生活信息";

        $sql = "select * from params where type = 8 order by sort";
        $this->data['生活信息分类'] = $this->db->query($sql)->result();
    }

    public function index() {

        $data = lz_tag();

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

        $sql = "select * from cms_photo where menu_id = 218 limit 0,7";
        $data['首页幻灯'] = $this->db->query($sql)->result();


        $sql = "select p.value, info.* from life_info info, params p where info.type_id = p.id order by info.id desc limit 0,15";
        $data['生活信息'] = $this->db->query($sql)->result();

        $sql = "select * from params where type = 8 order by sort";
        $data['生活信息分类option'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =36 order by id desc limit 0,10";
        $data['房产'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =37 order by id desc limit 0,10";
        $data['人才'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =38 order by id desc limit 0,10";
        $data['交友'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =39 order by id desc limit 0,10";
        $data['教育'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =40 order by id desc limit 0,10";
        $data['汽车'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id =41 order by id desc limit 0,10";
        $data['家政服务'] = $this->db->query($sql)->result();

        $sql = "select * from life_info where type_id = 56 order by  id desc limit 0,10";
        $data['促销信息'] = $this->db->query($sql)->result();


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

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

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

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

        $sql = "select * from cms_menu where id = " . $data['文章'][0]->menu_id;

        $data['面包屑'] = $this->db->query($sql)->result();

        $date = date("Y-m-d", time());
        $sql = "select * from hits where menu_id=10 and date = '" . $date . "'";
        $hitss = $this->db->query($sql)->row();
        if ($hitss == null) {
            $sql = "INSERT INTO `hits` (`menu_id`, `click`, `date`) VALUES ('10', '0', '" . $date . "'); ";
            $this->db->query($sql);
        } else {
            $sql = "update hits set click = click+1 where menu_id=10 and date = '" . $date . "'";
            $this->db->query($sql);
        }
//        $sql = "select id,title,photo from cms_article where menu_id = 195 and isgood = '推荐' limit 0,8";
//        $data['推荐'] = $this->db->query($sql)->result();


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

    /*
     * 信息单页
     */

    public function info($id) {
        $data = lz_tag();

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

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

        $data['commentary_id'] = $id;

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

        $sql = "select * from params where id = " . $data['文章'][0]->type_id;

        $data['面包屑'] = $this->db->query($sql)->result();

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

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

        $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();


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

    /*
     * ajax 信息发布
     */

    public function info_publish() {
        $saved = $this->session->userdata("captcha_code");

        if ($this->input->post('captcha_code') != $saved) {
            echo "验证码错误!";
            return;
        }
        $data->type_id = $this->input->post('type');
        $data->title = $this->input->post('title');
        $data->content = $this->input->post('content');

        $this->db->insert('life_info', $data);

        $sql = "select * from life_info where title = '" . $this->input->post('title') . "' order by id desc limit 0,1";
        $rs = $this->db->query($sql)->result();
        $user_id = $this->session->userdata('current_user');

        foreach ($rs as $r) {
            $data = array(
                'user_id' => $user_id->id,
                'one_id' => $r->id,
                'footprint' => $this->input->post('title'),
                'state' => 0,
            );
            $this->db->insert('friend_footprint', $data);
        }



        echo "OK";
    }

    /*
     * 分类信息列表页
     */

    public function more($type_id, $start) {
        $config['per_page'] = 20;
        $config['uri_segment'] = 4;

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

        $sql = "select *  ";
        $from_where = "from life_info " .
                "where type_id = $type_id ";

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

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


        $sql = "select * from params where id = " . $type_id;
        $data['面包屑'] = $this->db->query($sql)->row()->value;


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

        $date = date("Y-m-d", time());
        $sql = "select * from hits where menu_id=10 and date = '" . $date . "'";
        $hitss = $this->db->query($sql)->row();
        if ($hitss == null) {
            $sql = "INSERT INTO `hits` (`menu_id`, `click`, `date`) VALUES ('10', '0', '" . $date . "'); ";
            $this->db->query($sql);
        } else {
            $sql = "update hits set click = click+1 where menu_id=10 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 : finance.php */
