<?php

include_once ('Database.php');

class Model {

    public function __construct($item = array()) {
        foreach ($item as $k => $v) {
            $this->{$k} = mysql_real_escape_string($v);
        }
    }
}

class ModelService extends Database
{

    var $tablename = "model";
    var $connection;

    public function __construct($tablename="")
    {
        if (! empty($tablename))
            $this->tablename = $tablename;
        
        $this->connection = mysqli_connect(
                        $this->server,
                        $this->username,
                        $this->password,
                        $this->databasename
        );

        $this->throwExceptionOnError($this->connection);
    }

    public function getAllModels($updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename";
        else
            $query = "SELECT * FROM $this->tablename WHERE approved=1";

        $stmt = mysqli_prepare($this->connection, $query);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);

        while (mysqli_stmt_fetch($stmt))
        {
            $rows[] = $row;
            $row = new stdClass();
            mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);
        }

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rows;
    }

    public function getAllModelsByBrand($brand, $updater = false)
    {
        if ($updater)
            $query = "SELECT * FROM $this->tablename where brand_id=?";
        else
            $query = "SELECT * FROM $this->tablename where brand_id=? AND approved=1";

        $stmt = mysqli_prepare($this->connection, $query);
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $brand);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);

        while (mysqli_stmt_fetch($stmt))
        {
            $rows[] = $row;
            $row = new stdClass();
            mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);
        }

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rows;
    }

    public function getAllModelsByGeometry($geo, $updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename where geometry_id=?";
        else
            $query = "SELECT * FROM $this->tablename where geometry_id=? and approved=1";

        $stmt = mysqli_prepare($this->connection, $query);
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $geo);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);

        while (mysqli_stmt_fetch($stmt))
        {
            $rows[] = $row;
            $row = new stdClass();
            mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);
        }

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rows;
    }

    public function getModelByID($itemID)
    {

        $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where id=?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $itemID);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);

        if (mysqli_stmt_fetch($stmt))
        {
            return $row;
        }
        else
        {
            return null;
        }
    }

    public function createModel($item)
    {

        $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (brand_id, geometry_id, name, notes, approved, user_id) VALUES (?, ?, ?, ?, ?, ?)");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'iissii', $item->brand_id, $item->geometry_id, $item->name, $item->notes, $item->approved, $item->user_id);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $autoid = mysqli_stmt_insert_id($stmt);

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $autoid;
    }

    public function updateModel($item)
    {

        $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET brand_id=?, geometry_id=?, name=?, notes=?, approved=?, user_id=? WHERE id=?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'iissiii', $item->brand_id, $item->geometry_id, $item->name, $item->notes, $item->approved, $item->user_id, $item->id);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);
    }

    public function deleteModel($itemID)
    {

        $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE id = ?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $itemID);
        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);
    }

    public function count()
    {
        $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_bind_result($stmt, $rec_count);
        $this->throwExceptionOnError();

        mysqli_stmt_fetch($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rec_count;
    }

    public function getModel_paged($startIndex, $numItems)
    {

        $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        $rows = array();

        mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);

        while (mysqli_stmt_fetch($stmt))
        {
            $rows[] = $row;
            $row = new stdClass();
            mysqli_stmt_bind_result($stmt, $row->id, $row->brand_id, $row->geometry_id, $row->name, $row->notes, $row->approved, $row->user_id);
        }

        mysqli_stmt_free_result($stmt);
        mysqli_close($this->connection);

        return $rows;
    }

    private function throwExceptionOnError($link = null)
    {
        if ($link == null)
        {
            $link = $this->connection;
        }
        if (mysqli_error($link))
        {
            $msg = mysqli_errno($link) . ": " . mysqli_error($link);
            throw new Exception('MySQL Error - ' . $msg);
        }
    }

}

?>
