<?PHP
	// definitions for InventoryItem class: item in inventory

class InventoryItem {
	var $is_on_db;
	var $status;
	var $id;
	var $file_name;
	var $directory_name;
	var $size;
	var $update_date;
	var $date_checked;
	var $is_verified;
	
	// constructor for InventoryItem
	function InventoryItem ($file_name = NULL, $directory_name = NULL) {
		$this->file_name = $file_name;
		$this->directory_name = $directory_name;
	}

	// use database row to set object's values
	function setValues ($row) {
		$this->is_on_db = true;
		$this->id = $row['id'];
		$this->file_name = $row['file_name'];
		$this->directory_name = $row['directory_name'];
		$this->size = $row['size'];
		$this->update_date = $row['update_date'];
		$this->date_checked = $row['date_checked'];
		$this->is_verified = $row['is_verified'];
	}

	// create json string (allows this object to be created in JavaScript)
	function toJson () {
		$json = '{"type":"InventoryItem"';
		$json = '}';
		return $json;
	}

	// uses id to retrieve from db
	function getById ($db_link) {

		$db_sql = "SELECT * FROM inventory_items WHERE id = " . $this->id;
		$result = $db_link->connection->query($db_sql);
		if ( $result ) {
			$row = $result->fetch_array();
			if ($row) {
				$this->setValues($row);
				
			} else {
				$this->status = "Row not found on inventory_items table with key: id=" . $this->id;
			}
		} else {
			$this->status = "MySql error: " . $db_link->connection->error;
		}
	}

	// store object on database, optionally refresh object from database (caters for database generated values)
	function store ($db_link, $refresh = true) {

		// check minimum info supplied
		if (empty($this->file_name) || empty($this->directory_name)) {
			$this->status = "No key information supplied: file_name and directory_name required";
			return; // skip out
		}
		$db_sql = "SELECT * FROM dir_items WHERE file_name = '" . $this->file_name . "' and directory_name = '" . $this->directory_name . "'";
        if($result = $db_link->connection->query($db_sql)){
            if($result->num_rows > 0){
     			$row = $result->fetch_array();
				$this->is_on_db = true;
				$this->id = $row['id'];
                // Free result set
                $result->free();
        		// timestamp
    			$setClause = "date_checked = NOW()";
    			//$setClause .= ",size = " . $this->size;
    			//$setClause .= ",update_date' = '" . $this->update_date ."'";
                //$setClause .= ",is_verified = '" . $row['is_verified'] ."'";    			
     			$db_sql = "UPDATE dir_items SET " . $setClause . " WHERE id=" . $this->id;
                if($result = $db_link->connection->query($db_sql)){
    				$this->status = "Updated OK!";
    				$this->getById($db_link); //refresh values not updated from interface
    			} else {
    				$this->status = "Update error: " . $db_sql . $db_link->connection->error;
    			}

            } else{

    			$columns = "date_checked";
    			$values = "NOW()";

    			$columns = ", file_name";
    			$values = ", '" . $this->file_name ."'";

    			$columns .= ", directory_name";
    			$values .= ", '" . $this->directory_name ."'";
    			
    			$columns .= ", size";
    			$values .= ", '" . $this->size ."'";
    			
    			$columns .= ", update_date";
    			$values .= ", '" . $this->update_date ."'";
    			
    			$db_sql = "INSERT INTO dir_items (" . $columns . ") VALUES(" . $values . ")";
    
                if($result = $db_link->connection->query($db_sql)){
    				$this->id = $db_link->connection->insert_id;
    				// reread from db to get all values (some can be populated on INSERT)
    				$this->getById($db_link);
    				$this->status = "New dir_item added!";
    			} else {
    				$this->status = "Insert error: " . $db_link->connection->error;
			    }
            }
        } else{
            echo "ERROR: Unable to update datebase. " . $db_link->connection->error;
        }
	}
}

?>
