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

class Topic 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');
		$this->CsdjUser->User_Login();
	}

	public function index()
	{
                $data='';
                $cid = intval($this->input->get('cid', TRUE));   //cid，1为歌曲，2为文章，3为视频
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('topic-list.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","专题 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[topic:cid]",$cid,$Mark_Text); //CID

		//预先除了分页
		$pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
		preg_match_all('/{cscms:topic(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:topic}/',$Mark_Text,$page_arr);//判断是否有分页标识
		if(!empty($page_arr) && !empty($page_arr[2])){

                        $sqlstr="select * from ".CS_SqlPrefix."topic where CS_User='".$this->session->userdata('cs_name')."'";
                        if($cid==2){
                            $sqlstr.=" and cs_sid=2";
                        }elseif($cid==3){
                            $sqlstr.=" and cs_sid=3";
                        }else{
                            $sqlstr.=" and cs_sid=1";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/topic?cid='.$cid,$page);//sql,每页显示条数
			$result=$this->CsdjDB->db->query($Arr[2]);
			$recount=$result->num_rows();
			if($recount==0){
				$data_content="<div align=center><strong><font style=font-size:12px;>暂无数据！</font></strong></div>";
			}else{
				if($result){
					$sorti=1;
					foreach ($result->result() as $row2) {
						$datatmp=$this->CsdjSkins->Datatopic($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						$sorti=$sorti+1;
						$data_content.=$datatmp;
					}
				}
			}// end if recount
			$Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			$Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			unset($Arr);
                }
		unset($page_arr);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('topic-add.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","专题 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:topicaddsave]",site_url('user/topic/add_save'),$Mark_Text); //发表连接
        $token=md5(uniqid(rand(), true));
		$this->session->set_userdata('token',$token);
		$Mark_Text=str_replace("[user:token]",$token,$Mark_Text);

                $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
                if($row[0]->CS_Vip==0 && User_TopicNum>0){
		       $Mark_Text=str_replace("[user:topicaddcount]",User_TopicNum,$Mark_Text); //普通会员限制数量
                }else{
		       $Mark_Text=str_replace("[user:topicaddcount]",'不限制',$Mark_Text); //VIP会员限制数量
                }

		$Mark_Text=str_replace("[user:dayaddcount]",$this->CsdjSkins->getusercount($row[0]->CS_Name,'topic','day'),$Mark_Text); //会员今日数量
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->labelif($Mark_Text);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add_save()
	{
                $this->load->helper('string'); //装载字符串辅助函数

                $topic['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_name', TRUE));   //名称
                $topic['cs_sid']=intval($this->input->post('cs_sid', TRUE));   //分类
                $topic['cs_pic']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_pic', TRUE));   //图片
                $topic['cs_tags']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_tags', TRUE));   //标签
                $topic['cs_yuyan']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_yuyan', TRUE));   //语言
                $topic['cs_year']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_year', TRUE));   //年份
                $topic['cs_fxgs']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_fxgs', TRUE));   //发行公司
                $topic['cs_diqu']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_diqu', TRUE));   //地区
                $topic['cs_content']=$this->CsdjSkins->uhtml($this->input->post('cs_content'));   //内容

				//token check
                $token=$this->input->post('token', TRUE);
				if(!$this->session->userdata('token') || $token!=$this->session->userdata('token'))  $this->CsdjSkins->Msg_url('非法提交数据!','javascript:history.back();'); 

                if(empty($topic['cs_name']))  $this->CsdjSkins->Msg_url('专题名称不能为空!','javascript:history.back();'); 
                if(empty($topic['cs_pic']))  $this->CsdjSkins->Msg_url('请上传专题图片!','javascript:history.back();'); 
                if(empty($topic['cs_content']))  $this->CsdjSkins->Msg_url('专题介绍不能为空!','javascript:history.back();'); 
				if(!preg_match("/^[a-z | 0-9 | A-Z| \.\_\/]+$/",$topic['cs_pic']))  $this->CsdjSkins->Msg_url('请先上传图片!','javascript:history.back();'); 

                if(empty($topic['cs_sid'])) $topic['cs_sid']=1;
                $topic['CS_User']=$this->session->userdata('cs_name');
                $topic['CS_AliasName']=random_string('alpha',8);
                $topic['CS_AddTime']=date('Y-m-d H:i:s');

                //判断审核
                if(User_TopicFun==1){
                    $title='，请等待管理员审核';
                    $topic['CS_YID']=1;
                    $url=site_url('user/topic').'?yid=2';
                }else{
                    $title='';
                    $topic['CS_YID']=0;
                    $url=site_url('user/topic');
                }


                //获取当天上传成功数量
                $daycount=$this->CsdjSkins->getusercount($this->session->userdata('cs_name'),'topic','day');
                if(User_TopicNum>0 && $daycount >= User_TopicNum){
                     $this->CsdjSkins->Msg_url('今天已经制作了('.$daycount.')张专题,明天在制作吧!','javascript:history.back();'); 
                }

                $res=$this->CsdjDB->get_insert('topic',$topic);
                if($res>0){

                       //写入动态
                       $dt['CS_CID']=4;
                       $dt['CS_DID']=$res;
                       $dt['CS_YID']=$topic['CS_YID'];
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$topic['cs_name'];
                       $dt['CS_AddTime']=date('Y-m-d H:i:s');
                       $this->CsdjDB->get_insert('dt',$dt);
                       $this->session->unset_userdata('token');
                       $this->CsdjSkins->Msg_url('恭喜您，专题制作成功'.$title.'!',$url); 

                }else{
                       $this->CsdjSkins->Msg_url('抱歉，专题制作失败，请联系管理员!','javascript:history.back();'); 
                }
        }

	public function del()
	{
                $id=intval($this->input->get('id', TRUE));   //id
                $hid=intval($this->input->get('hid', TRUE));   //hid 等于1时为还原
                if($id==0) $this->CsdjSkins->Msg_url('抱歉，参数错误，ID为空!','javascript:history.back();'); 
 
                if($hid==1){
                     $this->db->query("update ".CS_SqlPrefix."topic set cs_hid=0 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                     $this->CsdjSkins->Msg_url('恭喜您，还原成功!',@$_SERVER['HTTP_REFERER']);  
                }else{
                     //判断数据
	             $sid=$this->getziduan('topic','cs_sid',$id);
	             if($sid>0){ //歌曲
                          $datacount=$this->gettopiccount($id,$sid);
                          if($datacount>0){
                                $this->CsdjSkins->Msg_url('抱歉，该专题下有数据，请先删除该专题下的数据!','javascript:history.back();'); 
                          }
                     }

                     $this->db->query("update ".CS_SqlPrefix."topic set cs_hid=1 where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'");
                     $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']); 
                }
        }
}


