<?php

if ($_GET['action'] == 'del')
{
	$qry = $wpdb->get_row("DELETE FROM `".$wpdb->prefix."wct".$tableid."` WHERE `id`='".mres($_GET['rid'])."' LIMIT 1;");
	$_GET['action'] = '';
}
elseif ($_GET['action'] == 'edit2')
{
	for ($i=0;$_POST[wctff][$i] != '';$i++) {
		$string .= "`".mres($_POST[wctff][$i])."`='".mres($_POST[wctf][$i])."' ";
	}
	$string = str_replace("' `","', `",$string);
	$wpdb->get_row("UPDATE `".$wpdb->prefix."wct".$tableid."` SET ".$string." WHERE `id`='".mres($_GET['rid'])."' LIMIT 1;");
	$message = __('Changes has been saved', 'wct')."<br/>";
	$_GET['action'] = 'edit';
}
elseif ($_GET['action'] == 'new2')
{
	for ($i=0;$_POST[wctff][$i] != '';$i++) {
		$string .= "`".mres($_POST[wctff][$i])."`='".mres($_POST[wctf][$i])."' ";
	}
	$string = str_replace("' `","', `",$string);
	$wpdb->get_row("INSERT INTO `".$wpdb->prefix."wct".$tableid."` SET ".$string.";");
	$_GET['rid'] = mysql_insert_id();
	$message = __('Entry has been saved', 'wct')."<br/>";
	$_GET['action'] = 'edit';
}

