<?php
include('includes/application_top.php');
require_once(DIR_WS_CLASSES.'import_ship_collies.php');
require_once(DIR_WS_CLASSES.'import_ships.php');

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

$dbf = new dbform('import_ship_collies','import_ship_collies_id',$conn);

$dbf->fields['quantity']->set_title(ENTRY_QUANTITY,0);
$dbf->fields['quantity']->set_required(1,'int');
$dbf->fields['quantity']->set_default_value(1);

$dbf->fields['format']->set_title(ENTRY_COLLIE_FORMAT,1);
$dbf->fields['format']->set_options(yes_get_collie_formats(),'normal');

$dbf->fields['format_text']->set_title(ENTRY_COLLIE_FORMAT_TEXT,2);
$dbf->fields['format_text']->set_comments(ENTRY_COLLIE_FORMAT_TEXT_DESC);

$dbf->fields['weight']->set_title(ENTRY_WEIGHT,3);

$dbf->fields['collie_content']->set_title(ENTRY_CONTENT,6);
$dbf->fields['collie_content']->set_required(3);

$dbf->fields['duane_status_id']->set_title(ENTRY_DUANE_STATUS,7);
$dbf->fields['duane_status_id']->set_options(yes_get_duane_statuses(),'');
$dbf->fields['duane_status_id']->set_show_field_relations(array('duane_reminder_date'),array('show'=>array('3')));

$dbf->fields['duane_reminder_date']->set_title(ENTRY_DUANE_STATUS_REMINDER,8);

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

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

$dbf->fields['depth']->set_title(ENTRY_DEPTH,12);
$dbf->fields['depth']->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';
	$import_ship_collies = new import_ship_collies;
	$import_ship_collie = $import_ship_collies->get_import_ship_collie($dbf->select_id);
	$ships_id = $import_ship_collie->get_ships_id();
	$imports_id = $import_ship_collie->get_imports_id();
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
	$imports_id = ($_GET['iID']) ? xtc_db_input($_GET['iID']) : xtc_db_input($_POST['imports_id']); 
	$import_ships_id = ($_GET['isID']) ? xtc_db_input($_GET['isID']) : xtc_db_input($_POST['import_ships_id']);
	$import_ships = new import_ships;
	$import_ship = $import_ships->get_import_ship($import_ships_id);
	$dbf->add_hidden_field('import_ships_id',$import_ships_id);
	$ships_id = $import_ship->data->orderer_ships_id;
	$dbf->add_hidden_field('imports_id',$imports_id,$extern_table = true);
}

// 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_IMPORTS,'id='.$imports_id.'&action=overview'),$access='imports');
$smarty->assign('PAGE_MENU',$menu);
$heading_title_param = yes_get_ships_name($ships_id);

$result = $dbf->get_form($form_name = 'import_ship_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);
			if($_POST['duane_status_id'] != 3){
				$sql_update_array['duane_reminder_date'] = '';
			}
			
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_COLLIE_UPDATED,$_POST['collie_content'],yes_get_ships_name($ships_id)),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'id='.$imports_id.'&action=overview'));
			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('imports',sprintf(HISTORY_IMPORT_SHIP_COLLIE_ADDED,$_POST['collie_content'],yes_get_ships_name($ships_id)),$imports_id);
						
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'id='.$imports_id.'&action=overview'));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
