<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'ship_names.php');

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

$ship_names = new ship_names();

$dbf = new dbform('ship_names','ship_names_id',$conn);

$ships_id = ($_GET['ships_id']) ? $_GET['ships_id'] : $_POST['ships_id']; 
$dbf->add_hidden_field('ships_id',$ships_id);
$dbf->fields['name']->set_title(ENTRY_NAME,0);
$dbf->fields['name']->set_required($min_length=3,$validate_func = '');
$dbf->fields['is_default']->set_title(TEXT_DEFAULT,2);
$dbf->fields['is_default']->set_options(array(0=>array('id'=>'0','text'=>NO),1=>array('id'=>'1','text'=>YES)),false);
$dbf->fields['is_baptismal_name']->set_title(TEXT_BAPTISMAL_NAME,2);
$dbf->fields['is_baptismal_name']->set_options(array(0=>array('id'=>'0','text'=>NO),1=>array('id'=>'1','text'=>YES)),false);

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);
$owner_id = yes_get_ship_companies_id_by_role($ships_id,'owner');
$heading_title_param = yes_get_companies_name($owner_id);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$ship_name = $ship_names->get_ship_name($dbf->select_id);
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
}

// 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_SHIP_NAMES,'sID='.$ships_id),$access='ship_names');
$smarty->assign('PAGE_MENU',$menu);

$result = $dbf->get_form($form_name = 'ship_names',$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);
			unset($sql_update_array['date_added']);
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			// IS_DEFAULT GESETZT -> DEAKTIVIERE ALLE ANDEREN
			if($_POST['is_default'] == 1){
				$ship_names->set_default_name($dbf->select_id,$ships_id);
			}
			// UEBERPRUEFT OB MINDESTENS EIN EINTRAG AUF IS_DEFAULT IST
			$ship_names->update_default_name($ships_id);

			// IS_BAPTISM_NAME GESETZT -> DEAKTIVIERE ALLE ANDEREN
			if($_POST['is_baptismal_name'] == 1){
				$ship_names->set_baptismal_name($dbf->select_id,$ships_id);
			}
			// UEBERPRUEFT OB MINDESTENS EIN EINTRAG AUF IS_DEFAULT IST
			$ship_names->update_baptismal_name($ships_id);

			$YES_HISTORY->add('ships',sprintf(HISTORY_SHIP_NAME_UPDATED,$_POST['name']),$ships_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_SHIP_NAMES,'sID='.$ships_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['date_added'] = 'now()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			// IS_DEFAULT GESETZT -> DEAKTIVIERE ALLE ANDEREN
			if($_POST['is_default'] == 1){
				$ship_names->set_default_name($id,$ships_id);
			}
			// UEBERPRUEFT OB MINDESTENS EIN EINTRAG AUF IS_DEFAULT IST
			$ship_names->update_default_name($ships_id);
			
			// IS_BAPTISM_NAME GESETZT -> DEAKTIVIERE ALLE ANDEREN
			if($_POST['is_baptismal_name'] == 1){
				$ship_names->set_baptismal_name($id,$ships_id);
			}
			// UEBERPRUEFT OB MINDESTENS EIN EINTRAG AUF IS_DEFAULT IST
			$ship_names->update_baptismal_name($ships_id);

			$YES_HISTORY->add('ships',sprintf(HISTORY_SHIP_NAME_ADDED,$_POST['name']),$ships_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_SHIP_NAMES,'sID='.$ships_id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
