<?php

$string = "";

$act = array_key_exists("act",$postData) ? $postData['act'] : "";

if (array_key_exists("Init",$postData)) {
	$string .= '<ul>'."\n";
	$lng = $this->oDb->toArray("SELECT * FROM langs WHERE lang != 'default'");
	for ($a=0;$a<count($lng);$a++) {
		$res = $this->oDb->toArray("SELECT * FROM lang_keys");
		for ($i=0;$i<count($res);$i++) {
			$doIt = TRUE;
			if ($act == "ADD") {
				$cl = $this->oDb->toArray("SHOW COLUMNS FROM `".$res[$i]['tabname']."`");
				$type = "";
				for ($b=0;$b<count($cl);$b++) {
					if ($cl[$b]['Field'] == $res[$i]['keyname']."_".$lng[$a]['lang'])
						$doIt = FALSE;
					if ($cl[$b]['Field'] == $res[$i]['keyname'])
						$type = $cl[$b]['Type'];
				}
				if (empty($type))
					// Sem by sa nemal dostat
					die("Field not exists.");
				$sql = "ALTER TABLE `".$res[$i]['tabname']."` ".$act." `".$res[$i]['keyname']."_".$lng[$a]['lang']."` ".$type." NOT NULL DEFAULT '' AFTER `".$res[$i]['keyname']."`";
			} else
				$sql = "ALTER TABLE `".$res[$i]['tabname']."` ".$act." `".$res[$i]['keyname']."_".$lng[$a]['lang']."`";
			if ($doIt && $this->oDb->query($sql)) {
				$string .= '	<li>'.$act.': '.$res[$i]['tabname'].'.'.$res[$i]['keyname'].'_'.$lng[$a]['lang'].'</li>'."\n";
				if ($act == "ADD") {
					$upd = $this->oDb->toArray("SELECT * FROM `".$res[$i]['tabname']."`");
					for ($b=0;$b<count($upd);$b++) {
						$sql = "UPDATE `".$res[$i]['tabname']."` SET `".$res[$i]['keyname']."_".$lng[$a]['lang']."` = '".addslashes($upd[$b][$res[$i]['keyname']])."' WHERE id_".$res[$i]['tabname']." = '".$upd[$b]['id_'.$res[$i]['tabname'].'']."'";
						$this->oDb->query($sql);
					}
				}
			}
		}
	}
	$string .= '</ul>'."\n";
	$string .= '<p class="okmsg">Recache up to date.</p>'."\n";
}

$oForm = new cForm();
$string .= '<form method="post" action="'.$this->oUrl->href().'" enctype="multipart/form-data">'."\n";
$string .= $oForm->optionBox("Choose one action", "act", Array (
	Array (
		"name" => "Upgrade tables",
		"value" => "ADD"
	),
	Array (
		"name" => "Downgrade tables",
		"value" => "DROP"
	)
), "ADD");
$string .= $oForm->submitButton(0, "Execute", FALSE, "Init");
$string .= '</form>'."\n";

return $string;

?>
