<?php require_once("../../classes/DBConnection.php"); $DB = new DBConnection();

$source_t		 		= trim($_POST['source_t']);
$source_t_match_cols	= explode(',',trim($_POST['source_t_match_cols']));
$source_t_pick_cols		= explode(',',trim($_POST['source_t_pick_cols']));

$destination_t			= trim($_POST['destination_t']);
$destination_t_match_cols	= explode(',',trim($_POST['destination_t_match_cols']));
$destination_t_insert_cols	= explode(',',trim($_POST['destination_t_insert_cols']));

//Clean up strings
$matches_1 = array();
$matches_1_val = array();
foreach($source_t_match_cols as $m){
	$m = trim($m); if($m=="") continue;
	$matches_1[] = $m;
}

$matches_2 = array();
$matches_2_val = array();
foreach($destination_t_match_cols as $m){
	if(is_array($m)){ if(sizeof($m)<1) continue; }else{ $m = trim($m); if($m=="") continue; }
	$matches_2[] = $m;
}

$picks = array();
$picks_val = array();
foreach($source_t_pick_cols as $r){
	$r = trim($r); if($r=="") continue;
	$picks[] = $r;
}

$inserts = array();
$inserts_val = array();
foreach($destination_t_insert_cols as $r){
	$r = trim($r); if($r=="") continue;
	$inserts[] = $r;
}

$lenomatches_1 = sizeof($matches_1);
$lenomatches_2 = sizeof($matches_2);

$lenopicks = sizeof($picks);
$lenoinserts = sizeof($inserts);

$id = "";

if($lenomatches_1==$lenomatches_2){
	$no = 0;
	echo "<br />'INSERT WHERE UNAVAILABLE' Started";
	$res = $DB->execute("select id,".toCSL_rmvDup($matches_1,$picks)." from `{$source_t}` order by id ASC");
	if(!is_null($res)){
		$res = $res[0];
		while($W = $DB->nextRow($res)){
			$id = $W['id'];
			$no++;
			for($i=0; $i<sizeof($matches_1); $i++){ $matches_1_val[$i] = trim($W[$matches_1[$i]]); }
			for($i=0; $i<sizeof($picks); $i++){ $picks_val[$i] = trim($W[$picks[$i]]); }
			$res2 = $DB->execute("select count(*) as no from `{$destination_t}` where ".andCondition($matches_2, $matches_1_val));
			if($res2[2]){ $W2 = $DB->nextRow($res2[0]);
				if($W2['no']<1){
					if($DB->simpleQuery("insert into `{$destination_t}` (".toCSL($inserts).") values (".toCSL_val($picks_val).")")){ echo "<br />Done at #{$no}"; }
					else{ echo "<br />Error at #{$no}"; }
				}
			}else{
				echo "<br />Empty at #{$no}";
			}
		}
		echo "<br />ENDED<hr />";
	}
}else{
	echo "<br />SIZE MISMATCH ERROR!<br />Please check<br />";
}

function holePegStr($holes, $pegs){
	$hpstr = "";
	for($i=0; $i<sizeof($holes); $i++){
		$hpstr .= ",`{$holes[$i]}`='{$pegs[$i]}'";
	}
	$hpstr = substr($hpstr,1);
	return $hpstr;
}

function andCondition($names, $values){
	$cond = "";
	for($i=0; $i<sizeof($names); $i++){

		if(strpos($names[$i],'-')>-1){
			$t = explode('-',$names[$i]);
			$cond .= " and (`{$t[0]}`<={$values[$i]} and `{$t[1]}`>={$values[$i]})";
		}else{
			$cond .= " and `{$names[$i]}`='{$values[$i]}'";
		}
	}
	$cond = substr($cond, 5);
	return $cond;
}

function toCSL($arr){
	$ret = "";
	foreach($arr as $a){ $ret .= ",`{$a}`"; }
	$ret = substr($ret,1);
	return $ret;
}
function toCSL_rmvDup($arr1, $arr2){
	$ret = "";
	foreach($arr1 as $a){ $ret .= ",`{$a}`"; }
	foreach($arr2 as $a){ if(!in_array($a, $arr1)) $ret .= ",`{$a}`"; }
	$ret = substr($ret,1);
	return $ret;
}

function toCSL_val($arr){
	$ret = "";
	foreach($arr as $a){ $ret .= ",'{$a}'"; }
	$ret = substr($ret,1);
	return $ret;
}

function getTableHeaders($t){
	global $DB; $r = array();
	$res = $DB->execute("describe `{$t}`");
	if($res[2]){ while($row=$DB->nextRow($res[0])){ $r[] = $row['Field']; }}
	return $r;
}
?>