<?php

if ($_GET['page'] != 'wct_table_create' && $_GET['wcttab'] == '')
{
	$tablename = $wpdb->get_row("SELECT `name`,`overlay`,`headerline`,`sheme` FROM `".$wpdb->prefix."wct_list` WHERE `id`='".mres($tableid)."' LIMIT 1;");
	
	$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$tableid."`;");
	/*Hack to transform PHP Object to Array, because Objectname has a Space in the middle*/
	$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
	$felder = explode(",",str_replace(array(",","8 ,2"),array(" ,","8.2"),$array['Create Table']));
	for ($i=2;$felder[$i] != '';$i++) {
		$feldoutput .= preg_replace("/`(.*)`\s(.*?)\s.*/","<input type=\"hidden\" name=\"vipInfoC[]\" value=\"$1\"><div id=\"wctf_$1\"><label><input type=\"text\" name=\"vipInfo[]\" value=\"$1\" readonly><input type=\"text\" name=\"vipInfoB[]\" value=\"$2\" style=\"width:108px;\" readonly><input type=\"button\" value=\"". __('Del','wct')."\" style=\"marginLeft:5px;\" onclick=\"removeField2('wctf_$1')\" /></label></div>",$felder[$i-1]);
		$renaming .= preg_replace("/`(.*)`\s.*\sDEFAULT NULL/","<option value=\"$1\">$1</option>",$felder[$i-1]);
		$renaming2 .= preg_replace("/`(.*)`\s(.*?)\s.*/","if (selection == '$1') { document.getElementById(\"wcthidden\").value = \"$2\"; }",$felder[$i-1]);
	}
}
else
{
	$feldoutput = "<div class=\"field\"><label><input type=\"text\" name=\"vipInfo[]\" value=\"\"><select name=\"vipInfoB[]\" style=\"width:103px;\"><option value=\"varchar32\">varchar(32)</option><option value=\"varchar64\">varchar(64)</option><option value=\"int11\">int(11)</option><option value=\"smallint6\">smallint(6)</option><option value=\"text\">text</option><option value=\"float82\">float(8,2)</option></select></label></div>";
}

/*Seite im Background um die Tabelle zu erstellen und Felder Anzahl zu ändern*/
echo "<div style=\"float:left;width:350px;\"><h2>";
if ($tablename->name == '') { echo __('Create Table', 'wct'); } else { echo __('Edit Table', 'wct'); }
echo "</h2>".
     "<script type=\"text/javascript\">

var nFloor = \"\";	

function removeField2(nField) {
	var answer = confirm('". __ ('Attention, with that action, all data in this field will be erased in the database.\nDo you want to proceed?', 'wct')."');
	if (answer) {
		var d = document.getElementById(nField); 
		d.parentNode.removeChild(d);
	}
}
	
