<!-- Realisatsiooni aluseks on võetud:
PHP Object Oriented CRUD Example with Bootstrap
Kasutatud 6.04.2016
https://www.codeofaninja.com/2014/06/php-object-oriented-crud-example-oop.html
-->

<?php
/*
======================
Kataloogi klass
======================
*/
class Kataloog
{
    
    private $conn;
    private $tabel = "kataloogid";
    
    public $id;
    public $nimi;
    public $aeg;
    
    public function __construct($db)
    {
        $this->conn = $db;
    }
    
    function read()
    {
        // Kõikide kataloogide lugemine andmebaasist. Järjestamine ID alusel
        $query = "SELECT id, nimi, aeg FROM " . $this->tabel . " ORDER BY id DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }
    
    function readNames()
    {
        // Kõikide kataloogide nimede lugemine
        $query = "SELECT nimi FROM " . $this->tabel . "";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }
    
    function readInfo()
    {
        // Kataloogi ID järgi informatsiooni lugemine
        $query = "SELECT nimi, aeg FROM " . $this->tabel . " WHERE id = ? limit 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        
        $this->nimi = $row['nimi'];
        $this->aeg  = $row['aeg'];
    }
    
    function readTulevased()
    {
        // Tulevaste saalioksjonite kataloogide lugemine
        $query = "SELECT id, nimi, aeg FROM " . $this->tabel . " WHERE aeg > now() ORDER BY aeg DESC";
        
        $stmt = $this->conn->prepare($query);
        
        $stmt->execute();
        
        return $stmt;
    }
    
    function readMoodunud()
    {
        // Möödunud saalioksjonite kataloogide lugemine
        $query = "SELECT id, nimi, aeg FROM " . $this->tabel . " WHERE aeg < now() ORDER BY aeg DESC";
        
        $stmt = $this->conn->prepare($query);
        
        $stmt->execute();
        
        return $stmt;
    }
    
    
    function create()
    {
        // Saalioksjoni kataloogi loomine
        $query = "INSERT INTO " . $this->tabel . " SET nimi= ?, aeg= ?";
        
        $stmt = $this->conn->prepare($query);
        
        $this->nimi = htmlspecialchars(strip_tags($this->nimi));
        $this->aeg  = htmlspecialchars(strip_tags($this->aeg));
        
        
        $stmt->bindParam(1, $this->nimi);
        $stmt->bindParam(2, $this->aeg);
        
        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
    }
    
    function delete()
    {
        // Saalioksjoni kataloogi kustutamine
        $query = "DELETE FROM " . $this->tabel . " WHERE id = ?";
        
        $stmt = $this->conn->prepare($query);
        
        $stmt->bindParam(1, $this->id);
        
        if ($result = $stmt->execute()) {
            return true;
        } else {
            return false;
        }
    }
    
    function update()
    {
        // Saalioksjoni kataloogi uuendamine
        $query = "UPDATE " . $this->tabel . " SET nimi = :nimi, aeg = :aeg WHERE id = :id";
        
        $stmt = $this->conn->prepare($query);
        
        $this->nimi = htmlspecialchars(strip_tags($this->nimi));
        $this->aeg  = htmlspecialchars(strip_tags($this->aeg));
        $this->id   = htmlspecialchars(strip_tags($this->id));
        
        $stmt->bindParam(':nimi', $this->nimi);
        $stmt->bindParam(':aeg', $this->aeg);
        $stmt->bindParam(':id', $this->id);
        
        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
    }
    
    public function esemeteArv()
    {
        // Esemete arvu lugemine kataloogi ID järgi
        $query = "SELECT id FROM esemed_saal WHERE kataloogi_id =?";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        
        $num = $stmt->rowCount();
        
        return $num;
    }
    
}