<?php

global $dbprefix, $f, $a, $id, $t, $menu, $Sname, $Svalue, $Fname, $Fvalue;



global $f, $a, $id, $t, $menu, $page, $Fname, $Fvalue;
	/*
if($menu=="0"){
$menushow = "<a href=\"".$PHP_SELF."?f=$f&a=$a&Fname=$Fname&Fvalue=$Fvalue&menu=1\">show menu</a>";
}else{
$menushow =  "<a href=\"".$PHP_SELF."?f=$f&a=$a&Fname=$Fname&Fvalue=$Fvalue&menu=0\">hide menu</a>";
}

echo $menushow;
*/
$foliodomain=".";


//if($menu<>"0"){

echo "<div id=\"ADMIN_TOPMENU\">";

//echo "<a href=\"#\" class=\"HEADBUTTON_SELECTED\"><center>1.</center></a>";
//$itemlist = array('news', 'pages','styles','comments','categories', 'files', 'folders', 'keywords', 'links', 'persons', 'groups', 'areas', 'roles', 'permissions','companies', 'addresses', 'sessions', 'baskets', 'deliveries', 'items','suppliers','products', 'materials', 'colors', 'palettes', 'stock', 'brands', 'events',  'todo', 'values',  'binder', 'log', 'stats', 'statrules', 'feeds', 'shelves');


global $curUser;
		
$isGOD=CheckGOD($curUser);

if ($isGOD>0){
$itemlist = array('news', 'pages','styles','comments','categories', 'files', 'folders', 'keywords', 'links', 'feeds', 'persons', 'sessions', 'palettes', 'hours','binder');

} else {

$itemlist = array('news', 'pages', 'comments','categories', 'files', 'folders', 'keywords', 'links', 'binder');


}

//$itemlist = array('news', 'categories', 'files', 'folders', 'links', 'binder');

$i=0;
foreach ($itemlist as $item) {

	if (isset($f)){
		if($f==$item){$SELECTED="_SELECTED";}else{$SELECTED="";}
	}

echo "<a href=\"index.php?admin=1&f=$item\" class=\"ADMIN_HEADBUTTON".$SELECTED."\"><center><img src=\"./ic/tables/".$item.".png\" border=\"0\"><br>$item</center></a>";
$i++;
}

//echo "<a href=\"#\" class=\"HEADBUTTON_SELECTED\"><center>2.</center></a>";

//$itemlist2 = array('tools', 'upload', 'filegrab', 'folderscan', 'thumbnails', 'backupdb', 'search', 'clean', 'reset', 'help', 'about', 'optimize', 'seed','newswizard','keywordwizard', 'colorextract', 'itemwizard', 'bindcount', 'groupwizard', 'statscount', 'linkcheck', 'rssfeeds', 'itemqueue', 'bindview', 'fillshelf', 'openinghours');

if ($isGOD>0){

$itemlist2 = array('folderscan', 'backupdb', 'search', 'about', 'newswizard', 'thumbnails', 'keywordwizard', 'bindcount', 'linkcheck', 'openinghours');

} else {

$itemlist2 = array('folderscan', 'backupdb', 'search', 'about', 'newswizard','keywordwizard', 'bindcount', 'linkcheck', 'openinghours');


}
//$itemlist2 = array('upload', 'filegrab', 'folderscan', 'thumbnails','newswizard');
echo "</div>";
echo "<div id=\"ADMIN_TOPMENU\">";

$i=0;
foreach ($itemlist2 as $item) {

	if (isset($f)){
		if($f==$item){$SELECTED="_SELECTED";}else{$SELECTED="";}
	}

echo "<a href=\"index.php?admin=1&f=$item\" class=\"ADMIN_HEADBUTTON".$SELECTED."\"><center><img src=\"./ic/tools/".$item.".png\" border=\"0\"><br>$item</center></a>";
$i++;
}

echo "</div>";

//} //end if menu show


