<?
/*
simple pal:
id,u_id,pal_id, rel

rel:
[1] uID follow pID 
[2] uID asked pID  
[3] uID friend with pID {pID friend with uID[3]}
[4] uID blocked by pID (no rel before)							[7] pID blocking uID
[5] uID blocked by pID (uID followed pID [1])					[8] pID blocking uID
[6] uID blocked by pID (uID was a friend with pID [2 || 3] )	[9] pID blocking uID

//////////////////////////////////////////////////

1 = all can see [default= full name, number of followers]
2 = only registered users  [reviews title ,votes, 
3 = only followers and followed
4 = (future) only friends

//////////////////////////////////////////////////
main sh ->->->->
,
images[show],
profile image[show],
posts[show],
email[don't show],
phone [don't show],
birth date [show {no year}] (full/no year),
nationality [don't show],
location [show], 

*/
class pal{
// clean Arr should be : relArr=array(1=>array(1,'follow',array('unFollow','add','block'),'Follow','Following confirmed'))
	function pal(){
	global $db;	
	global $ses;
	$this->db=$db;
	$this->ses=$ses;
	$this->butArr=array('follow'=>'Follow','add'=>'Add a Friend','confirmF'=>'Confirm Friendship','block'=>'Block User','unBlock'=>'Unblock User','unFollow'=>'Unfollow','unFriend'=>'Unfriend');

		$this->relArr=array(
			'X'=>array('','noRel','No Relations',array('follow','add','block')),
			1=>array(1,'follow','you\'re following this user',array('unFollow','add','block')),
			2=>array(2,'asked','Sent friend request',array('unFriend','follow','block')),
			3=>array(3,'friend','A friend',array('unFriend','block')),
			4=>array(4,'blocked','you can\'t view this user\'s details',array('')),
			5=>array(5,'blocked','you can\'t view this user\'s details',array('')),
			6=>array(6,'blocked','you can\'t view this user\'s details',array('')),
			7=>array(7,'blockedByU','You blocked this user',array('unBlock')),
			8=>array(8,'blockedByU','You blocked this user',array('unBlock')),
			9=>array(9,'blockedByU','You blocked this user',array('unBlock'))
		);
	}

	function checkPals($uID,$pID,$uDef){
	$r=$this->db->select('rel','pals',' WHERE u_id=\''.$uID.'\' AND pal_id=\''.$pID.'\'',' LIMIT 0,1');
		if (empty($r)){
		return $this->relArr['X'];
		// have no relations
		}
		else{
		return $this->relArr[$r[0]['rel']];
		}
	}
	
	//// toKill
	function getPals(){
	$back=array();
	$r=$this->db->select('rel,pal_id','pals',' WHERE u_id=\''.$this->ses->getSesVar('uID').'\' AND (rel=1 OR rel=2 OR rel=3)','');
		if (!empty($r)){
			foreach($r as $i=>$v){
			$back[$r[$i]['pal_id']]=$r[$i]['rel'];
			}
		}
	return $back;
	}
	//////END TOKILL
	
	function folWhom(){
	$back=array();
	$r=$this->db->select('pal_id,rel','pals',' WHERE u_id=\''.$this->ses->getSesVar('uID').'\' AND (rel=1 OR rel=2 OR rel=3)','');
		if (!empty($r)){
			foreach($r as $i=>$v){
			$back[]=array($r[$i]['pal_id'],$r[$i]['rel']);
			}
		}
	return $back;
	}
	
