<?php
class User {
    private $conn;
    private $table_name = "user";

    public $id;
    public $name;
    public $surname;
    public $code;
    public $picture;
    public $admin;
    public $table;
    public $witness;
    public $invited_by;

    // constructor with $db as database connection
    public function __construct($db){
        $this->conn = $db;
    }

    static function get($db, $uid){
        $query = "
            SELECT *
            FROM user AS u
			WHERE u.id = :uid;
		";

        // prepare query statement
        $stmt = $db->prepare($query);
		$stmt->bindParam(":uid", $uid);

        // execute query
        $stmt->execute();

        return $stmt;
    }

    public static function createFromRow($row, $prefix = '') {
		if ($prefix)
			$prefix = $prefix . '_';
		else
			$prefix = '';

		// If user is null
		if (!$row[$prefix . 'id'])
			return null;

        $instance = new self(null);
        $instance->id = $row[$prefix . 'id'];
        $instance->name = $row[$prefix . 'name'];
        $instance->surname = $row[$prefix . 'surname'];
        $instance->code = $row[$prefix . 'code'];
        $instance->picture = $row[$prefix . 'picture'];
        $instance->admin = $row[$prefix . 'admin'] ? true : false;
        $instance->table = $row[$prefix . 'table'];
        $instance->witness = $row[$prefix . 'witness'] ? true : false;
        $instance->invited_by = $row[$prefix . 'invited_by'];
        return $instance;
    }


}
