<?php
class TokenData{
    

	private $conn;
	    // constructor
    public function __construct($db){
        $this->conn = $db;
		require('ps_object_token.php');
    }
    
    function getGenratedTokenList($createdBy){
		$columns = "`id`, `FK_AppUser`, `consultantId`, `token`, `token_type`, `activation_type`, `phone_os`, `phone_model`, `activation_email`, `activated`, `activated_on`, `created_at`, `created_by`, `allocation` ";
		if(strcmp($createdBy,"all")!=0){
		    $query = "SELECT ". $columns. "  FROM `UserToken`  WHERE `created_by` =:createdBy  order by `created_at` desc, `activated_on` asc,`id` asc";
		}else{
		    $query = "SELECT ". $columns. "  FROM `UserToken` order by `created_by` asc , `created_at` desc, `activated_on` asc,`id` asc";
		}
  
             // prepare the query
        $stmt = $this->conn->prepare( $query );
        if(strcmp($createdBy,"all")!=0){
            $stmt->bindParam(':createdBy', $createdBy);
        }
        
        
        $stmt->execute(); 
        $num = $stmt->rowCount();
    //	$recCounter = 0;
    	$list = array();
    	$recCounter = 0;
        if ($stmt->rowCount() > 0) {
           $response["responseCode"] = 200;
    		$response["result"] = "success";
    	    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    			$psObjectToken = new PsObjectToken();
    			$psObjectToken->id = $row["id"];
    			$recCounter++;
    			$psObjectToken->recCounter = $recCounter;
    			$psObjectToken->fk_AppUser = $row["FK_AppUser"];
    			$psObjectToken->consultantId = $row["consultantId"];
    			$psObjectToken->token = $row["token"];
    			$psObjectToken->token_type = $row["token_type"];
    			
    			$psObjectToken->activation_type = $row["activation_type"];
    			$psObjectToken->phone_os = $row["phone_os"];
    			$psObjectToken->phone_model = $row["phone_model"];
    			
    			$psObjectToken->activation_email = $row["activation_email"];
    			$psObjectToken->activated = $row["activated"];
    			$psObjectToken->activated_on = $row["activated_on"];
    			$psObjectToken->created_at = $row["created_at"];
    			$psObjectToken->created_by = $row["created_by"];
    			$psObjectToken->allocation = $row["allocation"];
    		//	$recCounter++;
    			$list[] = $psObjectToken;
    			//$user = "user" . $recCounter;$response[$user] = $psObjectUser;
    
    		 }
    		 $response["tokenlist"] = $list;
    			
    	    return 	json_encode($response);
          
        }else{
            $response["responseCode"] = 200;
    		$response["result"] = "no data";
            $psObjectToken = new PsObjectToken();
    		$psObjectToken->id = 0;
    		$psObjectToken->recCounter = 0;
    		$psObjectToken->fk_AppUser = 0;
    		$psObjectToken->consultantId = 0;
    		$psObjectToken->token = "";
    		$psObjectToken->token_type = "";
    			
    		$psObjectToken->activation_type = "";
    		$psObjectToken->phone_os = "";
    		$psObjectToken->phone_model = "";
    			
    		$psObjectToken->activation_email = "";
    		$psObjectToken->activated = "";
    		$psObjectToken->activated_on = "";
    		$psObjectToken->created_at = "";
    		$psObjectToken->created_by = "";
    		$psObjectToken->allocation = "";
    		//	$recCounter++;
    		$list[] = $psObjectToken;
            $response["tokenlist"] = $list;
             return json_encode($response);
        }
     

	}
    
}
	