<?php

include_once ('Database.php');

class Geometry {

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

class GeometryService extends Database
{

    var $tablename = "geometry";
    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 getAllGeometries($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->name, $row->approved, $row->user_id, $row->is_road);

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

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

        return $rows;
    }

    public function getAllGeometriesByBrand($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->name, $row->approved, $row->user_id, $row->is_road);

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

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

        return $rows;
    }
    
    public function getAllTriGeometriesByBrand($brand, $updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename where brand_id=? AND is_road IS NOT TRUE";
        else
            $query = "SELECT * FROM $this->tablename where brand_id=? AND approved=1 AND is_road IS NOT TRUE";

        $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->name, $row->approved, $row->user_id, $row->is_road);

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

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

        return $rows;
    }
    
    public function getAllRoadGeometriesByBrand($brand, $updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename where brand_id=? AND is_road IS NOT FALSE";
        else
            $query = "SELECT * FROM $this->tablename where brand_id=? AND approved=1 AND is_road IS NOT FALSE";

        $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->name, $row->approved, $row->user_id, $row->is_road);

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

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

        return $rows;
    }

    public function getGeometryByID($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->name, $row->approved, $row->user_id, $row->is_road);

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

    public function createGeometry($item)
    {

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

        mysqli_stmt_bind_param($stmt, 'isiii', $item->brand_id, $item->name, $row->approved, $row->user_id, $row->is_road);
        $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 updateGeometry($item)
    {

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

        mysqli_stmt_bind_param($stmt, 'isiiii', $item->brand_id, $item->name, $item->approved, $item->user_id, $item->is_road, $item->id);
        $this->throwExceptionOnError();

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

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

    public function deleteGeometry($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 getGeometry_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->name);

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

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

}

?>
