<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class DbConnection{
    private $host = 'localhost';
    private $user = 'ovohgiqi_ovoh';
    private $password = 'ovohgiqi_ovoh';
    private $db="ovohgiqi_ovoh";
    private $conn='';
    
     public function __construct() {
        
        $this->conn = new mysqli($this->host, $this->user, $this->password, $this->db);
        
        
   
    }
    
    function runBaseQuery($query){
        
        $result = mysqli_query($this->conn, $query);
        
        
            while($row = mysqli_fetch_assoc($result)){
            
            $resultSet[] = $row;
        }
        
       
        if(!empty($resultSet)){
            return $resultSet;
        }
    }
    
    function runQuery($query, $param_type, $param_value_array){
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $result = $sql->get_result();
        
        if($result->num_rows > 0){
            
           while($row = mysqli_fetch_assoc($result)){
               $resultSet[] = $row;
           }
        }
        
        if(!empty($resultSet)){
            return $resultSet;
        }
    }
    
    function bindQueryParams($sql, $param_type, $param_value_array){
        $param_value_reference[] = & $param_type;
        
        for($i = 0; $i < count($param_value_array); $i++){
            $param_value_reference[] = & $param_value_array[$i];
        }
        
        call_user_func_array(array($sql, 'bind_param'), $param_value_reference);
    }
    
    function insertRecord($query, $param_type, $param_value_array){
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $result = $sql->execute();
        
        return $result;
    }
    
    function update($query, $param_type, $param_value_array){
        $sql = $this->conn->prepare($query);
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $result = $sql->execute();
        return $result;
    }
    
    function delete($query){
        $result = mysqli_query($this->conn, $query);
        
        return $result;
    }
    
    function generateReferralCode($start, $end){
        $startStr = "RO";
        $endStr = "21";
        $random = rand($start,$end);
        
        $referralCode= $startStr.$random.$endStr;
        
        return $referralCode;
    }
    
    function sanitize($data){
        $data = trim($data);
        $data = htmlentities($data);
        $data = htmlspecialchars($data);
       
        return $data;
    }
    
    function send_mail($email,$messag,$subject)
	{						
		require_once('mailer/PHPMailer.php');
		$mail = new PHPMailer();
		$mail->IsSMTP(); 
		$mail->SMTPDebug  = 0;                     
		$mail->SMTPAuth   = true;                  
		$mail->SMTPSecure = "ssl";                 
		$mail->Host       = "ovohinitiative.com";      
		$mail->Port       = 465;             
		$mail->AddAddress($email);
	//	$mail->AddEmbeddedImage('../assets/img/logo.png', 'image1');
		$mail->Username="no-reply@ovohinitiative.com";  
		$mail->Password="no-reply@ovohinitiative.com";            
		$mail->SetFrom('no-reply@ovohinitiative.com','Ovoh Initiative');
		//$mail->AddReplyTo("customercare@ovohinitiative.com","Swift Stock Exchange");
		$mail->Subject    = $subject;
		$mail->MsgHTML($messag);
		$mail->Send();
	}	
	
	function lastID(){
        return $this->conn->insert_id;
    }


}
