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

$ship_names = new ship_names();
switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$ship_name = $ship_names->get_ship_name($id);
			$ship_names->delete( $id );
			$YES_HISTORY->add('ships',sprintf(HISTORY_SHIP_NAME_DELETED,$ship_name->data->name),$ship_name->data->ships_id);
			// testen ob default-address fuer kunden noch existiert
			$ship_names->update_default_name($ship_name->data->ships_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_SHIP_NAMES,'sID='.$ship_name->data->ships_id));
		break;
}

$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';


include(DIR_WS_CLASSES.'class.adodb-pager.php');

$pager = new ADODB_Pager($conn,sprintf("SELECT ship_names_id,name,is_default,is_baptismal_name,date_added FROM %s",$ship_names->get_tablename() ));
$pager->whereConditions = array(
	sprintf("ships_id='%s'",xtc_db_input($_GET['sID']))
);
$pager->orderbyDefault = array(
	'name'
);
$pager->set_additional_get_params(array('sID='.$_GET['sID']));
$pager->index_field = $ship_names->get_index();

$pager->get_row_selector = 'ID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_NAME,TABLE_HEADING_DEFAULT,TABLE_HEADING_BAPTISMAL_NAME,TABLE_HEADING_ADDED);
$pager->sortOrders = array(
	'ship_names_id'=>TABLE_HEADING_ID,
	'name'=>TABLE_HEADING_NAME,
	'date_added'=>TABLE_HEADING_ADDED
);

$owner_id = yes_get_ship_companies_id_by_role(xtc_db_input($_GET['sID']),'owner');
$heading_title_param = yes_get_companies_name($owner_id);


include(DIR_WS_INCLUDES.'header.php');

$pager->Render($rows_per_page=MAX_DISPLAY_SEARCH_RESULTS_ADMIN);

$selected_item = $pager->selected_row;

$grid_smarty = $smarty;
$smarty->assign('pager_tab_file',$pager->pager_tab_file);
$smarty->assign('HEADER',$pager->getHeader());
$smarty->assign('SORTING',$pager->getSortingInput());

$grid_smarty->assign('grid',$pager->getGrid());
$grid = $grid_smarty->fetch(DIR_FS_ADMIN.'templates/pager_grid.tpl');

$smarty->assign('GRID',$grid);
$smarty->assign('FOOTER',$pager->getFooter());


$smarty->assign('NEW_ENTRY_BUTTON',yes_draw_href_button(BUTTON_NEW_SHIP_NAME,xtc_href_link(FILENAME_SHIP_NAMES_EDIT,'ships_id='.$_GET['sID']),'ship_names_edit'));


echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');

echo '&nbsp;'.yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_SHIPS,'id='.$_GET['sID']),'ships');

include('includes/footer.php');
