<?php
// 'consultant' object
class Consultant{
 
    // database connection and table name
    private $conn;
    private $table_name = "Consultants";
 
    // object properties
    public $id;
    public $firstname;
    public $surname;
    public $celnr;
    public $email;
    public $password;
    public $town;
    public $province;
    public $country;
    public $biography;
    public $photo;
    public $created;
    public $modified;
 
    // constructor
    public function __construct($db){
        $this->conn = $db;
    }
 
// create new user record
function create(){
 
    // insert query
    $query = "INSERT INTO `Consultants`(`first_name`, `surname`, `celnr`, `email`, `town`, `province`, `country`, `password`, `biography`, `photo`) VALUES (:firstname,:surname,:celnr,:email,:town,:province,:country,:password,:biography,:photo)";
    
  
   //return $query;
    // prepare the query
    $stmt = $this->conn->prepare($query);
 
    // sanitize
    $this->firstname=htmlspecialchars(strip_tags($this->firstname));
    $this->surname=htmlspecialchars(strip_tags($this->surname));
    $this->email=htmlspecialchars(strip_tags($this->email));
    $this->password=htmlspecialchars(strip_tags($this->password));
    $this->town=htmlspecialchars(strip_tags($this->town));
    $this->province=htmlspecialchars(strip_tags($this->province));
    $this->country=htmlspecialchars(strip_tags($this->country));
    $this->biography=htmlspecialchars(strip_tags($this->biography));
 
    // bind the values
    $stmt->bindParam(':firstname', $this->firstname);
    $stmt->bindParam(':surname', $this->surname);
     $stmt->bindParam(':celnr', $this->celnr);
    $stmt->bindParam(':email', $this->email);

 
    // hash the password before saving to database
    $password_hash = password_hash($this->password, PASSWORD_BCRYPT);
    $stmt->bindParam(':password', $password_hash);
    $stmt->bindParam(':town', $this->town);
    $stmt->bindParam(':province', $this->province);
    $stmt->bindParam(':country', $this->country);
    $stmt->bindParam(':biography', $this->biography);
    $stmt->bindParam(':photo', $this->photo);
 
    // execute the query, also check if query was successful
    if($stmt->execute()){
        return true;
    }
 
    return false;
}

function updateConsultant(){
    $noPhoto="NO PHOTO";
     if(strcmp($this->photo,$noPhoto)==0){
         $query = "UPDATE `Consultants` SET  `first_name`=:firstname,`surname`=:surname,`celnr`=:celnr,`email`=:email,`town`=:town,`province`=:province,`country`=:country,`biography`=:biography, `modified`=:modified WHERE `id`= :id";
     }else{
         $query = "UPDATE `Consultants` SET  `first_name`=:firstname,`surname`=:surname,`celnr`=:celnr,`email`=:email,`town`=:town,`province`=:province,`country`=:country,`biography`=:biography,`photo`=:photo,`modified`=:modified WHERE `id`= :id";
     }
     
    
     $stmt = $this->conn->prepare($query);
    
     // sanitize
    $this->firstname=htmlspecialchars(strip_tags($this->firstname));
    $this->surname=htmlspecialchars(strip_tags($this->surname));
    $this->email=htmlspecialchars(strip_tags($this->email));
    $this->password=htmlspecialchars(strip_tags($this->password));
    $this->town=htmlspecialchars(strip_tags($this->town));
    $this->province=htmlspecialchars(strip_tags($this->province));
    $this->country=htmlspecialchars(strip_tags($this->country));
    $this->biography=htmlspecialchars(strip_tags($this->biography));
 
    // bind the values
    $stmt->bindParam(':firstname', $this->firstname);
    $stmt->bindParam(':surname', $this->surname);
    $stmt->bindParam(':celnr', $this->celnr);
    $stmt->bindParam(':email', $this->email);
    $stmt->bindParam(':town', $this->town);
    $stmt->bindParam(':province', $this->province);
    $stmt->bindParam(':country', $this->country);
    $stmt->bindParam(':biography', $this->biography);
     if(strcmp($this->photo,$noPhoto)!=0){
        $stmt->bindParam(':photo', $this->photo);

    }
    
    $stmt->bindParam(':id', $this->id);
    
     date_default_timezone_set('Africa/Johannesburg');
    $dateTime = date('Y-m-d H:i:s', strtotime("now"));
    $stmt->bindParam(':modified', $dateTime);
    
    $stmt->execute();
    $affected_rows = $stmt->rowCount();
    if($affected_rows > 0){
       return $updateStatus = "true";
    }else{
      return $updateStatus = "false";
    }
 
    
    
    
}
 
// check if given email exist in the database
function emailExists(){
 
    // query to check if email exists
    $query = "SELECT `id`, `first_name`, `surname`, `celnr`, `email`, `town`, `province`, `country`, `password`, `biography`, `photo`, `created`, `modified` FROM `Consultants` WHERE `email`= :email  LIMIT 0,1";
 
    // prepare the query
    $stmt = $this->conn->prepare( $query );
        // sanitize
    $this->email=htmlspecialchars(strip_tags($this->email));
 
    $stmt->bindParam(':email', $this->email);
 
    // 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->firstname = $row['first_name'];
        $this->surname = $row['surname'];
        $this->celnr = $row['celnr'];
        $this->email = $row['email'];
        $this->town = $row['town'];
        $this->province = $row['province'];
        $this->country = $row['country'];
        $this->password = $row['password'];
        $this->biography = $row['biography'];
        $this->photo = $row['photo'];
        $this->created = $row['created'];
        $this->modified = $row['modified'];
 
        // return true because email exists in the database
        return true;
    }
 
    // return false if email does not exist in the database
    return false;
}

// get Consultant details
function getConsultantById(){

 
    // query to check if email exists
    $query = "SELECT `id`, `first_name`, `surname`, `celnr`, `email`, `town`, `province`, `country`, `biography`, `photo` FROM `Consultants` WHERE `id` = :id  LIMIT 0,1";
 
    // prepare the query
    $stmt = $this->conn->prepare( $query );
 

    // bind given email value
    $stmt->bindParam(':id', $this->id);
 
    // 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
    
    //======================== Populate JSON ============================
     if ($stmt->rowCount() > 0) {
       

        $response["message"] = "Success";
	    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
			$innerObject["id"] = $row["id"];
			$innerObject["firstname"] = $row["first_name"];
			$innerObject["surname"] = $row["surname"];
			$innerObject["celnr"] = $row["celnr"];
			$innerObject["email"] = $row["email"];
			$innerObject["town"] = $row["town"];
			$innerObject["province"] = $row["province"];
			$innerObject["country"] = $row["country"];
			$innerObject["biography"] = $row["biography"];
			$innerObject["photo"] =  base64_encode($row["photo"]);
		//	$innerObject["photo"] =  $row["photo"];

		  $response["consultant"] = $innerObject;
		 }
	    return 	json_encode($response);
      
    }else{
         //$response["userList"]["Error"] = "Error";
         $response["Status"] = "No Data";
         return json_encode($response);
    }
    //===================================================================
   
}

}