switch ($_GET['action'])
{
	case 'edit':
		echo "<div><h2>".__('Edit Entry','wct')."</h2>";
		$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$tableid."`;");
		$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
		$felder = explode("\n",$array['Create Table']);
		for ($i=3;$felder[$i] != '';$i++) {
			$x++;
			$feld[$x] =  preg_replace("/.*`(.*)`.*/","$1",$felder[$i-2]);
			$feld2[$x] =  preg_replace("/.*`(.*)`\s(.*?)\s.*/","$2",str_replace(array(",","8 ,2"),array(" ,","8.2"),$felder[$i-2]));
		}

		$qry = $wpdb->get_row("SELECT * FROM `".$wpdb->prefix."wct".$tableid."` WHERE `id`='".mres($_GET['rid'])."' LIMIT 1;");
		echo $message."<form action=\"admin.php?page=wct_table_".$tableid."&wcttab=content&action=edit2&wcts=".$_GET['wcts']."&rid=".$_GET['rid']."\" method=\"post\"><table>";
		for($i=1;$i<=$x;$i++) {
			if ($feld[$i] != 'id') {
				echo "<input type=\"hidden\" name=\"wctff[]\" value=\"".$feld[$i]."\" /><tr><td><strong>".$feld[$i]."</strong>&nbsp;</td><td>";
				if ($feld2[$i] == "smallint(6)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"".stripslashes($qry->$feld[$i])."\" maxsize=\"6\"/>"; }
				elseif ($feld2[$i] == "int(11)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"".stripslashes($qry->$feld[$i])."\" maxsize=\"11\"/>"; }
				elseif ($feld2[$i] == "varchar(32)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"".stripslashes($qry->$feld[$i])."\" maxsize=\"32\"/>"; }
				elseif ($feld2[$i] == "varchar(64)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"".stripslashes($qry->$feld[$i])."\" maxsize=\"64\"/>"; }
				elseif ($feld2[$i] == "text") { echo "<textarea cols=\"100\" rows=\"10\" name=\"wctf[]\">".stripslashes($qry->$feld[$i])."</textarea>"; }
				elseif ($feld2[$i] == "float(8.2)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"".stripslashes($qry->$feld[$i])."\" maxsize=\"11\"/>"; }
				echo "</td></tr>";
			}
		}
		echo "</table><input type=\"submit\" name=\"submit\" value=\"". __('Save all Changes', 'wct') ."\"></form>";
	break;

	case 'new':
		echo "<div><h2>".__('Create Entry','wct')."</h2>";
		$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$tableid."`;");
		$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
		$felder = explode("\n",$array['Create Table']);
		for ($i=3;$felder[$i] != '';$i++) {
			$x++;
			$feld[$x] =  preg_replace("/.*`(.*)`.*/","$1",$felder[$i-2]);
			$feld2[$x] =  preg_replace("/.*`(.*)`\s(.*?)\s.*/","$2",str_replace(array(",","8 ,2"),array(" ,","8.2"),$felder[$i-2]));
		}

		echo $message."<form action=\"admin.php?page=wct_table_".$tableid."&wcttab=content&action=new2&wcts=".$_GET['wcts']."\" method=\"post\"><table>";
		for($i=1;$i<=$x;$i++) {
			if ($feld[$i] != 'id') {
				echo "<input type=\"hidden\" name=\"wctff[]\" value=\"".$feld[$i]."\" /><tr><td><strong>".$feld[$i]."</strong>&nbsp;</td><td>";
				if ($feld2[$i] == "smallint(6)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"\" maxsize=\"6\"/>"; }
				elseif ($feld2[$i] == "int(11)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"\" maxsize=\"11\"/>"; }
				elseif ($feld2[$i] == "varchar(32)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"\" maxsize=\"32\"/>"; }
				elseif ($feld2[$i] == "varchar(64)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"\" maxsize=\"64\"/>"; }
				elseif ($feld2[$i] == "text") { echo "<textarea cols=\"100\" rows=\"10\" name=\"wctf[]\"></textarea>"; }
				elseif ($feld2[$i] == "float(8.2)") { echo "<input size=\"34\" type=\"text\" name=\"wctf[]\" value=\"\" maxsize=\"11\"/>"; }
				echo "</td></tr>";
			}
		}
		echo "</table><input type=\"submit\" name=\"submit\" value=\"". __('Save Entry', 'wct') ."\"></form>";
	break;

	default:
		echo "<div><h2>".__('Edit Content','wct')."</h2>";
		echo __('Attention, only the first 6 fields (or less) are visible in this list. Please click on edit to see all fields of an entry.', 'wct')."<br/><br/><script type=\"text/javascript\">
		function chkconfirm() {
			var answer = confirm('". __ ('Do you really want to delete this record?', 'wct')."');
			if (answer) { return true; } else { return false; }
		}
		</script><table cellpadding-left=\"15\" cellpadding-right=\"15\"><thead><tr>";

		$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$tableid."`;");
		$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
		$felder = explode("\n",$array['Create Table']);
		$x = 0;
		for ($i=3;$felder[$i] != '';$i++) {
			$x++;
			$feld[$x] =  preg_replace("/.*`(.*)`.*/","$1",$felder[$i-2]);
			echo "<td>&nbsp;<strong>".$feld[$x]."</strong>&nbsp;</td>";
			if ($i >= '8') { $i = '9999'; }
		}
		echo "<td></td><td></td></tr></thead><tbody>";

		if ($x >= '6') { $x = '6'; }

		$qry = $wpdb->get_row("SELECT Count(id) AS `anzahl` FROM `".$wpdb->prefix."wct".$tableid."`;");
		$anz = $qry->anzahl;

		if (is_numeric($_GET['wcts']) AND $_GET['wcts'] != '') { $starting = (integer)$_GET['wcts']; } else { $starting = '0'; }
		if ($starting > $anz) { $starting = $anz - 30; }

		$qry = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."wct".$tableid."` LIMIT ".mres($starting).",30;");
		foreach ($qry as $row)
		{

			if ($color == "#EEEEEE" ) { $color = "#DDDDDD"; } else { $color = "#EEEEEE"; }
			echo "<tr onMouseOver=\"this.style.backgroundColor='#FFCCCC';\" onMouseOut=\"this.style.backgroundColor='".$color."';\" style=\"background-color:".$color." !important;\">";
			for($i=1;$i<=$x;$i++) {
				echo "<td><div style=\"overflow:hidden;height:18px;\">&nbsp;".$row->$feld[$i]."&nbsp;</div></td>";
			}
			echo "<td style=\"background-color:#FFFFFF !important;\">&nbsp;<a style=\"text-decoration:none;\" href=\"admin.php?page=wct_table_".$tableid."&wcttab=content&action=edit&wcts=".$starting."&rid=".$row->id."\">[". __('Edit','wct')."]</a></td>".
			     "<td style=\"background-color:#FFFFFF !important;\"><a style=\"text-decoration:none;\" href=\"admin.php?page=wct_table_".$tableid."&wcttab=content&action=del&wcts=".$starting."&rid=".$row->id."\" onclick=\"return chkconfirm()\">[". __('Del','wct')."]</a></td></tr>";
		}

		$r = ceil($anz / 30);
		if ($r >= '2') {
			echo "<tr><td colspan=\"".($i + 1)."\" align=\"center\">".__('Page', 'wct').":&nbsp;";
			for($i=0;$i<$r;$i++) {
				if (($i * 30) == $starting) {
					echo $i."&nbsp;";
				}
				else {
					echo "<a href=\"admin.php?page=wct_table_".$tableid."&wcttab=content&wcts=".(($i*30))."\">".$i."</a>&nbsp;";
				}
			}
			echo "</td></tr>";
		}
		echo "</tbody></table><br/><form action=\"admin.php?page=wct_table_".$tableid."&wcttab=content&action=new&wcts=".$_GET['wcts']."\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"". __('New Entry', 'wct') ."\"></form>";
	break;
}

echo "</div>";

?>