<?php
require_once("../includes/config.php");
require_once("../includes/functions/func.global.php");
require_once("../includes/functions/loggincheck.php");
require_once("class.menu.php");

$errors='';
db_connect($config);

IF(isset($_POST['Submit'])){
	demoversion();
	$times = 0;

	IF(!$file || $parentid == '0'){
        $errors = 'Please select parent-node and add a textfile';
		if($cat_locale == 'de'){$sel_de = "SELECTED";}
        if($cat_locale == 'us'){$sel_us = "SELECTED";}
        if($cat_locale == 'uk'){$sel_uk = "SELECTED";}
        if($cat_locale == 'ca'){$sel_ca = "SELECTED";}
        if($cat_locale == 'jp'){$sel_jp = "SELECTED";}
        if($cat_locale == 'fr'){$sel_fr = "SELECTED";}
    	$parentloc = $cat_locale;

	}ELSEIF(isset($_FILES['file'])){

		// Find Category Parent Id (category_pid)
 		$query = "SELECT category_node, category_amzname FROM `amzs_categories` WHERE category_id='".$parentid."'";
		$query_result = @mysql_query($query);
		while ($info = @mysql_fetch_array($query_result)){
			$new_parent = $info['category_node'];
			$new_mode = $info['category_amzname'];
		}

        $handle = fopen($_FILES['file']['tmp_name'], "r");
		while (!feof($handle)){
			$buffer = fgets($handle, 4096);
			IF(trim($buffer) != ''){
				$values = explode("::", $buffer);
				$title = $values[0];
				$node = $values[1];
				$query = "INSERT INTO `amzs_categories` ( `category_locale` , `category_pid` , `category_name` , `category_node` , `category_amzname` , `category_type` )
				VALUES ('" . $_POST['cat_locale'] . "', '" . $parentid . "', '" . $title . "', '" . $node . "', '" . $new_mode . "', '0');";
				mysql_query($query);
			}
			if($errors==""){$errors="SQL Update Successful";}
		}
		fclose($handle);

//  Update the path Data
 	$query = "SELECT category_id FROM `amzs_categories` WHERE category_pid='".$parentid."'";
	$query_result = @mysql_query($query);
	while ($info = @mysql_fetch_array($query_result)){
		$new_id = $info['category_id'];
		$new_path = "0|".$parentid."|".$new_id;
    	$update = "UPDATE `amzs_categories` SET category_path='".$new_path."' WHERE category_id='".$new_id."'";
   		$result = mysql_query($update);
	}

	log_action('Categories Imported');
	transfer('sql_insert.php','Categories Imported');
    exit;
	}

}else{

	if($cat_locale){
		if($cat_locale == 'de'){$sel_de = "SELECTED";}
        if($cat_locale == 'us'){$sel_us = "SELECTED";}
        if($cat_locale == 'uk'){$sel_uk = "SELECTED";}
        if($cat_locale == 'ca'){$sel_ca = "SELECTED";}
        if($cat_locale == 'jp'){$sel_jp = "SELECTED";}
        if($cat_locale == 'fr'){$sel_fr = "SELECTED";}
    	$parentloc = $cat_locale;
    }else{
		$sel_de = "SELECTED";
    	$parentloc = 'de';
    }
}

$query = "SELECT * from `amzs_categories` WHERE category_locale='".$parentloc."' AND category_pid='0' ORDER BY category_name";
$result = mysql_query($query);
if(!$result){ $errors = mysql_error();exit; }
$num_cats = mysql_num_rows($result);
$i = 0;

//-- Get category's data
while($i < $num_cats ){
	$cat_id = mysql_result($result,$i,"category_id");
	$cat_name = mysql_result($result,$i,"category_name");
    $cat_node = mysql_result($result,$i,"category_node");
	$sel_parent .= '<option value="'.$cat_id.'">'.$cat_name.' ( '.$cat_node.' )</option>';
$i++;
}

