<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
@header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
@header("Cache-Control: no-cache, must-revalidate"); 
@header("Pragma: no-cache");
if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:848769359');}
class Ajax extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
	}

        /*
            动态CID，0为全部，1为分享歌曲，2为分享视频，3为发表文章，4为制作专集，5为上传图片，6为收藏歌曲，7为下载歌曲，8为发表说说
            动态AC， all为全站，my为自己
        */
	public function dt()
	{
	    $this->CsdjUser->User_Login();
            $ac = $this->input->get_post('ac', TRUE);
            $cid = intval($this->input->get_post('cid', TRUE));
	    $page = intval($this->input->get_post('page'));
            if($page==0) $page=1;


            $sql_string="select * from ".CS_SqlPrefix."dt where CS_Yid=0";
            if($ac=='my'){
                $sql_string.=" and  CS_User='".$this->session->userdata('cs_name')."'";
            }
            if($cid>0){
                $sql_string.=" and cs_cid='".$cid."'";
            }

	    $query = $this->db->query($sql_string); 
	    $total = $query->num_rows();


	    $per_page = 30; 
            $totalPages = ceil($total / $per_page); // 总页数
            if($total<$per_page){
               $per_page=$total;
            }
            $sql_string.=' order by CS_ID desc limit '. $per_page*($page-1) .','. $per_page;

	    $query = $this->db->query($sql_string);

            $this->load->get_user_templates();
	    $template=$this->load->view('feed-ajax.html','',true);
	    $Mark_Text=$this->CsdjSkins->TopandBottom($template,'user');

            $dt='';
                if($total>0){
			$sorti=1;
			foreach ($query->result() as $row) {
				$datatmp=$this->CsdjSkins->DataDt($Mark_Text,$Mark_Text,$row,$sorti);
				$sorti=$sorti+1;
				$dt.=$datatmp;
			}
                        $dt=$this->CsdjSkins->labelif($dt);
                }else{
                          $dt='暂时没有数据!';
                }
            echo $dt;
	}
         
        /*发表说说*/
	public function blogadd()
	{

            $neir = $this->input->get_post('neir', TRUE);
            $neir = iconv('utf-8','gbk',$neir);
            if(empty($neir)){
                 exit('10001'); //内容为空
            }
            if(strlen($neir)>280){
		exit("10002");//内容过长
            }
            if(!$this->CsdjUser->User_Login(1)){
		exit("10003");//未登入
            }
	    if($this->CsdjSkins->DateDiff($this->session->userdata('addblogtime'),date("Y-m-d H:i:s"))<30){
		exit("10004"); //时间太短
	    }

		   $blog['CS_User'] = $this->session->userdata('cs_name');
		   $blog['CS_Neir'] = $neir;	
		   $blog['CS_AddTime'] = date('Y-m-d H:i:s');

		  $blogadd=$this->CsdjDB->get_insert ('blog',$blog);
                  if($blogadd){
		         $pldata['addblogtime']=date("Y-m-d H:i:s");
                         $this->session->set_userdata($pldata);
                         //写入动态
		         $dt['CS_Title'] = $neir;
		         $dt['CS_User'] = $this->session->userdata('cs_name');
		         $dt['CS_CID'] = 8;
		         $dt['CS_AddTime'] = date('Y-m-d H:i:s');
                         $res=$this->CsdjDB->get_insert('dt',$dt);

                         //判断说说数量
                         if(User_Bkts>0){
                              $nums=$this->CsdjSkins->getusercount($this->session->userdata('cs_name'),'blog');
                              if($nums>User_Bkts){
                                     $this->db->query("DELETE FROM ".CS_SqlPrefix."blog where CS_User='".$this->session->userdata('cs_name')."' order by CS_AddTime asc LIMIT 1");
                              }
                         }

		         exit("10006"); //发表成功
                  }else{
		         exit("10005"); //发表失败
                  }
        }

	public function gbookhf() //回复留言
	{
                $gbook['cs_usera'] = $this->input->get_post('cs_usera', TRUE); //回复会员
                $gbook['cs_fid'] = $this->input->get_post('cs_fid', TRUE); //回复ID
                $gbook['cs_neir']=$this->CsdjSkins->str_checkhtml($this->input->get_post('cs_neir', TRUE));   //消息内容
                $gbook['cs_neir'] = iconv('utf-8','gbk',$gbook['cs_neir']);
                if(empty($gbook['cs_fid']))  exit('10001'); 
                if(empty($gbook['cs_usera']))  exit('10002'); 
                if(empty($gbook['cs_neir']))  exit('10003'); 
                if(!$this->CsdjUser->User_Login(1)) exit("10002");//未登入


                //判断接收会员是否存在
	        $row=$this->CsdjDB->get_select('user','CS_Name','CS_ID',''.$gbook['cs_usera'].'');
	        if(!$row){
                      exit('10004'); 
                }

                $gbook['cs_cid'] = 0;
                $gbook['CS_Userb']=$this->session->userdata('cs_name');
                $gbook['CS_AddTime']=date('Y-m-d H:i:s');
                $res=$this->CsdjDB->get_insert('gbook',$gbook);
                if($res>0){
                       exit('10006'); 
                }else{
                       exit('10005'); 
                }
        }
}