echo "<div id=\"page_middle\">";

	
	if (isset($f)){
		
		//echo "you seem to have pushed $f <br>";
		
		switch ($f){
		
		
		//SETTINGS
		
		case "about":
		include ('./settings/setting_about.php');
		break;
		
		case "help":
		include ('./settings/setting_help.php');
		break;
		

		//TOOLS
		
		case "search":
		include ('./tools/tool_search.php');
		break;
		
		case "colorextract":
		include ('./tools/tool_colorextract.php');
		break;
		
		case "itemwizard":
		include ('./tools/tool_itemwizard.php');
		break;
		
		case "backupdb":
		include ('./tools/tool_backupdb.php');
		break;
		
		case "upload":
		include ('./tools/tool_upload.php');
		break;
		
		case "linkcheck":
		include ('./tools/tool_linkcheck.php');
		break;
		
		case "rssfeeds":
		include ('./tools/tool_rssfeeds.php');
		break;
		
		case "itemqueue":
		include ('./tools/tool_itemqueue.php');
		break;
		
		case "tools":
		include ('./tools/tool_tools.php');
		break;
		
		case "fillshelf":
		include ('./tools/tool_fillshelf.php');
		break;
		
		case "groupwizard":
		include ('./tools/tool_groupwizard.php');
		break;
		
		case "folderscan":
		include ('./tools/tool_folderscan.php');
		break;
	
		case "optimize":
		include ('./tools/tool_optimize.php');
		break;
		
		case "bindview":
		include ('./tools/tool_bindview.php');
		break;
		
		case "clean":
		include ('./tools/tool_clean.php');
		break;
		
		case "reset":
		include ('./tools/tool_reset.php');
		break;
		
		case "seed":
		include ('./tools/tool_seed.php');
		break;
		
		case "statscount":
		include ('./tools/tool_statscount.php');
		break;
		
		case "keywordwizard":
		include ('./tools/tool_keywordwizard.php');
		break;
		
		case "newswizard":
		include ('./tools/tool_newswizard.php');
		break;
			
		case "filegrab":
		include ('./tools/tool_filegrab.php');
		break;
		
		case "thumbnails":
		include ('./tools/tool_thumbnails.php');
		break;
		
		case "bindcount":
		include ('./tools/tool_bindcount.php');
		break;
		
		case "openinghours":
		include ('./tools/tool_openinghours.php');
		break;
		
			
		

		//TABLES
		
		default:
		
		
		
		switch ($a){
		
		
			/*
			case "show_change":
			
			$f=$_POST['f'];
			$thisid=$_POST['id'];
			$show=$_POST['show'];
			
			//connect to db
			require ("./include/connect_db.php");
			mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
			mysql_query("UPDATE $f SET show=$show WHERE id='$thisid'"); 
		
			echo "<H1>Item $thisid from table $f has been changed</H1>";
			
			break;
			*/
			
			
		
			case "delete":
			
			$thisid=$_GET['id'];
			
			echo "<H1>Item $thisid from table $f has been deleted</H1>";
			
			echo "<table><tr><th></th><th>message</th></tr>";
			
			//connect to db
			require ("./include/connect_db.php");
			mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
			
			if ($f=="files"){
			
			$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."files WHERE id='$thisid'");
			$resultrow=mysql_fetch_array($result);
			$folderid=$resultrow[folderid];
			$file="$resultrow[file]";
			
			$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."folders WHERE id='$folderid'");
			$resultrow=mysql_fetch_array($result);
			$mydir="$foliodomain/files/$resultrow[folder]";
			
			unlink($mydir."/".$file); 
			
			echo "<tr><td>#</td><td>actual file $file from $mydir deleted from harddisk</td></tr>";
			
			//update the folderinfo
				
			folderscan($mydir);
				
			echo "<tr><td>#</td><td>folder information for $resultrow[folder] updated</td></tr>";
			
			}
			
			if ($f=="folders"){
			
			//remove corresponding files from binder
			
			$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."files WHERE folderid='$thisid'");
			
			while($resultrow=mysql_fetch_array($result)){
				$fileid=$resultrow[id];
				mysql_query("DELETE FROM ".$globalConfig['dbprefix']."binder WHERE table1='files' AND record1='$fileid'");
				mysql_query("DELETE FROM ".$globalConfig['dbprefix']."binder WHERE table2='files' AND record2='$fileid'");
			}
			
			echo "<tr><td>#</td><td>all files out of this folder removed from binder</td></tr>";
			
			//remove all files for folderid
			mysql_query("DELETE FROM ".$globalConfig['dbprefix']."files WHERE folderid=$thisid");
			
			echo "<tr><td>#</td><td>all file entries out of this folder removed</td></tr>";
			
					
			//get folder location
			$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."folders WHERE id='$thisid'");
			
			$resultrow=mysql_fetch_array($result);
			$mydir="$resultrow[folder]";
			
			//remove folder and its contents physically
			RemoveDirectory($mydir);
			
							
			echo "<tr><td>#</td><td>actual folder and all its files removed from harddisk</td></tr>";	
			//echo "<tr><td>#</td><td>$removeaction</td></tr>";	
			
			//optimize table		
			mysql_query("OPTIMIZE TABLE files");
			
			echo "<tr><td>#</td><td>table files optimized</td></tr>";
			
			}
			
			//delete folder
			mysql_query("DELETE FROM ".$globalConfig['dbprefix']."folders WHERE folder='$mydir'");
			mysql_query("OPTIMIZE TABLE folders");
			
			//delete db entry
			mysql_query("DELETE FROM ".$globalConfig['dbprefix']."$f WHERE id=$thisid");
			
			echo "<tr><td>#</td><td>item $thisid deleted from table $f</td></tr>";
		
			//optimize table		
			mysql_query("OPTIMIZE TABLE $f");
			
			echo "<tr><td>#</td><td>table ".$globalConfig['dbprefix']."$f optimized</td></tr>";
			
			//remove all links in the binder
			mysql_query("DELETE FROM ".$globalConfig['dbprefix']."binder WHERE table1='$f' AND record1='$thisid'");
			
			echo "<tr><td>#</td><td>item $thisid from table $f removed from binder masters</td></tr>";
			
			mysql_query("DELETE FROM ".$globalConfig['dbprefix']."binder WHERE table2='$f' AND record2='$thisid'");
			
			echo "<tr><td>#</td><td>item $thisid from table $f removed from binder slaves</td></tr>";
			
			//optimize table		
			mysql_query("OPTIMIZE TABLE ".$globalConfig['dbprefix']."binder");
			
			echo "<tr><td>#</td><td>table binder optimized</td></tr>";
			
			echo "</table>";
			
			echo "<br><a href=\"index.php?admin=1&f=$f\">click here to go back to $f</a>";
		
			break;
			
			
			
			case "comment":
			
			global $dblang, $lang, $cat, $page, $mode, $news, $dbname, $ezdb, $globalConfig, $userid, $sessionid, $admin, $f, $Fname, $Fvalue; 
		
				
			$posttable=$_POST['posttable'];
			$postrecord=$_POST['postrecord'];
			$postcom=$_POST['postcom'];
			$postvote=$_POST['postvote'];
					
			$ezdb->query("INSERT INTO ".$globalConfig['dbprefix']."comments VALUES (NULL, NOW(), '0', '1', '$postcom', '$postvote', '$sessionid', '$userid')");
			
			
			$varid=$ezdb->get_var("SELECT id FROM ".$globalConfig['dbprefix']."comments WHERE comment='$postcom' AND vote='$postvote' AND sessionid='$sessionid' AND userid='$userid' ");
			
			
			$ezdb->query("INSERT INTO ".$globalConfig['dbprefix']."binder VALUES (NULL, NOW(), '0', '1', '$posttable', '$postrecord', 'comments', '$varid', '', '', '$userid')");
	
			
			echo "<H1>Comment</H1>";
			
			echo "<p>Your comment has been added.</p>";
			
			echo "<a href=\"index.php?admin=1&f=$f&Fname=$Fname&Fvalue=$Fvalue\"><img src=\"./ic/tools/start.png\" border=\"0\"> return</a><br><br>";		
			
			break;
			
			
			
			case "edit":
			
				
			echo "<H1>We will edit record $id from table $f</H1>";
		
			
			echo "<a href=\"index.php?admin=1&f=$f\"><img src=\"./ic/tools/cancel.png\" border=\"0\"> cancel</a><br><br>";
			
			//connect to database
	
			require ("./include/connect_db.php");
			mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
	
			$jobs=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."$f WHERE id='$id' LIMIT 1");
			
			//make form to submit changes
			echo "<form accept-charset=\"utf-8\"  action=\"index.php?admin=1&\" method=\"post\" name=\"\" target=\"_self\">";
			echo "<input name=\"f\" type=\"hidden\" value=\"".$f."\">";
			//echo "we're trying";
			
			$fieldnames = array();
			
			while ($fields=@mysql_fetch_field($jobs)) {
			$fieldnames[]=$fields->name;
			}
			
			
				while($jobs_row = mysql_fetch_array($jobs))
				{
				
				$fieldstotal = mysql_num_fields($jobs);
				
				
				
				//echo "we're still trying";
				//generate editable list of fields for this record
					//for ($x=0; $x<@mysql_num_fields($jobs_row); $x++) {
					
					
					
					echo "<table><tr><th>field</th><th>value</th></tr>";
					
					for ($x=0; $x<$fieldstotal; $x++) {
					$fields=@mysql_fetch_field($jobs[$x][0]);
					
						if ($fieldnames[$x]=="public"){
							
							if ($jobs_row[$x]==1){$checked=" checked=\"checked\"";}else{$checked="";}
						
							echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"update[]\" type=\"checkbox\"  $checked></td></tr>");
						
						
						//if not god also hide these
						}elseif(($isGOD<1) && (($fieldnames[$x]=="file") OR ($fieldnames[$x]=="folder"))){
						
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"update[]\" type=\"hidden\" value=\"".$jobs_row[$x]."\">".$jobs_row[$x]."</td></tr>");
						
						
						}elseif(($fieldnames[$x]=="type")  OR ($fieldnames[$x]=="id")  OR ($fieldnames[$x]=="posted") OR ($fieldnames[$x]=="userid") OR ($fieldnames[$x]=="updated") OR ($fieldnames[$x]=="bound")  OR ($fieldnames[$x]=="sessionid")  OR ($fieldnames[$x]=="folderid")  OR ($fieldnames[$x]=="files")  OR ($fieldnames[$x]=="size") OR ($fieldnames[$x]=="lastlog") OR ($fieldnames[$x]=="session") OR ($fieldnames[$x]=="pagecount") OR ($fieldnames[$x]=="duration") OR ($fieldnames[$x]=="ipaddress") OR ($fieldnames[$x]=="hostname") OR ($fieldnames[$x]=="referrer")){
						
							echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"update[]\" type=\"hidden\" value=\"".$jobs_row[$x]."\">".$jobs_row[$x]."</td></tr>");
						
						
						}elseif(($fieldnames[$x]=="text_uk") OR ($fieldnames[$x]=="text_jp")){
						
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">
						
						 <textarea name=\"update[]\" cols=60 rows=10>".$jobs_row[$x]."</textarea>
						
						</td></tr>");
						
						}elseif($fieldnames[$x]=="sort_jp"){
						
						//now give a dropdown list 
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">";
						
						echo "<label for=\"$fieldnames[$x]\"></label><select name=\"update[]\" id=\"$fieldnames[$x]\">";
						
						$alphabet=$globalConfig['alphabet_jp'];
						for($i = 0; $i < count($alphabet); $i++)
						{
						
						echo "<option value=\"".$alphabet[$i]['roman']."\">".$alphabet[$i]['utf']."</option>";	
						}
						
						
						echo "</select></td></tr>";
						
						
						
						}else{
						
							echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"update[]\" type=\"text\" value=\"".$jobs_row[$x]."\" size=\"80\" maxlength=\"255\"></td></tr>");
						
						}
					
						
					
					}
					
					echo "</table>";
									
				}
			echo "<input name=\"a\" type=\"hidden\" value=\"update\">";
			echo "<input name=\"total\" type=\"hidden\" value=\"".$fieldstotal."\">";
			echo "<input name=\"submit\" type=\"submit\" value=\"submit\"></form>";
			
			
			
			break;
			
			case "update":
			
						
			require ("./include/connect_db.php");
			mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
			$total=$_POST['total'];
			$thisid=$_POST['update'][0];
			$f=$_POST['f'];
			$a=$_POST['a'];
			
			
			echo "<H1>Updated $thisid in $f</H1>";
			/*
			$realtotal1=count($_POST['fieldname']);
			$realtotal2=count($_POST['update']);
			echo "$realtotal1 | $realtotal2";	
			*/
			$updatequery="UPDATE ".$globalConfig['dbprefix']."$f SET id=id";
			
			echo "<table><tr><th>fieldname</th><th>value</th></tr>";
			
			$j=2; //we use $j for fieldvalues since unchecking the checkbox leaves a void
			for( $i=2;$i<$total;$i++) {     
			
				$fieldname=$_POST['fieldname'][$i];
				$fieldvalue=$_POST['update'][$j];
				$fieldvalue=trim($fieldvalue);
				
				$fieldvalue=preg_replace('/<br\\s*?\/??>/i', '', $fieldvalue);
				$fieldvalue=nl2br($fieldvalue); //keep line breaks
				
				//$fieldvalue=addslashes($fieldvalue);
				//$fieldvalue=nl2br($fieldvalue); //keep line breaks
				
				
				
				if ($fieldname=="public"){
				
					if ($fieldvalue=="on"){
						$fieldname="public";
						$fieldvalue=1;
					}else{
						$fieldname="public";
						$fieldvalue=0;
						$j=$j-1;
					}
				
				}
			
				
				if ($fieldname=="file"){
								
				//get old file name
				$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."files WHERE id='$thisid'");
				$resultrow=mysql_fetch_array($result);
				
				$file="$resultrow[file]";
				$folderid=$resultrow[folderid];
				
				$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."folders WHERE id='$folderid'");
				$resultrow=mysql_fetch_array($result);
				$folder=$resultrow[folder];
				
				$mydir="$foliodomain/files/$folder/";
				$newfile="$fieldvalue";
				$oldfile="$file";
				
				rename ($mydir.$oldfile,$mydir.$newfile);
				
				
				
				}
				
				//if (($fieldname=="text_uk") || ($fieldname=="text_jp")){
				//}
				
				if ($fieldname=="updated"){
				
				$fieldvalue = date("Y-m-d H:i:s", Time());
				
				echo $datemodified;
				
				//$updatequery.=", $fieldname='$datemodified' ";
				
				}
				
				if ($fieldname=="folder"){
				
				$newfolder="$foliodomain/files/$fieldvalue";
				
				//get old folder name
				
				
				//get folder id
				$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."folders WHERE id='$thisid'");
				$resultrow=mysql_fetch_array($result);
				$oldfolder=$resultrow[folder];
				
				$oldfolder="$foliodomain/files/$oldfolder";
				
				//rename folder
				rename ($oldfolder,$newfolder);
				}
				//$mysqlstring = "UPDATE $f SET posted=NULL";
				
				echo "<tr><td>$fieldname</td><td>$fieldvalue</td></tr>";
				
				$updatequery.=", $fieldname='$fieldvalue' ";
				
				$j++;
				
			}
			echo "</table>";
				
			//updating one by one since it's the only thing that seems to work for now
			$updatequery.=" WHERE id='$thisid'";
			mysql_query($updatequery); 
			//echo $updatequery."<br>"; 
			
			echo "<br><a href=\"index.php?admin=1&f=$f\"><img src=\"./ic/tools/back.png\" border=\"0\"> click here to go back to $f</a>";
			echo "<br><a href=\"index.php?admin=1&a=edit&f=$f&id=$thisid\"><img src=\"./ic/tools/start.png\" border=\"0\"> click here to edit $thisid in $f</a>";
			
			break;
			
			
			
						
			case "new":
			echo "<H1>Post a new item for $f</H1>";
			
			
			
			$colorpicker=0; //color picker has not been printed yet, only print one time
			
			echo "<a href=\"index.php?admin=1&f=$f\"><img src=\"./ic/tools/cancel.png\" border=\"0\"> cancel</a><br><br>";
			//connect to database
			
			 // requires the class
			require "./classes/datepicker/class.datepicker.php";
			require "./classes/folio_color_picker/color_picker.php";
			
			require ("./include/connect_db.php");
			mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
	
			$jobs=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."$f LIMIT 1");
			
			//make form to submit changes
			echo "<form accept-charset=\"utf-8\" action=\"index.php?admin=1\" method=\"post\" name=\"\" target=\"_self\">";
			echo "<input name=\"f\" type=\"hidden\" value=\"".$f."\">";
			//echo "we're trying";
			
			$fieldstotal = mysql_num_fields($jobs);
			//echo "<br>$fieldstotal";
			
			$fieldnames = array();
						
			while ($fields=@mysql_fetch_field($jobs)) {
			$fieldnames[]=$fields->name;
			}
			
							
				
			//echo "we're still trying";
			//generate editable list of fields for this record
				//for ($x=0; $x<@mysql_num_fields($jobs_row); $x++) {
				
				
				
				echo "<table><tr><th>field</th><th>value</th></tr>";
				
				for ($x=0; $x<$fieldstotal; $x++) {
				$fields=@mysql_fetch_field($jobs[$x][0]);
				
					if (($fieldnames[$x]=="id") OR ($fieldnames[$x]=="posted") OR ($fieldnames[$x]=="updated") OR ($fieldnames[$x]=="bound")  OR ($fieldnames[$x]=="userid")){
					
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"insert[]\" type=\"hidden\" value=\"\">auto</td></tr>");
					
					
									
					} elseif ($fieldnames[$x]=="sessionid"){
					
						$session=session_id();
						$sessionquery=mysql_query("SELECT * FROM ".$dbprefix."sessions WHERE session='$session'");
						$sessionqueryrow=mysql_fetch_array($sessionquery);
						$sessionid=$sessionqueryrow['id'];
						
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"insert[]\" type=\"hidden\" value=\"$sessionid\">auto</td></tr>");
					
						
					} elseif (($fieldnames[$x]=="table1") OR ($fieldnames[$x]=="table2")){
					
						//now give a dropdown list with table names as defined in $itemlist
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">";
						
						echo "<label for=\"$fieldnames[$x]\"></label><select name=\"insert[]\" id=\"$fieldnames[$x]\">";
						
						foreach ($itemlist as $item) {
						
						echo "<option value=\"$item\">$item</option>";					
						
						}
						
						echo "</select></td></tr>";
						
					}elseif($fieldnames[$x]=="sort_jp"){
						
						//now give a dropdown list 
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">";
						
						echo "<label for=\"$fieldnames[$x]\"></label><select name=\"insert[]\" id=\"$fieldnames[$x]\">";
						
						$alphabet=$globalConfig['alphabet_jp'];
						for($i = 0; $i < count($alphabet); $i++)
						{
						
						echo "<option value=\"".$alphabet[$i]['roman']."\">".$alphabet[$i]['utf']."</option>";	
						}
						
						
						echo "</select></td></tr>";
					
					
					} elseif (($fieldnames[$x]=="field1") OR ($fieldnames[$x]=="field2") OR ($fieldnames[$x]=="field3")){
					
						//now give a dropdown list with table names as defined in $itemlist
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">";
						
						echo "<label for=\"$fieldnames[$x]\"></label><select name=\"insert[]\" id=\"$fieldnames[$x]\">";

						
						//fieldnames for first table listed
						$table=$itemlist[0];
						
						
						global $curUser, $dbprefix, $dbname,$globalConfig;
						
						require ("./include/connect_db.php");
						mysql_select_db($dbname, $link) or die("couldn't select database");
						
						$detailsresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."$table ");
						$detailsresultrow=mysql_fetch_array($detailsresult);
						$numfields=mysql_num_fields($detailsresult);
						echo "<option value=\"-1\">choose</option>";
						
							$i=0;
							
							while($i<$numfields){
							$meta = mysql_fetch_field($detailsresult, $i);
							$fieldname=$meta->name;
							$fieldvalue=$detailsresultrow[$i];
							echo "<option value=\"$fieldname\">$fieldname</option>";
							$i++;
							}
							
					echo "</select></td></tr>";
					
						
					
					
					} elseif (($fieldnames[$x]=="op1") OR ($fieldnames[$x]=="op2")){
					
						//now give a dropdown list with table names as defined in $itemlist
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						echo "<option value=\"-1\">choose</option>";
						echo "<option value=\"AND\">AND</option>";
						echo "<option value=\"OR\">OR</option>";	
						echo "<option value=\"NOT\">NOT</option>";						
							
						echo "</select></td></tr>";
						
					} elseif ($fieldnames[$x]=="statact"){
					
						//now give a dropdown list with table names as defined in $itemlist
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						//echo "<option value=\"-1\">NONE</option>";
						//echo "<option value=\"0\">ALL</option>";
						echo "<option value=\"1\">REGULAR COUNT</option>";
						//echo "<option value=\"2\">SUM COUNT</option>";	
						//echo "<option value=\"2\">GROUP BY COUNT</option>";	
						//echo "<option value=\"3\">MIN</option>";	
						//echo "<option value=\"4\">MAX</option>";
						//echo "<option value=\"5\">AVERAGE</option>";
						//echo "<option value=\"6\">DIFFERENCE</option>";
								
						echo "</select></td></tr>";
						
					} elseif ($fieldnames[$x]=="brandid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
	
						$brandresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."brands");
												
						while($brandresultrow=mysql_fetch_array($brandresult)){
						$brandid=$brandresultrow[id];
						$brandname=$brandresultrow[title_uk];
												
						echo "<option value=\"$brandid\">$brandname</option>";					
						
						}
						
						echo "</select></td></tr>";
						
					
					
					} elseif ($fieldnames[$x]=="productid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
	
						$brandresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."products");
												
						while($brandresultrow=mysql_fetch_array($brandresult)){
						$brandid=$brandresultrow[id];
						$brandname=$brandresultrow[title_uk];
												
						echo "<option value=\"$brandid\">$brandname</option>";					
						
						}
						
						echo "</select></td></tr>";
						
					} elseif ($fieldnames[$x]=="areaid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
						
						echo "<option value=\"-1\">none</option>";	
						echo "<option value=\"-5\">all</option>";	
			
	
						$arearesult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."areas");
												
						while($arearesultrow=mysql_fetch_array($arearesult)){
						$areaid=$arearesultrow[id];
						$areaname=$arearesultrow[title_uk];
												
						echo "<option value=\"$areaid\">$areaname</option>";					
						
						}
						
						echo "</select></td></tr>";
						
					} elseif ($fieldnames[$x]=="roleid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
						
						echo "<option value=\"-1\">none</option>";	
						echo "<option value=\"-5\">all</option>";	
			
	
						$roleresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."roles");
												
						while($roleresultrow=mysql_fetch_array($roleresult)){
						$roleid=$roleresultrow[id];
						$rolename=$roleresultrow[title_uk];
												
						echo "<option value=\"$roleid\">$rolename</option>";					
						
						}
						
						echo "</select></td></tr>";
					
					} elseif ($fieldnames[$x]=="hierarchy"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
					
						echo "<option value=\"1\">A [master] ----->> B [slave]</option>";	
						echo "<option value=\"2\">A [master] <<--->> B [master]</option>";	
						echo "<option value=\"3\">A [slave]  <<----- B [master]</option>";	
							
						
						
						echo "</select></td></tr>";
						
					} elseif ($fieldnames[$x]=="hierarchyid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
						
						echo "<option value=\"-1\">A <--> B</option>";	
							
						$hierarchyresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."hierarchies");
												
						while($hierarchyresultrow=mysql_fetch_array($hierarchyresult)){
						$hierarchyid=$hierarchyresultrow[id];
						
						
						if ($hierarchyresultrow[hierarchy]=="1"){
							$hierarchyarrows="A --> B";	
						}elseif($hierarchyresultrow[hierarchy]=="2"){
							$hierarchyarrows="A <-> B";	
						}elseif($hierarchyresultrow[hierarchy]=="3"){
							$hierarchyarrows="A <-- B";	
						}
						
						
						$hierarchyname=$hierarchyresultrow[title1_uk]." ".$hierarchyarrows." ".$hierarchyresultrow[title2_uk];
												
						echo "<option value=\"$hierarchyid\">$hierarchyname</option>";					
						
						}
						
						echo "</select></td></tr>";
					
					
					} elseif ($fieldnames[$x]=="personid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
						
						echo "<option value=\"-1\">none</option>";	
						echo "<option value=\"-5\">all</option>";	
			
	
						$personresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."persons");
												
						while($personresultrow=mysql_fetch_array($personresult)){
						$personid=$personresultrow[id];
						$personname=$personresultrow[first_uk]." ".$personresultrow[last_uk];
												
						echo "<option value=\"$personid\">$personname</option>";					
						
						}
						
						echo "</select></td></tr>";
					
					} elseif ($fieldnames[$x]=="groupid"){
					
						//now give a dropdown list with brand names
						
						echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><select name=\"insert[]\">";
						
						require ("./include/connect_db.php");
						mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
			
						echo "<option value=\"-1\">none</option>";	
						echo "<option value=\"-5\">all</option>";	
						
						$groupresult=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."groups");
												
						while($groupresultrow=mysql_fetch_array($groupresult)){
						$groupid=$groupresultrow[id];
						$groupname=$groupresultrow[title_uk];
												
						echo "<option value=\"$groupid\">$groupname</option>";					
						
						}
						
						echo "</select></td></tr>";
					
					} elseif (($fieldnames[$x]=="datefrom") OR ($fieldnames[$x]=="dateto")) {
					
					
					
					// instantiate the object
					$db=new datepicker();
					
					// uncomment the next line to have the calendar show up in german
					//$db->language = "dutch";
					
					$db->firstDayOfWeek = 1;
				
					// set the format in which the date to be returned
					$db->dateFormat = "Y-m-d";
				
					echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"insert[]\" type=\"text\" id=\"".$fieldnames[$x]."\">";
					
					?>
					<html><input type="button" value="date picker" onClick="<?=$db->show("".$fieldnames[$x]."")?>"></html>
					<?
					
					echo "&nbsp;&nbsp;&nbsp; datetime-> yyyy-mm-dd hh:mm:ss</td></tr>";
					
					} elseif ($fieldnames[$x]=="public"){
					
					
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"insert[]\" type=\"text\" value=\"1\" size=\"80\" maxlength=\"255\"></td></tr>");
											
					} elseif (($fieldnames[$x]=="text_uk") || (($fieldnames[$x]=="text_jp"))){
					
					echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><textarea name=\"insert[]\" cols=60 rows=10></textarea></td></tr>";
					
					
					
					//colors
					} elseif (($fieldnames[$x]=="col_1") || (($fieldnames[$x]=="col_2")) || (($fieldnames[$x]=="col_3")) || (($fieldnames[$x]=="col_4"))  || (($fieldnames[$x]=="col_5"))){
					
					echo "<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\">";
					
					echo "<div class=\"form-item\"><input type=\"text\" id=\"".$fieldnames[$x]."\" name=\"insert[]\" class=\"colorwell\" value=\"#123456\" /></div>";
					
					if ($colorpicker=="0"){		
					echo "<div id=\"dragbox\"><div id=\"picker\" ></div></div>";		
					$colorpicker=1;
					}
					
					echo"</td></tr>";
					
									
					
					
					} else {
					
						echo ("<tr><td>".$fieldnames[$x]."</td><td><input name=\"fieldname[]\" type=\"hidden\" value=\"".$fieldnames[$x]."\"><input name=\"insert[]\" type=\"text\" value=\"\" size=\"80\" maxlength=\"255\"></td></tr>");
					}
									
				}
				
				echo "</table>";
									
				
			echo "<input name=\"a\" type=\"hidden\" value=\"insert\">";
			echo "<input name=\"total\" type=\"hidden\" value=\"".$fieldstotal."\">";
			echo "<input name=\"submit\" type=\"submit\" value=\"submit\"></form>";
			
			
			
			break;
			
			case "insert":
			echo "<H1>New item added in $f.</H1>";
			
			$total=$_POST['total']-1;
			
			$userid=GetUserId($curUser);
						
			$mysqlstring = "";
			
			echo "<table><tr><th>fieldname</th><th>value</th></tr>";
			for( $i=3;$i<$total;$i++) {
			
			//$fieldvalue=isUTF8($_POST['insert'][$i]);
			//$fieldvalue=utf8_decode($_POST['insert'][$i]);
			$fieldvalue=$_POST['insert'][$i];
			
			if (($fieldname=="text_uk") || ($fieldname=="text_jp")){
				
				
				//keep line breaks
				$fieldvalue = nl2br ( $fieldvalue );
				
				}
			     
			echo "<tr><td>".$_POST['fieldname'][$i]."</td><td>".$fieldvalue."</td></tr>";
				
				$mysqlstring.=",'".$fieldvalue."'";
				
				//if it's a folder, also create the actual folder
				if (($f=="folders") AND ($_POST['fieldname'][$i]=="folder")){
				$folderpath="$foliodomain/files/$fieldvalue";
				mkdir($folderpath);
				chmod($folderpath,0777);
				}
				
			}
			echo "</table>";
			
			echo "<br><a href=\"index.php?admin=1&f=$f\"><img src=\"./ic/tools/back.png\" border=\"0\"> click here to go back to $f</a>";
			echo "<br><a href=\"index.php?admin=1&f=$f&a=new\"><img src=\"./ic/tools/add.png\" border=\"0\"> post a new item in $f</a>";
						
			//echo "<br>$mysqlstring";
			
			//connect to database
			global $globalConfig;
			
			$dbname=$globalConfig['dbname'];
			$dbprefix=$globalConfig['dbprefix'];
	
			require ("./include/connect_db.php");
			mysql_select_db($dbname, $link) or die("couldn't select database");
			mysql_query("INSERT INTO ".$dbprefix."$f VALUES(NULL,NOW(),''  $mysqlstring ,'$userid')");
			
			
			//echo "<br>DB UPDATED";
			
			$lastid=$ezdb->get_var("SELECT id FROM ".$globalConfig['dbprefix']."$f ORDER BY id DESC LIMIT 1");
			
			echo "<br><a href=\"index.php?admin=1&a=edit&f=$f&id=$lastid\"><img src=\"./ic/tools/start.png\" border=\"0\"> click here to edit $lastid in $f</a>";
			
			break;
			
			
			
			default:
			
			echo "<H1>Overview of $f</H1>";
			
			//CountBindingsTable($f);
			
			if( ($f<>"files") AND ($f<>"sessions") AND ($f<>"stats") AND ($f<>"log") ){
			echo "<a href=\"index.php?admin=1&f=$f&a=new\"><img src=\"./ic/tools/add.png\" border=\"0\"> post a new item in $f</a><br><br>";
			}
			
						
			$db=new mysql_dialog("1");
			$db->connect($globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass'], $globalConfig['dbname']);
			
			//FILTER
			$Fname=$_GET['Fname']; 
			$Fvalue=$_GET['Fvalue'];
			
			
			
			if ((isset($Fname)) AND (isset($Fvalue)) AND ($Fname<>NULL) AND ($Fvalue<>NULL)){
					
			$condition="WHERE $Fname='$Fvalue'";
			
			} else {
			
			$condition="";
			
			}
			
			
			
			//PAGINATION
			$page=$_GET['page'];
			$perpage=$globalConfig['adminperpage'];
			
			//pagination buttons
			$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."$f $condition");
			$totalrows=mysql_num_rows($result);
			$pages=ceil($totalrows/$perpage);
			echo "items: $totalrows | ";
			for( $i=1;$i<=$pages;$i++) {
			echo "<a href=\"index.php?admin=1&f=$f&Fname=$Fname&Fvalue=$Fvalue&page=$i&menu=$menu\">page $i</a> | ";
			}
			
			//pagination filter
			if (isset($page)){
			$offset=($page*$perpage)-$perpage;
			$pagination="LIMIT $perpage OFFSET $offset";
			}else{
			$pagination="LIMIT $perpage";
			}
			
			
			
			
			//SORT
			$Sname=$_GET['Sname'];
			$Svalue=$_GET['Svalue'];
			
			
			if ((isset($Sname)) AND (isset($Svalue)) AND ($Sname<>NULL) AND ($Svalue<>NULL)){
					
			$sorting="ORDER BY $Sname $Svalue";
			
			} else {
			
			$sorting="ORDER BY id DESC";
			
			}
			
			
			
			//CountBindings($f, $Fvalue);
			
			$SQL="SELECT * FROM ".$globalConfig['dbprefix']."$f $condition $sorting $pagination";
			
			$db->speak($SQL);
			$db->onscreen($SQL);
			
			if(isset($f) AND ($Fname=="id")){
			
			echo "<div id=\"recorddetailview\">";
			
			//bind wizard block
			$bindsingle=AdminBindSingle($f, $Fvalue);
			echo "<div id=\"recordbind\">Bind: <p>$bindsingle</p></div>";
			
			
			//related block
			$related=AdminGetRelated($f, $Fvalue);
			echo "<div id=\"recordrelated\">Related: <p>$related</p></div>";
			
			//detail block
			$details=AdminGetDetails($f, $Fvalue);
			echo "<div id=\"recorddetails\">Details: <p>$details</p></div>";
			
			
			/*
			//comment block
			$comment=AdminAllowComments($f, $Fvalue);
			echo "<div id=\"recordcomment\">Comment: <p>$comment</p></div>";
			*/
			/*
			//value block
			$value=AdminAllowValues($f, $Fvalue);
			echo "<div id=\"recordvalue\">Value: <p>$value</p></div>";
			
			echo "<img src=\"./img/spacer.gif\" border=0 width=\"800\" height=\"0\"></div>"; //end recorddetailview
			*/
			}
			
			break;
		
		}//ending the switch a
		
		} //ending the switch f
		
		echo "</div>"; //ending page_middle
	
	} else {
	
		echo "<br><br>Welcome to the Folio CMS admin section. Please select from menu.";
		
		echo "</div>"; //ending page_middle
	
	}



