<?
require_once ("BaseDAO.php");

// Vehicle Value Object
class VehicleVO extends BaseVO {
    var
$vehicleid;
    var
$year;
    var
$make;
    var
$model;
    var
$color;
    var
$price;

    
// create a new VO
    
function VehicleVO(
        
$vehicleid= 0,
        
$year= "",
        
$make= "",
        
$model= "",
        
$color= "",
        
$price= 0
    
) {
        
$this->vehicleid = $vehicleid;
        
$this->year = $year;
        
$this->make = $make;
        
$this->model = $model;
        
$this->color = $color;
        
$this->price = $price;
    }

    
// compare to another VO
    
function equals($vo) {
        return
$this->vehicleid == $vo->vehicleid &&
            
$this->year == $vo->year &&
            
$this->make == $vo->make &&
            
$this->model == $vo->model &&
            
$this->color == $vo->color &&
            
$this->price == $vo->price;
    }
    
// copies another VO
    
function copy($vo) {
        
$this->vehicleid = $vo->vehicleid;
        
$this->year = $vo->year;
        
$this->make = $vo->make;
        
$this->model = $vo->model;
        
$this->color = $vo->color;
        
$this->price = $vo->price;
    }

    
// output as a string
    
function toString() {
        return
$this->vehicleid . "," . $this->year . "," . $this->make . "," . $this->model . "," . $this->color . "," . $this->price;
    }

    
// output as XML node
    
function toXML() {
        return
"<row>\n" .
        
"<vehicleid>$this->vehicleid</vehicleid>\n" .
        
"<year>$this->year</year>\n" .
        
"<make>$this->make</make>\n" .
        
"<model>$this->model</model>\n" .
        
"<color>$this->color</color>\n" .
        
"<price>$this->price</price>\n" .
        
"</row>\n";
    }

    
// read from an html form
    
function readForm() {
        
$this->vehicleid = $this->formHelper("vehicleid", 0);
        
$this->year = $this->formHelper("year", "");
        
$this->make = $this->formHelper("make", "");
        
$this->model = $this->formHelper("model", "");
        
$this->color = $this->formHelper("color", "");
        
$this->price = $this->formHelper("price", 0);
    }

    
// read from the query string
    
function readQuery() {
        
$this->vehicleid = $this->queryHelper("vehicleid", 0);
        
$this->year = $this->queryHelper("year", "");
        
$this->make = $this->queryHelper("make", "");
        
$this->model = $this->queryHelper("model", "");
        
$this->color = $this->queryHelper("color", "");
        
$this->price = $this->queryHelper("price", 0);
    }

    
// extra functions here
    
function getPK() {
        return
$this->vehicleid;
    }
}

// Vehicle Data Access Object
class VehicleDAO extends BaseDAO {
    var
$SQL_SELECT = "SELECT * FROM `vehicles` ";
    var
$SQL_COUNT = "SELECT count(*) AS cnt FROM `vehicles` ";
    
// insert with no value for an auto_increment field.
    
var $SQL_INSERT = "INSERT INTO `vehicles` (year,make,model,color,price) VALUES ('%A','%B','%C','%D',%E)";
    var
$SQL_UPDATE = "UPDATE `vehicles` SET ";
    var
$SQL_DELETE = "DELETE FROM `vehicles` WHERE vehicleid=%A";

    
// default constructor
    
function VehicleDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
        
// calls the parent constructor which
        // makes the database connection.
        
parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
    }

    
// returns a single VehicleVO obeject
    
function findByPK($vehicleid) {    
    
$this->sql = $this->SQL_SELECT . "WHERE (vehicleid='$vehicleid')";
        
$this->exec($this->sql);
        if(
$this->numRows() > 0 ) {
            
$row = $this->getObject();
            
$vo = new VehicleVO(
                
$row->vehicleid,
                
$row->year,
                
$row->make,
                
$row->model,
                
$row->color,
                
$row->price
            
);
            return
$vo;
        } else {
            return
null;    
        }
    }

    
// returns an array of VehicleVO objects
    // given a complete SQL query
    
function findBySQL($sql) {
        
$voList = array();
        
$this->sql = $sql;
        
$this->exec($this->sql);
        while(
$row = $this->getObject()) {
            
$vo = new VehicleVO(
                
$row->vehicleid,
                
$row->year,
                
$row->make,
                
$row->model,
                
$row->color,
                
$row->price
            
);
            
array_push($voList, $vo);
        }
        return
$voList;
    }

    
// returns an array of VehicleVO objects
    // value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
    // value for $order_by is a comma separated list of columns "company, name"
    
function findWhere($where = "", $orderby = "") {
        
$this->sql = $this->SQL_SELECT;
        
$voList = array();
        if(
strlen($where) > 0 ) {
            
$where = "WHERE (" . $where . ") ";
        }
        if(
strlen($orderby) > 0) {
            
$orderby = "ORDER BY " . $orderby;
        } else {
            
$orderby = "ORDER BY 1";
        }
        
$this->sql .= $where . " " . $orderby;
        
$this->exec($this->sql);
        while(
$row = $this->getObject()) {
            
$vo = new VehicleVO(
                
$row->vehicleid,
                
$row->year,
                
$row->make,
                
$row->model,
                
$row->color,
                
$row->price
            
);
            
array_push($voList, $vo);
        }
        return
$voList;
    }

    
// insert a record from a vo
    
function insertVO($vo) {
        
$this->sql = $this->SQL_INSERT;
        
$this->sql = str_replace("%A", $vo->year, $this->sql);
        
$this->sql = str_replace("%B", $vo->make, $this->sql);
        
$this->sql = str_replace("%C", $vo->model, $this->sql);
        
$this->sql = str_replace("%D", $vo->color, $this->sql);
        
$this->sql = str_replace("%E", $vo->price, $this->sql);

        
$this->exec($this->sql);
        return
$this->affectedRows();
    }

    
// update a record from a vo
    
function updateVO($vo) {
        
$this->sql = $this->SQL_UPDATE;
        
$this->sql .= "year = '" . $vo->year . "', ";
        
$this->sql .= "make = '" . $vo->make . "', ";
        
$this->sql .= "model = '" . $vo->model . "', ";
        
$this->sql .= "color = '" . $vo->color . "', ";
        
$this->sql .= "price = '" . $vo->price . "' ";

        
$this->sql .= "WHERE vehicleid=" . $vo->vehicleid;

        
$this->exec();
        return
$this->affectedRows();
    }

    
// delete a record from a vo
    
function deleteVO($vo) {
        
$this->sql = $this->SQL_DELETE;
        
$this->sql = str_replace("%A", $vo->vehicleid, $this->sql);
        
$this->exec($this->sql);
        return
$this->affectedRows();
    }

}