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

class News_model extends CI_Model {

    public $table = 'news';


    public function __construct()
    {
        parent::__construct();
    }
    
    public function getattacurl($temp){
        @include(APPPATH.'config/database.php');
       $this->db->set_dbprefix();
      if($temp==1){
      $query=$this->db->query("select svalue from " . $db['discuz']['dbprefix'] . "common_setting where skey='ftp'");
      if ($query->num_rows()>0) {
          $data= $query->result_array(); 
        }
      $this->db->set_dbprefix("mocuz_card_");
      return $data[0]['svalue'];
      }else{
$query=$this->db->query("select svalue from " . $db['discuz']['dbprefix'] . "common_setting where skey='attachurl'");
if ($query->num_rows()>0) {
          $data= $query->result_array(); 
        }
      $this->db->set_dbprefix("mocuz_card_");
      return $data[0]['svalue'];
      } 
    }

   public function get_top($index_hot_news){
    $where=array();
    if($index_hot_news){
      $where['fid']=$index_hot_news['value'];
    }
    $where['top']=1;
    $where['isdelete']=0;
    $this->db->where($where);
    return $this->db->get($this->table)->row_array();

   }

    public function gettotal_jiekou(){
        @include(APPPATH.'config/database.php');
        $wherea="where a.idtype='tid'";
        $this->db->set_dbprefix();
        $query=$this->db->query("select count(tt.id) as total from (select a.id,a.picflag,IFNULL(b.title,a.title) as title ,IFNULL(b.pic,a.pic) as pic,IFNULL(b.type,0) as pictype,IFNULL(b.type,1) as type ,c.dateline ,a.dateline as time from " . $db['discuz']['dbprefix'] . "common_block_item_data as a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id $wherea) as tt ");
        if ($query->num_rows()>0) {
           $data  = $query->result_array(); 
        }
        $this->db->set_dbprefix("mocuz_card_");
       return $data[0];
     }

    public function getlist_jiekou($num,$offset){
        @include(APPPATH.'config/database.php');
      if(!$offset){
        $limit=" LIMIT ".$num;
      }elseif ($offset==$num) {
        $limit=" LIMIT ".$num.",".$offset;
      }elseif($offset>$num){
        $limit=" LIMIT ".$offset.",".$num;
      }
        $wherea="where a.idtype='tid'";
        $temp='';
        $temp.= " order by tt.time desc";
        $temp.=$limit;
        $data=array();
        $this->db->set_dbprefix();
        $query=$this->db->query("select tt.id,tt.picflag,tt.title,tt.pic,tt.pictype,tt.views,tt.type,tt.dateline as create_time ,tt.time as tstime from (select a.id,a.picflag,IFNULL(b.title,a.title) as title,c.views,IFNULL(b.pic,a.pic) as pic,IFNULL(b.type,0) as pictype,IFNULL(b.type,1) as type ,c.dateline ,a.dateline as time from " . $db['discuz']['dbprefix'] . "common_block_item_data as a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id $wherea) as tt $temp");
             if ($query->num_rows()>0) {
            foreach ($query->result_array() as $row) { 
                    $data[]=$row;
            }
        }
       $this->db->set_dbprefix("mocuz_card_");
     return $data;
      }

      public function insertnews($data,$id){
      $list=array();
      $list_id=array();
      $this->db->set_dbprefix();
      $query=$this->db->query("select id from mocuz_card_news");
      if ($query->num_rows()>0) {
          $list[]= $query->result_array(); 
        }
        if(isset($list[0])&&$list[0]){
          foreach ($list[0] as $key => $value) {
         $list_id[]=$value['id'];
        }
        if(in_array($id,$list_id)){
          $this->db->set_dbprefix("mocuz_card_");
          $this->db->where('id', $id);
          $this->db->update($this->table, $data);
          return 1;
        }else{
          $this->db->set_dbprefix("mocuz_card_");
           $this->db->insert($this->table, $data);
          return 1;
        }   
        }
      else{
          $this->db->set_dbprefix("mocuz_card_");
           $this->db->insert($this->table, $data);
          return 1;
        }    
    }

