<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import_ship.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import_ship_costs.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import_ship_collies.php');
/**
 * import_ships class 
 *
 */
class import_ships extends databasetable
{
	/**
	 * initiates the import_ships class
	 *
	 */
	function import_ships(){
		$this->set_tablename(TABLE_IMPORT_SHIPS);
		$this->set_index('import_ships_id');
		
		$this->related_tables = array(
			0=>array('table'=>TABLE_IMPORT_SHIP_COLLIES,'key'=>$this->get_index(),'class'=>'import_ship_collies' ),
			1=>array('table'=>TABLE_IMPORT_SHIP_COSTS,'key'=>$this->get_index(),'class'=>'import_ship_costs' )
		);
		
	}
	
	/**
	 * return a import_ship object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_import_ship($id){
		$values = $this->get_row_by_index($id);
		
		$costs = $this->get_costs($id);
		if(is_array($costs) and sizeOf($costs)>0)
			$values[0]['costs']=$costs;
			
		$collies = $this->get_collies($id);
		if(is_array($collies) and sizeOf($collies)>0)
			$values[0]['collies']=$collies;
		
		$import_ship_instance = new import_ship( (object) current($values) );
		return $import_ship_instance;
	}
	
	function get_costs($id){
		$costs = new import_ship_costs;
		$array = array();
		$query = $this->query(sprintf("SELECT import_ship_costs_id FROM %s WHERE import_ships_id='%s'",TABLE_IMPORT_SHIP_COSTS,$id));
		if($query){
			foreach($query as $record){
				$cost = $costs->get_import_ship_cost($record['import_ship_costs_id']);
				$array[] = $cost->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function get_collies($id){
		$ships = new import_ship_collies;
		$array = array();
		$query = $this->query(sprintf("SELECT import_ship_collies_id FROM %s WHERE import_ships_id='%s'",TABLE_IMPORT_SHIP_COLLIES,$id));
		if($query){
			foreach($query as $record){
				$ship = $ships->get_import_ship_collie($record['import_ship_collies_id']);
				$array[] = $ship->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function delete_collie($id,$collie_id){
		$query_raw = sprintf("DELETE FROM %s WHERE import_ships_id='%s' and import_ship_collies_id='%s'",TABLE_IMPORT_SHIP_COLLIES,$id,$collie_id);
		$this->query($query_raw);
	}
	
	function delete_costs($id,$costs_id){
		$query_raw = sprintf("DELETE FROM %s WHERE import_ships_id='%s' and import_ship_costs_id='%s'",TABLE_IMPORT_SHIP_COSTS,$id,$costs_id);
		$this->query($query_raw);
	}
	
	function update_status($id,$status_id){
		if($status_id == 1){
			$date_export = date('Y-m-d H:i:s');
		}else{
			$date_export = 0;
		}
		$this->query(sprintf("UPDATE %s SET status='%s',date_export='%s' WHERE %s='%s'",$this->get_tablename(),$status_id,$date_export,$this->get_index(),$id));
	}
}
