<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class News extends Mocuz_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("news_model");
        $this->load->library('pagination');
       $this->load->helper('url');
       $this->load->helper('my_api');
       $this->load->model('mocuz/setting_model', 'setting');
        //$this->load->helper(array('form','url'));
//        $this->load->helper('mocuz_client_helper');
    }

    //新闻
    public function index()
    {redirect('news/shownewslist');
       //$this->news_model->getlist();

        //redirect('news/shownews');
      
    }

    public function shownewslist($type_search='',$keywords_search='',$stime_search='', $etime_search='', $offset = '')
    {
        //$result=$this->news_model->getlist();
        $index_hot_news=$this->setting->get('index_hot_news');
        $type_search =  empty($type_search) ? (empty($_POST['type_search']) ? '' : $_POST['type_search']) : $type_search;
        $keywords_search = empty($keywords_search) ? (empty($_POST['keywords_search']) ? '' : $_POST['keywords_search']) : $keywords_search;
        $stime_search =  empty($stime_search) ? (empty($_POST['stime_search']) ? '' : $_POST['stime_search']) : $stime_search;
        $etime_search =   empty($etime_search) ? (empty($_POST['etime_search']) ? '' : $_POST['etime_search']) : $etime_search;
        $data = array();
        $where=array();
        $ret = array(
            'status' => 1,
            'data' => array(
                array('id' => 1, 'tname' => '一图'),
                array('id' => 2, 'tname' => '三图'),
                array('id' => 3, 'tname' => '全部')
            )
        );
        if ($ret['status'] == 1) {
            $data['topic_type'] = $ret['data'];
            if (!empty($data['topic_type'])) {
                foreach ($data['topic_type'] as $value) {
                    $types[$value['id']] = $value['tname'];
                }
            }
        } else {
            $data['topic_type'] = array();
        }
         if( !empty($type_search)){
            $data['type_name_search'] = $types[$type_search];
        }
        if(empty($type_search)||$type_search==3){
            $type_search=0;
        }
        $chuanz='';
        $keywords_search=urldecode($keywords_search);
        if( !empty($keywords_search)){
             if($type_search=="0"||$type_search=='1'){
            $chuanz= "tt.title like '%" . $keywords_search . "%'";
        }else{
             $where[]="a.title like '%" . $keywords_search . "%'";
        }
        }
        if( !empty($stime_search)){
             if($type_search=="0"||$type_search=='1'){
            $where[]= "c.dateline >= " . strtotime($stime_search);
        }else{
             $where[]= "a.create_time >= " . strtotime($stime_search);
        }
        }
        if( !empty($etime_search)){
            if($type_search=="0"||$type_search=='1'){
            $where[]= "c.dateline < " . (strtotime($etime_search) + 24*60*60);
        }else{
             $where[]= "a.create_time < " . (strtotime($etime_search) + 24*60*60);
        }
        }
       //$where[] = "isdelete = 0";
        $result=array();
        $temp=array();
        $pic=array();
        $date=$this->news_model->gettotal($where,$type_search,$chuanz,$index_hot_news); 
        if(empty($keywords_search)){
            $keywords_search=0;
        }
        if(empty($stime_search)){
             $stime_search=0;
        }
        if(empty($etime_search)){
             $etime_search=0;
        }
        $del_count=$this->news_model->get_del_total($type_search,$keywords_search,$stime_search,$etime_search,$index_hot_news);
        $this->load->library('pagination');
        $total=$date['total']-count($del_count);
              

        $config['base_url']=site_url('news/shownewslist/'. $type_search
            . '/' . $keywords_search .'/' . $stime_search . '/' . $etime_search);
         $config ['uri_segment'] = 7;
        $offset = $this->uri->segment(7);
        $config['total_rows'] = $total;
        $config['per_page'] = 10;
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li id="first">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '末页';
        $config['last_tag_open'] = '<li id="last">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li id="down">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li id="top">';
        $config['prev_tag_close'] = '</li>';
        $config['num_links'] = 10;

        $config['full_tag_open'] = '<div class="wrap"><div class="fenye"><ul>';
        $config['full_tag_close'] = '</ul></div></div>';
        $this->pagination->initialize($config); 
        $page=$this->pagination->create_links();
        //$data['page']=$page;
        $limit = $config["per_page"];
        $temp=$this->news_model->getlist($where,$type_search,$chuanz,$index_hot_news);

        $top=$this->news_model->get_top($index_hot_news);

        $attacurlkey=$this->news_model->getattacurl(1);
        $attacurlkey=unserialize($attacurlkey);
        if($attacurlkey['on']==0){
           $attacurl=$this->news_model->getattacurl(0);
           $attacurl="http://".$_SERVER['HTTP_HOST'].'/'.$attacurl.'/';
        }else{
            if(!strchr($attacurlkey['attachurl'],'http')){
                if(!strchr($attacurlkey['host'],'http')){
                    $attacurl="http://".$attacurlkey['host'].'/'.$attacurlkey['attachurl'].'/';
                }else{
                     $attacurl=$attacurlkey['host'].'/'.$attacurlkey['attachurl'].'/';
                } 
            }else{
                $attacurl=$attacurlkey['attachurl'].'/';
            }
            
        }
        $del_ids=array();
        if(count($del_count)>=1){
            foreach ($del_count as $king=> $box) {
               $del_ids[]=$box['id'];
           }
        }
        
        foreach ($temp as $key=>$value) {
           if(in_array($value['id'],$del_ids)){
            continue;
           }
            if($value['pictype']==0){
                $pic=array(0=>$attacurl.$value['pic']);
            }else{
                $pic=json_decode($value['pic'],true);
            }
            $picurl=implode("--,", $pic);
           $views=$value['views_differ']+$value["discuz_views"];
           $result[]=array('id'=>$value['id'],'fid'=>$value['fid'],"title"=>$value["title"],"discuz_views"=>$value["discuz_views"],"type"=>$value["type"],"pic"=>$pic,"picurl"=> $picurl,"create_time"=>format_date($value['create_time']),'c_time'=>$value['create_time'],'t_time'=>$value['ts_time'],"top"=>$value['top'],"ts_time"=>format_date($value['ts_time']),"views"=>$views);
             }  
        //print_r($config['per_page']);
             $result=array_slice($result, $offset,$config['per_page']);
        //                echo "<pre>";
        // print_r($result);die;
        $fenyelist=array();
        $fenyelist['type_search']=$type_search;
        $fenyelist['keywords_search']=$keywords_search;
        $fenyelist['stime_search']=$stime_search;
        $fenyelist['etime_search']=$etime_search;
        $fenyelist['offset']=$offset;
        $data['fenyelist']=json_encode($fenyelist);
        $data['val']=$fenyelist;
        $data['data']=$result;
         if($top){
            $data['is_top']=1;
        }else{
             $data['is_top']=0;
        }
        $data['page']=$page;
        $this->load->view('common/innerheader');
        $this->load->view('news',$data);
        $this->load->view('common/footer');
    }




    public function upload_news($name){
        $path = '/uploads/news/';
        $this->ci = &get_instance();
        $config['upload_path']      = FCPATH . $path;
        if (!is_dir($config['upload_path'])) {
            mkdir($config['upload_path'],0777,true);
        }
        $config['allowed_types']    = 'gif|jpg|png';
        $url = base_url();
        $config['file_name'] = date('YmdHis').rand(0,100);
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload("$name"))
        {
            $error = array('error' => $this->upload->display_errors());
            echo json_encode($error);exit;
        } else {
            $this->ci->load->library('image_lib');
            $data = $this->upload->data();
            list($width, $height, $type, $attr) = getimagesize($url);
            $url = $url.$path.$data['file_name'];
            $thumconfig['image_library'] = 'gd2';
            $thumconfig['maintain_ratio'] = TRUE;
            $thumconfig['source_image'] = FCPATH.$path.$data['file_name'];
            $thumconfig['width']     = 200;
            $thumconfig['height']   = 200;
            //$this->load->library('image_lib', $thumconfig);
            $this->ci->image_lib->initialize($thumconfig);
            $this->image_lib->resize();
            
//            $pic['url'] = $url.'|'.$width.'|'.$height;
            //$pic['url'] = $url;
            return $url;
        }

    }

        public function news_delete(){
            echo '{}';
        }

        public function news_save()
    {  
        $imgtempa=$_FILES['imgtempa']['name'];
        if(!$imgtempa){
            $imgtempaa=$this->input->post('imgtempaa');
        }else{
             $name="imgtempa";
             $imgtempaa=$this->upload_news($name);
        }
        $imgtempb=$_FILES['imgtempb']['name'];
        if(!$imgtempb){
            $imgtempbb=$this->input->post('imgtempbb');
        }else{
             $name="imgtempb";
             $imgtempbb=$this->upload_news($name);
        }
        $imgtempc=$_FILES['imgtempc']['name'];
        if(!$imgtempc){
            $imgtempcc=$this->input->post('imgtempcc');
        }else{
             $name="imgtempc";
             $imgtempcc=$this->upload_news($name);
        }
        $pic=array();
        array_push($pic,$imgtempaa);
        if($imgtempbb){
            array_push($pic,$imgtempbb);
        }
        if($imgtempcc){
            array_push($pic,$imgtempcc);
        }
        $param = $_POST;
        $fenye=$this->input->post('fenyelist');
        $fenyelist=json_decode($fenye,true);
        $fenyelist['keywords_search']=urldecode($fenyelist['keywords_search']);
        $titleg = strip_tags($this->input->post('titleg'));
        $titleg = preg_replace('/((&nbsp;(\s+)?)+)/', '', $titleg);
        $titleg = preg_replace("/[\r\n]/", '', $titleg);
        // if(mb_strlen($titleg)>195){
        //    $titleg=str_replace(mb_substr($titleg, 195,mb_strlen($titleg)), "...", $titleg) ;
        // }
        // $data = array();
        $rules_config = array(
            array(
                'field' => 'titleg',
                'label' => '标题',
                'rules' => 'trim|required|max_length[200]'
            ),
        );

        $this->load->library('form_validation', $rules_config);
        $this->form_validation->set_data($param);


        if($this->form_validation->run() == FALSE)
        {
            print_r($this->form_validation->error_array());
        }
        else
        {
            $data['id']=$this->input->post('idg');
            $data['title'] = $titleg;
           if(count($pic)==3){
                $data['type'] = 2;
            }else{
                $data['type'] = 1;
                $pic=array($pic[0]);

            }
            $data['fid']=$this->input->post('fidg');
            $data['pic'] = json_encode($pic);
            $data['views_differ'] = $this->input->post('viewsg')-$this->input->post('viewsgg');
            $data['top'] = $this->input->post('topg');
            $data['isdelete'] = 0;
            $data['create_time'] =$this->input->post('create_timeg');
            $data['ts_time'] =$this->input->post('ts_timeg');
            $data['old_views']=$this->input->post('viewsgg');
            $result=$this->news_model->insertnews($data,$data['id']);
            if($result){
                $type_search = isset($fenyelist['type_search']) ? $fenyelist['type_search'] : '';
                $keywords_search = isset($fenyelist['keywords_search']) ? $fenyelist['keywords_search'] : '';
                $stime_search = isset($fenyelist['stime_search']) ? $fenyelist['stime_search'] : '';
                $etime_search = isset($fenyelist['etime_search']) ? $fenyelist['etime_search'] : '';
                if(empty($fenyelist['offset'])){
                   redirect("news/shownewslist/".$type_search.'/'.$keywords_search.'/'.$stime_search.'/'.$etime_search);
                }else{
                    redirect("news/shownewslist/".$type_search.'/'.$keywords_search.'/'.$stime_search.'/'.$etime_search.'/'.$fenyelist['offset']);
                }
            
            }
           
        }
    }
    public function news_edit(){

        $fid=$this->input->post('fid');
        $id=$this->input->post('id');
        $views=$this->input->post('views');
        $top=$this->input->post('top');
        $create_time=$this->input->post('create_time');
        $ts_time=$this->input->post('ts_time');
        $title=$this->input->post('title');
        $type=$this->input->post('type');
        $picurl=$this->input->post('picurl');
        $fenye=$this->input->post('fenye');
        $discuz_views=$this->input->post('discuz_views');
        $pic=array();
        if($picurl){
            
            $pic=explode("--,", $picurl);
        }
        $data=array();
        $data['code'] = 1;
        $data['fid']=$fid;
        $data['ida'] = $id;
        $data['views'] =$views;
        $data['topa'] = $top;
        $data['create_time'] = $create_time;
        $data['ts_time'] = $ts_time;
        $data['titlea'] =$title;
        $data['type'] = $type;
        $data['pic'] = $pic;
        $data['count']=count($pic);
        $data['fenye'] = $fenye;
        $data['discuz_views'] =$discuz_views;
        echo json_encode($data);
        exit;
       

    }
