<?
require_once("../includes/config.php");
require_once("../includes/functions/func.global.php");
require_once("../includes/functions/loggincheck.php");
require_once("class.menu.php");

db_connect($config);

$obj = new Menu();
$nav = $obj->get_js_menu(0);

IF(isset($_POST['Submit']))
{
	demoversion();
	$times = 0;

	$parts = explode('/', $_POST['cat_mode']);
	
	$locale = $parts['0'];
	$mode = $parts['1'];
	
	IF(!isset($_POST['cat_node']))
	{
		$_POST['cat_node'] = '';
	}

	// Loop through text file (Use "|" to Delimit Fields) (Title|Parent|Node)
	$fp = @fopen($_POST['file_name'], "rb") or die("Couldn't open file"); 
	$data = fread($fp, filesize($fp)); 
	
	while(!feof($fp)) 
	{ 
		$data = fgets($fp, 1024); 
	
		//$values = explode("\r\n", $data); 
		$values = explode("|", $data); 
		$Title = $values[0];
		$Parent = $values[1];
		$Node = $values[2];
	
		
		// Find Category Parent Id (category_pid)
		$query = "SELECT category_id FROM `amzs_categories` WHERE category_node='" . $Parent . "'";
		$query_result = @mysql_query ($query);
		while ($info = @mysql_fetch_array($query_result))
		{
			$new_parent = $info['category_id'];
		}
		// See if root
		if ($new_parent =='')
		{
			$new_parent = 0;
		}
	
		$query = "INSERT INTO `amzs_categories` ( `category_id` , `category_locale` , `category_pid` , `category_name` , `category_node` , `category_amzname` , `category_type` , `category_search` , `category_path` ) 
			VALUES ('', '" . $locale . "', '" . $new_parent . "', '" . $Title . "', '" . $Node . "', '" . $mode . "', '" . $_POST['type'] . "', '', '0');";
		mysql_query($query);
		
		$insert_id = mysql_insert_id();
		
		IF($new_parent == 0)
		{
			$path = "0|" . $insert_id;
		}
		ELSE
		{
			$query = "SELECT category_path FROM `amzs_categories` WHERE category_id='" . $new_parent . "' LIMIT 1";
			$query_result = @mysql_query ($query);
			while ($info = @mysql_fetch_array($query_result))
			{
				$parent = $info['category_path'];
			}
			
			$path = $parent . "|" . $insert_id;
		}
		
		mysql_query("UPDATE `amzs_categories` SET `category_path` = '" . $path . "' WHERE `category_id` = '" . $insert_id . "' LIMIT 1 ;");
	
		// loop until file finished
	} 
	
	fclose($fp); 

	log_action('Added Category');
	transfer('cat_add_file.php','Added Categories');
	exit;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Add Category</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
	background-color: #EEEEEE;
}
.title {	
	align:left;
	color: #FF9933;
	font-weight:bold;
	font-size:18px;
	font-family:arial;
	border-bottom-style:solid;
	border-bottom-width:3px;
	border-color:#006699;
	padding-bottom:3px;
}
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #000000;
	text-decoration: none;
}
.title1 {	align:left;
	color: #FF9933;
	font-weight:bold;
	font-size:18px;
	font-family:arial;
	border-bottom-style:solid;
	border-bottom-width:3px;
	border-color:#006699;
	padding-bottom:3px;
}
.style2 {
	color: #FFCC00;
	font-weight: bold;
}
-->
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function sub(page) {
OpenWin = this.open(page, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=320,height=400");
}
// End -->

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</SCRIPT>
</head>
<body>
<form name="f" method="post" action="">
  <table width="500"  border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><div align="center" class="title1">Add Categories by File<br>
              <br>
      </div></td>
    </tr>
    <tr>
      <td><br>        <table width="100%"  border="0" cellspacing="0" cellpadding="4">
        <tr>
          <td colspan=2 valign="middle" class="style1">File Format: Title|Parent|Node</td>
        </tr>
        <tr>
          <td width="50%" valign="middle" class="style1">File Name:</td>
          <td>
            <input name="file_name" type="text" size="30" id="cat_name" value="c:\inetpub\computers.txt">          </td>
        </tr>
        <tr>
          <td width="50%" valign="middle" class="style1">Category Type: <sup>(<span class="style2"><a href="#" onClick="MM_openBrWindow('help.php?id=0','help','width=250,height=400')">?</a></span>)</sup></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="style1">Mode:&nbsp;<sup>(<span class="style2"><a href="#" onClick="MM_openBrWindow('help.php?id=1','help','width=250,height=400')">?</a></span>)</sup></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>
      </table>              </td>
    </tr>
    <tr>
      <td height="50"><div align="center">
          <input type="submit" name="Submit" value="Add Categories">
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>