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

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

$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));
}


$dbf = new dbform('address_book','address_book_id',$conn);
$gender_array = array(0=>array('id'=>'m','text'=>MALE),1=>array('id'=>'f','text'=>FEMALE));
$bool_array = array(0=>array('id'=>'0','text'=>NO),1=>array('id'=>'1','text'=>YES));

$dbf->fields['entry_gender']->set_title(ENTRY_GENDER,1);
$dbf->fields['entry_gender']->set_options($gender_array,'m');
$dbf->fields['entry_firstname']->set_title(ENTRY_FIRST_NAME,2);
$dbf->fields['entry_firstname']->set_required($min_length=3,$validate_func = '');
$dbf->fields['entry_lastname']->set_title(ENTRY_LAST_NAME,4);
$dbf->fields['entry_lastname']->set_required($min_length=3,$validate_func = '');
$dbf->fields['entry_street_address']->set_title(ENTRY_STREET_ADDRESS,8);
$dbf->fields['entry_suburb']->set_title(ENTRY_SUBURB,10);
$dbf->fields['entry_postcode']->set_title(ENTRY_POST_CODE,12);
$dbf->fields['entry_city']->set_title(ENTRY_CITY,14);
$dbf->fields['entry_country_id']->set_title(ENTRY_COUNTRY,16);
$dbf->fields['entry_country_id']->set_options(xtc_get_countries(),STORE_COUNTRIES_ID);

$dbf->add_hidden_field('customers_id',$customers_id);

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);


if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
}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_ADDRESS_BOOKS,'customers_id='.$customers_id),$access='address_books');
$smarty->assign('PAGE_MENU',$menu);

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

			$YES_HISTORY->add('customers',sprintf(HISTORY_ADDRESS_BOOK_UPDATED,$_POST['entry_firstname'].' '.$_POST['entry_lastname']),$customers_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_ADDRESS_BOOKS,'customers_id='.$customers_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['address_date_added'] = 'now()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('customers',sprintf(HISTORY_ADDRESS_BOOK_ADDED, $_POST['entry_firstname'].' '.$_POST['entry_lastname']),$customers_id);

			$customers = new customers;
			$count = $customers->count_address_books($customers_id);
			if($count == 1){
				$customers->set_default_address_book_entry($customers_id,$id);
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_ADDRESS_BOOKS,'customers_id='.$customers_id));
			break;
	}
}
$heading_title_param = xtc_get_editor_name($customers_id);
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
