<?php
include(dirname(__FILE__) . '/lib/common_include.php');
//パラメータは
//$_FILES['CSV']
//db_id
//id
//として入ってくる
$id = null;
$tid = null;
$errReq = null;
if( array_key_exists('db_id' , $_POST ) ){
	$id = $_POST['db_id'];
	if( !is_string($id) ){
		$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	}
}else{
	$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
}
if( array_key_exists('id' , $_POST ) ){
	$tid = $_POST['id'];
	if( !is_string($id) ){
		$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	}
}else{
	$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
}

$file = null;
$tmpfile = null;
if( array_key_exists('CSV' , $_FILES ) ){
	$file = $_FILES['CSV'];
	if(
		 ! array_key_exists('name' , $file ) ||
		 ! array_key_exists('type' , $file ) ||
		 ! array_key_exists('size' , $file ) ||
		 ! array_key_exists('tmp_name' , $file ) ||
		 ! array_key_exists('error' , $file )
	){
		errLog(
			'PARAM SUB KEYS NOT FOUND' ."\r\n"
		);
		$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	};
	if( $file['error'] > 2 ){
		$errReq = CC\RES::RES_W;
		$errReq['error'] = 'Database import error';
		$errReq['discription'] = 'Failed to upload the file';

		errLog(
			'UPLOAD ERROR' ."\r\n"
		);
	};
	if( ! file_exists( $file['tmp_name'] ) ){
		$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		errLog(
			'TMP_FILE NOT FOUND' ."\r\n"
		);
	};
	$tmpfile = $file['tmp_name'];
}else{
	$errReq = CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	errLog(
		'PARAM db NOT FOUND' ."\r\n"
	);
}


if( $errReq == null){
	$res = null;
	$res = fileAccess::impCSV( $id, $tid , $tmpfile );
	C\res($res);
}else{
	C\res($errReq);
}

