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

$harbours = new harbours();
switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$harbour = $harbours->get_harbour($id);
			$harbours->delete( $id );
			$YES_HISTORY->add('system',sprintf(HISTORY_HARBOUR_DELETED,$harbour->data->name));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_HARBOURS));
		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 harbours_id,code,name,airport_code,countries_id,date_added FROM %s",$harbours->get_tablename() ));
$pager->index_field = $harbours->get_index();

$pager->get_row_selector = 'cID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_CODE,TABLE_HEADING_NAME,TABLE_HEADING_AIRPORT_CODE,TABLE_HEADING_COUNTRY,TABLE_HEADING_ADDED);
$pager->gridHelper = array(
	'countries_id'=>'xtc_get_country_name'
);
$pager->sortOrders = array(
	'harbours_id'=>TABLE_HEADING_ID,
	'name'=>TABLE_HEADING_NAME,
	'code'=>TABLE_HEADING_CODE,
	'date_added'=>TABLE_HEADING_ADDED
);

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_HARBOUR,xtc_href_link(FILENAME_HARBOURS_EDIT),'harbours_edit'));


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

include('includes/footer.php');
