<?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(isset($_FILES['file']))
	{

		// Get highest id value
		$query = "SELECT MAX(category_id) max_category_id FROM `amzs_categories` LIMIT 1";
		$query_result = @mysql_query ($query);
		while ($info = @mysql_fetch_array($query_result))
		{
			$insert_id = $info['max_category_id'];
		}
		
		$handle = fopen($_FILES['file']['tmp_name'], "r");
		while (!feof($handle)) 
		{
			$buffer = fgets($handle, 4096);

			IF(trim($buffer) != '')
			{

				$OldParent = $Parent;
				//$values = explode("\r\n", $data); 
				$values = explode("|", $buffer); 
				$Title = $values[0];
				$Parent = $values[1];
				$Node = $values[2];
			

				$insert_id = $insert_id + 1;
				
				if ($OldParent != $Parent)
				{
					// Find Category Parent Id (category_pid)
					$query = "SELECT category_id, category_path FROM `amzs_categories` WHERE category_node='" . $Parent . "'";
					$query_result = @mysql_query ($query);
					while ($info = @mysql_fetch_array($query_result))
					{
						$new_parent = $info['category_id'];
						$new_parent_path = $info['category_path'];
					}
					// See if root
					if ($new_parent =='')
					{
						$new_parent = 0;
					}

					IF($new_parent == 0)
					{
						$path = "0|" . $insert_id;
					}
					ELSE
					{
						$path = $new_parent_path . "|" . $insert_id;
					}
					
				}
							
				$query = "INSERT INTO `amzs_categories` ( `category_id` , `category_locale` , `category_pid` , `category_name` , `category_node` , `category_amzname` , `category_type` , `category_search` , `category_path` ) 
					VALUES ('" . $insert_id . "', '" . $_POST['cat_locale'] . "', '" . $new_parent . "', '" . $Title . "', '" . $Node . "', '" . $_POST['cat_mode'] . "', '0', '', '" . $path . "');";
				mysql_query($query);
				
				
				// loop until file finished
				
				
				
				if($errors==""){
				$errors="SQL Update Successful";
				}
			}
		}
		fclose($handle);
	
	log_action('Categories Imported');
	transfer('sql_insert.php','Categories Imported');
	exit;				
		
	}
}	

	
$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 width="50%" valign="middle" class="style5"><strong>Category Type:</strong></td>
          <td>
            <select name="type" id="type" style="width:203px;">
              <option value="0" selected>Amazon Node</option>
            </select>
					</td>
        </tr>
        <tr>
          <td valign="middle" class="style5"><strong>Mode:</strong></td>
          <td valign="top">
            <select name="cat_mode" id="cat_mode" style="width:203px;">
							<?
							    $query = "SELECT mode_name FROM `amzs_modes` ORDER BY mode_name";
							    $query_result = @mysql_query ($query) OR error(mysql_error());
								while ($info = @mysql_fetch_array($query_result))
								{
									echo "<option value=\"" . $info['mode_name'] . "\">" . $info['mode_name'] . "</option>\n";
								}
							?>
            </select>
          </td>
        </tr>
        <tr>
          <td valign="middle" class="style5"><strong>Locale:</strong></td>
          <td valign="top">
            <select name="cat_locale" id="cat_locale" style="width:203px;">
              <option value="us">Amazon.com</option>
              <option value="uk">Amazon.co.uk</option>
              <option value="de">Amazon.de</option>
              <option value="jp">Amazon.co.jp</option>
              <option value="ca">Amazon.ca</option>
              <option value="fr">Amazon.fr</option>
            </select></td>
        </tr>

      </table>
    </td>
    </tr>
    <tr>
      <td height="50"><div align="center">
          <input type="submit" name="Submit" value="Import">
      </div></td>
    </tr>
  </table>
  
</form>

<table width="50%"  border="0" align="center" cellpadding="4" cellspacing="0">
	<tr>
		<td>
<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.&nbsp; This 
	script is not very forgiving and may import bad records if the text file is 
	not setup right.</li>
	<li>Text files may need to be split.&nbsp; Larger files may get a &quot;max 
	execution time reached&quot; PHP error.&nbsp; I have imported&nbsp; 400k files 
	without a problem but it all depends on your server speed.</li>
	<li>Script tested on version 5.0.5 of Amazon Shop</li>
	<li>Text file must contain nodes from only one mode.</li>
	<li>For some reason Browse Nodes does not seem to match 100% the node 
	structure from Amazon.</li>
	<li>If Parent Node does not exist in database, script will assume the node 
	is a top level node of your store.</li>
	<li>Below are the settings of the text file you need to set when you 
	download your node list from
	<a target="_blank" href="http://www.BrowseNodes.com">www.BrowseNodes.com</a>.<br>
&nbsp;</li>
</ul>
<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 either)</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Just This Node's 
		Immediate Children<br>
		All of This Node's Children</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		AWS Version</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Return Mode for 
		Each Node (AWS 3)</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		Format</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Title|Parent|Node</td>
	</tr>
	<tr>
		<td vAlign="center" style="font-family: Arial, Helvetica, sans-serif">
		Null Parent</td>
		<td style="font-family: Arial, Helvetica, sans-serif">Use &quot;0&quot; for null 
		Parent Node</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>



		</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>
