<?php
include('includes/application_top.php');
include(DIR_FS_CATALOG.'lang/'.$_SESSION['language'].'/admin/configuration.php');
include(DIR_WS_CLASSES.'configurations.php');

include(DIR_WS_CLASSES.'class.dbform.php');

function _get_input_field(){
      if ($cInfo->set_function) {
        eval('$value_field = ' . $cInfo->set_function . '"' . htmlspecialchars($cInfo->configuration_value) . '");');
      } else {
        $value_field = xtc_draw_input_field('configuration_value', $cInfo->configuration_value);
      }

}

$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';

$dbf = new dbform('configuration','configuration_id',$conn);

$dbf->fields['configuration_value']->set_title(ENTRY_VALUE,1);

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

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$configurations = new configurations;
	$configuration = $configurations->get_configuration($dbf->select_id);
	if(xtc_not_null($configuration->data->set_function)){
		$dbf->fields['configuration_value']->set_title(constant($configuration->data->configuration_key.'_TITLE'),2);
		if(xtc_not_null($configuration->data->use_function)){
			if(xtc_not_null($configuration->data->set_function)){
				$config_value_text = xtc_call_function($configuration->data->use_function, $configuration->data->configuration_value);
			}else{
				$config_value_text = $configuration->data->configuration_value;
			}
			$configuration_options = array(0=>array('id'=>$configuration->data->configuration_value,'text'=>$config_value_text));
		}else{
			$configuration_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
		}
		$dbf->fields['configuration_value']->set_options($configuration_options,$configuration->data->configuration_value);
		$dbf->fields['configuration_value']->set_required(1);
		$dbf->fields['configuration_value']->set_ajax_function('getAirportsByLetters');
	}
}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_CONFIGURATIONS,xtc_get_all_get_params(array('action'))),$access='configurations');
$smarty->assign('PAGE_MENU',$menu);
$heading_title_param = $configuration->data->name;

$result = $dbf->get_form($form_name = 'configurations',$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('system',sprintf(HISTORY_CONFIGURATION_UPDATED,$configuration->data->name,$_POST['configuration_value']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_CONFIGURATIONS,xtc_get_all_get_params(array('action'))));
			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('configurations',sprintf(HISTORY_CONFIGURATION_ADDED,$_POST['name']),$id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_CONFIGURATIONS));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
