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

if($_GET['action'] == 'overview'){
	$_GET['customers_id'] = $_GET['id'];
}

$customers_id = ($_GET['customers_id']) ? xtc_db_input($_GET['customers_id']) : xtc_db_input($_POST['customers_id']);
if(!xtc_not_null($customers_id)){
	$messageStack->add_session(MESSAGE_ERROR_NO_CUSTOMER_SELECTED,'error');
	xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS));
}

function _get_default_status($id){
	global $customers_id;
	$address_book_id = yes_get_customers_default_address_book_id($customers_id);
	if($address_book_id == $id)
		return YES;
	else
		return NO;
}

$address_books = new address_books();
switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$address_book = $address_books->get_address_book($id);
			$address_books->delete( $id );
			$customers = new customers;
			
			// testen ob default-address fuer kunden noch existiert
			$customers->update_default_address_book_entry($customers_id);
			
			$YES_HISTORY->add('customer',sprintf(HISTORY_ADDRESS_BOOK_DELETED,$address_book->getFullName()),$address_book->data->customers_id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_ADDRESS_BOOKS,'customers_id='.$address_book->data->customers_id));
		break;
	case 'set_default':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$address_book = $address_books->get_address_book($id);
			$customers = new customers;
			$customers->set_default_address_book_entry($customers_id,$id);
			
			$YES_HISTORY->add('customer',sprintf(HISTORY_DEFAULT_ADDRESS_BOOK_SAVED,$address_book->getFullName()),$address_book->data->customers_id);
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_UPDATE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_ADDRESS_BOOKS,'customers_id='.$address_book->data->customers_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 address_book_id,address_book_id as default_address,entry_gender,concat(entry_firstname,' ',entry_lastname) as name,entry_country_id,address_date_added FROM %s",$address_books->get_tablename() ));
$pager->whereConditions = array(
	"customers_id='$customers_id'"
);

$pager->set_additional_get_params(array("customers_id=".$customers_id));

$pager->index_field = $address_books->get_index();

$pager->get_row_selector = 'abID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_DEFAULT,TABLE_HEADING_GENDER,TABLE_HEADING_NAME,TABLE_HEADING_COUNTRY,TABLE_HEADING_ADDED);
$pager->gridHelper = array(
	'entry_country_id'=>'xtc_get_country_name',
	'default_address'=>'_get_default_status'
);

$pager->sortOrders = array(
	'address_book_id'=>TABLE_HEADING_ID,
	'entry_country_id'=>TABLE_HEADING_COUNTRY,
	'address_date_added'=>TABLE_HEADING_ADDED
);

$heading_title_param = xtc_get_editor_name($customers_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_BACK_TO_CUSTOMER,xtc_href_link(FILENAME_CUSTOMERS,'id='.$customers_id.'&action=overview'),'customers').'&nbsp;'.yes_draw_href_button(BUTTON_NEW_ADDRESS_BOOK,xtc_href_link(FILENAME_ADDRESS_BOOKS_EDIT,'customers_id='.$customers_id),'address_books_edit'));


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

include('includes/footer.php');
