<?php

include_once ('Database.php');

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

function ensure_digits($test, $len, &$values, &$errors)
{
    if (strlen($values[$test]) !== $len OR !ctype_digit($values[$test]))
    {
        $errors[$test] = "$test needs to be a $len digit number";
        return false;
    }

    return true;
}

function ensure_range($test, $max, $min, &$values, &$errors)
{
    if (strlen($values[$test]) > $max OR strlen($values[$test]) < $min  OR !ctype_digit($values[$test]))
    {
        $errors[$test] = "$test needs to be a $min to $max digit number";
        return false;
    }

    return true;
}

function VerifyFrame(&$values, &$errors)
{
    $ret = true;
    if (!ensure_digits('stack', 3, $values, $errors))
        $ret = false;

    if (!ensure_digits('reach', 3, $values, $errors))
        $ret = false;

    if (!ensure_digits('trail', 2, $values, $errors))
        $ret = false;

    if (!ensure_digits('front_center', 3, $values, $errors))
        $ret = false;

    if (!ensure_range('head_tube', 3, 2, $values, $errors))
        $ret = false;

    if (!ensure_digits('sta_min', 2, $values, $errors))
        $ret = false;

    if (isset($values['sta_max']))
    {
        if (!ensure_digits('sta_max', 2, $values, $errors))
            $ret = false;
    }

    return $ret;
}

class FrameService extends Database
{

    var $tablename = "frame";
    var $editstablename = "frame_edits";
    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 getAllFrames($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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $row->approved, $row->user_id);
        }

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

        return $rows;
    }

    public function getAllFramesOrdered($updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename  where approved=1 ORDER BY brand_id, geometry_id, is_650 DESC, reach";
        else
            $query = "SELECT * FROM $this->tablename ORDER BY brand_id, geometry_id, is_650 DESC, reach";

        $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $row->approved, $row->user_id);
        }

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

        return $rows;
    }

    public function getAllFramesByBrand($brand, $updater = false)
    {

        if ($updater)
            $query = "SELECT * FROM $this->tablename where brand_id=? ORDER BY geometry_id, is_650 DESC, reach";
        else
            $query = "SELECT * FROM $this->tablename where brand_id=? and approved=1 ORDER BY geometry_id, is_650 DESC, reach";

        $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $row->approved, $row->user_id);
        }

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

        return $rows;
    }

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

        if ($updater)
            $query = "SELECT * FROM $this->tablename where geometry_id=? ORDER BY is_650 DESC, reach";
        else
            $query = "SELECT * FROM $this->tablename where geometry_id=? AND approved=1 ORDER BY is_650 DESC, reach";

        $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $row->approved, $row->user_id);
        }

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

        return $rows;
    }

    public function getFrameByID($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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $row->approved, $row->user_id);

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

    public function createFrame($item)
    {

        $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (brand_id, geometry_id, size_name, stack, reach, trail, front_center, head_tube, internal_headset, sta_min, sta_max, is_650, approved, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'iisiiiiiiiiiii',
                $item->brand_id,
                $item->geometry_id,
                $item->size_name,
                $item->stack,
                $item->reach,
                $item->trail,
                $item->front_center,
                $item->head_tube,
                $item->internal_headset,
                $item->sta_min,
                $item->sta_max,
                $item->is_650,
                $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 updateFrame($item)
    {
        $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET brand_id=?, geometry_id=?, size_name=?, stack=?, reach=?, trail=?, front_center=?, head_tube=?, internal_headset=?, sta_min=?, sta_max=?, is_650=?, approved=?, user_id=? WHERE id=?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'iisiiiiiiiiiiii',
                $item->brand_id,
                $item->geometry_id,
                $item->size_name,
                $item->stack,
                $item->reach,
                $item->trail,
                $item->front_center,
                $item->head_tube,
                $item->internal_headset,
                $item->sta_min,
                $item->sta_max,
                $item->is_650,
                $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 deleteFrame($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 getFrame_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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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->size_name, $row->stack, $row->reach, $row->trail, $row->front_center, $row->head_tube, $row->internal_headset, $row->sta_min, $row->sta_max, $row->is_650, $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);
        }
    }

}

?>