	function doPals($do,$pNick,$uID){
	$p=$this->db->select('u_id','sb_u',' WHERE nick=\''.$pNick.'\'','LIMIT 0,1');
	$pID=$p[0]['u_id'];
	$rel='';
		if($do=='follow' || $do=='add' || $do=='unFriend' || $do=='unFollow'){$c=$this->db->select('*','pals',' WHERE u_id=\''.$uID.'\' AND pal_id=\''.$pID.'\' ','LIMIT 0,1');}
		elseif($do=='block' || $do=='confirmF' || $do=='unBlock'){$c=$this->db->select('*','pals',' WHERE u_id=\''.$pID.'\' AND pal_id=\''.$uID.'\' ','LIMIT 0,1');}
		if(!empty($c)){$rel=$c[0]['rel'];}
		if($do=='follow'){
			if($rel==''){if($this->db->insert('pals','u_id,pal_id,rel','"'.$uID.'","'.$pID.'","1"')==true){$_SESSION['folWhom']=$this->folWhom();	return 'follow';}else {return false;}}
			elseif( $rel=='4' || $rel=='5' || $rel=='6'){return 'You can\'t follow this user post'; /*bag*/}
			elseif( $rel=='7' || $rel=='8' || $rel=='9'){return 'You can\'t follow users you blocked, first you need to unblock (maybe you\'re already a follower';}
			elseif($rel=='1' || $rel=='2' || $rel=='3'){return 'You\'re already following this user posts'; /*bag*/}
		}
		elseif($do=='add'){
			if($rel=='' || $rel=='1'){if($this->updateOrIns($uID,$pID,'2')==true) {$_SESSION['folWhom']=$this->folWhom();	return 'asked'; }else {return false;}}
			elseif($rel=='4' || $rel=='5' || $rel=='6'){return 'You can\'t ask friendship from this user'; /*bag*/}
			elseif($rel=='7' || $rel=='8' || $rel=='9'){return 'You blocked this user, release the blocking (maybe you\'re already friends)'; /*bag*/}			
			elseif($rel=='3'){return 'You\'re already Friends'; /*bag*/}
			elseif($rel=='2'){return 'You already sent a friend request'; /*bag*/}
		}		
		elseif($do=='block'){
			if($rel=='4' || $rel=='5' || $rel=='6'){/*bag*/ return 'This user is already blocked';}
			else {
				if($rel=='1'){$uRel=8; $pRel=5;}
				elseif($rel=='3' || $rel=='2'){$uRel=9; $pRel=6;}
				elseif($rel==''){$uRel=7; $pRel=4;}
			if($this->updateOrIns($pID,$uID,$pRel)==true && $this->updateOrIns($uID,$pID,$uRel)==true) {$_SESSION['folWhom']=$this->folWhom();	return 'blocked'; }
			else {return false;}
			}
		}
		elseif($do=='unBlock'){
			if($rel=='4'){				
				if($this->db->delete('pals','WHERE id=\''.$c[0]['id'].'\'')==true && $this->db->delete('pals','WHERE u_id=\''.$uID.'\' AND p_id=\''.$pID.'\' AND rel=\'7\'')==true){$_SESSION['folWhom']=$this->folWhom();	return 'unBlocked';}
				else {return false;}
			}
			elseif($rel=='5' || $rel=='6'){
				if ($rel=='5'){$pRel=$uRel='1';}
				elseif ($rel=='6'){$pRel=$uRel='3';}
				if($this->updateOrIns($pID,$uID,$pRel)==true && $this->updateOrIns($uID,$pID,$uRel)==true) {$_SESSION['folWhom']=$this->folWhom();	return 'unBlocked'; }
				else {return false;}
			}
			else {return false;}
		}
		elseif($do=='unFriend'){
			if($rel=='2' || $rel=='3'){				
				if($this->db->delete('pals','WHERE id=\''.$c[0]['id'].'\'')==true){
				$this->db->delete('pals','WHERE u_id=\''.$pID.'\' AND pal_id=\''.$uID.'\'');
				$_SESSION['folWhom']=$this->folWhom();	
				return 'Unfriended Successfully';				
				}
				else {return false;}
			}
			else {return false;}
		}		
		elseif($do=='confirmF'){
		$cToC='';
		$back='';
			if($rel=='2' || $rel=='6'){			
				if($this->db->update('pals','rel=3','WHERE id=\''.$c[0]['id'].'\'')==true){
				$back='friend'; $cToC='ok';
				}
				else {return false;}
			}
			if($rel=='5'){
				if($this->db->update('pals','rel=1','WHERE id=\''.$c[0]['id'].'\'')==true){
				 $cToC='ok';
				$back='confirmF';
				}
				else {return false;}
			}
			elseif($rel=='4'){
				if($this->db->delete('pals','WHERE id=\''.$c[0]['id'].'\'')==true){
				$back='unBlocked';
				}
				else {return false;}
			}
			elseif($rel=='3' || $rel=='1'){
			/*bag*/ return false;
			}
			elseif($rel==''){
			return false;
			}
			if ($cToC=='ok'){
				if($this->updateOrIns($uID,$pID,'3')==true) {$_SESSION['folWhom']=$this->folWhom();	return $back;}
				else {return false;}
			}
			else {return $back;}
		}
	}
	
	
	function palBut($do,$palNick){
	return '<input type="button" class="palBut"  value="'.$this->butArr[$do].'" tag="'.$palNick.'" do="'.$do.'"/>';
	}
	function checkPalRel($uID,$pID,$l){
	$c=$this->db->select('*','pals',' WHERE u_id=\''.$pID.'\' AND pal_id=\''.$uID.'\' ',$l);
		if(!empty($c)){return $c;}
		else {return false;}
	}
		
	private function updateOrIns($uID,$pID,$stat){
	$c=$this->db->select('*','pals',' WHERE u_id=\''.$uID.'\' AND pal_id=\''.$pID.'\' ','LIMIT 0,1');
		if(!empty($c)){
			if($this->db->update('pals','rel='.$stat.'','WHERE id=\''.$c[0]['id'].'\'')==true){return true;}
			else {return false;}			}
		else {
			if($this->db->insert('pals','u_id,pal_id,rel','"'.$uID.'","'.$pID.'","'.$stat.'"')==true){return true;}
			else {return false;}
		}
	}
}

?>