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

$t1_name		 	= trim($_POST['t1_name']);
$t1_match_cols		= explode(',',trim($_POST['t1_match_cols']));
$t1_insert_cols		= explode(',',trim($_POST['t1_insert_cols']));

$t2_name			= trim($_POST['t2_name']);
$t2_match_cols		= explode(',',trim($_POST['t2_match_cols']));
$t2_pick_cols		= explode(',',trim($_POST['t2_pick_cols']));

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

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

$inserts_1 = array();
$inserts_1_val = array();
foreach($t1_insert_cols as $r){
	$r = trim($r); if($r=="") continue;
	$inserts_1[] = $r;
}

$picks_2 = array();
$picks_2_val = array();
foreach($t2_pick_cols as $r){
	$r = trim($r); if($r=="") continue;
	$picks_2[] = $r;
}

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

$lenoinserts_1 = sizeof($inserts_1);
$lenopicks_2 = sizeof($picks_2);

$id = "";

if(($lenomatches_1==$lenomatches_2) && ($lenoinserts_1==$lenopicks_2)){
	$no = 0;
	echo "<br />'FIND and LOAD' Started";
	$res = $DB->execute("select id,".toCSL($matches_1)." from `{$t1_name}` order by id ASC");

	if(!is_null($res)){
		$res = $res[0];
		while($W = $DB->nextRow($res)){
			$picks_2_val = array();//reset

			$id = $W['id'];
			$no++;
			for($i=0; $i<sizeof($matches_1); $i++){
				$matches_1_val[$i] = trim($W[$matches_1[$i]]);
			}

			//INITIAL VERSION
			$res2 = $DB->execute("select ".toCSL($picks_2)." from `{$t2_name}` where ".andCondition($matches_2, $matches_1_val)." limit 1");
			
			if($res2[2]){
				$res2 = $res2[0]; $W2 = $DB->nextRow($res2);
				for($i=0; $i<sizeof($picks_2); $i++){
					$picks_2_val[$i] = trim($W2[$picks_2[$i]]);
				}
				if($DB->simpleQuery("update `{$t1_name}` set ".holePegStr($inserts_1, $picks_2_val)." where id='{$id}'")){
					echo "<br />Done at #{$no}";
				}else{
					echo "<br />Error at #{$no}";
				}
			}else{
				echo "<br />Empty at #{$no}";
			}

			/*//ALTERED VERSION (checks for several focus values)
			$res2 = $DB->execute("select ".toCSL($picks_2)." from `{$t2_name}` where ".andCondition($matches_2, $matches_1_val)." group by ".toCSL($picks_2));
			if($res2[2]){
				$res2 = $res2[0];
				for($i=0; $i<sizeof($picks_2); $i++){
					while($W2 = $DB->nextRow($res2)){
						$picks_2_val[$i] .= "|".trim($W2[$picks_2[$i]]);
					}
					$picks_2_val[$i] = substr($picks_2_val[$i], 1);
				}
				if($DB->simpleQuery("update `{$t1_name}` set ".holePegStr($inserts_1, $picks_2_val)." where id='{$id}'")){
					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){
	global $DB;
	$hpstr = "";
	for($i=0; $i<sizeof($holes); $i++){
		$hpstr .= ",`{$holes[$i]}`='".$DB->escape_string($pegs[$i])."'";
	}
	$hpstr = substr($hpstr,1);
	return $hpstr;
}

function andCondition($names, $values){
	global $DB;
	$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]}`='".$DB->escape_string($values[$i])."'";
		}
	}
	$cond = substr($cond, 5);
	return $cond;
}

function toCSL($arr){
	$ret = "";
	foreach($arr as $a){ $ret .= ",`{$a}`"; }
	$ret = substr($ret,1);
	return $ret;
}
?>