/**
 * Returns <kbd>true</kbd> if the string or array of string is encoded in UTF8.
 *
 * Example of use. If you want to know if a file is saved in UTF8 format :
 * <code> $array = file('one file.txt');
 * $isUTF8 = isUTF8($array);
 * if (!$isUTF8) --> we need to apply utf8_encode() to be in UTF8
 * else --> we are in UTF8 :)
 * </code>
 * @param mixed A string, or an array from a file() function.
 * @return boolean
 */
function isUTF8($string)
{
   if (is_array($string))
   {
       $enc = implode('', $string);
       return @!((ord($enc[0]) != 239) && (ord($enc[1]) != 187) && (ord($enc[2]) != 191));
   }
   else
   {
       return (utf8_encode(utf8_decode($string)) == $string);
   }   
}







function MovePantone(){

		$dbprefix="folio_";
		
		require ("./include/connect_db.php");
		mysql_select_db($globalConfig['dbname'], $link) or die("couldn't select database");
		
		mysql_query("UPDATE ".$globalConfig['dbprefix']."colors SET hex=trim(hex)");
		
		$result=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."colors WHERE pms<>''");
		
		while($resultrow=mysql_fetch_array($result)){
		
		$pmsvalue=$resultrow[pms];
		$hexvalue=$resultrow[hex];
		$oldid=$resultrow[id];
		
		echo $pmsvalue." ".$hexvalue." ".$oldid;
		
		$result2=mysql_query("SELECT * FROM ".$globalConfig['dbprefix']."colors WHERE hex='$hexvalue' AND id<>$oldid");
		
			if(mysql_num_rows($result2)>0){
			
				mysql_query("UPDATE ".$globalConfig['dbprefix']."colors SET pms='$pmsvalue' WHERE hex='$hexvalue' AND id<>$oldid");
				
				echo " move and delete<br>";
				
				mysql_query("DELETE FROM ".$globalConfig['dbprefix']."colors WHERE id='$oldid' LIMIT 1");
			
			} else {
			
				echo " keep<br>";
			}
		
		}

}

			
?>