function validate(form) {
  for(var i = 0; i < form.elements.length; i++) {
    if(form.elements[i].type == \"text\" && form.elements[i].value == \"\") {
      alert(\"". __ ('Please fillout all fields or delete them if not needed!', 'wct')."\"); 
      return false;
    }
  }
  return true;
}

function chkrename() {
	if (document.getElementById('wctrfield').value == \"\") {
		alert('". __ ('Please select a Field to rename', 'wct')."!');
		return false;
	}
	if (document.getElementById('wctfren').value == \"\") {
		alert('". __ ('Please give the field a new name', 'wct')."!');
		return false;
	}
	return true;
}
function chkdelete() {
	if (document.wctdform.wctdelete.checked == true) {
		return true;
	}
	else {
		alert('". __ ('Please accept Checkbox that all data\nwill be deleted from this table!', 'wct')."');
		return false;
	}
}


function removeField(nField) {
	nField.parentNode.parentNode.removeChild(nField.parentNode);
}

function insertField(){

	var newFieldContainer = document.createElement('div');
	var newFieldLabel = document.createElement('label');
	newFieldLabel.innerHTML = \"\"; 
		
	var newField = document.createElement('input');
	newField.type = \"text\";
	newField.name = \"vipInfo[]\";
	newFieldContainer.appendChild(newFieldLabel);
	newFieldLabel.appendChild(newField);

	var objSelect = document.createElement('select');
	objSelect.name = \"vipInfoB[]\";
	objSelect.style.width = \"103px\";

	var objOption = document.createElement(\"option\");
	objOption.text=\"varchar(32)\";
	objOption.value=\"varchar32\";
	objSelect.options.add(objOption);

	var objOption = document.createElement(\"option\");
	objOption.text=\"varchar(64)\";
	objOption.value=\"varchar64\";
	objSelect.options.add(objOption);

	var objOption = document.createElement(\"option\");
	objOption.text=\"smallint(6)\";
	objOption.value=\"smallint6\";
	objSelect.options.add(objOption);

	var objOption = document.createElement(\"option\");
	objOption.text=\"int(11)\";
	objOption.value=\"int11\";
	objSelect.options.add(objOption);

	var objOption = document.createElement(\"option\");
	objOption.text=\"text\";
	objOption.value=\"text\";
	objSelect.options.add(objOption);

	var objOption = document.createElement(\"option\");
	objOption.text=\"float(8,2)\";
	objOption.value=\"float82\";
	objSelect.options.add(objOption);

	newFieldLabel.appendChild(objSelect); 

	var deleteBtn = document.createElement('input');
	deleteBtn.type = \"button\";
	deleteBtn.value = \"". __ ('Del', 'wct')."\";
	deleteBtn.style.marginLeft = \"5px\";
	deleteBtn.onclick = function(){removeField(this)};
	newFieldContainer.appendChild(deleteBtn);
	document.forms[0].insertBefore(newFieldContainer,nFloor);
}

function init(){

	var insertBtn = document.getElementById('newFieldBtn');
	insertBtn.onclick = function()
		{
		 insertField();
		}
	nFloor = insertBtn;		
}

function hidden_def(){
	var selection = document.getElementById(\"wctfren\").value;
	if (selection == '') { document.getElementById(\"wcthidden\").value = \"\"; }
	".$renaming2."
}

navigator.appName == \"Microsoft Internet Explorer\" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
</script>".
     "<form action=\"admin.php?page=".$_GET['page']."\" method=\"post\" onSubmit=\"return validate(this)\" name=\"wctform\">";
if ($tablename->name != '')	{ echo "<input type=\"hidden\" name=\"viptID\" value=\"".$tableid."\">"; }
echo "<div class=\"field\"><label><strong>". __('Table Name', 'wct') .":</strong>&nbsp;<input size=\"32\" type=\"text\" name=\"vipName\" value=\"".$tablename->name."\"></label></div>".
     "<br/><strong>".__('Fields', 'wct').":</strong><br/>".
     "<input type=\"text\" name=\"id\" value=\"id\" readonly><input type=\"text\" name=\"idtype\" value=\"int(11) UNIQUE\" readonly><br/>".$feldoutput.
     "<input type=\"button\" id=\"newFieldBtn\" value=\"". __('Add new Field', 'wct') ."\">&nbsp;".
     "<input type=\"submit\" name=\"submit\" value=\"". __('Save all Changes', 'wct') ."\"><br/><br/><input type=\"checkbox\" name=\"wctheaderline\" value=\"1\" id=\"wctheaderline\"";
if ($tablename->headerline == '1') { echo " checked"; }
echo "> ". __('Show Headerline','wct')."<br/><input type=\"checkbox\" name=\"wctoverlay\" value=\"1\" id=\"wctoverlay\"";
if ($tablename->overlay == '1') { echo " checked"; }
echo "> ". __('Overlay Function','wct')."<br/><select name=\"wctsheme\"><option value=\"0\"";
if ($tablename->sheme != '1') { echo " selected"; }
echo ">". __('Black','wct') ."</option><option value=\"1\"";
if ($tablename->sheme == '1') { echo " selected"; }
echo ">". __('White','wct') ."</option></select>". __('color for activated sort buttons', 'wct')."</form>";

if ($tablename->name != '')	{
	echo "<hr/><h2>". __('Rename field', 'wct')."</h2>".
	     "<form action=\"admin.php?page=wct_table_".$tableid."\" method=\"post\" onSubmit=\"return chkrename()\" name=\"wctrform\" id=\"wctrform\">".
	     "<input type=\"hidden\" id=\"wcthidden\" name=\"wcthidden\" value=\"\"><b>".__('Field to rename', 'wct').":</b> <select name=\"wctfren\" id=\"wctfren\" onChange=\"hidden_def()\"><option value=\"\"></option>".$renaming."</select><br/>".
	     "<b>".__('New Fieldname', 'wct').":</b> <input type=\"text\" name=\"wctrfield\" id=\"wctrfield\" value=\"\"><br/>".
	     "<input type=\"submit\" name=\"submit\" value=\"". __('Rename Field', 'wct') ."\"></form>";
}
else { echo "<br/>&nbsp;"; }

if ($tablename->name != '')	{
	echo "<hr/><h2>". __('Delete Table', 'wct')."</h2>".
	     "<form action=\"admin.php?page=wct_table_create\" method=\"post\" onSubmit=\"return chkdelete()\" name=\"wctdform\">".
	     "<input type=\"hidden\" name=\"vipID\" value=\"".$tableid."\"><input type=\"checkbox\" name=\"wctdelete\" value=\"1\" id=\"wctdelete\"> ". __('Yes, delete this table with the complete content.','wct').
	     " <input type=\"submit\" name=\"submit\" value=\"". __('Delete Table including all content', 'wct') ."\"></form>";
}

echo "</div><div><h2>" . __('Instructions', 'wct') . "</h2>".
     __('Please use only Alphanumberic Characters for all Fieldnames.', 'wct')."<br/>".
     __('The field `id` cannot be deleted and is needed in the database.', 'wct')."<br/><br/>".
     __('Fields can have following definitions:', 'wct')."<table>".
     "<tr><td>varchar(34)</td><td>";
printf( __('This Field can save Text with a length of %s Characters', 'wct'), '32');
echo "</td></tr><tr><td>varchar(64)&nbsp;</td><td>";
printf( __('This Field can save Text with a length of %s Characters', 'wct'), '64');
echo "</td></tr><tr><td>text</td><td>";
printf( __('This Field can save Text with a length of %s Characters', 'wct'), 'unlimited');
echo "</td></tr><tr><td>smallint(6)</td><td>"; 
printf( __('This Field can save Numbers from %s to %s', 'wct'), '0','65535');
echo "</td></tr><tr><td>int(11)</td><td>"; 
printf( __('This Field can save Numbers from %s to %s', 'wct'), '0','4294967295');
echo "</td></tr><tr><td>float(8,2)</td><td>"; 
printf( __('This Field can save Numbers from %s to %s', 'wct'), '-65535.00','65535.00');
echo "</td></tr></table><br/>".
     __('You cannot change the definition of a field, if the field has already been saved. Please delete the field and create a new one with the correct definition.<br/><br/>You can activate the Overlay Function, which will enable a preview of the page, when the user get with the mousecurser over an Entry.','wct');
		if ($tableid != '1') { $g = " id=\"'.$tableid.'\""; $l ='16'; } else { $l = '8'; }

		if ($_GET['page'] != 'wct_table_create') {
			printf( __('This Table can be added with %s or %s to the page.', 'wct')."</br>", '<input type=\'text\' value=\'[wctable id="'.$tableid.'" limit="50"]\' size=\'27\' readonly/>', '<input type=\'text\' value=\'[wctable'.$g.']\' size=\''.$l.'\' readonly/>');
			printf( __('This Searchform for this table with %s or %s to the page.', 'wct'), '<input type=\'text\' value=\'[wctsearch felder="*" text="sss"]\' size=\'34\' readonly/>', '<input type=\'text\' value=\'[wctsearch]\' size=\'11\' readonly/>');
		}

echo "</div><div style=\"clear:left;\"></div>";
?>