<?php session_start();?>
<?php include("../includes/connection.php"); 
include("../includes/func.php");?>
<?php if (empty($_SESSION["admin"])) redirect("index.php?e=1");

//Single Delete
if (!empty($_GET["delid"]) && $_GET["delid"]!=1) {
	mysql_query("delete from `category_master` where `category_id`=" . $_GET["delid"]);
	redirectS($_SERVER["HTTP_REFERER"]);
}

$pg = new paging;
$pg->setpages("SELECT count(*) from category_master");
if ($_GET["sort"]!='')
	$sort=$_GET["sort"];
else
	$sort="category_parent_id,category_name";
	
if ($_GET["sortorder"]!='')
	$sortorder=$_GET["sortorder"];
else
	$sortorder="asc";
$sql="SELECT * from category_master order by ".$sort." ".$sortorder;
$sql .=$pg->getlimit();


if ($_GET["what"]!='')
{
	$sql="Select * from category_master where " . $_GET["criteria"] . " like '%" . $_GET["what"] . "%' order by category_id asc";
	$_SESSION["errorMessage"]="Filtered Results";
}

$cmd = mysql_query($sql);
?>
<?php include("includes/header.php"); ?>
<??>

<table width="98%" height="100" border="0" align="center" cellpadding="5" cellspacing="5">
	<tr>
		<td height="50"><span style="font-weight: bold">Category Management </span> [<a href="#" onClick="openWin('categoryPopup.php', 'width=520, height=310');" class="BlackAri12B">Add New Category</a>]
	      <hr size="1">  </td>
	</tr>
	
	<?php
	//if any records retrieved
	if (mysql_num_rows($cmd) != 0) {
	?>
	
	<tr>
		<td class="BlackAri12B" colspan=2><table width="100%" border="0" cellpadding="2" cellspacing="1" class="bgDarkGray">
            <tr class="bgLightGray">
                <td width="5%">&nbsp;</td>
				<td width="15%" class="BlackAri12B"><small>Category Name</small></td>
                <td width="35%" class="BlackAri12B"><small>Path</small></td>
                <td width="20%" class="BlackAri12B"><small>Description</small></td>
				<td width="10%" class="BlackAri12B"><small>Status</small></td>
				<td width="10%" class="BlackAri12B"><small># Products</small></td>
			</tr>
			<?php getErr("6"); ?>
            <tr>
                <td colspan="7" height="10" class="bgWhite"></td>
            </tr>
			<?php
			//loop thru records and display category listing
			while ($rs = mysql_fetch_array($cmd)) {
				if ($bgclass == "bgExtraLightGray")
					$bgclass = "bgWhite";
				else
					$bgclass = "bgExtraLightGray";
					
				
			?>
			
            <tr class="<?php echo($bgclass); ?>">
                <td><table border="0" align="center">
                    <tr>
                        <td><a href="#" onClick="openWin('categoryPopup.php?category_id=<?php echo($rs["category_id"]); ?>', 'width=620, height=510');"><img src="../images/icons/view.gif" width="16" height="16" border="0" alt="Edit Record"></td>
                        <td><a href="category_master.php?delid=<?php echo($rs["category_id"]); ?>" onClick="return window.confirm('Are you sure you want to delete this record.');"><img 
						src="../images/icons/delete.gif" width="16" height="16" border="0" alt="Delete Record"></td>
					 </tr>
                </table></td>
				<td class="BlackAri12" valign=top width="20%">&nbsp;<small><?php echo($rs["category_name"]); ?><small></td>
                <td class="BlackAri12" valign=top width="20%">&nbsp;<small><?php echo  categorypath($rs["category_id"]); ?><small>
				</td>
				<td class="BlackAri12" width="50%">&nbsp;<small><?php echo(GetRecortarTexto(strip_tags($rs["category_description"]),50)); ?></small></td>
				<td align="center"><?php stsfx($rs["category_status"]); ?></td>
				<td class="BlackAri12" valign=top width="10%">&nbsp;<small><?php echo(categorycount($rs["category_id"],1)); ?><small></td>
            </tr>
			<?php } ?>
            <?if ($_GET["what"]=='')
				{?>
					<tr class="bgGray">
					   <td colspan="9" align="center" class="BlackAri12">
							<?php $pg->showPaging("", "&ps=".$_GET["ps"]."&sort=".$_GET["sort"]."&sortorder=".$_GET["sortorder"]."&criteria=".$_GET["criteria"]."&what=".$_GET["what"]); ?>
					  </td>
					</tr>
			<?}?>
        </table></td>
	</tr>
	
	<?php } else { ?>
	<tr>
		<td align="center" class="err">No Category found.</td>
	</tr>
	<?php } ?>
 <tr class="bgLightGray"><form name=search action="" method=get>
     <td width="100%" colspan=7 class="BlackAri12B">Search Category <a name=search></a>:
	
<input type=text name=what size=15 value="<?=$_GET["what"];?>">
<select name=criteria>
<option value=category_name >Category Name</option>
<option value=category_description  <?if ($_GET["criteria"]=='category_description') echo "selected";?>>Category Description</option>
</select>
<input type=submit name=search value=Search>
</form>
</td></tr>
<tr class="bgLightGray"><form name=search action="" method=get>
<td width="100%" colspan=7 class="BlackAri12B">Sort Categories:<br>
No Of Category Per Page:	
<input type=text name=ps size=3 value="<?=$_GET["ps"];?>"><br>
Sort By:
<select name=sort>
<option value=category_name>Category Name</option>
<option value=category_level <?if ($_GET["sort"]=='category_level') echo "selected";?>>Category lavel</option>
<option value=category_status <?if ($_GET["sort"]=='category_status') echo "selected";?>>Category Status</option>
</select>
<select name=sortorder>
<option value=desc <?if ($_GET["sortorder"]=='desc') echo "selected";?>>Decreasing</option>
<option value=asc <?if ($_GET["sortorder"]=='asc') echo "selected";?>>Increasing</option>

</select>
<input type=submit name=sortme value="Sort Articles">
</form>
</td></tr>


	
</table>
</form>

<?php include("includes/footer.php"); ?>
