<?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 = 'spcxinvs_com';
    private $password = 'spcxinvs_com';
    private $db="spcxinvs_com";
    public $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 lastId(){
        $id = mysqli_insert_id($this->conn);
        
        return $id;
    }
    
    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('../../en/php/mailer/class.phpmailer.php');
		$mail = new PHPMailer();
		$mail->IsSMTP(); 
		$mail->SMTPDebug  = 0;                     
		$mail->SMTPAuth   = true;                  
		$mail->SMTPSecure = "ssl";                 
		$mail->Host       = "mail.spcxinvs.com";      
		$mail->Port       = 465;             
		$mail->AddAddress($email);
		$mail->AddEmbeddedImage('../assets/img/logo.png', 'image1');
		$mail->Username="support@spcxinvs.com";  
		$mail->Password="adw@1AE-*xGX";            
		$mail->SetFrom('support@spcxinvs.com','SpaceX Tesla Investments Trades');
		$mail->AddReplyTo("support@spcxinvs.com","SpaceX Tesla Investments Trades");
		$mail->Subject    = $subject;
		$mail->MsgHTML($messag);
		$mail->Send();
	}	

}