    public function get_del_total($type_search,$keywords_search,$stime_search,$etime_search,$index_hot_news){
      $where=array();
      $where['isdelete']=1;
      if($index_hot_news){
      $where['fid']=$index_hot_news['value'];
    }
      if(!empty($type_search)){
      $where['type']=$type_search;
      }
      if(!empty($keywords_search)){
      $this->db->like('title',$keywords_search, 'both');
      }
       if(!empty($stime_search)){
      $where['create_time >=']= strtotime($stime_search);
      }
       if(!empty($etime_search)){
      $where['create_time <=']= strtotime($etime_search);
      }
      $this->db->where($where);
       return $this->db->get($this->table)->result_array();
       
    }
     public function gettotal($where,$type_search,$chuanz,$index_hot_news){
         @include(APPPATH.'config/database.php');
      $whereb='';
      if($type_search=="0"||$type_search=='1'){
            $wherea="where a.idtype='tid'";
             if($index_hot_news){
      $wherea.=" and a.bid=".$index_hot_news['value'];
    }
    $wherea.=" AND c.displayorder IN(0,1,2,3)";
      if(count($where)>0)
        {   $whereb.=" and ";
            $wherec = implode(' and ', $where);
            $whereb.=$wherec;
        }
        $wherea.=$whereb;
        if($type_search=="1"){
           $temp=" where tt.type=1 ";
          if($chuanz){
           $temp.=" and ".$chuanz;
          }
        }else{
          if($chuanz){
           $temp  =" where   ".$chuanz;
          }else{
          $temp='';
          }   
        }
        $this->db->set_dbprefix();
        $query=$this->db->query("select count(tt.id) as total from (select a.id,IFNULL(b.title,a.title) as title ,IFNULL(b.pic,a.pic) as pic,IFNULL(b.type,0) as pictype,IFNULL(b.type,1) as type ,c.dateline from " . $db['discuz']['dbprefix'] . "common_block_item_data as a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id $wherea) as tt $temp");
        if ($query->num_rows()>0) {
           $data  = $query->result_array(); 
        }
        $this->db->set_dbprefix("mocuz_card_");
       return $data[0];
     }else{
       $whereb="where a.type=2 ";
       $wherec='';
        $this->db->set_dbprefix();
        if(count($where)>0)
        {   $wherec=" and ";
            $wherea = implode(' and ', $where);
            $wherec.=$wherea;
        }
        $whereb.=$wherec;
        if($index_hot_news){
      $whereb.=" and a.fid=".$index_hot_news['value'];
    }
    $whereb.=" AND c.displayorder IN(0,1,2,3)";
        $query=$this->db->query("select count(a.id) as total from mocuz_card_news a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid  $whereb");
        if ($query->num_rows()>0) {
           $data  = $query->result_array(); 
        }
        $this->db->set_dbprefix("mocuz_card_");
        return $data[0];
     }
    }

      public function getlist($where,$type_search,$chuanz,$index_hot_news){
          @include(APPPATH.'config/database.php');

      // if(!isset($offset)){
      //   $limit=" LIMIT ".$num;

      // }elseif ($offset==$num) {
      //   $limit=" LIMIT ".$num.",".$offset;
      // }elseif($offset>$num){
      //   $limit=" LIMIT ".$offset.",".$num;
      // }
      $whereb='';
      if($type_search=="0"||$type_search=="1"){
          $wherea="where a.idtype='tid'";
           if($index_hot_news){
      $wherea.=" and a.bid=".$index_hot_news['value'];
    }
    $wherea.=" AND c.displayorder IN(0,1,2,3)";
      if(count($where)>0)
        {   $whereb=" and ";
            $wherec = implode(' and ', $where);
            $whereb.=$wherec;
        }
        $wherea.=$whereb;

        if($type_search=="1"){
           $temp=" where tt.type=1 ";
        if($chuanz){
           $temp.=" and ".$chuanz;
          } 
        }else{
          if($chuanz){
           $temp  =" where   ".$chuanz;
          }else{
          $temp='';
          }   
        }
        $temp.= " order by tt.time desc";
        //$temp.=$limit;

        // print_r($wherea);die;
             $data=array();
      $this->db->set_dbprefix();
      $query=$this->db->query("select tt.id,tt.discuz_views,tt.fid,tt.views_differ,tt.title,tt.pic,tt.pictype,tt.type,tt.top,tt.dateline as create_time ,tt.time as ts_time from (select a.id,a.bid as fid,IFNULL(b.title,a.title) as title ,c.views as discuz_views,IFNULL(b.top,0) as top,IFNULL(b.pic,a.pic) as pic,IFNULL(b.views_differ,0) as views_differ,IFNULL(b.type,0) as pictype,IFNULL(b.type,1) as type ,c.dateline ,a.dateline as time from " . $db['discuz']['dbprefix'] . "common_block_item_data as a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid left join mocuz_card_news as b on a.id=b.id $wherea) as tt $temp");
             if ($query->num_rows()>0) {
            foreach ($query->result_array() as $row) { 
                    $data[]=$row;
            }
        }
        // echo "<pre>";
        // print_r($data);die;
       $this->db->set_dbprefix("mocuz_card_");
     return $data;
      }else{
        $data=array();
        $whereb="where a.type=2 ";
       $wherec='';
        $this->db->set_dbprefix();
        if(count($where)>0)
        {   $wherec=" and ";
            $wherea = implode(' and ', $where);
            $wherec.=$wherea;
        }
        $whereb.=$wherec;
         if($index_hot_news){
      $whereb.=" and a.fid=".$index_hot_news['value'];
    }
    $whereb.=" AND c.displayorder IN(0,1,2,3)";
        $whereb.=" order by a.ts_time desc";
        $query=$this->db->query("select a.fid,a.id,a.title,a.type,a.pictype,a.pic,IFNULL(a.views_differ,0) as views_differ,a.create_time,a.ts_time,a.top,c.views as discuz_views from mocuz_card_news a left join " . $db['discuz']['dbprefix'] . "forum_thread as c on a.id=c.tid  $whereb");
         if ($query->num_rows()>0) {
            foreach ($query->result_array() as $row) { 
                    $data[]=$row;
            }
        }
        // echo "<pre>";
        // print_r($data);die;
       $this->db->set_dbprefix("mocuz_card_");
     return $data;
      }
   } 
    
    
}