<?php
include(dirname(__FILE__) . "/lib/common_include.php");
//パラメータの整形
//パラメータは
//db_id
//table_id
//col_table[]
//col_relats[]
//column_id[]
//Not[]
//Mode[]
//Criteria[][]
//として入ってくるので、これを変形する。
//$id(db_id)
//$tid(table_id)
//$params=[
//	table=>col_table,
//	rlts=>col_relats,
//	col=>column_id,
//	Not=>Not,
//	Mode=>Mode,
//	Criteria=>Criteria[],
//]
//の形にする。
function main(){
	//基本パラメータのチェック
	$id = null;
	$table_id = null;

	if( array_key_exists('db_id' , $_POST ) ){
		$id = $_POST['db_id'];
		if( !is_string($id) ){
			errLog('Post["db_id"] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}else{
		errLog('Post["db_id"] is not Found');
		return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	}
	if( array_key_exists('table_id' , $_POST ) ){
		$table_id = $_POST['table_id'];
		if( !is_string($table_id) ){
			errLog('Post["table_id"] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}else{
		errLog('Post["table_id"] is not Found');
		return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
	}

	//列定義パラメータのチェック
	$aryTable =[];
	$aryRlts =[];
	$aryCol =[];
	$aryNot =[];
	$aryMode =[];
	$aryCriteria =[];

	if( array_key_exists('col_table' , $_POST ) ){
		$aryTable = $_POST[ 'col_table' ];
		if( !is_array($aryTable) ){
			errLog('Post["col_table"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('col_relats' , $_POST ) ){
		$aryRlts = $_POST[ 'col_relats' ];
		if( !is_array($aryRlts) ){
			errLog('Post["col_relats"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('column_id' , $_POST ) ){
		$aryCol = $_POST[ 'column_id' ];
		if( !is_array($aryCol) ){
			errLog('Post["column_id"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('Not' , $_POST ) ){
		$aryNot = $_POST[ 'Not' ];
		if( !is_array($aryNot) ){
			errLog('Post["Not"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('Mode' , $_POST ) ){
		$aryMode = $_POST[ 'Mode' ];
		if( !is_array($aryMode) ){
			errLog('Post["Mode"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('Criteria' , $_POST ) ){
		$aryCriteria = $_POST[ 'Criteria' ];
		if( !is_array($aryCriteria) ){
			errLog('Post["Criteria"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}

	$params = [];
	foreach( $aryTable as $idx => $tid ){
		if( !is_string($tid) ){
			errLog('Post["col_table"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $tid == null || $tid == '' ){
			errLog('Post["col_table"]['.$idx.'] is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		$params[ $idx ] = [
			'table' => $tid ,
			"rlts"=>null,
			"col"=>null,
			"Not"=>false,
			"Mode"=>null,
			'Criteria'=>[],
		];
	}
	foreach( $aryRlts as $idx => $rlts ){
		if( !is_string($rlts) && $rlts !== null ){
			errLog('Post["col_relats"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $rlts == null ){
			$rlts = '';
		}
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["col_relats"]['.$idx.'] is set.but else is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		$params[ $idx ]['rlts'] = $rlts;
	}
	foreach( $aryCol as $idx => $col ){
		if( !is_string($col) ){
			errLog('Post["column_id"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $col == null || $col == '' ){
			errLog('Post["column_id"]['.$idx.'] is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["column_id"]['.$idx.'] is set.but else is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		$params[ $idx ]['col'] = $col;
	}
	foreach( $aryNot as $idx => $Not ){
		if( !is_string($Not) && $Not !== null ){
			errLog('Post["Not"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["Not"]['.$idx.'] is set.but else is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		if( strtolower($Not) != 'false' && $Not != '' && strtolower($Not) != 'null' && strtolower($Not) != 'undefined' ){
			$params[ $idx ]['Not'] = true;
		};
	}
	foreach( $aryMode as $idx => $mode ){
		if( !is_string($mode) ){
			errLog('Post["Mode"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $mode == null || $mode == '' ){
			errLog('Post["Mode"]['.$idx.'] is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["Mode"]['.$idx.'] is set.but else is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		$params[ $idx ]['Mode'] = $mode;
	}
	foreach( $aryCriteria as $idx => $criteria ){
		if( !is_array($criteria) && $criteria !== null ){
			errLog('Post["Criteria"]['.$idx.'] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $criteria == null ){
			$criteria = [];
		}
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["Criteria"]['.$idx.'] is set.but else is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		foreach( $criteria as $idx2 => $strCriteria){
			if( !is_string($strCriteria) && $strCriteria !== null ){
				errLog('Post["Criteria"]['.$idx.']['.$idx2.'] is not string');
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			};
			if( $strCriteria == null ){
				$strCriteria = '';
			}
			$params[ $idx ]['Criteria'][] = $strCriteria;
		}
	}
	$db = new usrDb($id);
	if( ! $db->isEnable() ){
		errLog(
			'DB INITIALIZE ERROR'
		);
		return $db->error;
	}else{
		return $db->getJoinTable($table_id,$params);
	};
}

C\res( main() );


