<?php
include(dirname(__FILE__) . "/lib/common_include.php");
//パラメータの整形
//パラメータは
//db_id
//relation_id[]
//table2_id[]
//relation_col_id[][]
//table2_columns[][]
//table1_id[]
//table1_columns[][]
//として入ってくるので、これを変形する。
//[
//	"id"=>relation_id[i],
//	"pTable"=>table2_id[i],
//	"cTable"=>table1_id[i],
//	"cols"=>[
//		"id"=>relation_col_id[i][j],
//		"uId"=>relation_id[i],
//		"pCol"=>table2_columns[i][j],
//		"cCol"=>table1_columns[i][j],
//	],
//]
//の形にする。
function main(){

	$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'];
	}



	$aryRIds =[];
	$aryRCIds =[];
	$aryPTables =[];
	$aryCTables =[];
	$aryPCols =[];
	$aryCCols =[];

	if( array_key_exists('relation_id' , $_POST ) ){
		$aryRIds = $_POST[ 'relation_id' ];
		if( !is_array($aryRIds) ){
			errLog('Post["relation_id"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('relation_col_id' , $_POST ) ){
		$aryRCIds = $_POST[ 'relation_col_id' ];
		if( !is_array($aryRCIds) ){
			errLog('Post["relation_col_id"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('table2_id' , $_POST ) ){
		$aryPTables = $_POST[ 'table2_id' ];
		if( !is_array($aryPTables) ){
			errLog('Post["table2_id"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('table1_id' , $_POST ) ){
		$aryCTables = $_POST[ 'table1_id' ];
		if( !is_array($aryCTables) ){
			errLog('Post["table1_id"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('table2_columns' , $_POST ) ){
		$aryPCols = $_POST[ 'table2_columns' ];
		if( !is_array($aryPCols) ){
			errLog('Post["table2_columns"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	if( array_key_exists('table1_columns' , $_POST ) ){
		$aryCCols = $_POST[ 'table1_columns' ];
		if( !is_array($aryCCols) ){
			errLog('Post["table1_columns"] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
	}
	$params = [];

	foreach( $aryRIds as $idx => $rid ){
		if( !is_string($rid) && $rid !== null ){
			errLog('Post["relation_id"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $rid == '' ){
			$rid = null;
		}
		$params[ $idx ] = [
			'id' => $rid ,
			"pTable"=>null,
			"cTable"=>null,
			'cols'=>[],
		];
	}
	foreach( $aryPTables as $idx => $pTable ){
		if( !is_string($pTable) && $pTable !== null ){
			errLog('Post["table2_id"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $pTable == '' ){
			$pTable = null;
		}
		if( ! array_key_exists($idx , $params ) ){
			$params[ $idx ] = [
				'id' => null ,
				"pTable"=>null,
				"cTable"=>null,
				'cols'=>[],
			];
		};
		$params[ $idx ]['pTable'] = $pTable;
	}
	foreach( $aryCTables as $idx => $cTable ){
		if( !is_string($cTable) && $cTable !== null ){
			errLog('Post["table1_id"]['.$idx.'] is not string');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		};
		if( $cTable == '' ){
			$cTable = null;
		}
		if( ! array_key_exists($idx , $params ) ){
			$params[ $idx ] = [
				'id' => null ,
				"pTable"=>null,
				"cTable"=>null,
				'cols'=>[],
			];
		};
		$params[ $idx ]['cTable'] = $cTable;
	}
	foreach( $aryRCIds as $idx => $rcids ){
		if( !is_array($rcids) ){
			errLog('Post["relation_col_id"]['.$idx.'] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["relation_col_id"]['.$idx.'] is set , but other is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		foreach( $rcids as $rcidx => $rcid ){
			if( !is_string($rcid) && $rcid !== null ){
				errLog('Post["relation_col_id"]['.$idx.']['.$rcidx.'] is not string');
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			};
			if( $rcid == '' ){
				$rcid = null;
			}
			$params[ $idx ]['cols'][ $rcidx ] = [
				"id"=>$rcid,
				"rId"=>$params[ $idx ]['id'],
				"pCol"=>null,
				"cCol"=>null,
			];
		}
	}
	foreach( $aryPCols as $idx => $pcols ){
		if( !is_array($pcols) ){
			errLog('Post["table2_columns"]['.$idx.'] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["table2_columns"]['.$idx.'] is set , but other is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		foreach( $pcols as $rcidx => $pcol ){
			if( !is_string($pcol) && $pcol !== null ){
				errLog('Post["table2_columns"]['.$idx.']['.$rcidx.'] is not string');
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			};
			if( $pcol == '' ){
				$pcol = null;
			}
			if( ! array_key_exists( $rcidx , $params[ $idx ]['cols'] ) ){
				$params[ $idx ]['cols'][ $rcidx ] = [
					"id"=>null,
					"rId"=>$params[ $idx ]['id'],
					"pCol"=>null,
					"cCol"=>null,
				];
			}
			$params[ $idx ]['cols'][ $rcidx ]['pCol'] = $pcol;
		}
	}

	foreach( $aryCCols as $idx => $ccols ){
		if( !is_array($ccols) ){
			errLog('Post["table1_columns"]['.$idx.'] is not array');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		if( ! array_key_exists( $idx , $params) ){
			errLog('Post["table1_columns"]['.$idx.'] is set , but other is nothing');
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		foreach( $ccols as $rcidx => $ccol ){
			if( !is_string($ccol) && $ccol !== null ){
				errLog('Post["table2_columns"]['.$idx.']['.$rcidx.'] is not string');
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			};
			if( $ccol == '' ){
				$ccol = null;
			}
			if( ! array_key_exists( $rcidx , $params[ $idx ]['cols'] ) ){
				errLog('Post["table1_columns"]['.$idx.']['.$rcidx.'] is set , but other is nothing');
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			}
			$params[ $idx ]['cols'][ $rcidx ]['cCol'] = $ccol;
		}
	}


	//ここから、更にチェックを行う
	//pTable,cTableなしは不正
	//colsが空のものは不正
	//pCol,cColなしは不正

	foreach( $params as $param ){
		if( $param['pTable'] == null ||  $param['cTable'] == null  ){
			errLog(
				'relation table is nothing'."\t".
				'param:'.json_encode($param)."\r\n"
			);
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		if( count( $param['cols'] ) == 0 ){
			errLog(
				'relation column is nothing'."\t".
				'param:'.json_encode($param)."\r\n"
			);
			return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
		}
		foreach( $param['cols'] as $colParam ){
			if( $colParam['pCol'] == null ||  $colParam['cCol'] == null  ){
				errLog(
					'relation column is nothing'."\t".
					'param:'.json_encode($param)."\r\n"
				);
				return CC\RES::RES_SYSERRORS['ILLEGAL_PARAM'];
			}
		}
	}

	$db = new usrDb($id);
	if( ! $db->isEnable() ){
		errLog(
			'DB INITIALIZE ERROR'
		);
		return $db->error;
	}else{
		return $db->chgSysRlts( $params );
	};

}



C\res( main() );
