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

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

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

$radius				= (int) trim($_POST['radius']);

//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;
	$matches_2[] = $m;
}

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

if($lenomatches_1==$lenomatches_2){
	$no = 0;
	echo "<br />'EXPANSION' STARTED<hr />";
	$temptbl = '1_prefixing_'.date('dmy');
	$temptbl_definitions = getTableHeaderDefinitions($t2_name);
	createTable($temptbl, $temptbl_definitions);

	$t2_replicate_cols =  array();
	foreach($temptbl_definitions as $k => $v){
		if($k=='id') continue;
		$t2_replicate_cols[] = $k;
	}

	$t2_replicate_vals = array();

	$res = $DB->execute("select ".toCSL($t2_replicate_cols).",".toCSL($matches_2)." from `{$t2_name}` order by id ASC");
	//next time marge the column arrays to get rid of duplicates
	if(!is_null($res)){
		$res = $res[0];
		while($W = $DB->nextRow($res)){
			$no++;
			//get the t2_name picked values as $matches_2_val
			for($i=0; $i<sizeof($matches_2); $i++){
				$matches_2_val[$i] = trim($W[$matches_2[$i]]);
			}
			$prefix = $W['prefix'];
			//execute a match attempt to find the focal point id
			$Q = $DB->execute("select id from `{$t1_name}` where ".andCondition($matches_1, $matches_2_val)." limit 1");
			if($Q[2]){
				$Q = $Q[0]; $R = $DB->nextRow($Q);
				$id = $R['id'];
				//now find all records in range ($radius)
				$Q2 = $DB->execute("select ".toCSL($matches_1)." from `{$t1_name}` where (id>='".($id - $radius)."' and id<='".($id + $radius)."') order by id");
				if($Q2[2]){
					$Q2 = $Q2[0];
					while($R2 = $DB->nextRow($Q2)){
						$symbol = $R2['code'];
						$year = $R2['year'];
						//hard code the remainder
						$Q3 = $DB->execute("insert into `{$temptbl}` (`prefix`,`symbol`,`year`) values ('{$prefix}','{$symbol}','{$year}')");
					}
				}else{
					echo "<br />Error at Focal #{$id}";
				}
			}else{
				echo "<br />Match values not found";
			}
		}
		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++){
		$cond .= " and `{$names[$i]}`='".$DB->escape_string($values[$i])."'";
	}
	$cond = substr($cond, 5);
	return $cond;
}

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

function createTable($tblname, $coldefinitions){
	global $DB;	$sqlpart = "(";
	foreach($coldefinitions as $col => $definition){ $sqlpart .= "`{$col}` {$definition},"; }
	$sqlpart .= "PRIMARY KEY(id)) ENGINE=InnoDB";
	$DB->silentExecute("DROP TABLE IF EXISTS {$tblname}");
	if($DB->silentExecute("CREATE TABLE {$tblname} {$sqlpart}")) return true;
	else return false;
}

function getTableHeaderDefinitions($t){
	global $DB; $h =array();
	$res = $DB->execute("describe `{$t}`");
	if($res[2]){
		while($row=$DB->nextRow($res[0])){
			$f = $row['Field']; $t = $row['Type']; $e = $row['Extra'];
			$h[$f] = trim("{$t} {$e}");
		}
	}
	return $h;
}
?>