<?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']));
$t1_filter_cond		= trim($_POST['t1_filter_cond']);

$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){
	$m = trim($m); if($m=="") continue;
	if(strpos($m,'-')>-1){ $m = explode('-',$m); }
	$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($t1_filter_cond!='') $t1_filter_cond = " where ".$t1_filter_cond;

if(($lenomatches_1==$lenomatches_2) && ($lenoinserts_1==$lenopicks_2)){
	$no = 0;
	echo "<br />'MATCHTHROUGH FIND and LOAD' Started";
	$res = $DB->execute("select id,".toCSL($matches_1)." from `{$t1_name}`{$t1_filter_cond} 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]]);
			}

			$matches_left = assoc($matches_2, $matches_1_val);
			$matches_in_action = array();

			//initialize contents
			$temparr = array_shift($matches_left);
			$key = array_keys($temparr);
			$matches_in_action[$key[0]] = $temparr;

			$condval = false;
			$sizeofleft = 100;//any number not small
			$keepon = true;

			while( $keepon ){
				if($sizeofleft>1){
					$qry = "select count(*) as no from `{$t2_name}` where ".andCondition_assoc($matches_in_action)." limit 1";
					$res2 = $DB->execute($qry);
					$row2 = $DB->nextRow($res2[0]);
					if($row2['no']>0) $condval = true; else $condval = false;
				}else{
					$qry = "select ".toCSL($picks_2)." from `{$t2_name}` where ".andCondition_assoc($matches_in_action)." limit 1";
					$res2 = $DB->execute($qry);
					if($res2[2]){ $row2 = $DB->nextRow($res2[0]);
						$condval = true;
						//populate holders
						for($i=0; $i<sizeof($picks_2); $i++){
							$picks_2_val[$i] = trim($row2[$picks_2[$i]]);
						}
					}else{ $condval = false; }
				}
				if($condval){
					//matched; keep update tracking and append next filter
					$temparr = array_shift($matches_left);
					if($temparr===NULL){//no more matches (sizeof($matches_left)=0)
						$keepon = false;
					}else{
						$key = array_keys($temparr);
						$matches_in_action[$key[0]] = $temparr;
					}
				}else{
					//failed; replace with next filter
					array_pop($matches_in_action);
					$temparr = array_shift($matches_left);
					if($temparr===NULL){//no more matches (sizeof($matches_left)=0)
						$keepon = false;
					}else{
						$key = array_keys($temparr);
						$matches_in_action[$key[0]] = $temparr;
					}
				}
				$sizeofleft = sizeof($matches_left);
			}

			//update with found item(s) and the load to pick
			if($DB->simpleQuery("update `{$t1_name}` set ".holePegStr_assoc($matches_in_action).",".holePegStr($inserts_1, $picks_2_val)." where id='{$id}'")){
				echo "<br />Through with #{$no}";
			}else{
				echo "<br />Failed at #{$no}";
			}
		}
		echo "<br />ENDED<hr />";
	}
}else{
	echo "<br />SIZE MISMATCH ERROR!<br />Please check<br />";
}

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

function assoc($names, $values){
	$arr = array();
	for($i=0; $i<sizeof($names); $i++){
		if(is_array($names[$i])){//compute what to use a key
			$k = $names[$i][0]."-".$names[$i][1];
			$arr[$k] = array($k=>$values[$i]);

		}else{ $arr[$names[$i]] = array($names[$i]=>$values[$i]); }
	}
	return $arr;
}
function andCondition_assoc($arr){
	$cond = "";
	foreach($arr as $names=>$values){
		foreach($values as $name => $value){
			if(strpos($name,'-')>-1){
				$t = explode('-',$name);
				$cond .= " and (`{$t[0]}`<={$value} and `{$t[1]}`>={$value})";
			}else{
				$cond .= " and `{$name}`='{$value}'";
			}
		}
	}
	$cond = substr($cond, 5);
	return $cond;
}
function holePegStr($holes, $pegs){
	$hpstr = "";
	for($i=0; $i<sizeof($holes); $i++){
		$hpstr .= ",`{$holes[$i]}`='{$pegs[$i]}'";
	}
	$hpstr = substr($hpstr,1);
	return $hpstr;
}
function holePegStr_assoc($arr){
	$hpstr = "";
	foreach($arr as $name=>$value){
		foreach($value as $hole => $peg){
			if(strpos($hole,'-')>-1){
				//actual col name lost so use beginning chars as a rule
				$hole = substr($hole, 0, strpos($hole,'_'));
			}
			$hpstr .= ",`{$hole}_mt`='{$peg}'";
		}
	}
	$hpstr = substr($hpstr,1);
	return $hpstr;
}
?>