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

    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDType, name, description FROM cass_type WHERE IDType = %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;
        }
    }

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

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


         $table = 'cass_material';
    }

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

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

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

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

    public function sqlWPTable() {
        global $wpdb;

        $sql = "
            SELECT IDType, name, description 
            FROM cass_type;
             ";
     
        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
            SELECT IDType, name, description 
            FROM cass_type;
             ";
     
        return $wpdb->get_results($sql);
    }

    // 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;
    }
}
