<?php
class Identite{
     // database connection and table name
     private $conn;
     private $table_name = "identites";
   
     // object properties
     public $id;
     public $login;
     public $password;
     public $pseudo;
     public $age;
     public $poids;
     public $taille;
     public $sexe;
     public $niveaudusport;
     public $icalories;
   
     // constructor with $db as database connection
     public function __construct($db){
         $this->conn = $db;
     }

     // read identites
    function read(){
  
        // select all query
        $query = "SELECT * FROM $this->table_name";
    
        // prepare query statement
        $stmt = $this->conn->prepare($query);
    
        // execute query
        $stmt->execute();
    
        return $stmt;
    }
    // check if given login exist in the database
    function loginExists(){
        
        // query to check if email exists
        $query = "SELECT *
        FROM $this->table_name
        WHERE login = ?
        LIMIT 0,1";
        
        // prepare the query
        $stmt = $this->conn->prepare( $query );
        
        // sanitize
        $this->login=htmlspecialchars(strip_tags($this->login));
        
        // bind given email value
        $stmt->bindParam(1, $this->login);
        // 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->password = $row['password'];
            $this->pseudo = $row['pseudo'];
            $this->age = $row['age'];
            $this->poids = $row['poids'];
            $this->taille = $row['taille'];
            $this->sexe = $row['sexe'];
            $this->niveaudusport = $row['niveaudusport'];
            $this->icalories = $row['icalories'];
            
            // return true because email exists in the database
            return true;
        }
        
        // return false if email does not exist in the database
        return false;
    }
    
   
    // create identite
    function create(){
        
        // query to insert record
        $query = "INSERT INTO
                    ".$this->table_name."
                SET
        login=:login, password=:password, pseudo=:pseudo, age=:age, poids=:poids, taille=:taille, sexe=:sexe, niveaudusport=:niveaudusport, icalories=:icalories";
        
        // prepare query
        $stmt = $this->conn->prepare($query);
        
        // sanitize
        $this->login=htmlspecialchars(strip_tags($this->login));
        $this->password=htmlspecialchars(strip_tags($this->password));
        $this->pseudo=htmlspecialchars(strip_tags($this->pseudo));
        $this->age=htmlspecialchars(strip_tags($this->age));
        $this->poids=htmlspecialchars(strip_tags($this->poids));
        $this->taille=htmlspecialchars(strip_tags($this->taille));
        $this->sexe=htmlspecialchars(strip_tags($this->sexe));
        $this->niveaudusport=htmlspecialchars(strip_tags($this->niveaudusport));
        $this->icalories=htmlspecialchars(strip_tags($this->icalories));
        
        // bind values
        $stmt->bindParam(":login", $this->login);
        $stmt->bindParam(":password", $this->password);
        $stmt->bindParam(":pseudo", $this->pseudo);
        $stmt->bindParam(":age", $this->age);
        $stmt->bindParam(":poids", $this->poids);
        $stmt->bindParam(":taille", $this->taille);
        $stmt->bindParam(":sexe", $this->sexe);
        $stmt->bindParam(":niveaudusport", $this->niveaudusport);
        $stmt->bindParam(":icalories", $this->icalories);
        
        
        // execute query
        if($stmt->execute()){
            return true;
        }
        
        return false;
        
    }
    // used when filling up the update product form
    function readOne(){
        
        // query to read single record
        $query = "SELECT * FROM $this->table_name WHERE login = ?";
        
        // prepare query statement
        $stmt = $this->conn->prepare( $query );
        
        // bind id of product to be updated
        $stmt->bindParam(1, $this->login);
        
        // execute query
        $stmt->execute();
        
        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        
        // set values to object properties
        $this->id = $row['id'];
        $this->password = $row['password'];
        $this->pseudo = $row['pseudo'];
        $this->age = $row['age'];
        $this->poids = $row['poids'];
        $this->taille = $row['taille'];
        $this->sexe = $row['sexe'];
        $this->niveaudusport = $row['niveaudusport'];
        $this->icalories = $row['icalories'];
    }
    // update the product
    function update(){
        
        // update query
        $query = "UPDATE
        " . $this->table_name . "
        SET
        login=:login,
        password=:password,
        pseudo=:pseudo,
        age=:age,
        poids=:poids,
        taille=:taille,
        sexe=:sexe,
        niveaudusport=:niveaudusport,
        icalories =:icalories
        WHERE
        id=:id";
        
        // prepare query statement
        $stmt = $this->conn->prepare($query);
        
        // sanitize
        $this->id=htmlspecialchars(strip_tags($this->id));
        $this->login=htmlspecialchars(strip_tags($this->login));
        $this->password=htmlspecialchars(strip_tags($this->password));
        $this->pseudo=htmlspecialchars(strip_tags($this->pseudo));
        $this->age=htmlspecialchars(strip_tags($this->age));
        $this->poids=htmlspecialchars(strip_tags($this->poids));
        $this->taille=htmlspecialchars(strip_tags($this->taille));
        $this->sexe=htmlspecialchars(strip_tags($this->sexe));
        $this->niveaudusport=htmlspecialchars(strip_tags($this->niveaudusport));
        $this->icalories=htmlspecialchars(strip_tags($this->icalories));
        
        
        // bind new values
        $stmt->bindParam(':id', $this->id);
        $stmt->bindParam(':login', $this->login);
        $stmt->bindParam(':password', $this->password);
        $stmt->bindParam(':pseudo', $this->pseudo);
        $stmt->bindParam(':age', $this->age);
        $stmt->bindParam(':poids', $this->poids);
        $stmt->bindParam(':taille', $this->taille);
        $stmt->bindParam(':sexe', $this->sexe);
        $stmt->bindParam(':niveaudusport', $this->niveaudusport);
        $stmt->bindParam(':icalories', $this->icalories);
        
        
        // execute the query
        if($stmt->execute()){
            return true;
        }
        return false;
    }
    
    // delete the product
    function delete(){
        
        // delete query
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        
        // prepare query
        $stmt = $this->conn->prepare($query);
        
        // sanitize
        $this->id=htmlspecialchars(strip_tags($this->id));
        
        // bind id of record to delete
        $stmt->bindParam(1, $this->id);
        
        // execute query
        if($stmt->execute()){
            return true;
        }
        
        return false;
    }
    // search products
    function search($keywords){
        
        // select all query
        $query = "SELECT * FROM
        " . $this->table_name . "
        WHERE
        login LIKE ? OR pseudo LIKE ? ";
        
        // prepare query statement
        $stmt = $this->conn->prepare($query);
        
        // sanitize
        $keywords=htmlspecialchars(strip_tags($keywords));
        $keywords = "%{$keywords}%";
        
        // bind
        $stmt->bindParam(1, $keywords);
        $stmt->bindParam(2, $keywords);
        
        // execute query
        $stmt->execute();
        
        return $stmt;
    }

     
 }
 ?>

