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

class Pic 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='';
                $op = $this->input->get('op', TRUE);   //op，my为自己，all为全站，默认为自己
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;
                $ops=($op=='all')?'all':'my';

                $this->load->get_user_templates();
		$template=$this->load->view('pic-list-'.$ops.'.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("[pic:op]",$op,$Mark_Text); //OP

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

                        if($op=='all'){
                             $sqlstr="select * from ".CS_SqlPrefix."pic where CS_YID=0";
                        }elseif($op=='ds'){
                             $sqlstr="select * from ".CS_SqlPrefix."pic where CS_YID=1 and CS_User='".$this->session->userdata('cs_name')."'";
                        }else{
                             $sqlstr="select * from ".CS_SqlPrefix."pic where CS_YID=0 and CS_User='".$this->session->userdata('cs_name')."'";
                        }

                        $sqlstr.=" order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/pic?op='.$op,$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->DataPic($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('pic-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:picaddsave]",site_url('user/pic/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_PicNum>0){
		       $Mark_Text=str_replace("[user:picaddcount]",User_PicNum,$Mark_Text); //普通会员限制舞曲数量
                }else{
		       $Mark_Text=str_replace("[user:picaddcount]",'不限制',$Mark_Text); //VIP会员限制舞曲数量
                }
		$Mark_Text=str_replace("[user:dayaddcount]",$this->CsdjSkins->getusercount($row[0]->CS_Name,'pic','day'),$Mark_Text); //会员今日上传数量
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	public function add_save()
	{
                $pic['cs_name']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_name', TRUE));   //名称
                $pic['cs_pic']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_pic', TRUE));   //图片
                $pic['cs_content']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_content', TRUE));   //介绍

				//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($pic['cs_name']))  $this->CsdjSkins->Msg_url('图片标题不能为空!','javascript:history.back();'); 
               

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

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

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

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

                       if($daycount < Cion_Num && Cion_UpPic>0 && User_PicFun==2){  //奖励金币
                              $title.='，系统奖励给您'.Cion_UpPic.'个金币';
                              $this->db->query("update ".CS_SqlPrefix."user set CS_Cion=CS_Cion+".Cion_UpPic." where CS_ID='".$this->session->userdata('cs_id')."'");
                       }

                       //写入动态
                       $dt['CS_CID']=5;
                       $dt['CS_DID']=$res;
                       $dt['CS_YID']=$pic['CS_YID'];
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$pic['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()
	{
                $ids=$this->security->xss_clean($this->input->get_post('id', TRUE));   //id
                if(empty($ids)) $this->CsdjSkins->Msg_url('抱歉，参数错误，ID为空!','javascript:history.back();'); 
 
                if(is_array($ids)){  //多选
                      $idss=implode(',', $ids);
 		      $Arr=explode(',',$idss);

          	      for($i=0;$i<count($Arr);$i++){
					  $id=intval($Arr[$i]);
		            $row=$this->db->query("select CS_Pic from ".CS_SqlPrefix."pic where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'")->row();
		            if($row){
                                  if(file_exists('./attachment/pic/'.$row->CS_Pic)){
                                        @unlink('./attachment/pic/'.$row->CS_Pic);
                                  }
                                  //删除评论
                                  $this->db->query("delete from ".CS_SqlPrefix."pl where cs_sid=4 and cs_did='".$id."'");
                                  //删除动态
                                  $this->db->query("delete from ".CS_SqlPrefix."dt where cs_cid=5 and cs_did='".$id."'");
                                  //删除图片
                                  $this->db->query("delete from ".CS_SqlPrefix."pic where cs_id='".$id."'");
                            }
                      }
                }else{
					$id=intval($ids);
                      $row=$this->db->query("select CS_Pic from ".CS_SqlPrefix."pic where cs_user='".$this->session->userdata('cs_name')."' and cs_id='".$id."'")->row();
                      //删除图片文件
                      if(file_exists('./attachment/pic/'.$row->CS_Pic)){
                           @unlink('./attachment/pic/'.$row->CS_Pic);
                      }
                      //删除评论
                      $this->db->query("delete from ".CS_SqlPrefix."pl where cs_sid=4 and cs_did='".$id."'");
                      //删除动态
                      $this->db->query("delete from ".CS_SqlPrefix."dt where cs_cid=5 and cs_did='".$id."'");
                      $this->db->query("delete from ".CS_SqlPrefix."pic where cs_user='".$this->session->userdata('cs_name')."' and cs_id='$id'");
                }
                $this->CsdjSkins->Msg_url('恭喜您，删除成功!',@$_SERVER['HTTP_REFERER']);  
        }
}


