<?php
include_once("class.MySQL.php");
//$db	=new MySQL();

class messaging{
	
	
	function __construct(){
		$this->db =new MySQL(); 
	}
	
	function listEmployee($search){
		$where['status'] = 1;
		$condition = '1';
		if($search!=''){
		$search = trim($search);
			$condition .= " AND A.e_fname LIKE '%$search%' ";
		}
		
		$sql = "SELECT A.e_id,e_fname,e_lname,username,e_gender,branchid,departid,photo FROM employee A LEFT JOIN file B ON A.e_id = B.e_id WHERE $condition AND A.status='1'";

		$employees = $this->db->fetchQuery($sql);
		return $employees;
	
	}
	
	function createConversation($data){

		$fromuser = $data['from'];
		$touser   = $data['to'];
		$ip		  = $_SERVER['REMOTE_ADDR'];
		$time     = time();
		$message_type = $data['type'];
		
		
		$previous_msg = $this->db->fetchQuery("SELECT c_id 
											FROM conversation 
											WHERE 
											(user_one='$fromuser' AND user_two='$touser' AND message_type='$message_type') 
											OR
											(user_one='$touser' AND user_two='$fromuser' AND message_type='$message_type')
											");
				
		if(count($previous_msg) < 1){
			$this->db->fetchQuery("INSERT INTO conversation (user_one,user_two,ip,time,message_type)VALUES('$fromuser','$touser','$ip','$time','$message_type')");
			$data['cid'] = mysql_insert_id();
		}else{
			
			$data['cid'] = $previous_msg[0]['c_id'];
		}
		
		$this->createMessage($data);


	}
	
	function createMessage($data){

		//$data = $this->db->SecureData($data);
		$cid 	= $data['cid'];
		$reply 	= $data['message'];
		$userid = $data['from']; 
		$ip		  = $_SERVER['REMOTE_ADDR'];
		$time     = time();
		$referance_id = $data['referance_id'];

		if($data['dnd'] == 1){

			$this->db->fetchQuery("UPDATE conversation SET delete_from='1', delete_to='0',is_read_from='0' WHERE c_id='".$cid."'");
		}else{
			$this->db->fetchQuery("UPDATE conversation SET delete_from='0',  delete_to='0',is_read_from='0' WHERE c_id='".$cid."'");
		}
		
		$this->db->fetchQuery("INSERT INTO conversation_reply (reply,referance_id,user_id_fk,ip,time,c_id_fk)VALUES('$reply','$referance_id','$userid','$ip','$time','$cid')");
		$messageid = mysql_insert_id();
		return $this->getSingleMessages($messageid);
		
		
	}
	
	function getMessages($user,$cid,$limit=10,$last=NULL){
	
			if($last==NULL || $last==''){
		
			$countToday = $this->db->fetchQuery("SELECT count(*) FROM conversation_reply WHERE FROM_UNIXTIME(`time`, '%Y-%m-%d') = CURDATE() and c_id_fk='$cid'");
			
			if(count($countToday) > $limit){
				$limit = count($countToday);
			}
					$conversation = $this->db->fetchQuery("SELECT *,A.time as msgtime
												FROM conversation_reply A
												LEFT JOIN employee B ON A.user_id_fk = B.e_id
												LEFT JOIN file D ON B.e_id = D.e_id
												LEFT JOIN conversation C ON A.c_id_fk = C.c_id
												WHERE CASE 
												 WHEN (A.user_id_fk = '$user')
												 THEN A.is_delete_from = '0'
												 WHEN (A.user_id_fk != '$user')
												 THEN A.is_delete_to = '0'
												 END
												AND c_id_fk='$cid' ORDER BY cr_id DESC  LIMIT $limit");
												
										
			}else{
				$conversation = $this->db->fetchQuery("SELECT *,A.time as msgtime
												FROM conversation_reply A
												LEFT JOIN employee B ON A.user_id_fk = B.e_id
												LEFT JOIN file D ON B.e_id = D.e_id
												LEFT JOIN conversation C ON A.c_id_fk = C.c_id
												WHERE CASE 
												 WHEN (A.user_id_fk = '$user')
												 THEN A.is_delete_from = '0'
												 WHEN (A.user_id_fk != '$user')
												 THEN A.is_delete_to = '0'
												 END
												AND c_id_fk='$cid' and cr_id < '$last' ORDER BY cr_id DESC LIMIT $limit");
			}
			
			return $conversation;
				
	}
	function getMessagesNew($user){

			
				$conversation = $this->db->fetchQuery("SELECT *,B.time as msgtime FROM conversation A
													LEFT JOIN conversation_reply B ON A.c_id = B.c_id_fk
													LEFT JOIN employee C ON B.user_id_fk = C.e_id
													LEFT JOIN file D ON C.e_id = D.e_id
													WHERE (A.user_one = '$user' OR A.user_two='$user') AND B.user_id_fk !='$user' AND is_read ='0' ");
										
												
								/*				FROM conversation_reply A
												LEFT JOIN employee B ON A.user_id_fk = B.e_id
												WHERE CASE 
												 WHEN (A.user_id_fk = '$user')
												 THEN A.is_delete_from = '0'
												 WHEN (A.user_id_fk != '$user')
												 THEN A.is_delete_to = '0'
												 END
												AND c_id_fk='$cid' and cr_id > '$last' ORDER BY cr_id DESC LIMIT $limit");*/
												
		return $conversation;
				
	}
	
	function updateStatus($crid){
		$this->db->fetchQuery("UPDATE conversation_reply SET is_read = '1' WHERE cr_id='$crid'");
	}
	
	
	function getSingleMessages($mid){
	

				$conversation = $this->db->fetchQuery("SELECT *,A.time as msgtime 
												FROM conversation_reply A
												LEFT JOIN employee B ON A.user_id_fk = B.e_id
												WHERE cr_id='$mid'");
			
	/*		$a=0;
			foreach($conversation as $conversation1){
				$conversation[$a]['timeleft'] = $this->RelativeTime($conversation1['time']);
				$a++;
			}
		*/

		return $conversation;
				
	}
	
	function getConversation($user,$touser,$type){
			$conversation = $this->db->fetchQuery("SELECT * FROM conversation WHERE (user_one='$user' AND user_two='$touser') OR (user_one='$touser' AND user_two='$user')");
			return $conversation[0]['c_id'];
	}
	function userOnline($uid){
		$where['user_id'] = $uid;
		
		
		$onlineusers = $this->db->CountRows('user_online',$where);
		$data['update_time'] = date("Y-m-d H:i:s");

		
		if($onlineusers == 1 ){
			$this->db->Update("user_online",$data,$where);
		}else{
			$data['user_id']	 = $uid;
			$this->db->Insert($data,"user_online");
		}
		$time = date("Y-m-d H:i:s", time() - 30);
		
		$this->db->fetchQuery("DELETE FROM user_online WHERE update_time < '$time'");
		$onlineUsers = $this->db->fetchAll("user_online");
		return $onlineUsers;
		
	}
	
	

}

?>