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

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

$dbf = new dbform('customers','customers_id',$conn);

$dbf->fields['customers_status']->set_title(ENTRY_CUSTOMERS_STATUS,3);
$dbf->fields['customer_type']->set_title(ENTRY_CUSTOMER_TYPES,30);
$dbf->fields['customer_type']->set_options(yes_get_customer_types(),'');
$dbf->fields['customers_gender']->set_title(ENTRY_GENDER,4);
$dbf->fields['customers_firstname']->set_title(ENTRY_FIRST_NAME,5);
$dbf->fields['customers_firstname']->set_required($min_length=3,$validate_func = '');
$dbf->fields['customers_lastname']->set_title(ENTRY_LAST_NAME,6);
$dbf->fields['customers_lastname']->set_required($min_length=3,$validate_func = '');
$dbf->fields['customers_telephone']->set_title(ENTRY_TELEPHONE_NUMBER,8);
$dbf->fields['customers_fax']->set_title(ENTRY_FAX_NUMBER,9);
$dbf->fields['mobile']->set_title(ENTRY_MOBILE,10);
$dbf->fields['comments']->set_title(COMMENTS,12);
$dbf->fields['customers_email_address']->set_title(ENTRY_EMAIL_ADDRESS,2);
$dbf->fields['customers_email_address']->set_required($min_length=3,$validate_func = 'valid_email_address');

// SET SELECT FIELD OPTIONS
$cs_array = array();
$array = xtc_get_customers_statuses();
foreach($array as $k=>$v){
	$cs_array[] = array('id'=>$v['id'],'text'=>$v['text']);
}

$bool_array = array(0=>array('id'=>0,'text'=>NO),1=>array('id'=>1,'text'=>YES));
$gender_array = array(0=>array('id'=>'m','text'=>MALE),1=>array('id'=>'f','text'=>FEMALE));

$dbf->fields['customers_status']->set_options($cs_array,5);
$dbf->fields['customers_newsletter']->set_options($bool_array,0);
$dbf->fields['customers_gender']->set_options($gender_array,'m');

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

$customers = new customers;
if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$customer = $customers->get_customer($dbf->select_id);
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
}

$default_companies = array(0=>array('id'=>'','text'=>TEXT_SELECT));
$default_companies_id = '';
if($mode == 'add'){
	if($_GET['companies_id'] and $_GET['companies_id']>0){
		$default_companies = array(0=>array('id'=>$_GET['companies_id'],'text'=>yes_get_companies_name(xtc_db_input($_GET['companies_id']))));
		$default_companies_id = $_GET['companies_id'];
	}
	$options_size = sizeOf($default_companies);
}else{
	$default_companies_array = $customer->get_allocated_companies_array();
	$default_companies_id = array();
	if(sizeOf($default_companies_array)>0){
		$default_companies = array();
		for($i=0;$i<sizeOf($default_companies_array);$i++){
			$default_companies[] = array('id'=>$default_companies_array[$i],'text'=>yes_get_companies_name($default_companies_array[$i]));
			$default_companies_id = $default_companies_array[$i];
		}
	}
	$options_size = sizeOf($default_companies);
}
$dbf->add_field( 'companies','int(1)',$extern_table = true );
$dbf->fields['companies']->set_title(ENTRY_COMPANIES_ALLOCATION,11);
$dbf->fields['companies']->set_options($default_companies,$default_companies_id,$size=$options_size);
$dbf->fields['companies']->set_ajax_multiselect(1);
$dbf->fields['companies']->set_ajax_function('getCompaniesByLetters');

$menu = yes_draw_href_button(IMAGE_BACK,FILENAME_CUSTOMERS,$access='customers');
$smarty->assign('PAGE_MENU',$menu);

$result = $dbf->get_form($form_name = 'customers',$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['customers_last_modified'] = 'now()'; 
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");
			
			// SAVE THE CUSTOMER->COMPANIES ALLOCATION
			$customer->save_companies_allocation($_POST['companies']);
			
			$YES_HISTORY->add('customers',sprintf(HISTORY_CUSTOMER_UPDATED,$_POST['customers_firstname'].' '.$_POST['customers_lastname']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS,'action=overview&id='.$dbf->select_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['customers_date_added'] = 'now()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			$YES_HISTORY->add('customers',sprintf(HISTORY_CUSTOMER_ADDED,$_POST['customers_firstname'].' '.$_POST['customers_lastname']),$id);

			// SAVE THE CUSTOMER->COMPANIES ALLOCATION
			$customer = $customers->get_customer($id);
			$customer->save_companies_allocation($_POST['companies']);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_CUSTOMERS,'action=overview&id='.$id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
