<?php
class user
{
	function userdetail($userid,$what="")
	{
		global $db;
		if($what=="")$what="*";
		$udetail=array();$i=0;
		$userDetail = $db->getRow("select $what from ".SITE_USER." where id='".$userid."'");
		if($userDetail && count($userDetail)>0){
			
				$i++;
				$udetail=$userDetail;
				$udetail["childdetail"]=json_decode($userDetail["childdetail"],true);
			
		}
		
		return $udetail;
	}
	function ucountry()
	{
		global $db;
		$CountryDetail = $db->getRows("select * from ".COUNTRY);
		return $CountryDetail;
	}
	function detail($what,$id=""){
		global $db;
		if($id=="")$id=$_SESSION["user"]["uid"];
		$userDetail = $db->getVal("select $what from ".SITE_USER." where id='".$id."' and status=1");
		return $userDetail;
	}
	function allusers()
	{
		global $db;
		//$userlist=array();
		$userDetail = $db->getRows("select * from ".SITE_USER." where status==1");
		return $userDetail;
	}
	function update_profile($array)
	{
		global $db;
		$status="";
		$all_child="";
	if(is_array($_POST["name"]) && count($_POST["name"])>0)
	{
		for($i=0; $i<=count($_POST['name'])-1; $i++)
		{
			if($_POST["name"][$i]!="" && $_POST['age'][$i]!="" && $_POST['gender'][$i]!=""){
				$attr[] = array(	
						'name'		=>		$_POST['name'][$i],
						'age'		=>		$_POST['age'][$i],
						'gender'		=>		$_POST['gender'][$i]		
				);
			}
		}
		$all_child=json_encode($attr);
	}
			$aryData=array('username' => $array['username'],
					   'lastname' => $array['lastname'],
					   'email' => $array['email'],
					   'fathername' => $array['fathername'],
					   'mothername' => $array['mothername'],
					   'age' => $array['age'],
					   'birthplace' => $array['birthplace'],
					   'postaladdress' => $array['postaladdress'],
					   'houseno' => $array['houseno'],
					   'landmark' => $array['landmark'],
					   'postoffice' => $array['postoffice'],
					   'street' => $array['street'],
					   'state' => $array['state'],
					   'city' => $array['city'],
					   'country' => $array['country'],
					   'zipcode' => $array['zipcode'],
					   'mobileno' => $array['mobileno'],
					   'workplace' => $array['workplace'],
					   "childdetail"=>$all_child
					   
					   
					   
		);	
		$updata=$db->updateAry(SITE_USER,$aryData," where id='".$_SESSION['user']['uid']."'");
		
		if(!is_null($updata))
		{
			$status=array("status"=>"success","msg"=>"Profile Update Successfully");
		}else{
			$status=array("status"=>"error","msg"=>"Error: ".$db->getErMsg());	
		}
		return $status;
	}
	function changePass($array)
	{
		global $db;
		$status="";
		$pass=md5($array['cpass']);
		$aryData=array('pass' => $pass);	
		$updata=$db->updateAry(SITE_USER,$aryData," where id='".$_SESSION['user']['uid']."'");
		if(!is_null($updata))
		{
			$status=array("status"=>"success","msg"=>"Password Update Successfully");
		}else{
			$status=array("status"=>"error","msg"=>"Error: ".$db->getErMsg());	
		}
		return $status;
	}
	function feedBack($array)
	{
		global $db;
		
		if(isset($array["name"]) && trim($array["name"])==""){
			$status=array("status"=>"error","msg"=>"Please enter Name");
		}
		//echo "error==>Yes".$array["name"];exit;
		elseif(!isset($array["email"]) || trim($array["email"])==""){
			$status=array("status"=>"error","msg"=>"Please enter Email ID");
		}
		elseif(!isset($array["phone"]) || trim($array["phone"])==""){
			$status=array("status"=>"error","msg"=>"Please enter Mobile Number");
		}
		elseif(!isset($array["comments"]) || trim($array["comments"])==""){
			$status=array("status"=>"error","msg"=>"Please enter Comments");
		}else{
			$aryData=array('name' => $array['name'],'email' => $array['email'],'mobile' => $array['phone'],'comment' => $array['comments']);
			//echo "error==>";print_r($array);	
			$updata=$db->insertAry(FEEDBACK,$aryData);
			$notice=$array["name"]." provide a feedback.";
			$nid=notification($notice,"feedback",$updata);
			if(!is_null($updata))
			{
				$url=URL_ROOT;
				$status=array("status"=>"success","url"=>$url);
			}else{
				$status=array("status"=>"error","msg"=>"Error: ".$db->getErMsg());	
			}
		}
		return $status;
	}
	
