<?php

/*
 * Copyright (C) xiuno.com
 */

!defined('FRAMEWORK_PATH') && exit('FRAMEWORK_PATH not defined.');

include POST_PATH.'core/control/common_control.class.php';

class ua_control extends common_control {

	function __construct(&$conf) {
		parent::__construct($conf);
		$this->check_login();
		
		// 初始化用户信息
		$uid = $this->_user['uid'];
		$user = $this->member->read($uid);
		$this->check_user_exists($user);
		//$this->member->format($user);
		
		//$user['groupname'] = $this->conf['grouparr'][$user['groupid']];
		$this->_user = $user;
	}
	//加入贴吧
	public function on_join(){
		$uid=$this->_user['uid'];
		$fid=intval(core::gpc('fid'));
		$forum = $this->forum->read($fid);
		$this->check_forum_exists($forum);
		$this->check_forum_status($forum);
		//$this->check_access($forum, 'join');
		if($forum['user_join']==1) $this->message('本吧禁止加入',0);
		
		$rs=$this->forum_users->check_status($fid,$uid);
		if($rs){
		    $msg=!$rs['status']?'您的申请待审核':($rs['status']==1?'您已加入本吧':'您的申请已被拒绝');	
			$this->message($msg,0);
		}else{
			$status=$forum['user_join']==2?1:0;
			$this->forum_users->create(array('fid'=>$fid,'uid'=>$uid,'dateline'=>$_SERVER['time'],'status'=>$status));
			if($status==1){
			   	$forum['users']++;
				$this->forum->update($forum);
				$this->mcache->clear('forum',$fid);
			}
			$this->message($status);
		}
		
	}
	//退出贴吧
	public function on_unjoin(){
		$uid=$this->_user['uid'];
		$fid=intval(core::gpc('fid'));
		$forum = $this->forum->read($fid);
		$this->check_forum_exists($forum);
		$this->check_forum_status($forum);
		//$this->check_access($forum, 'join');
		if($forum['user_join']==1) $this->message('本吧禁止加入',0);
		
		$rs=$this->forum_users->check_status($fid,$uid);
		if($rs && $rs['status']==1){
			$this->forum_users->delete($fid,$uid);
			$forum['users']--;
			$this->forum->update($forum);
			$this->mcache->clear('forum',$fid);
			
			$this->message('成功退出本吧');
		}else{
			$this->message('您没有加入本吧',0);
		}
		
	}
	//每日签到
	public function on_sign(){
		$this->conf += $this->kv->xget('conf_ext');
		
		$uid=$this->_user['uid'];
		
		$fid=intval(core::gpc('fid'));
		$forum = $this->forum->read($fid);
		$this->check_forum_exists($forum);
		$this->check_forum_status($forum);
		$rs=$this->forum_users->check_status($fid,$uid);
		
		$user=$this->_user;
		
		if(!$rs){
		   $status=$forum['user_join']==2?1:0;
		   $this->forum_users->create(array('fid'=>$fid,'uid'=>$uid,'sign_days'=>1,'sign_days_l'=>1,'lastsign'=>$_SERVER['time'],'dateline'=>$_SERVER['time'],'status'=>$status));
		    
			$forum['users']++;
			$this->forum->update($forum);
			$this->mcache->clear('forum',$fid);
			
		   $user['credits']+=$this->conf['credits_sign_1'];
		   $user['golds']+=$this->conf['golds_sign_1'];
		   $this->member->update($user);
		   $arr=array('msg'=>'签到成功，积分+'.$this->conf['credits_sign_1'].' 金币+'.$this->conf['golds_sign_1'],'html'=>'连续打卡1天');
		   $this->message($arr);
		}else{
		  // if($rs['lastsign']>$_SERVER['time_today']) $this->message('今天已经打卡了',0);
		   
		   $rs['lastsign']=$_SERVER['time'];
		   $rs['sign_days']++;
		   if($rs['lastsign']<($_SERVER['time_today']-86400)){
			   $rs['sign_days_l']=1; 
		   }else{
			   $rs['sign_days_l']++;
		   }
		   if($rs['sign_days_l']<11){
			   $c=$this->conf['credits_sign_1'];
			   $g=$this->conf['golds_sign_1'];
		   }elseif($rs['sign_days_l']<31){
			   $c=$this->conf['credits_sign_2'];
			   $g=$this->conf['golds_sign_2'];
		   }else{
			   $c=$this->conf['credits_sign_3'];
			   $g=$this->conf['golds_sign_3'];   
		   }
		   $user['credits']+=$c;
		   $user['golds']+=$g;
		   $this->member->update($user);
		   $this->forum_users->update($rs);
		   $arr=array('msg'=>'签到成功，积分+'.$c.' 金币+'.$g,'html'=>'连续打卡1天');
		   $this->message($arr);
		}
		
		
	}
	
}

?>