HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/chat.metaguise.com/classes/Master-v1.php
<?php
require_once('../config.php');
header("Access-Control-Allow-Origin: *");
set_time_limit(2000); 
Class Master extends DBConnection {
	private $settings;
	public function __construct(){
		global $_settings;
		$this->settings = $_settings;
		parent::__construct();
	}
	public function __destruct(){
		parent::__destruct();
	}
	
	public function save_response(){
		extract($_POST);
		if(!empty($id)){
			$del = $this->conn->query("DELETE FROM `questions` where id= '$id' ");
			if(!$del){
				return 2;
				exit;
			}
		}
		$data = "";
		$ins_resp = $this->conn->query("INSERT INTO `responses` set response_message = '{$response_message}' ");
		if(!$ins_resp){
			return 2;
			exit;
		}
		$resp_id = $this->conn->insert_id;

		foreach($question as $k => $v){
			$data = " response_id = {$resp_id} ";
			$data .= ", `question` = '$question[$k]' ";
			$ins[] = $this->conn->query("INSERT INTO `questions` set $data ");
		}
		if(isset($ins) && count($ins) == count($question)){
			$this->settings->set_flashdata("success"," Data successfully saved");
			return 1;
		}else{
			return 2;
			exit;
		}

	}
	public function delete_response(){
		extract($_POST);
		 $del = $this->conn->query("DELETE FROM `questions` where id = $id");
		 if($del){
			$this->settings->set_flashdata("success"," Data successfully deleted");
		 	return 1;
		 }else{
		 	$this->conn->error;
		 }
	}

	public function get_response(){


		extract($_POST);
		$message = str_replace(array("?"), '', $message);
		$chatId = str_replace(array("?"), '', $chatId);
		$option_value = str_replace(array("?"), '', $option_value);
		$preQuestionId = str_replace(array("?"), '', $preQuestionId);

		
		$cdate = date('Y-m-d',time());
		$not_question = array("what", "what is","who","who is", "where");
		if(in_array($message,$not_question)){
			$resp['status'] = "success";
			$resp['message'] = $this->settings->info('no_result');
			return json_encode($resp);
			exit;
		}
		$response =  array();


		$selected_language = '';
		$sqlC = "SELECT * from `chat_history` where chatId = '".$chatId."' and questionId in ('0990')";
		$qryC = $this->conn->query($sqlC);
		$qArray = array();
		$questionIdC = '';
		$qchatCount = $qryC->num_rows;
		if($qryC->num_rows > 0)
		{
			//$resultC = $qryC->fetch_array();	
			while($rowC = $qryC->fetch_assoc())
			{	
				$selected_language = $rowC['answer'];				 			 
			}
		}



		$Language = isset($_POST['Language']) ? $_POST['Language'] : 'en';

		$factoryType = isset($_POST['factoryType']) ? $_POST['factoryType'] : '';

		
		 


		if($preQuestionId == '5000' && $option_value == 'No')
		{
			$questionId = '4000';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}
			$questionId = '5000';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}

			$questionId = '6601';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}

			$questionId = '6602';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}

			$questionId = '6603';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}

			$questionId = '6604';
			$sqlChatInsert = "DELETE FROM `chat_history` where chatId = '".$chatId."' and questionId = '".$questionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "updated";
			}
		}

		/*********************** */
		if($preQuestionId == '1000' && $option_value == 'Yes') //क्या आप अपनी पहचान गुप्त रखना चाहते हैं?
		{

			$message = '';
			$questionId = '2000';
			$salesforce_key = 'Fullname';
			$questionTitle = '';
			$sqlChatInsert = "insert into `chat_history` (chatId, questionId, question, answer, create_date, updated_date, salesforce_key) values ('".$chatId."', '".$questionId."',  '".$questionTitle."', '".$message."', '".$cdate."', '".$cdate."', '".$salesforce_key."')";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "inserted";
			}

			$questionId = '3000';
			$salesforce_key = 'Phone';
			$sqlChatInsert = "insert into `chat_history` (chatId, questionId, question, answer, create_date, updated_date, salesforce_key) values ('".$chatId."', '".$questionId."',  '".$questionTitle."', '".$message."', '".$cdate."', '".$cdate."', '".$salesforce_key."')";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$insertResponse = "inserted";
			}
		}
			 
		/*********************** */


 
		 
		$sqlC = "SELECT * from `chat_history` where chatId = '".$chatId."'";
		$qryC = $this->conn->query($sqlC);
		$qArray = array();
		$questionIdC = '';
		$qchatCount = $qryC->num_rows;
		if($qryC->num_rows > 0)
		{
			//$resultC = $qryC->fetch_array();	
			while($rowC = $qryC->fetch_assoc()){	
			$questionIdC = $rowC['questionId'];
			$qArray[] = $questionIdC; 
			}
		}

		if(strtoupper($message) != strtoupper("Yes") && !$questionIdC)
		{
			
			// $resp['status'] = "success";
			// $resp['message'] = $this->settings->info('no_result');
			// return json_encode($resp);
			// exit;
		}
		 
		$Language = isset($_POST['Language']) ? $_POST['Language'] : 'en';
 

		$tablename = 'tbl_labs_question_answer_english';
		if(strtoupper($Language) == strtoupper('hi')) // Hindi
		{
			$tablename = 'tbl_labs_question_answer_hindi';
		}
		if(strtoupper($Language) == strtoupper('en')) // English
		{
			$tablename = 'tbl_labs_question_answer_english';
		}
		else if(strtoupper($Language) == strtoupper('kn')) //Kannada
		{
			$tablename = 'tbl_labs_question_answer_kannada';
		}
		else if(strtoupper($Language) == strtoupper('te')) //Telugu
		{
			//$tablename = 'tbl_labs_question_answer_telugu';
		}
		else if(strtoupper($Language) == strtoupper('ta')) //Tamil
		{
			$tablename = 'tbl_labs_question_answer_tamil';
		}
		else if(strtoupper($Language) == strtoupper('vi')) //Vietnamese
		{
			$tablename = 'tbl_labs_question_answer_vietnamese';
		}
		else if(strtoupper($Language) == strtoupper('km')) //Khmer
		{
			$tablename = 'tbl_labs_question_answer_khmer';
		}
		else if(strtoupper($Language) == strtoupper('id')) //Bahasa Indonesia
		{
			$tablename = 'tbl_labs_question_answer_indonesia';
		}

		if($qchatCount > 0)
		{
			$sql = "SELECT * from $tablename where status= '1' and questionId NOT IN (".implode(',',$qArray).") order by Sequence asc";

			// if($factoryType == 'Other')
			// {
			// 	$sql = "SELECT * from $tablename where factoryType= '".$factoryType."' and questionId NOT IN (".implode(',',$qArray).") order by Sequence asc";
			// }
		}
		else
		{
			//$sql = "SELECT * from `tbl_labs_question_answer_hindi` where status= '1' and questionId in ('0990') limit 0,1";

			if($tablename)
			{
				$sql = "SELECT * from $tablename where status= '1' and questionId in ('1000') limit 0,1";
			}
			else
			{
				$sql = "SELECT * from `tbl_labs_question_answer_english` where status= '1' and questionId in ('1000') limit 0,1";
			}			
		}

		$sql_question = $sql;
 
		
		
		$qry = $this->conn->query($sql);
		 
		$QuestionId = '';
		$question="";
		if($qry->num_rows > 0)
		{
			$result = $qry->fetch_array();		
			$questionId = $result['questionId'];		
			$question = $result['question'];

			 
			$question2 = '';

			$convert_lang = $Language;			        									
			$response['convert_language_lang'] = $convert_lang;
			$response['convert_language'] = $question2;
			$salesforce_key = $result['salesforce_key'];
			$questionTitle  = $question;
			$questiontype = $result['questiontype'];
						
			//return json_encode($resp);
			$cdate = date('Y-m-d',time());
			$response['status'] = "success";
			
			$response['questiontype'] = $questiontype;
			$htmlQuestion1 = "";

			$question2 = $question;
			if($question){
				if($convert_lang == 'te'){
				$question2 = $this->translate($question, "en", "$convert_lang");
				}
			}

			$response['question2'] = $question2;

			$htmlQuestion1 = $question2;
			if(strtoupper($questiontype) == strtoupper('radio'))
			{
				$titleOption1 = $result['titleOption1'];
				$valueOption1 = $result['valueOption1'];
				$titleOption2 = $result['titleOption2'];
				$valueOption2 = $result['valueOption2'];
				$titleOption3 = $result['titleOption3'];
				$valueOption3 = $result['valueOption3'];
				$titleOption4 = $result['titleOption4'];
				$valueOption4 = $result['valueOption4'];
				$titleOption5 = $result['titleOption5'];
				$valueOption5 = $result['valueOption5'];
				$titleOption6 = $result['titleOption6'];
				$valueOption6 = $result['valueOption6'];
				$titleOption7 = $result['titleOption7'];
				$valueOption7 = $result['valueOption7'];
				$titleOption8 = $result['titleOption8'];
				$valueOption8 = $result['valueOption8'];
				 
				if($convert_lang == 'te'){
					 
					if($titleOption1){
					$titleOption1 = $this->translate($titleOption1, "en", "$convert_lang");
					}
					if($titleOption2){
					$titleOption2 = $this->translate($titleOption2, "en", "$convert_lang");
					}
					if($titleOption3){
					$titleOption3 = $this->translate($titleOption3, "en", "$convert_lang");
					}
					if($titleOption4){
					$titleOption4 = $this->translate($titleOption4, "en", "$convert_lang");
					}
					if($titleOption5){
					$titleOption5 = $this->translate($titleOption5, "en", "$convert_lang");
					}
					if($titleOption6){
					$titleOption6 = $this->translate($titleOption6, "en", "$convert_lang");
					}
					if($titleOption7){
					$titleOption7 = $this->translate($titleOption7, "en", "$convert_lang");
					}
					if($titleOption8){
					$titleOption8 = $this->translate($titleOption8, "en", "$convert_lang");
					}
				}

				
				$htmlQuestion1 .=  "<br/>";
				if($titleOption1)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption1."', '".$chatId."', '".$titleOption1."', '".$questionId."')\"  > ".$titleOption1." <a/>";
				}
				if($titleOption2)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption2."', '".$chatId."', '".$titleOption2."', '".$questionId."')\"  > ".$titleOption2." <a/>";
				}
				if($titleOption3)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption3."', '".$chatId."', '".$titleOption3."', '".$questionId."')\"  > ".$titleOption3." <a/>";
				}

				if($titleOption4)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption4."', '".$chatId."', '".$titleOption4."', '".$questionId."')\"  > ".$titleOption4." <a/>";
				}

				if($titleOption5)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption5."', '".$chatId."', '".$titleOption5."', '".$questionId."')\"  > ".$titleOption5." <a/>";
				}

				if($titleOption6)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption6."', '".$chatId."', '".$titleOption6."', '".$questionId."')\"  > ".$titleOption6." <a/>";
				}

				if($titleOption7)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption7."', '".$chatId."', '".$titleOption7."', '".$questionId."')\"  > ".$titleOption7." <a/>";
				}

				if($titleOption8)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption8."', '".$chatId."', '".$titleOption8."', '".$questionId."')\"  > ".$titleOption8." <a/>";
				}				 							
			}

			if(strtoupper($questiontype) == strtoupper('datalist'))
			{				
				

				if($questionTitle){
					if($convert_lang == 'te'){
						$questionTitle = $this->translate($questionTitle, "en", "$convert_lang");
					}
				}

				$htmlQuestion1 = $questionTitle;

				$htmlQuestion1 .= "<br/>";
				$htmlQuestion1 .= $this->htmlFactoryList($chatId, $questionId, $convert_lang);
			}

			if(strtoupper($questiontype) == strtoupper('select'))
			{				
				if($questionTitle){
					if($convert_lang == 'te'){
						$questionTitle = $this->translate($questionTitle, "en", "$convert_lang");
					}
				}
 
				$htmlQuestion1 = $questionTitle;
				$htmlQuestion1 .= "<br/>";
				$htmlQuestion1 .= $this->htmlProductList($chatId, $questionId, $convert_lang);
				
			}

			if(strtoupper($questiontype) == strtoupper('file'))
			{

				if($questionTitle){
					if($convert_lang == 'te'){
						$questionTitle = $this->translate($questionTitle, "en", "$convert_lang");
					}
				}

				$htmlQuestion1 = $questionTitle;

				

				$htmlQuestion1 .= "<br/>";
				$htmlQuestion1 .= $this->htmlFileUpload($chatId, $questionId, $convert_lang);
			}
			 
			
			$response['sql_question'] = $sql_question;
			$response['selected_language'] = $selected_language;
			$response['message'] = $htmlQuestion1;
			$response['questionId'] = $questionId;	
			$insertResponse = "";

			$chatId = isset($_POST['chatId']) ? $_POST['chatId'] : '';
			$preQuestionId = isset($_POST['preQuestionId']) ? $_POST['preQuestionId'] : '';
			$factoryId = isset($_POST['factoryId']) ? $_POST['factoryId'] : '';

			$Countryid = isset($_POST['Countryid']) ? $_POST['Countryid'] : '';

			
			$s3url = isset($_POST['s3url']) ? $_POST['s3url'] : NULL;

			
			 			
			$sqlC = "SELECT * from `chat_history` where chatId = '".$chatId."' and questionId = '".$preQuestionId."'";
			$qryC = $this->conn->query($sqlC);
			$qArray = array();
			$questionIdC = '';
			$qchatCount = $qryC->num_rows;

			if(strtoupper($questiontype) == strtoupper('radio'))
			{
				$question = $option_value;
			}
			$otherFactoryName = '';
			$sqlChatInsertu = '';
			if($qchatCount > 0)
			{
				if($preQuestionId == '4000')
				{

					if(!$option_value)
					{
						$otherFactoryName = $message;
					}

				 
					$message = $factoryId;
					 
					
				}

				if($preQuestionId == '3500')
				{

					if(!$option_value)
					{
						$otherFactoryName = $message;
					}

				 
					$message = $Countryid;
					 
					
				}

				if($preQuestionId == '1000')
				{					 			 
					$message = $option_value;					 					
				}

				if($preQuestionId) {
					$sqlChatInsertu = "update `chat_history` set answer = '".$message."', other_factory_name = '".$otherFactoryName."', updated_date = '".$cdate."' where chatId = '".$chatId."' and questionId = '".$preQuestionId."'";
					$qryChatInsert = $this->conn->query($sqlChatInsertu);
					if($qryChatInsert)
					{
						$insertResponse = "updated";
						$sqlChatInsert = "insert into `chat_history` (chatId, questionId, question, answer, create_date, updated_date, salesforce_key) values ('".$chatId."', '".$questionId."',  '".$questionTitle."', '".$message."', '".$cdate."', '".$cdate."', '".$salesforce_key."')";
				        $qryChatInsert = $this->conn->query($sqlChatInsert);
				        if($qryChatInsert)
				        {
					       $insertResponse = "inserted";
				        }
					}
				}
			}
			else
			{
				$sqlChatInsert = "insert into `chat_history` (chatId, questionId, question, answer, create_date, updated_date, salesforce_key) values ('".$chatId."', '".$questionId."',  '".$questionTitle."', '".$message."', '".$cdate."', '".$cdate."', '".$salesforce_key."')";
				$qryChatInsert = $this->conn->query($sqlChatInsert);
				if($qryChatInsert)
				{
					$insertResponse = "inserted";
				}

				if($questionId == '5000')
				{
					$questionId = '5000';
					$sqlChatInsert = "update `chat_history` set answer = '".$option_value."',  updated_date = '".$cdate."' where chatId = '".$chatId."' and questionId = '".$questionId."'";
					$qryChatInsert = $this->conn->query($sqlChatInsert);
					if($qryChatInsert)
					{
						$insertResponse = "updated";
					}
				}
			}
			
			if($preQuestionId == '8000')
			{
				$sqlChatInsert = "update `chat_history` set  updated_date = '".$cdate."', S3url = '".$s3url."' where chatId = '".$chatId."' and questionId = '".$preQuestionId."'";
				$qryChatInsert = $this->conn->query($sqlChatInsert);
			}


			
			$response['histroy'] = $insertResponse;			
			$response['sql'] = $sqlChatInsert;
			$response['sqlChatInsertu'] = $sqlChatInsertu;
			
			$response['end'] = "no";			
			return json_encode($response);
		}

		$CaseId = '';
		$salesforcePost = array();
		if($preQuestionId == '9000' && $message) {
			$sqlChatInsert = "update `chat_history` set answer = '".$message."',  updated_date = '".$cdate."' where chatId = '".$chatId."' and questionId = '".$preQuestionId."'";
			$qryChatInsert = $this->conn->query($sqlChatInsert);
			if($qryChatInsert)
			{
				$response['sql'] = $sqlChatInsert; 
			    $sql = "SELECT * from `chat_history` where chatId = '".$chatId."'";		        		        
		        $qry = $this->conn->query($sql);
		        if($qry->num_rows > 0)
		        {		
					$files = array();			   
					while ($row = $qry->fetch_row()) 
					{
						$questionId = $row[1];		
			            $salesforce_key = $row[5];
						$salesforce_answer = $row[6];
						$other_factory_name = $row[7];
						$salesforce_s3url = $row[8];

						if($other_factory_name)
						{
							$salesforcePost['Otherfactoryname'] = $other_factory_name;
						}

						

						if($salesforce_key)
						{
							$salesforcePost[$salesforce_key] = $salesforce_answer;

							if($salesforce_key == 'Files')
							{
								$allowed_image = array('gif', 'png', 'jpg', 'jpeg');
								$allowed_video = array('mp4', 'wav', 'avi', 'mov', 'mpeg', '3gp');
								$allowed_audio = array('m4a','mp3', 'ogg', 'flac');
								$allowed_doc = array('pdf');
								$ext = pathinfo($salesforce_s3url, PATHINFO_EXTENSION);
								$fileType = '';
								if (in_array($ext, $allowed_image)) {
									$fileType = 'image';
								}
								else if (in_array($ext, $allowed_video)) {
									$fileType = 'video';
								}
								else if (in_array($ext, $allowed_audio)) {
									$fileType = 'audio';
								}
								else if (in_array($ext, $allowed_doc)) {
									$fileType = 'pdf';
								}

								$files[] = array("FileType"=>$fileType,"ConcernDetails"=>$salesforce_answer, "S3_URL"=>$salesforce_s3url,"CaseId"=>"");

								$salesforcePost[$salesforce_key] = $files;

							}

							if($salesforce_key == 'listofProducts')
							{
								$selectedproduct = array();
								$selectedproduct[] = $salesforce_answer;
								$salesforcePost['listofProducts'] = $selectedproduct;
								$salesforcePost['listofProducts'] = $selectedproduct;
							}
							$salesforcePost['leadSource'] = 'Website';
							
						}
						//echo $questionId.",";
					} 

					$postdata = $salesforcePost; 

					if($postdata)
					{
						unset($postdata['Fullname']);
						unset($postdata['Phone']);
					}
					$response['salesforce_post_data'] = $postdata;
				   
					$postdata = json_encode($postdata);

					$this->my_log($postdata);

					$curl = curl_init();
					curl_setopt_array($curl, array(
					CURLOPT_URL =>SALESFORCE_LEAD_CREATION,				 
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$postdata,
					CURLOPT_HTTPHEADER => array(
					  'Content-Type: application/json',
					  'Cookie: CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1; BrowserId=z9mjH4z7Ee2Ob-W8c40DpA'
					),
				  ));
				  $apiresponse = curl_exec($curl);

				  $resdata = json_decode($apiresponse,true);
				  curl_close($curl);


				  $this->my_log($apiresponse);


				 // echo $response;
				  $response['salesforce_response'] = $apiresponse;
				  
				  $CaseId = isset($resdata['RecordId']) ? $resdata['RecordId'] : '';
				  $dres = '';
				  if($CaseId)
				  {
					$sqlChatDelete = "DELETE FROM chat_history WHERE chatId = '".$chatId."'";
					$qryChatDelete = $this->conn->query($sqlChatDelete);
					if($qryChatDelete)
					{
						$dres = "Deleted";
					}
				  }

				  $response['chat_data'] = $dres;
				 
			   }

			}
		}

		$convert_lang = isset($_POST['Language']) ? $_POST['Language'] : '';

		$sql = "SELECT r.response_message,q.id from `questions` q inner join `responses` r on q.response_id = r.id where q.question Like '%{$message}%' ";
		$qry = $this->conn->query($sql);
	 
		if($qry->num_rows > 0){
			$result = $qry->fetch_array();
			// var_dump($result);
			$resp['status'] = "success";
			$cquestion = $result['response_message'];

			if($convert_lang)
			{               
				if($cquestion && strtolower($convert_lang) != 'en')
				{          
					$cquestion = $this->translate($cquestion, "en", "$convert_lang");
				}
			}

			 
			$resp['message'] = $cquestion;


			$resp['sql'] = $sql;
			//$this->conn->query("INSERT INTO `frequent_asks` set question_id = '{$result['id']}' ");
			$resp = '{"status":"success","message":"testing ?"}';
			$resp = json_decode($resp);
			return json_encode($resp);
		}else{
			$resp['status'] = "success";
			$resp['message'] = $this->settings->info('no_result');
			$chk = $this->conn->query("SELECT * FROM `unanswered` where `question` = '{$message}' ");
			// if($chk->num_rows > 0){
			// 	$this->conn->query("UPDATE `unanswered` set no_asks = no_asks + 1 ");
			// }else{
			// 	$this->conn->query("INSERT INTO `unanswered` set question = '{$message}' ");
			// }

			// $resp = '{"status":"success","message":"Please share your name?"}';
			// $resp = json_decode($resp);
			// return json_encode($resp);

			 
			
			$response['status'] = "success";
			$response['end'] = "yes";
			$response['postdata'] = $postdata;
			$response['SALESFORCE_LEAD_CREATION'] = SALESFORCE_LEAD_CREATION;
			
			$endmessage = "Thank you for sharing the details, here is your reference number - ".$CaseId.", have a great day.";
			if($endmessage){
				if($convert_lang != 'en'){
					$endmessage = $this->translate($endmessage, "en", "$convert_lang");
				}
			}

			$btntxt = "Start Again";
			if($btntxt){
				if($convert_lang != 'en'){
					$btntxt = $this->translate($btntxt, "en", "$convert_lang");
				}
			}

			$endmessage .= '<br/><br/><div class="center"><button class="btn btn-warning" type="button" name="StartAgain" id="StartAgain"  onclick="StartAgain();">'.$btntxt.'</button></div>';


			$response['message'] = $endmessage;			
			return json_encode($response);
			 
		}
	}



	//load language
	public function get_language_response(){

		$chatId = isset($_GET['chatId']) ? $_GET['chatId'] : '';
		 
		$convert_lang = 'en';
		 
		$sql = "SELECT * from `tbl_default` where questionId in ('991') limit 0,1";
		$qry = $this->conn->query($sql);		 
		$QuestionId = '';
		$question="";
		if($qry->num_rows > 0)
		{
			$result = $qry->fetch_array();		
			$questionId = $result['questionId'];		
			$question = $result['question'];			 
			$question2 = '';
			 	        									
			 
			$salesforce_key = $result['salesforce_key'];
			$questionTitle  = $question;
			$questiontype = $result['questiontype'];									
			$cdate = date('Y-m-d',time());
			$response['status'] = "success";
			
			$response['questiontype'] = $questiontype;
			$htmlQuestion1 = "";

			

			 

			

			$htmlQuestion1 = '';

			$htmlQuestion1 .=  "<br/><br/>";
			$htmlQuestion1 .= $question;

			if(strtoupper($questiontype) == strtoupper('radio'))
			{
				$titleOption1 = $result['titleOption1'];
				$valueOption1 = $result['valueOption1'];
				$titleOption2 = $result['titleOption2'];
				$valueOption2 = $result['valueOption2'];
				$titleOption3 = $result['titleOption3'];
				$valueOption3 = $result['valueOption3'];
				$titleOption4 = $result['titleOption4'];
				$valueOption4 = $result['valueOption4'];
				$titleOption5 = $result['titleOption5'];
				$valueOption5 = $result['valueOption5'];
				$titleOption6 = $result['titleOption6'];
				$valueOption6 = $result['valueOption6'];
				$titleOption7 = $result['titleOption7'];
				$valueOption7 = $result['valueOption7'];
				$titleOption8 = $result['titleOption8'];
				$valueOption8 = $result['valueOption8'];
				 

				if($convert_lang == 'te'){
					if($titleOption1){
					$titleOption1 = $this->translate($titleOption1, "en", "$convert_lang");
					}
					if($titleOption2){
					$titleOption2 = $this->translate($titleOption2, "en", "$convert_lang");
					}
					if($titleOption3){
					$titleOption3 = $this->translate($titleOption3, "en", "$convert_lang");
					}
					if($titleOption4){
					$titleOption4 = $this->translate($titleOption4, "en", "$convert_lang");
					}
					if($titleOption5){
					$titleOption5 = $this->translate($titleOption5, "en", "$convert_lang");
					}
					if($titleOption6){
					$titleOption6 = $this->translate($titleOption6, "en", "$convert_lang");
					}
					if($titleOption7){
					$titleOption7 = $this->translate($titleOption7, "en", "$convert_lang");
					}
					if($titleOption8){
					$titleOption8 = $this->translate($titleOption8, "en", "$convert_lang");
					}
				}

				
				$htmlQuestion1 .=  "<br/>";
				if($titleOption1)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption1."', '".$chatId."', '".$titleOption1."', '".$questionId."')\"  > ".$titleOption1." <a/>";
				}
				if($titleOption2)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption2."', '".$chatId."', '".$titleOption2."', '".$questionId."')\"  > ".$titleOption2." <a/>";
				}
				if($titleOption3)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption3."', '".$chatId."', '".$titleOption3."', '".$questionId."')\"  > ".$titleOption3." <a/>";
				}

				if($titleOption4)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption4."', '".$chatId."', '".$titleOption4."', '".$questionId."')\"  > ".$titleOption4." <a/>";
				}

				if($titleOption5)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption5."', '".$chatId."', '".$titleOption5."', '".$questionId."')\"  > ".$titleOption5." <a/>";
				}

				if($titleOption6)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption6."', '".$chatId."', '".$titleOption6."', '".$questionId."')\"  > ".$titleOption6." <a/>";
				}

				if($titleOption7)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption7."', '".$chatId."', '".$titleOption7."', '".$questionId."')\"  > ".$titleOption7." <a/>";
				}

				if($titleOption8)
				{
					$htmlQuestion1 .= "<a class='btn btn-outline-secondary cm-radio radio_send_chat ".$questionId."' data-id='No' href='javascript:void(0);' onclick=\"selectAnsRadio('".$valueOption8."', '".$chatId."', '".$titleOption8."','".$questionId."')\"  > ".$titleOption8." <a/>";
				}				 							
			}			  			
			 
			
			$response['questionTitle'] = $questionTitle;
			$response['message'] = $htmlQuestion1;
			$response['questionId'] = $questionId;				 				 
			$response['end'] = "no";			
			echo json_encode($response);
		}		  		 
	}

	public function delete_unanswer(){
		extract($_POST);
		 $del = $this->conn->query("DELETE FROM `unanswered` where id = $id");
		 if($del){
			$this->settings->set_flashdata("success"," Data successfully deleted");
		 	return 1;
		 }else{
		 	$this->conn->error;
		 }
	}

	function translate($q, $sl, $tl){    
		if($q){
		$res= file_get_contents("https://translate.googleapis.com/translate_a/single?client=gtx&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&sl=".$sl."&tl=".$tl."&hl=hl&q=".urlencode($q), $_SERVER['DOCUMENT_ROOT']."/transes.html");
		$res=json_decode($res);
		if($res){
			//print_r($res);
			$res0 = isset($res[0]) ? $res[0] : '';
			$res1 = isset($res0[0]) ? $res0[0] : '';
			$val = isset($res1[0]) ? $res1[0] : '';
		return $val;
		}
	}
	}
	
	function get_lang()
	{
		$country_code = $this->ip_info("Visitor", "Country Code"); 
		$lang = $this->country_code_to_locale($country_code, $language_code = '');
		$selected_lang = str_replace("-".$country_code,"",$lang);
		$selected_lang = str_replace("_".$country_code,"",$selected_lang);
		return $selected_lang;
	}


	function get_country()
	{
		$country_code = $this->ip_info("Visitor", "Country Name"); 
		 
		return $country_code;
	}
	 
	 
	
	function country_code_to_locale($country_code, $language_code = '')
	{
		 
		$locales = array('af-ZA',
						'am-ET',
						'ar-AE',
						'ar-BH',
						'ar-DZ',
						'ar-EG',
						'ar-IQ',
						'ar-JO',
						'ar-KW',
						'ar-LB',
						'ar-LY',
						'ar-MA',
						'arn-CL',
						'ar-OM',
						'ar-QA',
						'ar-SA',
						'ar-SY',
						'ar-TN',
						'ar-YE',
						'as-IN',
						'az-Cyrl-AZ',
						'az-Latn-AZ',
						'ba-RU',
						'be-BY',
						'bg-BG',
						'bn-BD',
						'bn-IN',
						'bo-CN',
						'br-FR',
						'bs-Cyrl-BA',
						'bs-Latn-BA',
						'ca-ES',
						'co-FR',
						'cs-CZ',
						'cy-GB',
						'da-DK',
						'de-AT',
						'de-CH',
						'de-DE',
						'de-LI',
						'de-LU',
						'dsb-DE',
						'dv-MV',
						'el-GR',
						'en-029',
						'en-AU',
						'en-BZ',
						'en-CA',
						'en-GB',
						'en-IE',
						'en-IN',
						'en-JM',
						'en-MY',
						'en-NZ',
						'en-PH',
						'en-SG',
						'en-TT',
						'en-US',
						'en-ZA',
						'en-ZW',
						'es-AR',
						'es-BO',
						'es-CL',
						'es-CO',
						'es-CR',
						'es-DO',
						'es-EC',
						'es-ES',
						'es-GT',
						'es-HN',
						'es-MX',
						'es-NI',
						'es-PA',
						'es-PE',
						'es-PR',
						'es-PY',
						'es-SV',
						'es-US',
						'es-UY',
						'es-VE',
						'et-EE',
						'eu-ES',
						'fa-IR',
						'fi-FI',
						'fil-PH',
						'fo-FO',
						'fr-BE',
						'fr-CA',
						'fr-CH',
						'fr-FR',
						'fr-LU',
						'fr-MC',
						'fy-NL',
						'ga-IE',
						'gd-GB',
						'gl-ES',
						'gsw-FR',
						'gu-IN',
						'ha-Latn-NG',
						'he-IL',
						'hi-IN',
						'hr-BA',
						'hr-HR',
						'hsb-DE',
						'hu-HU',
						'hy-AM',
						'id-ID',
						'ig-NG',
						'ii-CN',
						'is-IS',
						'it-CH',
						'it-IT',
						'iu-Cans-CA',
						'iu-Latn-CA',
						'ja-JP',
						'ka-GE',
						'kk-KZ',
						'kl-GL',
						'km-KH',
						'kn-IN',
						'kok-IN',
						'ko-KR',
						'ky-KG',
						'lb-LU',
						'lo-LA',
						'lt-LT',
						'lv-LV',
						'mi-NZ',
						'mk-MK',
						'ml-IN',
						'mn-MN',
						'mn-Mong-CN',
						'moh-CA',
						'mr-IN',
						'ms-BN',
						'ms-MY',
						'mt-MT',
						'nb-NO',
						'ne-NP',
						'nl-BE',
						'nl-NL',
						'nn-NO',
						'nso-ZA',
						'oc-FR',
						'or-IN',
						'pa-IN',
						'pl-PL',
						'prs-AF',
						'ps-AF',
						'pt-BR',
						'pt-PT',
						'qut-GT',
						'quz-BO',
						'quz-EC',
						'quz-PE',
						'rm-CH',
						'ro-RO',
						'ru-RU',
						'rw-RW',
						'sah-RU',
						'sa-IN',
						'se-FI',
						'se-NO',
						'se-SE',
						'si-LK',
						'sk-SK',
						'sl-SI',
						'sma-NO',
						'sma-SE',
						'smj-NO',
						'smj-SE',
						'smn-FI',
						'sms-FI',
						'sq-AL',
						'sr-Cyrl-BA',
						'sr-Cyrl-CS',
						'sr-Cyrl-ME',
						'sr-Cyrl-RS',
						'sr-Latn-BA',
						'sr-Latn-CS',
						'sr-Latn-ME',
						'sr-Latn-RS',
						'sv-FI',
						'sv-SE',
						'sw-KE',
						'syr-SY',
						'ta-IN',
						'te-IN',
						'tg-Cyrl-TJ',
						'th-TH',
						'tk-TM',
						'tn-ZA',
						'tr-TR',
						'tt-RU',
						'tzm-Latn-DZ',
						'ug-CN',
						'uk-UA',
						'ur-PK',
						'uz-Cyrl-UZ',
						'uz-Latn-UZ',
						'vi-VN',
						'wo-SN',
						'xh-ZA',
						'yo-NG',
						'zh-CN',
						'zh-HK',
						'zh-MO',
						'zh-SG',
						'zh-TW',
						'zu-ZA',);
	
		foreach ($locales as $locale)
		{
			$locale_region = locale_get_region($locale);
			$locale_language = locale_get_primary_language($locale);
			$locale_array = array('language' => $locale_language,
								 'region' => $locale_region);
	
			if (strtoupper($country_code) == $locale_region &&
				$language_code == '')
			{
				return locale_compose($locale_array);
			}
			elseif (strtoupper($country_code) == $locale_region &&
					strtolower($language_code) == $locale_language)
			{
				return locale_compose($locale_array);
			}
		}
	
		return null;
	}
	
	
	
	function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
		$output = NULL;
		
		if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
			$ip = $_SERVER["REMOTE_ADDR"];
			if ($deep_detect) {
				if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
					$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
				if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
					$ip = $_SERVER['HTTP_CLIENT_IP'];
			}
		}
		$purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
		$support    = array("country", "countrycode", "state", "region", "city", "location", "address");
		$continents = array(
			"AF" => "Africa",
			"AN" => "Antarctica",
			"AS" => "Asia",
			"EU" => "Europe",
			"OC" => "Australia (Oceania)",
			"NA" => "North America",
			"SA" => "South America"
		);
	 
		//echo "https://www.geoplugin.net/json.gp?ip=" . $ip;
		
		if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {

			 

			 // API with IP address
			$url = "http://www.geoplugin.net/json.gp?ip=".$ip;
			 
			// get a requested data
			$ipInfo = file_get_contents($url);
			$ipdat = @json_decode($ipInfo);
			if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
				switch ($purpose) {
					case "location":
						$output = array(
							"city"           => @$ipdat->geoplugin_city,
							"state"          => @$ipdat->geoplugin_regionName,
							"country"        => @$ipdat->geoplugin_countryName,
							"country_code"   => @$ipdat->geoplugin_countryCode,
							"continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
							"continent_code" => @$ipdat->geoplugin_continentCode
						);
						break;
					case "address":
						$address = array($ipdat->geoplugin_countryName);
						if (@strlen($ipdat->geoplugin_regionName) >= 1)
							$address[] = $ipdat->geoplugin_regionName;
						if (@strlen($ipdat->geoplugin_city) >= 1)
							$address[] = $ipdat->geoplugin_city;
						$output = implode(", ", array_reverse($address));
						break;
					case "city":
						$output = @$ipdat->geoplugin_city;
						break;
					case "state":
						$output = @$ipdat->geoplugin_regionName;
						break;
					case "region":
						$output = @$ipdat->geoplugin_regionName;
						break;
					case "country":
						$output = @$ipdat->geoplugin_countryName;
						break;
					case "countrycode":
						$output = @$ipdat->geoplugin_countryCode;
						break;
				}
			}
		}
		return $output;
	}


		
	public function getFactoryList()
	{
		$curl = curl_init();
		curl_setopt_array($curl, array(
		CURLOPT_URL =>SALESFORCE_FACTORY_LIST,		 
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'GET',
		CURLOPT_HTTPHEADER => array(
			'Cookie: CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1; BrowserId=z9mjH4z7Ee2Ob-W8c40DpA'
		),
		));
		$response = curl_exec($curl);
		curl_close($curl);
		$factoryList = json_decode($response,true);
		return $factoryList;
	}


	public function getCountryList()
	{
		$curl = curl_init();
		curl_setopt_array($curl, array(
		CURLOPT_URL =>SALESFORCE_COUNTRY_LIST,		 
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'GET',
		CURLOPT_HTTPHEADER => array(
			'Cookie: CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1; BrowserId=z9mjH4z7Ee2Ob-W8c40DpA'
		),
		));
		$response = curl_exec($curl);
		curl_close($curl);
		$factoryList = json_decode($response,true);
		return $factoryList;
	}

	public function getProductList()
	{
		 
		$curl = curl_init();
		curl_setopt_array($curl, array(
		CURLOPT_URL =>SALESFORCE_PRODUCT_LIST,		 
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'GET',
		CURLOPT_HTTPHEADER => array(
			'Cookie: CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1; BrowserId=z9mjH4z7Ee2Ob-W8c40DpA'
		),
		));
		$response = curl_exec($curl);
		curl_close($curl);
		$factoryList = json_decode($response,true);
		return $factoryList;
	}


	//getCountryList
	public function htmlCountryList($chatId='', $questionId = '', $convert_lang='')
	{
		$countryData = $this->getCountryList();

		$defaultCountry = $this->get_country();

		$defaultCountry =  isset($defaultCountry) ? $defaultCountry : '';

		if($defaultCountry){
			if($convert_lang != 'en')
			{
				$defaultCountry = $this->translate($defaultCountry, "en", "$convert_lang");
			}
		}
		

		$countryList = isset($countryData['CountryList']) ? $countryData['CountryList'] : array();

		$selectedCountry = '';

		$html = '';
		if(count($countryList) > 0) 
		{

			$placeholder = "--Select country--";
			if($placeholder){
				if($convert_lang != 'en'){
				$placeholder = $this->translate($placeholder, "en", "$convert_lang");
				}
			}
			

			$html .= '<select class="form-control '.$questionId.' '.$defaultCountry.'"   name="browser" id="browser" onchange="selectProductName(this)" >';
						 
	
			$html .= '<option data-id="" id="" value="">'.$placeholder.'</option>';
			for($k = 0; $k < count($countryList); $k++)
			{

				$countryData = $countryList[$k];
				$Countryid = $countryData['CountryId'];
				$CountryName = $countryData['CountryName'];
				if($CountryName){
					if($convert_lang != 'en'){
						$CountryName = $this->translate($CountryName, "en", "$convert_lang");
					}
				}		
				if(strtoupper($CountryName) == strtoupper($defaultCountry))
				{
					 
					$html .= '<option selected data-id="'.$Countryid.'" id="'.$Countryid.'" value="'.$Countryid.'">'.$CountryName.'</option>';				
				}	
				else
				{
					$html .= '<option data-id="'.$Countryid.'" id="'.$Countryid.'" value="'.$Countryid.'">'.$CountryName.'</option>';				
				}	 				
				
			}			
			$html .= '</select>';	
			 	
		}
		return $html;
	}


	//getCountryList
	public function htmlProductList($chatId='', $questionId = '', $convert_lang='')
	{
		$ProductListData = $this->getProductList();

		$defaultCountry = array();

		 
		

		 

		$selectedCountry = '';

		$html = '';
		if(count($ProductListData) > 0) 
		{

			$placeholder = "--Select Product--";
			if($placeholder){
				if($convert_lang != 'en'){
				$placeholder = $this->translate($placeholder, "en", "$convert_lang");
				}
			}
			

			$html .= '<select id="multiple-checkboxes" class="form-control select '.$questionId.'" name="browser"   onchange="selectProductName(this)" >';
						 
	
			$html .= '<option data-id="" id="" value="">'.$placeholder.'</option>';
			for($k = 0; $k < count($ProductListData); $k++)
			{

				$ProductData = $ProductListData[$k];
				$ProductId = $ProductData['Id'];
				$ProductName = $ProductData['Name'];
				if($ProductName){
					if($convert_lang != 'en'){
						$ProductName = $this->translate($ProductName, "en", "$convert_lang");
					}
				}		
				 
			$html .= '<option data-id="'.$ProductId.'" id="'.$ProductId.'" value="'.$ProductId.'">'.$ProductName.'</option>';				
				   				
				
			}			
			$html .= '</select>';	
			 	
		}
		return $html;
	}


	//getCountryList
	public function htmlCountryListOld($chatId='', $questionId = '', $convert_lang='')
	{
		$countryData = $this->getCountryList();

		 

		$countryList = isset($countryData['CountryList']) ? $countryData['CountryList'] : array();

		 

		$html = '';
		if(count($countryList) > 0) 
		{

			$placeholder = "Note: Please select country.";
			if($placeholder){
				if($convert_lang != 'en'){
				$placeholder = $this->translate($placeholder, "en", "$convert_lang");
				}
			}
			

			$html .= '<input class="form-control '.$questionId.'" list="browsers" name="browser" id="browser" onchange="selectProductName(this)" >
						<datalist id="browsers">';
	
			for($k = 0; $k < count($countryList); $k++)
			{

				$countryData = $countryList[$k];
				$Countryid = $countryData['CountryId'];
				$CountryName = $countryData['CountryName'];
				if($CountryName){
					if($convert_lang != 'en'){
						$CountryName = $this->translate($CountryName, "en", "$convert_lang");
					}
				}				 				
				$html .= '<option data-id="'.$Countryid.'" id="'.$Countryid.'" value="'.$CountryName.'">';				
			}			
			$html .= '</datalist>';	
			 	
		}
		return $html;
	}


	//print_r($factoryList);

	public function htmlFactoryList($chatId='', $questionId = '', $convert_lang='')
	{
		$factoryList = $this->getFactoryList();
		$html = '';
		if(count($factoryList) > 0) 
		{

			$placeholder = "Note: Please type factory name in english.";
			if($placeholder){
				if($convert_lang != 'en'){
				$placeholder = $this->translate($placeholder, "en", "$convert_lang");
				}
			}
			

	$html .= '<input class="form-control '.$questionId.'" list="browsers" name="browser" id="browser" onchange="selectFactoryName(this)" >
	<datalist id="browsers">';
	
			for($k = 0; $k < count($factoryList); $k++)
			{
				$factoryData = $factoryList[$k];
				$factoryName = $factoryData['Name'];
				// if($factoryName){
				// 	if($convert_lang != 'en'){
				// 		$factoryName = $this->translate($factoryName, "en", "$convert_lang");
				// 	}
				// }
				$BillingCity = isset($factoryData['BillingCity']) ? $factoryData['BillingCity'] : '';
				$BillingStreet = isset($factoryData['BillingStreet']) ? $factoryData['BillingStreet'] : '';

				
				$factoryId = $factoryData['Id'];
				
				$html .= '<option data-id="'.$factoryId.'" id="'.$factoryId.'" value="'.$factoryName.'" data-address="'.$BillingStreet.'">';
				
			}			
			$html .= '</datalist>';	
			$html .= '<small>('.$placeholder.')</small>';			
		}
		return $html;
	}


	public function htmlFileUpload($chatId='', $questionId='', $convert_lang = '')
	{	 

		$notes = "Upload file should be image, mp4, mp3 and pdf";

		if($notes){
			if($convert_lang != 'en'){
			$notes = $this->translate($notes, "en", "$convert_lang");
			}
		}


		$html = '';	 
		//$html .= '<input class="form-control" type="file" name="fileToUpload" id="fileToUpload" onchange="fileUploadSubmit();"> <input type="button" value="Upload" id="btnFileUpload" class="btn btn-outline-success '.$questionId.'" onclick="fileUploadSubmit();">';

		//$html .= '<input class="form-control" type="file" name="fileToUpload" id="fileToUpload" onchange="fileUploadSubmit();">';
		
		$html .= '<input  type="file" name="fileToUpload" id="fileToUpload"  onchange="uploadS3file();"> <br/> <small>('.$notes.')</small>';
		$html .= '<br/> <div class="file-progress">
						<div class="file-progress-bar"></div>
					</div>';


	// 	$html = '<div class="input-group">
	// 	<input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
	// 	<div class="input-group-append">
	// 	  <span class="input-group-text">$</span>
	// 	  <input  class="input-group-text" type="file" name="fileToUpload" id="fileToUpload" onchange="fileUploadSubmit();">
	// 	</div>
	//   </div>';

		return $html;
	}


	function my_log($data){
		$message  = "Date: " .date("Y-m-d H:i:s P T").PHP_EOL.
			$data.PHP_EOL.
			"-------------------------".PHP_EOL;
		file_put_contents(__DIR__ . '/chatbot-case.log', $message, FILE_APPEND);
	}
	
}

$Master = new Master();
$action = !isset($_GET['f']) ? 'none' : strtolower($_GET['f']);
$sysset = new SystemSettings();
switch ($action) {
	case 'save_response':
		echo $Master->save_response();
	break;
	case 'delete_response':
		echo $Master->delete_response();
	break;
	case 'get_response':
		echo $Master->get_response();
	break;
	case 'get_language_response':
		echo $Master->get_language_response();
	break;
	case 'delete_unanswer':
		echo $Master->delete_unanswer();
	break;
	default:
		// echo $sysset->index();
		break;
}