<?PHP
//........................

$table = "$prefix" . "CATEGORIES";
$editpage = "categories";
$return = "categories.php";

//........................

$sql = "SELECT count(*) FROM $table"; $res = @mysql_query($sql); $row = @mysql_fetch_row($res); $numcats = $row[0];

//........................

if (!empty($_GET['id'])) {

        $SQL = "SELECT * from $table WHERE ID = '$_GET[id]'"; 
        $result = mySQL_query( $SQL ); 
        $row = mySQL_fetch_array( $result );

} 

//........................

$qt = insert;
if (!empty($_GET['id']))
        $qt = update;

//........................

if ($_GET['delete'] == 1) {
        foreach ($_POST as $key => $value) {
                $sql = "DELETE FROM $table WHERE ID = '$value'";
                mysql_query("$sql");

                $sql = "DELETE FROM $table WHERE PARENT = '$value'"; 
                mysql_query("$sql");

                $sql = "DELETE FROM $table WHERE GRANDPARENT = '$value'"; 
                mysql_query("$sql");
        }
}

//........................

?>

<h1><?PHP /* (LANGUAGE) */ $t = "Categories"; $f = "header"; include "langinc.php"; /* (LANGUAGE) */ ?></h1>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr> 

<td style="padding-right:10px;" width="50%" valign="top">

<form name="query" method="post" action="index.php?page=query">

<input type="hidden" name="controller" value="<?php echo $table; ?>~<?php echo $qt; ?>~<?php echo $return; ?>~<?php echo $_GET['id']; ?>">

<h3><?PHP /* (LANGUAGE) */ $t = "Category Title"; $f = "title"; include "langinc.php"; /* (LANGUAGE) */ ?></h3>

<input class="fieldbox" type="text" name="TITLE" value="<?php echo $row[TITLE]; ?>" style="width: 100%;">

<?php
//........................
if (empty($_GET['id']))
        $add = "Add Category";
else
        $add = "Edit Category";
//........................
?>

<input style="margin-top: 15px;" type="submit" name="Submit" class="deltabutton" value="<?PHP /* (LANGUAGE) */ $t = "$add"; $f = "addedit"; include "langinc.php"; /* (LANGUAGE) */ ?>">

</form>

<?php 
//........................
if (!empty($_GET[id]) && $_GET[nosub] != '1') { 
//........................
?>

<form name="query" method="post" action="index.php?page=query&id=<?php echo $_GET[id]; ?>">

<input type="hidden" name="controller" value="<?php echo $table; ?>~insert~<?php echo $return; ?>~<?php echo $nil; ?>">

<input type="hidden" name="PARENT" value="<?php echo $row[ID]; ?>">

<input type="hidden" name="GRANDPARENT" value="<?php echo $row[GRANDPARENT]; ?><?php if (empty($row[GRANDPARENT])) echo $_GET[gp]; ?>">

<h3 style="margin-top: 15px;"><?PHP /* (LANGUAGE) */ $t = "Subcategory Title"; $f = "subtitle"; include "langinc.php"; /* (LANGUAGE) */ ?></h3>

<input style="width: 100%;" class="fieldbox" type="text" name="TITLE">

<input style="margin-top: 15px;" type="submit" name="Submit" class="deltabutton" value="<?PHP /* (LANGUAGE) */ $t = "Add Subcategory"; $f = "addsubcategory"; include "langinc.php"; /* (LANGUAGE) */ ?>">

</form>

<?PHP } ?>

</td>

<td style="padding-left:10px;" width="50%" valign="top"> 

<form name="form" method="post" action="index.php?page=<?php echo $editpage; ?>&delete=1">

<h3><?PHP /* (LANGUAGE) */ $t = "Click a Category to Rename or Add Subcategories"; $f = "categorybrowsercaption"; include "langinc.php"; /* (LANGUAGE) */ ?></h3>

<div class="primebox" id="categorybox" style="width:100%; z-index:1; overflow: auto; margin-bottom: 15px;">

<?PHP if (empty($numcats)) { ?><?PHP /* (LANGUAGE) */ $t = "You do not have any categories yet."; $f = "nocats"; include "langinc.php"; /* (LANGUAGE) */ ?>
 
<?PHP  } else { ?>

<?PHP
//........................
$sqlC="SELECT * FROM $table WHERE PARENT = '$parent' ORDER BY TITLE ASC";
$resultC = mysql_query($sqlC);
while($rowC = mysql_fetch_array($resultC))
{
$crow = $rowC[ID];
//.....................
?>

<input type="checkbox" name="<?php echo $crow; ?>" value="<?php echo $crow; ?>" <?PHP if (!empty($row[$crow]) && empty($_GET[id])) echo checked; ?>>

<span class="linkmedium"> <a href="index.php?page=categories&id=<?php echo $crow; ?>"><?PHP echo $rowC[TITLE]; ?></a></span><br>

<?PHP
        //........................

        $sqlD="SELECT * FROM $table WHERE PARENT = '$rowC[ID]' ORDER BY TITLE ASC";
        $resultD = mysql_query($sqlD);
        while($rowD = mysql_fetch_array($resultD))
        {
        $crow = $rowD[ID];

        //.....................
        ?>
        
<input style="margin-left: 20px;" type="checkbox" name="<?php echo $crow; ?>" value="<?php echo $crow; ?>" <?PHP if (!empty($row[$crow]) && empty($_GET[id])) echo checked; ?>>

<span class="linksmall"><a href="index.php?page=categories&id=<?php echo $crow; ?>&gp=<?php echo $rowC[ID]; ?>"><?PHP echo $rowD[TITLE]; ?></a></span>

<br>

<?PHP
                //........................

                $sqlE="SELECT * FROM $table WHERE PARENT = '$rowD[ID]' ORDER BY TITLE ASC";
                $resultE = mysql_query($sqlE);
                while($rowE = mysql_fetch_array($resultE))
                {
                $crow = $rowE[ID];

                //.....................
                ?>
                
<input style="margin-left: 37px;" type="checkbox" name="<?php echo $crow; ?>" value="<?php echo $crow; ?>" <?PHP if (!empty($row[$crow]) && empty($_GET[id])) echo checked; ?>>

<span class="linkverysmall"><a href="index.php?page=categories&id=<?php echo $crow; ?>&nosub=1"><?PHP echo $rowE[TITLE]; ?></a></span>

<br>

<?PHP
                //........................

                }

                //........................
                ?>
<?PHP
        //........................

        }

        //........................
        ?>
<?PHP
//........................

}

//........................
?>

<?PHP  } ?>

</div>

<?php include "deletebar.php"; ?>

</form>

</td>

</tr>

</table>
