<?php
include('includes/application_top.php');
//include(DIR_WS_CLASSES.'export_target_collies.php');
//include(DIR_WS_CLASSES.'export_targets.php');
include(DIR_WS_CLASSES.'exports.php');

include(DIR_WS_CLASSES.'class.dbform.php');
$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';

$dbf = new dbform('export_target_collies','export_target_collies_id',$conn);

$dbf->fields['height']->set_title(ENTRY_HEIGHT,8);
$dbf->fields['height']->set_required(1,'int');

$dbf->fields['width']->set_title(ENTRY_WIDTH,10);
$dbf->fields['width']->set_required(1,'int');

$dbf->fields['depth']->set_title(ENTRY_DEPTH,12);
$dbf->fields['depth']->set_required(1,'int');

$dbf->fields['units_id']->set_title(ENTRY_UNITS,12);
$dbf->fields['units_id']->set_options(yes_get_units(),'');
$dbf->fields['units_id']->set_required(1,'int');

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$export_target_collies = new export_target_collies;
	$export_target_collie = $export_target_collies->get_export_target_collie($dbf->select_id);
	$etID = $export_target_collie->data->export_targets_id;
	$exports_id = $export_target_collie->data->exports_id;
	$ship_name = yes_get_ships_name(yes_get_ships_id_from_export_targets_id($export_target_collie->data->export_targets_id));
}else{
	$etID = ($_GET['etID']) ? xtc_db_input($_GET['etID']) : xtc_db_input($_POST['export_targets_id']);
	$ship_name = yes_get_ships_name(yes_get_ships_id_from_export_targets_id($etID));
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$dbf->add_hidden_field('export_targets_id',$etID);
	$export_targets = new export_targets;
	$export_target = $export_targets->get_export_target($etID);
	$exports_id = $export_target->data->exports_id; 
	$mode = 'add';
}

$exports = new exports;
$export = $exports->get_export($exports_id);
$available_import_ids = array();
foreach($export->data->targets as $kt=>$target){
	if($target['export_targets_id'] == $etID){
		if(!sizeOf($target['imports'])){
		    $messageStack->add_session(MSG_ERROR_NO_IMPORT,'error');
		    xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$exports_id));
		}
		foreach($target['imports'] as $import_item){
			$available_import_ids[] = $import_item['imports_id'];
		}
	}
}
$available_imports = array(0=>array('id'=>0,'text'=>TEXT_SELECT));
foreach($available_import_ids as $imports_id){
	$available_imports[] = array('id'=>$imports_id,'text'=>yes_get_import_number_from_imports_id($imports_id));
}

$dbf->fields['imports_id']->set_title(ENTRY_IMPORT,15);
$dbf->fields['imports_id']->set_options($available_imports,'');
$dbf->fields['imports_id']->set_required(1,'int');


$heading_title_param = $ship_name; // wird in der header.php eingesetzt

// POST DATA ABFANGEN BEIM SPEICHERN
$mode = ($_POST['action'] == 'edit' or $_POST['action'] == 'add') ? $_POST['action'] : $mode; 

$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$exports_id),$access='exports');
$smarty->assign('PAGE_MENU',$menu);

$result = $dbf->get_form($form_name = 'export_target_collies',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);

if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$sql_update_array = $dbf->get_sql_array($_POST);

			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_COLLIE_UPDATED,$ship_name),$exports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$exports_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_COLLIE_ADDED,$ship_name),$exports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$exports_id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