	function checkAddress($address,$zip,$id){
		global $db;
		$latlang=latlng($zip);
		$db->updateAry(SITE_USER,array("lat"=>$latlang["lat"],"lang"=>$latlang["lng"],"address"=>$latlang["address"]),"where id=$id");
			//echo $zip.$db->getLastQuery()."<pre>";print_r($latlang);exit;
		
	}
	function updateBackgroundPosition($x,$y){
		global $db;
		$db->updateAry(SITE_USER,array("backX"=>$x,"backY"=>$y)," where id=".$_SESSION["user"]["uid"]);
	}
	function notfriendList($id=""){
		global $db;
		if($id=="")$id=$_SESSION["user"]["uid"];
		$userlist=array();
		$i=0;
		$userDetail = $db->getRows("select * from ".SITE_USER." where status=1 and id!=".$id." order by udate DESC ");
		if(is_array($userDetail) && count($userDetail)>0){
			foreach($userDetail as $uDetail){
				$status = $db->getVal("select id from ".FRIEND." where (uid='".$id."' and fid='".$uDetail["id"]."') or (fid='".$id."' and uid='".$uDetail["id"]."')");
				if($status==""){
					$uid=$i++;
					
					$userlist["user"][$uid]=$uDetail;
					$userlist["user"][$uid]["commonFriend"]=$this->commonFriends($uDetail["id"]);
					$userlist["user"][$uid]["fcount"]=(int)$db->getVal("select count(id) from ".FRIEND." where status=1 and (fid='".$uDetail["id"]."' or uid='".$uDetail["id"]."')");
				}
			}
		}
		$userlist["count"]=count($userlist["user"]);
		$userlist["error"]=$db->getErMsg();
		//echo "<pre>";print_r($userlist);exit;
		return $userlist;
		
	}
	function friendList($id="")
	{
		global $db;
		if($id=="")$id=$_SESSION["user"]["uid"];
		$userlist=array();
		
		$userDetail = $db->getRows("select * from ".FRIEND." where status=1 and (uid='".$id."' or fid='".$id."')");
		$userlist["count"]=count($userDetail);
		$userlist["fRequestCount"]=$db->getVal("select count(id) from ".FRIEND." where status=3 and fid='".$id."'");
		if(is_array($userDetail) && count($userDetail)>0){
			foreach($userDetail as $uDetail){
				$uid=$uDetail["id"];
				$userlist["user"][$uid]=$uDetail;
				$userlist["user"][$uid]["count"]=(int)$db->getVal("select count(id) from ".FRIEND." where status=1 and (fid='".$uid."' or uid='".$uid."')");;
				if($uDetail["fid"]==$id){
					$userlist["user"][$uid]["uDetail"]=$this->userdetail($uDetail["fid"]);
					$userlist["user"][$uid]["fDetail"]=$this->userdetail($uDetail["uid"]);
					$userlist["user"][$uid]["commonFriend"]=$this->commonFriends($uDetail["uid"]);
					$userlist["user"][$uid]["unreadChat"]=$allmessage=$db->getVal("select count(id) from ".GROUPCHAT." where mto='".$uDetail["fid"]."' and mfrom='".$uDetail["uid"]."' and status=1 and ctype=1");
				}else{
					$userlist["user"][$uid]["uDetail"]=$this->userdetail($uDetail["uid"]);
					$userlist["user"][$uid]["fDetail"]=$this->userdetail($uDetail["fid"]);
					$userlist["user"][$uid]["commonFriend"]=$this->commonFriends($uDetail["fid"]);
					$userlist["user"][$uid]["unreadChat"]=$allmessage=$db->getVal("select count(id) from ".GROUPCHAT." where mto='".$uDetail["uid"]."' and mfrom='".$uDetail["fid"]."' and status=1 and ctype=1");
				}
				
			}
		}
		//echo "<pre>";print_r($userlist);exit;
		return $userlist;
	}
	function friendRequestList($id="")
	{
		global $db;
		$userlist=array();
		
		$userDetail = $db->getRows("select * from ".FRIEND." where status=3 and (fid='".$_SESSION["user"]["uid"]."')");
		$userlist["count"]=count($userDetail);
		$userlist["query"]=$db->getErMsg();
		if(is_array($userDetail) && count($userDetail)>0){
			foreach($userDetail as $uDetail){
				$uid=$uDetail["id"];
				$userlist["user"][$uid]=$uDetail;
				$userlist["user"][$uid]["count"]=$db->getVal("select count(id) from ".FRIEND." where status=3 and (fid='".$uDetail["uid"]."' or uid='".$uDetail["uid"]."')");
				//$userlist["user"][$uid]["uDetail"]=$this->userdetail($uDetail["fid"],"id,username,avatar,is_online");
				$userlist["user"][$uid]["fDetail"]=$this->userdetail($uDetail["uid"],"id,username,avatar,is_online,address,relationaship,datingpreference,agegroup");
				$userlist["user"][$uid]["Interests"]=$this->myInterestsCategory($uDetail["uid"]);
				//$userlist["user"][$uid]["commonFriend"]=$this->commonFriends($uDetail["uid"]);
			}
		}
		//echo "<pre>";print_r($userlist);exit;
		return $userlist;
	}
	function commonFriends($id){
		global $db;global $theme;
		$userlist=array();
		$userDetail = $db->getRows("SELECT a.friendID 
FROM
      ( SELECT CASE WHEN uid= ".$_SESSION["user"]["uid"]."
                      THEN fid 
                      ELSE uid 
               END AS friendID 
        FROM mss_vchat_friend
        WHERE uid = ".$_SESSION["user"]["uid"]." OR fid = ".$_SESSION["user"]["uid"]."
      ) a
  JOIN
      ( SELECT CASE WHEN uid = $id
                      THEN fid 
                      ELSE uid 
               END AS friendID 
        FROM mss_vchat_friend 
        WHERE uid = $id OR fid = $id
      ) b
    ON b.friendID = a.friendID ");
		$userlist["count"]=count($userDetail);
		$userlist["error"]=$db->getErMsg()."<br />".$db->getLastQuery();
		if(is_array($userDetail) && count($userDetail)>0){
			foreach($userDetail as $uDetail){
				$uid=$uDetail["friendID"];
				$newDetail=array();
				$newDetail=$this->userdetail($uDetail["friendID"]);
				//$userlist["friends"][$id]["detail"]=$userDetail;
				$userlist["friends"][$uid]["avatar"]=URL_ROOT."templates/".$theme."/uploads/avatar/50/50/".$newDetail["avatar"];
				$userlist["friends"][$uid]["id"]=$newDetail["id"];
			}
		}
		
		return $userlist;
	}
	function isFriend($fid){
		global $db;
		$status = $db->getVal("select status from ".FRIEND." where (uid='".$_SESSION["user"]["uid"]."' and fid='".$fid."') or (fid='".$_SESSION["user"]["uid"]."' and uid='".$fid."')");
		return $status;
	}
	function addFriend($fid){
		global $db;
		$isFriend=$this->isFriend($fid);
		if($isFriend==""){
			$status=$db->insertAry(FRIEND,array("uid"=>$_SESSION["user"]["uid"],"fid"=>$fid,"status"=>3));
			$notice="You received a new Friend Request";
			$body="Dear User<br />
				<br />
				You are received a new friend request on ".URL_ROOT."<br />
				<br />
				Please Login to see a request<br />
				<br />
				";
				mymail($LinksDetails["mail_sender_email"],$uid["email"],"Notification ! You received a new friend Request",$body,"MESSAGE");
			notification($notice,'user',$fid,$_SESSION["user"]["uid"],'2');
			return array("status"=>"ok","msg"=>"Friend request has been successfully sent");
		}else{
			return array("status"=>"error","msg"=>"You already sent friend request to this user");
		}
	}
	function ResponceFriend($fid,$status){
		global $db;
		$isFriend=$this->isFriend($fid);
		if($isFriend=="3"){
			$status=$db->updateAry(FRIEND,array("status"=>$status),"where fid='".$_SESSION["user"]["uid"]."' and uid='".$fid."'");
			$notice="Now You are Friends";
			if($status=="2")$notice="Your Request Has Been Rejected";
			notification($notice,'user',$fid,$_SESSION["user"]["uid"],'2');
			return array("status"=>"ok","msg"=>$notice);
		}elseif($isFriend=="1"){
			return array("status"=>"error","msg"=>"You already accept friend");
		}elseif($isFriend=="2"){
			return array("status"=>"error","msg"=>"You already reject friend");
		}
	}
	function fetchNotification(){
		global $db; global $theme;
		$userlist=array();
		$userDetail = $db->getRows("select * from ".NOTIFICATION." where link_id='".$_SESSION["user"]["uid"]."' and status=2 and type='user'");
		$userlist["count"]=count($userDetail);
		if(is_array($userDetail) && count($userDetail)>0){$i=0;
			foreach($userDetail as $uDetail){$i++;
				$uid=$uDetail["id"];
				$fdetail=$this->userdetail($uDetail["uid"]);
				$userlist["user"][$i]["id"]=$uDetail["id"];
				$userlist["user"][$i]["notice"]=$uDetail["notice"];
				$userlist["user"][$i]["username"]=$fdetail["username"];
				//$userlist["user"][$uid]["fdetail"]=$fdetail;
				$userlist["user"][$i]["avatar"]=URL_ROOT."templates/".$theme."/uploads/avatar/50/50/".$fdetail["avatar"];
			}
		}
		$userlist["error"]=$db->getErMsg();
		$userlist["lastQuery"]=$db->getLastQuery();
		return $userlist;
	}
	function updateNotification($id){
		global $db;
		$userDetail = $db->updateAry(NOTIFICATION,array("status"=>0)," where id='".$id."' and status=2 and type='user'");
		$msg=$db->getVal("select notice from ".NOTIFICATION." where id=$id");
		return array("msg"=>$msg);
	}
	function setStaus($status,$text){
		global $db;
		$userDetail = $db->updateAry(SITE_USER,array("is_online"=>$status,"online_status"=>$text)," where id='".$_SESSION["user"]["uid"]."'");
		$color="#f00";
		if($status==0)$color="#f00";
		elseif($status==2)$color="#FF0";
		elseif($status==1)$color="0C0";
		return array("msg"=>$userDetail,"color"=>$color);
	}
	function fetchStatus(){
		global $db;
		$userDetail = $db->getRows("select id,color,title from ".STATUS."");
		$list=array();
		if(is_array($userDetail) && count($userDetail)>0){
			foreach($userDetail as $udetail){
				$list[]=$udetail;
			}
		}
		return $list;
	}
	function updateProfile($what,$value){
		global $db;
		$db->updateAry(SITE_USER,array($what=>$value),"where id='".$_SESSION["user"]["uid"]."'");
	}
	function CategoryList(){
		global $db;
		$status = $db->getRows("select * from ".CATEGORY." where status='1'");
		return $status;
	}
	function myInterestsCategory($id){
		global $db;
		$statusR=array();$i=0;
		$status = $db->getRows("select c.* from ".INTERESTS." as i,".CATEGORY." as c where i.status='1' and i.uid='".$id."' and c.id=i.category group by i.category");
		if(is_array($status) && count($status)>0){
			foreach($status as $s){$i++;
				$statusR[$i]=$s;
				$detail=$db->getRows("select * from ".INTERESTS." where category='".$s["id"]."' and uid='".$id."' and status=1");
				$statusR[$i]["intrests"]=$detail;
			}
		}
		return $statusR;
	}
	function saveInterests($POST){
		global $db;
		$saveAry=array("title"=>POST($POST["title"]),
						"category"=>POST($POST["category"]),
						"file"=>POST($POST["file"]),
						"uid"=>$_SESSION["user"]["uid"]
		);
		$db->insertAry(INTERESTS,$saveAry);
		$status["error"]=$db->getErMsg();
		$status["lastQuery"]=$db->getLastQuery();
		$status["status"]="success";
		$status["msg"]="Interests updated";
		return $status;
	}
	function removeIntrests($id){
		global $db;
		$db->delete(INTERESTS,"where id=$id");
	}
	function track_order($oid,$email)
	{
		global $db;
		$getOrder=$db->getRows("select * from ".ORDER." where oid=$oid and  email='$email' ");
		if($getOrder!="")
		return array("status"=>"success","msg"=>$getOrder);
		else
		return array("status"=>"error","msg"=>"Invalid order ID");
	}
}