//置顶
    public function update_news(){
        $param=$_POST;
       $result=array();
       $result['fenyelist']=$this->input->post("fenye");
       $result['title']=$this->input->post("title");
       $result['type']=$this->input->post("type");
       $result['create_time']=$this->input->post("create_time");
       $fenyelist=json_decode($result['fenyelist'],true);
       $fenyelist['keywords_search']=urldecode($fenyelist['keywords_search']);
       $id=$this->input->post("ida");
       $top=$this->input->post("top");
       $discuz_views=$this->input->post("discuz_views");
       $ts_time=$this->input->post("ts_time");
       $picurl=$this->input->post("picurl");
       
       $pic=explode("--,",$picurl);
        if(isset($param['ding'])){
            $data=array();
            $data['fid']=$this->input->post("fid");
            $data['id']=$id;
            if($top){
                $data['top']=0;
            }else{
                $data['top']=1;
            }
            $data['pic']=json_encode($pic);
            $data['old_views']=$discuz_views;
            $data['create_time']=$result['create_time'];
            $data['ts_time']=$ts_time;
            $data['title']=$result['title'];
            $data['type']=$result['type'];
            $result=$this->news_model->insertnews($data,$id);
            if($result){
                if(empty($fenyelist['offset'])){
                   redirect("news/shownewslist/".$fenyelist['type_search'].'/'.$fenyelist['keywords_search'].'/'.$fenyelist['stime_search'].'/'.$fenyelist['etime_search']); 
                }else{
                    redirect("news/shownewslist/".$fenyelist['type_search'].'/'.$fenyelist['keywords_search'].'/'.$fenyelist['stime_search'].'/'.$fenyelist['etime_search'].'/'.$fenyelist['offset']); 
                }
            
            }
        }

        if(isset($param['del'])){
            $data=array();
            $data['id']=$id;
            $data['fid']=$this->input->post("fid");
            $data['top']=$top;
            $data['pic']=json_encode($pic);
            $data['old_views']=$discuz_views;
            $data['create_time']=$result['create_time'];
            $data['ts_time']=$ts_time;
            $data['title']=$result['title'];
            $data['type']=$result['type'];
            $data['isdelete']=1;
            $result=$this->news_model->insertnews($data,$id);
            if($result){
                if(empty($fenyelist['offset'])){
                   redirect("news/shownewslist/".$fenyelist['type_search'].'/'.$fenyelist['keywords_search'].'/'.$fenyelist['stime_search'].'/'.$fenyelist['etime_search']); 
                }else{
                    redirect("news/shownewslist/".$fenyelist['type_search'].'/'.$fenyelist['keywords_search'].'/'.$fenyelist['stime_search'].'/'.$fenyelist['etime_search'].'/'.$fenyelist['offset']); 
                }
            
            }
        }
        // $data = array();
        // $data['id']=$id;
        // //$data['data']=$result;
        // $result['pic']=$pic;
        // $result['id']=$id;
        // $data['data']=$result;
        // $this->load->view('common/innerheader');
        // $this->load->view('news_edit',$data);
        // $this->load->view('common/footer');
    }


}