<?php

date_default_timezone_set('Africa/Johannesburg');

class Token{
    
    private $conn;
    private $table_name = "UserToken";
 
    // object properties
    public $id;
	public $fkeyAppUser;
	public $consultantId;
    public $tokenValue;
    public $tokenType;
    public $phoneOs;
    public $phoneModel;
    public $activationEmail;
    public $activationIpAddress;
    public $createdIpAddress;
    public $activationCountry;
    public $activated;
    public $activatedOn ;
    public $expiryDate;
    public $duration;
    public $createdAt;
    public $updatedAt;
    public $createdBy; 
    public $validationMessage = "";
    private $emptyData= "0000-00-00 00:00:00";
    
    
    
     // constructor
    public function __construct($db){
        $this->conn = $db;
    }
    
    
function isTokenValid(){
 
    // query to check if token exists and is valid
    $query = "SELECT `id`,`activated`,`token_type` FROM `UserToken`
            WHERE token =:token and `activated` = 'no' LIMIT 0,1";
 
    // prepare the query
    $stmt = $this->conn->prepare( $query );
 
    // sanitize
   // $this->email=htmlspecialchars(strip_tags($this->email));
 
    // bind given email value
    $stmt->bindParam(':token', $this->tokenValue);
    
   // $stmt->bindParam(2,$this->emptyData);
 
    // execute the query
    $stmt->execute();
 
    // get number of rows
    $num = $stmt->rowCount();
 
    // if email exists, assign values to object properties for easy access and use for php sessions
    if($num>0){
 
        // get record details / values
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
 
        // assign values to object properties
        $this->id = $row['id'];
        $this->activated = $row['activated'];
         $this->tokenType = $row['token_type'];
        if(strcmp($this->activated,"no")==0){
            return true;
        }
        $this->validationMessage = "Token already activated";
        return false;

    }
 
     $this->validationMessage = "Token already activated";
    return false;
}

function updateValidToken(){
     // query to check if token exists and is valid
    $query = "UPDATE `UserToken` SET `phone_os`=:phoneOs,`phone_model`=:phoneModel,`activation_email`=:activationEmail,`activation_ip_address`=:activationIp,`activation_country`=:activationCountry,`activated`=:activated, activated_on =:activatedDate WHERE id =:recId";
            
             // prepare the query
    $stmt = $this->conn->prepare( $query );
            
    $activatedOn = date("Y-m-d H:i:s");
    $activated = "yes";
    
     $stmt->bindParam(':phoneOs',  $this->phoneOs);
     $stmt->bindParam(':phoneModel', $this->phoneModel);
     $stmt->bindParam(':activationEmail', $this->activationEmail);
     $stmt->bindParam(':activationIp', $this->activationIpAddress);
     $stmt->bindParam(':activationCountry', $this->activationCountry);
     $stmt->bindParam(':activated',$activated);
     $stmt->bindParam(':activatedDate',  $activatedOn);
    
    $stmt->bindParam(':recId',$this->id);
     // execute the query
    if($stmt->execute()){
        return true;
    }
    return false;
}

function saveGeneratedToken(){
    $tokenVal = mt_rand(100000,999999);
    $query= "INSERT INTO `UserToken`(`FK_AppUser`, `consultantId`, `token`,`phone_type`, `expiry_date`, `duration`, `created_by`) VALUES (:userId,:consultantId,:tokenValue,:phoneType,:expireDate,:duration,:createdBy)";
               // prepare the query
    $stmt = $this->conn->prepare( $query );

    
     $stmt->bindParam(':userId',$this->fkeyAppUser);
     $stmt->bindParam(':consultantId',$this->consultantId);
     $stmt->bindParam(':tokenValue',$this->tokenValue);
     $stmt->bindParam(':phoneType',$this->phoneType);
     $stmt->bindParam(':expireDate',$this->expiryDate);
     $stmt->bindParam(':duration',$this->duration);
    // $stmt->bindParam(':createdDate',$this->createdAt);
     $stmt->bindParam(':createdBy',$this->consultantId);
     
    if($stmt->execute()){
        return true;
        }
    return false;

}

function generateNewTokens($tokenCount,$adminUser,$allocation,$tokenType,$date){
    $response = array();
    // date_default_timezone_set('Africa/Johannesburg');
    // $date = date('yyy-MM-dd HH:mm:ss', time());
    

    
    for($counter = 1;$counter<=$tokenCount;$counter++){
        $tokenVal = mt_rand(100000,999999);
        $query= "INSERT INTO `UserToken`( `token`,`token_type`,`created_by`,`allocation`,`created_at`) VALUES (:tokenValue,:token_type,:createdBy,:allocation,:created_at)";
                   // prepare the query
        $stmt = $this->conn->prepare( $query );
         $stmt->bindParam(':tokenValue',$tokenVal);
          $stmt->bindParam(':token_type',$tokenType);
         $stmt->bindParam(':createdBy',$adminUser);
          $stmt->bindParam(':allocation',$allocation);
          $stmt->bindParam(':created_at',$date);
         
        if($stmt->execute()){
            $response[] = $tokenVal;
        }
    }
     return $response;
}


}



?>
