<?php
class Material {
    private $id;
    private $name;
    private $description;

    //Load from ID
    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDMaterial, name, description FROM cass_material WHERE IDMaterial = %d;
        ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $id));

        if ($result) {
            $this->setId($result->IDMaterial);
            $this->setName($result->name);
            $this->setDescription($result->description);
            return true;
        } else {
            return false;
        }
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
        SELECT IDMaterial, name, description  FROM cass_material;
        ";

        return $wpdb->get_results($sql);
    }

    //Update 
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_material
            SET name = %s, description = %s
            WHERE IDMaterial = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getDescription(), $this->getId()));
    }

    //Delete
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_material
            WHERE IDMaterial = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getId()));
    }

    //Insert
    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_material(name, description)
        VALUES(%s,%s);
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getDescription()));
    }

    public function sqlWPTable() {
        global $wpdb;
        
        $sql = "
            SELECT IDMaterial, name, description from cass_material;
             ";
     
        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    // Getter and setters
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setDescription($description) {
        $this->description = $description;
    }
}