$obj = new Menu();
$nav = $obj->get_js_menu(0);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Amazon Shop Admin Panel - SQL Insert</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="images/style.css">
<SCRIPT LANGUAGE="JavaScript" SRC="menu/JSCookMenu.js"></SCRIPT>
<LINK REL="stylesheet" HREF="menu/themes/Office/theme.css" TYPE="text/css">
<SCRIPT LANGUAGE="JavaScript" SRC="menu/themes/Office/theme.js"></SCRIPT>
<LINK REL="stylesheet" HREF="menu/themes/Office/theme.css" TYPE="text/css">
<SCRIPT LANGUAGE="JavaScript" SRC="menu/themes/Office/theme.js"></SCRIPT>
<style type="text/css">
<!--
.style2 {color: #FFFFFF;
	font-size: 12px;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
}
.style5 {font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
a:link {
	color: #0D5B94;
}
a:visited {
	color: #0D5B94;
}
-->
</style>
<script language="JavaScript"><!--
function checkBox(theBox){
  var aBox = theBox.form["list[]"];
  var selAll = false;
  var i;
  for(i=0;i<aBox.length;i++){
    if(aBox[i].checked==false) selAll=true;
  }
  if(theBox.name=="selall"){
    for(i=0;i<aBox.length;i++){
      aBox[i].checked = selAll;
    }
    selAll = !selAll;
  }
  //theBox.form.selall.checked = selAll;
}
function init(){
  var theForm = document.f1;
  var aBox = theForm["list[]"];
  var selAll = false;
  var i;
  for(i=0;i<aBox.length;i++){
    if(aBox[i].checked==false) selAll=true;
    aBox[i].onclick = function(){checkBox(this)};
  }
  //theForm.selall.checked = selAll;
}
//--></script>
</head>
<body>
<!--Start top-->
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="100%" height="42" align="left" background="images/bg_top.gif"><img src="images/logo.gif" width="159" height="31" hspace="10"></td>
  </tr>
  <tr>
    <td><img src="images/dot.gif" width="1" height="1" alt=""></td>
  </tr>
</table>
<!--End top-->
<!--Start topmenu-->
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#F0F0F0" height="25" style="padding-left:20px;" id="menu">
<SCRIPT language="JavaScript" type="text/javascript">
			var myMenu =

			// Start the menu
[
<? echo $nav; ?>
];

			// Output the menu
			cmDraw ('menu', myMenu, 'hbr', cmThemeOffice, 'ThemeOffice');
			</SCRIPT></td>
</tr>
<tr>
<td bgcolor="#333333"><img src="images/dot.gif" width="1" height="1" alt=""></td>
</tr>
</table><br>

<!--End topmenu-->
<!--Start heading page-->
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="heading">Category Import</td>
</tr>
<tr>
<td bgcolor="#333333"><img src="images/dot.gif" width="1" height="1" alt=""></td>
</tr>
</table>
<br>
<!--End heading page-->
<form name="form2" enctype="multipart/form-data" method="post" action="">
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F6F6F6" style="border:1px solid #CCCCCC;">
    <tr>
      <td><br>
      <table width="50%"  border="0" align="center" cellpadding="4" cellspacing="0">
        <tr>
          <td width="50%" valign="middle" class="style5"><strong>File Name:</strong></td>
          <td>
            <input type="file" name="file">
          </td>
        </tr>

        <tr>
          <td valign="middle" class="style5"><strong>Locale:</strong></td>
          <td valign="top">
            <select name="cat_locale" id="cat" style="width:250px;" onChange="javascript:document.forms[0].cat_locale.value = this.value; submit();">
              <option value="us" <? print $sel_us; ?> >Amazon.com</option>
              <option value="uk" <? print $sel_uk; ?> >Amazon.co.uk</option>
              <option value="de" <? print $sel_de; ?> >Amazon.de</option>
              <option value="jp" <? print $sel_jp; ?> >Amazon.co.jp</option>
              <option value="ca" <? print $sel_ca; ?> >Amazon.ca</option>
              <option value="fr" <? print $sel_fr; ?> >Amazon.fr</option>
            </select></td>
        </tr>

        <tr>
          <td width="50%" valign="middle" class="style5"><strong>Parent category:</strong></td>
          <td>
            <select name="parentid" id="parentid" style="width:250px;">
              <option value="0" selected></option>
              <? print $sel_parent; ?>
            </select>
					</td>
        </tr>

      </table>
    </td>
    </tr>
    <tr>
      <td colspan="2" height="50"><div align="center">
          <input type="submit" name="Submit" value="Import File">
      </div></td>
    </tr>
  </table>

</form>

<table width="45%"  border="0" align="center" cellpadding="4" cellspacing="0">
	<tr>
		<td>
        <center><font color="#CC0066"><b><? print $errors; ?></b></font></center><br><br>
<p>Here are a few notes on this script and some requirements of the text file
you are importing:</p>
<ul>
	<li><b>Use this script at your own risk!</b></li>
	<li>Backup your database.</li>
	<li>Do some test importing before running on your live site.</li>
	<li>Select the node, you add new categories to at <a href="http://www.gwaanin.com/browsenodelookup" target="_blank">http://www.gwaanin.com</a></li>
    <li>Copy & Paste the result it in a text file</li>
    <li>Import the textfile to your category</li>
	<li>Text file must contain nodes from only one mode.</li>
</ul>
<center>
<table cellSpacing="8" border="1" id="table1" style="border-collapse: collapse">
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		Download can pick</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Just This Node's
		Immediate Children</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		Format</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Title :: NewNode</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		Delimiter</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Use &quot;::&quot; to Delimit
		Fields</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		File Format (End-of-Line)</td>
		<td style="font-family: Arial, Helvetica, sans-serif">DOS/Windows Format
		(CrLf)</td>
	</tr>
</table>
</center>


		</td>
	</tr>
</table>


<br>
<!--Start bottom-->
<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#333333"><img src="images/dot.gif" width="1" height="1" alt=""></td>
</tr>
<tr>
<td style="padding:15px;" align="center">
<span class="copyright">Copyright &copy; 2005 <a href="http://www.ghostscripter.com" class="copyright" target="_blank">GhostScripter.Com</a> All Rights Reserved.</span></td>
</tr>
</table>
<!--End bottom-->
</body>
</html>