<?php
include('includes/application_top.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('companies','companies_id',$conn);
// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$companies = new companies;
	$company = $companies->get_company($dbf->select_id);
	$countries_id = $company->data->countries_id;
	$default_airports_id = $company->data->default_airports_id;
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$countries_id = STORE_COUNTRIES_ID;
	$mode = 'add';
	$default_airports_id = '';
}

$dbf->fields['name']->set_title(ENTRY_COMPANY,1);
$dbf->fields['name']->set_required($min_length=3,$validate_func = '');
$dbf->fields['name']->set_group_id('main');
$dbf->fields['companies_cid']->set_title(ENTRY_CID,2);
$dbf->fields['companies_cid']->set_group_id('main');
$dbf->fields['street_address']->set_title(ENTRY_STREET_ADDRESS,5);
$dbf->fields['street_address']->set_group_id('main');
$dbf->fields['suburb']->set_title(ENTRY_SUBURB,10);
$dbf->fields['suburb']->set_group_id('main');
$dbf->fields['postcode']->set_title(ENTRY_POST_CODE,15);
$dbf->fields['postcode']->set_group_id('main');
$dbf->fields['city']->set_title(ENTRY_CITY,20);
$dbf->fields['city']->set_group_id('main');
$dbf->fields['countries_id']->set_title(ENTRY_COUNTRY,25);
$dbf->fields['countries_id']->set_options(xtc_get_countries(),$countries_id);
$dbf->fields['countries_id']->set_group_id('main');
$dbf->fields['company_type']->set_title(ENTRY_COMPANY_TYPES,30);
$dbf->fields['company_type']->set_options(yes_get_company_types(),'');
$dbf->fields['company_type']->set_group_id('main');
$dbf->fields['homepage']->set_title(ENTRY_HOMEPAGE,35);
$dbf->fields['homepage']->set_group_id('main');
$dbf->fields['email_address']->set_title(ENTRY_EMAIL_ADDRESS,36);
$dbf->fields['email_address']->set_group_id('main');
$dbf->fields['telephone']->set_title(ENTRY_TELEPHONE_NUMBER,40);
$dbf->fields['telephone']->set_group_id('main');
$dbf->fields['telefax']->set_title(ENTRY_FAX_NUMBER,45);
$dbf->fields['telefax']->set_group_id('main');
$dbf->fields['vat_id']->set_title(ENTRY_VAT_ID,50);
$dbf->fields['vat_id']->set_group_id('main');
$dbf->fields['company_code']->set_title(ENTRY_COMPANY_CODE,2);
$dbf->fields['company_code']->set_group_id('main');


if(xtc_not_null($dbf->select_id)){
	$default_airport_options = array(0=>array('id'=>$default_airports_id,'text'=>yes_get_location_name($default_airports_id)));
}else{
	$default_airport_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}
$dbf->fields['default_airports_id']->set_title(ENTRY_DEFAULT_AIRPORT,55);
$dbf->fields['default_airports_id']->set_options($default_airport_options,$default_airports_id);
$dbf->fields['default_airports_id']->set_ajax_function('getAirportsByLetters');
$dbf->fields['default_airports_id']->set_group_id('main');

$dbf->fields['comments']->set_title(COMMENTS,55);
$dbf->fields['comments']->set_group_id('main');


// 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_COMPANIES),$access='companies');
$smarty->assign('PAGE_MENU',$menu);

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

			$YES_HISTORY->add('companies',sprintf(HISTORY_COMPANY_UPDATED,$_POST['name']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_COMPANIES,'action=overview&id='.$dbf->select_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['date_added'] = 'now()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('companies',sprintf(HISTORY_COMPANY_ADDED,$_POST['name']),$id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_COMPANIES,'action=overview&id='.$id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
