<?php
session_start();

include "../functions.php";

include "../conn.php";

checkAdmin(session_id());

if ($_POST["submit"] == 1)
{
	require "upload.php";
	$path = "files/";
	$upload_file_name = "userfile";
	$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
	$default_extension = "";
	$mode = 2;
	$my_uploader = new uploader('en');
	$my_uploader->max_filesize(100000);
	$my_uploader->max_image_size(500, 500);
	if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
	{
		$my_uploader->save_file($path, $mode);
	}
	
	if ($my_uploader->error && $my_uploader->error != "No file was uploaded") 
	{
		$err .= "Uploading image..." . $my_uploader->error;
	}
	elseif (! $my_uploader->error)
	{
		$catpic = $path . $my_uploader->file['name'];
	}
	
	if (! $error)
	{
		$catid = $_POST["cat"];
		$catname = $_POST["catname"];
		$parentid = $_POST["catid"];
		$catdesc = $_POST["catdesc"];
		mysql_query("update cats set parentid='$parentid', catname='$catname', catdesc='$catdesc' where catid='$catid'");
		
		if ($catpic)
		{
			$res = mysql_query("select catpic from cats where catid='$catid'");
			$oldpic = mysql_result($res, 0);
			mysql_query("update cats set catpic='$catpic' where catid='$catid'");
			@unlink($oldpic);
		}
		
		header ("location: products.php?catid=$parentid");
	}
}

$title = "Modify Category";
include "header.php";

$catid = $_POST["catid"];

$res = mysql_query("select catname, catdesc, catpic, parentid from cats where catid='$catid'");

if ($row = mysql_fetch_array($res))
{
	$catname = $row[0];
	$catdesc = $row[1];
	$catpic = $row[2];
	$parentid = $row[3];
}
?>

<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" enctype="multipart/form-data">
<table border=0 width=80% cellpadding=5 align=center>
<?php
if ($err)
{
	$err = "<font color=red>the following error occured when adding new group<p>" . $err . "</font>";
	echo "<tr><td colspan=2 bgcolor=yellow><ul>";
	echo $err;
	echo "</ul></td></tr>";
}
?>

<tr><td align=right>Parent Category:</td><td><?php echo "<select name=catid><option value=0>Select Parent Category</option>"; cats($parentid, $catid); echo "</select>"; ?></td></tr>

<tr><td align="right">Category Name:</td><td align="left"><input type="text" name="catname" size=30 value="<?php echo $catname; ?>"></td></tr>

<tr><td align=right>Category Picture:</td><td><input type="file" name="userfile" size=30></td></tr>

<tr><td align="right" valign="top">Category Description:</td><td align="left"><textarea name="catdesc" rows=6 cols=40 wrap=virtual><?php echo $catdesc; ?></textarea></td></tr>
<input type="hidden" name="submit" value=1>
<input type="hidden" name="cat" value="<?php echo $catid; ?>">
<tr><td align="center" colspan=2><input type="submit" value="Modify">&nbsp;<input type="reset" value="Reset"></td></tr>

</table>
</form>

<?php
include "footer.php";

function cats($catid=0, $tao, $parentid=0, $space=1)
{
	$res = mysql_query("select catid, catname from cats where parentid='$parentid' and catid<>'$tao' order by catname asc");
	while ($row = mysql_fetch_array($res))
	{
		if ($row[0] == $catid)
		{
			echo "<option value=$row[0] selected>";
		}
		else
		{
			echo "<option value=$row[0]>";
		}
		for ($i = 0; $i < 2*$space; $i++)
		{
			echo "&nbsp;";
		}
		echo "$row[1]</option>";
		
		$query = "select catid from cats where parentid=$row[0]";
		$newresult = mysql_query($query);
		if (mysql_num_rows($newresult) > 0)
		{
			$space++;
			cats($catid, $tao, $row[0], $space);
			$space--;
		}
	}
}
?>