<?php
class import_datasets extends myQuicty {

function pre_dispatch_index() {
	parent::pre_dispatch_index();
	if($_REQUEST['application_id']) {
		$this->application = $this->select_row('application','where id='.$_REQUEST['application_id']);
	} else {
		redirect('top');
	}
	//var_check($this->application);
	$this->page['base_url'] = quicty_base_url();
	$this->page['data_name'] = 'import_datasets';
	return true;
}

function file_upload() {
	$this->form = $this->new_data_obj('import_datasets');
	$this->form->bind_data_view('import_datasets.view.conf');
	$this->form->set_base_link_addr(quicty_base_url().'/import_datasets/upload.html');
	$this->form->set_restart_link_addr(quicty_base_url().'/import_datasets/upload.html');
	$this->form->validate_and_freeze();
	$this->form->set_button_label('insert','アップロード');
	$this->form->set_button_status('use_confirm',false);
	$this->form->bind_button_control();
	$this->form_insert_message = 'アップロードしました';
	//echo $this->QuictyStatus."\n";
	if($this->QuictyStatus=='INSERT') {
		$file = $this->form->move_upload_file2dir();
		//var_check($file);
		$fpath = pathinfo($file['path']);
		if($file['result'] and $fpath['extension']=='def') {
			require_once 'includes/import_app.class.php';
			$importer = new import_app;
			//echo "application_id=".$this->application['id']."<br>\n";
			$importer->init_field_define();
			$new_dsn['system'] = $this->application['system'];
			$new_dsn['dbname'] = $this->application['db_name'];
			$new_dsn['path'] = $this->application['path'];
			$new_dsn['host'] = $this->application['host'];
			$new_dsn['port'] = $this->application['port'];
			$new_dsn['user'] = $this->application['user'];
			$new_dsn['passowrd'] = $this->application['passowrd'];
			if($_POST['new_title']) {
				$new_dsn['title'] = $_POST['new_title'];
			}
			if($_POST['new_name']) {
				$new_dsn['dataname'] = $_POST['new_name'];
			}
			$datasets_ids = $importer->read_and_save_dataset($this->application['id'],$file['path'],$new_dsn);
			redirect('/info?id='.$this->application['id']);
		}
	}
	$result['message'] = $message;
	return $result;
}

//------------------------------------------------------------------

function dispatch_index() {
	$this->page['name'] = 'データセットのインポート';
	$result = $this->file_upload();
	$this->assign_form('input_form',$this->form);
	$this->page['message'] = nl2br($result['message']);
	$this->assign('page',$this->page);
	$this->display($this->current_template);
}


} // end of class import_datasets
?>