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

class Friend 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，默认为空为全部，xt为系统说说，wd为未读说说
                $page = intval($this->input->get('page', TRUE));   //page
                if($page==0) $page=1;


                $this->load->get_user_templates();
		$template=$this->load->view('friend-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("[friend:op]",$op,$Mark_Text); //CID

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

                        $sqlstr="select * from ".CS_SqlPrefix."friend where CS_Usera='".$this->session->userdata('cs_name')."' order by CS_AddTime desc";
                        $data_content='';
			$Arr=$this->CsdjSkins->SpanUsersPage($sqlstr,$page_arr[2][0],$pagenum,'user/friend?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) {
                                                $usera=$row2->CS_Usera;$userb=$row2->CS_Userb;
                                                if($op=='my'){
                                                     $row2->CS_Usera=$userb;
                                                     $row2->CS_Userb=$usera;
                                                }
						$datatmp=$this->CsdjSkins->DataFriend($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 del()
	{

                $id=intval($this->input->get('id', TRUE));   //id
                if($id==0) $this->CsdjSkins->blog_url('抱歉，参数错误!','javascript:history.back();');  

                $row=$this->db->query("select CS_Userb from ".CS_SqlPrefix."friend where cs_usera='".$this->session->userdata('cs_name')."' and cs_id='".$id."'")->row();
                if($row){
                     //先解除粉丝
                     $this->db->query("delete from ".CS_SqlPrefix."fans where cs_usera='".$row->CS_Userb."' and cs_userb='".$this->session->userdata('cs_name')."'");
                     $this->db->query("delete from ".CS_SqlPrefix."friend where cs_usera='".$this->session->userdata('cs_name')."' and cs_id='$id'");
                }
                $this->CsdjSkins->Msg_url('恭喜您，好友解除成功!',@$_SERVER['HTTP_REFERER']);  
        }
}
