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

$t=""; $y=""; $empchar = "^~``";

$source_t		 		= trim($_POST['source_t']);

$m = trim($_POST['source_t_match_cols']); if($m=="*"){ $m = getTableHeaders($source_t); }
$source_t_match_cols	= explode(',',$m);
$leno_match_cols 		= sizeof($source_t_match_cols);

$doclean		 		= ""; 	if(isset($_POST['doclean'])) $doclean = trim($_POST['doclean']);

$find_chars				= explode(',',$_POST['find_chars']);//don't trim
$leno_find_chars 		= sizeof($find_chars);

$v = $_POST['replace_chars'];
if($leno_find_chars==1 && $v==""){ $replace_chars = array($empchar); }else{ $replace_chars = explode(',',$_POST['replace_chars']); }//don't trim
$leno_replace_chars = sizeof($replace_chars);

$temparr 			= array_fill(0,($leno_match_cols*2),"");
//add references
$bindparams 		= array();
foreach($temparr as $key => $value){ $bindparams[$key] = &$temparr[$key]; }

//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;
	$y .= "s";
}
$y .= $y;//there will be x2
$types = array($y);

if($leno_find_chars==$leno_replace_chars){
	echo "<br />'TRIM and CLEAN' Started";
	if($DB->silentExecute("update `{$source_t}` set ".holePegStr_trim($matches_1))){
		echo "<br /><b style='color:#0cbd0c'>Trimming Done</b>"; $t = " Too";
	}else{ echo "<br />Trimming Failed"; }

	//check if should clean 
	if($doclean=="yes"){
		$stm = $DB->con->prepare("update `{$source_t}` set ".holePegStr_replace($matches_1));
		if(!is_null($stm)){
			//$bindparams[0] = "'{$y}'";
			call_user_func_array(array($stm, "bind_param"), array_merge($types, $bindparams));//$stm->bind_param($y, $find, $replace);
			
			for($i=0; $i<$leno_find_chars; $i++){
				for($j=0; $j<($leno_match_cols*2); $j+=2){
					$bindparams[$j] 	= $find_chars[$i];
					$v = $replace_chars[$i]; if($v==$empchar){ $v = ""; }
					$bindparams[$j+1] 	= $v;
				}
				$stm->execute();
				//echo mysqli_info($DB->con);$DB->silentExecute("SET profiling = 1");$res = $DB->silentExecute("SHOW PROFILES");
			}

			$stm->close();
			$DB->con->close();
			echo "<br /><b style='color:#0cbd0c'>Cleaned{$t}</b>";

		}else{ echo "<br />Failed to clean"; }
	}

	echo "<br />ENDED<hr />";
}else{
	echo "<br />SIZE MISMATCH ERROR!<br />Please check<br />";
}

function holePegStr_trim($holes){
	$hpstr = "";
	for($i=0; $i<sizeof($holes); $i++){
		$hpstr .= ",`{$holes[$i]}`=TRIM(`{$holes[$i]}`)";
	}
	$hpstr = substr($hpstr,1);
	return $hpstr;
}
function holePegStr_replace($holes){
	$hpstr = "";
	for($i=0; $i<sizeof($holes); $i++){
		$hpstr .= ",`{$holes[$i]}`=REPLACE(`{$holes[$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; $h ="";
	$res = $DB->execute("describe `{$t}`");
	if($res[2]){
		while($row=$DB->nextRow($res[0])){
			$f = $row['Field']; if($f=="id") continue; $h .= ",{$f}";
		}
		$h = substr($h,1);
	}
	return $h;
}
?>