<?php
class feedback{
	private static $instance;
	public $connection;
 
	function feedback(){
        global $DBHOST, $DBUSER, $DBNAME;
	    $this->connection= new db();
        $this->connection->connectDB($DBHOST['DEVHOST'],$DBUSER['DEVUSER'],$DBUSER['DEVPASS'],$DBNAME['DEVDBNAME']);
	}
 
	public static function getInstance() {
		if (!isset(self::$instance)) {
			self::$instance = new feedback();
		}
		return self::$instance;
	}
	function FnAddFeedback($VarName,$VarEmail,$VarTicketCode,$VarMobile,$VarFeedbackType,$VarComment){
		global $TABLE;
		$sql = "select tripid from ".$TABLE['BOOKING']." where ticketno='".$this->connection->dbEscapeQuetes($VarTicketCode)."'";
		$this->connection->execute($sql);
		$VarTripid = "";
		if($this->connection->getNumRows()>0){
			$ArrBooking = $this->connection->fetchArray('MYSQL_ASSOC');
			$VarTripid = $ArrBooking['tripid'];
		}
		$sql = "insert into ".$TABLE['CUSTOMERFEEDBACK']." (name,emailid,ticketno,mobileno,feedbacktype,comment,feedbackdate,tripid) values('".$this->connection->dbEscapeQuetes($VarName)."','".$this->connection->dbEscapeQuetes($VarEmail)."','".$this->connection->dbEscapeQuetes($VarTicketCode)."','".$this->connection->dbEscapeQuetes($VarMobile)."','".$this->connection->dbEscapeQuetes($VarFeedbackType)."','".$this->connection->dbEscapeQuetes($VarComment)."',now(),'".$this->connection->dbEscapeQuetes($VarTripid)."')";
		$this->connection->insert($sql);
		return "success";
	}
	function FnGetFeedbackList($VarType,$VarStart='',$VarEnd='',$VarFromDate='',$VarToDate='',$VarFeedbackType='',$VarResponse='',$VarTripId='',$VarReportType='datebased'){
        global $TABLE;
		$VarWhere 	= '';		
		$ArrWhere	= array();
		$VarLimit='';
  		if($VarStart >=0 && $VarEnd >0){
           		$VarLimit.= " limit ".$this->connection->dbEscapeQuetes($VarStart).", ".$this->connection->dbEscapeQuetes($VarEnd);
       	}
		if($VarFromDate!='' && $VarToDate!='' && $VarReportType=='datebased'){
			 $ArrWhere[] ="date(feedbackdate) between '".$VarFromDate."' and '".$VarToDate."'";
		} else if($VarFromDate!='' && $VarToDate=='' && $VarReportType=='datebased'){
			 $ArrWhere[] ="date(feedbackdate) = '".$VarFromDate."'";
		} else if ($VarFromDate!='' && $VarToDate!='' && $VarReportType == 'tripbased'){
			 $ArrWhere[] ="doj between '".$VarFromDate."' and '".$VarToDate."'";
		} elseif($VarFromDate!='' && $VarToDate=='' && $VarReportType=='tripbased'){
			$ArrWhere[] ="doj = '".$VarFromDate."'";
		}
		if($VarFeedbackType!='all'){
			$ArrWhere[] = "feedbacktype ='".$VarFeedbackType."'";
		}
		if($VarResponse!='all'){
			$ArrWhere[] = "statusresponse ='".$VarResponse."'";
		}
		if($VarTripId!=''){
			$ArrWhere[] = "tripid ='".$VarTripId."'";
		}
		
		if(count($ArrWhere)>0){
			$VarWhere	.= " where ".implode(" and ",$ArrWhere);
		}
		$Sql = "select c.id,c.statusresponse,c.name,c.emailid,c.ticketno,c.mobileno,c.feedbacktype,c.comment,c.feedbackdate,t.drname from ".$TABLE['CUSTOMERFEEDBACK']." c left join ".$TABLE['TRIP']." t on c.tripid = t.id ".$VarWhere." order by statusresponse desc,feedbackdate desc ".$VarLimit ;
		$this->connection->execute($Sql);
		if($VarType==1){
                	return $this->connection->getNumRows();
      		}else{
			$ArrFeedback = array();
        		while($ArrFeedbackInfo = $this->connection->fetchArray('MYSQL_ASSOC')){
               			$ArrFeedback[] = $ArrFeedbackInfo;
     			}
			unset($ArrFeedbackInfo);
		        return $ArrFeedback;
		}
	}
	function FnGetUpdateFeedback($VarId,$VarResponse){
		global $TABLE;
		if($VarResponse=='No'){
			$sql = "update ".$TABLE['CUSTOMERFEEDBACK']." set statusresponse='Yes' where id ='".$VarId."'";
			$this->connection->execute($sql);
		}
	}
	function FnCheckFeedbackExists($VarTicketNo){
		global $TABLE;
		$Sql = "select * from ".$TABLE['CUSTOMERFEEDBACK']." where ticketno= '".$VarTicketNo."'";
		$this->connection->execute($Sql);
		return $this->connection->getNumRows();
	}

}
?>
