<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'export_target.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'export_target_import_ships.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'export_target_collies.php');
/**
 * export_targets class 
 *
 */
class export_targets extends databasetable
{
	/**
	 * initiates the export_targets class
	 *
	 */
	function export_targets(){
		$this->set_tablename(TABLE_EXPORT_TARGETS);
		$this->set_index('export_targets_id');
		
		$this->related_tables = array(
			0=>array('table'=>TABLE_EXPORT_TARGET_COLLIES,'key'=>$this->get_index(),'class'=>'export_target_collies' ),
			1=>array('table'=>TABLE_EXPORT_TARGET_IMPORT_SHIPS,'key'=>$this->get_index(),'class'=>'export_target_import_ships' )
		);
		
	}
	
	/**
	 * return a export_target object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_export_target($id){
		$values = $this->get_row_by_index($id);
		
		$import_ships = $this->get_import_ships($id);
		if(is_array($import_ships) and sizeOf($import_ships)>0){
			$values[0]['imports'] = $import_ships;
		}
		
		$collies = $this->get_collies($id);
		if(is_array($collies) and sizeOf($collies)>0){
			$values[0]['collies'] = $collies;
		}
		
		$export_target_instance = new export_target( (object) current($values) );
		return $export_target_instance;
	}
	
	function get_import_ships($id){
		$array = array();
		$import_ships = new export_target_import_ships;
		$query = $this->query(sprintf("SELECT export_target_import_ships_id FROM %s WHERE export_targets_id='%s'",TABLE_EXPORT_TARGET_IMPORT_SHIPS,$id));
		if($query){
			foreach($query as $record){
				$import_ship = $import_ships->get_export_target_import_ship($record['export_target_import_ships_id']);
				$array[] = $import_ship->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function delete_import_ship($id,$import_ship_id){
		$this->query(sprintf("DELETE FROM %s WHERE %s='%s' 	and export_target_import_ships_id='%s' LIMIT 1",TABLE_EXPORT_TARGET_IMPORT_SHIPS,$this->get_index(),$id,$import_ship_id));
	}
	
	function get_collies($id){
		$array = array();
		$collies = new export_target_collies;
		$query = $this->query(sprintf("SELECT export_target_collies_id FROM %s WHERE export_targets_id='%s'",TABLE_EXPORT_TARGET_COLLIES,$id));
		if($query){
			foreach($query as $record){
				$collie = $collies->get_export_target_collie($record['export_target_collies_id']);
				$array[] = $collie->get_smarty_data_array();
			}
		}
		return $array;
	}

	function delete_collie($id,$collie_id){
		$this->query(sprintf("DELETE FROM %s WHERE %s='%s' and export_target_collies_id='%s' LIMIT 1",TABLE_EXPORT_TARGET_COLLIES,$this->get_index(),$id,$collie_id));
	}
	
	function get_available_targets_array($id){
		$array = array();
		$array[] = array('id'=>'','text'=>TEXT_SELECT);
		$query_raw = sprintf("SELECT s.ships_id FROM %s s LEFT JOIN %s et ON et.ships_id=s.ships_id AND et.exports_id=%s WHERE et.ships_id IS NULL",TABLE_SHIPS,TABLE_EXPORT_TARGETS,$id);
		$query = $this->query($query_raw);
		if($query){
			foreach($query as $record){
				$array[] = array('id'=>$record['ships_id'],'text'=>yes_get_ships_name($record['ships_id']));
			}		
		}
		return $array;
	}

	function get_available_import_ships_array($id){
		$array = array();
		$array[] = array('id'=>'','text'=>TEXT_SELECT);
		$ships_id = yes_get_ships_id_from_export_targets_id($id);
		
		$query_raw = sprintf("SELECT ish.import_ships_id,i.import_number FROM %s i LEFT JOIN %s ish USING(imports_id) WHERE i.status='0' AND ish.orderer_ships_id='%s' and ish.status='0'",TABLE_IMPORTS,TABLE_IMPORT_SHIPS,$ships_id);
		$query = $this->query($query_raw);
		if($query){
			foreach($query as $record){
				$array[] = array('id'=>$record['import_ships_id'],'text'=>$record['import_number']);
			}		
		}
		return $array;
	}
}
