<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2006 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------+
| Modified by PHP-Fusion SF Group
| Copyright © 2007 by SF
| http://www.rus-phpfusion.com/
+----------------------------------------------------*/

require_once "../maincore.php";
require_once BASEDIR."subheader.php";
require_once ADMIN."navigation.php";

include LOCALE.LOCALESET."admin/downloads.php";

if (!checkrights("DC") || !defined("iAUTH") || $aid != iAUTH) fallback("../index.php");
if (isset($cat_id) && !isNum($cat_id)) fallback(FUSION_SELF.$aidlink);

if (isset($status)) {
        if ($status == "deln") {
                $title = $locale['400'];
                $message = "<b>".$locale['401']."</b><br>\n".$locale['402'];
        } elseif ($status == "dely") {
                $title = $locale['400'];
                $message = "<b>".$locale['405']."</b>";
        }elseif ($status=="delc"){
                $title = $locale['400'];
                $message = "<b>".$locale['003d']."</b><br>".$locale['004d'];
        }
        opentable($title);
        echo "<div align='center'>".$message."</div>\n";
        closetable();
        tablebreak();
}

if (isset($step) && $step == "delete") {
        $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='$cat_id'");
        if (dbrows($result) != 0) {
                redirect(FUSION_SELF.$aidlink."&status=delc");
        }else{
       $result = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_cat='$cat_id'");
        if (dbrows($result) != 0) {
                redirect(FUSION_SELF.$aidlink."&status=deln");
        } else {
                $result = dbquery("DELETE FROM ".$db_prefix."download_cats WHERE download_cat_id='$cat_id'");
                redirect(FUSION_SELF.$aidlink."&status=dely");
        }
        }
} else {
        if (isset($_POST['save_cat'])) {
                $cat_name = stripinput($_POST['cat_name']);
                $cat_description = stripinput($_POST['cat_description']);
                $cat_access = isNum($_POST['cat_access']) ? $_POST['cat_access'] : "0";
                $cat_sorting = stripinput($_POST['cat_sort_by'])." ".stripinput($_POST['cat_sort_order']);
                $cat_idcat = $_POST['cat_idc'];
                if (isset($step) && $step == "edit") {
                        $result = dbquery("UPDATE ".$db_prefix."download_cats SET download_cat_idc='$cat_idcat', download_cat_name='$cat_name', download_cat_description='$cat_description', download_cat_sorting='$cat_sorting', download_cat_access='$cat_access' WHERE download_cat_id='$cat_id'");
                } else {
                        $result = dbquery("INSERT INTO ".$db_prefix."download_cats VALUES('', '$cat_idcat', '$cat_name', '$cat_description', '$cat_sorting', '$cat_access')");
                }
                redirect(FUSION_SELF.$aidlink);
        }
        if (isset($step) && $step == "edit") {
                $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='$cat_id'");
                $data = dbarray($result);
                $cat_name = $data['download_cat_name'];
                $cat_description = $data['download_cat_description'];
                $cat_sorting = explode(" ", $data['download_cat_sorting']);
                $cat_access = $data['download_cat_access'];
                $formaction = FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data['download_cat_id'];
                $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_id='".$data['download_cat_id']."'");
                $data = dbarray($result);
                $cat_idcat = $data['download_cat_idc'];
                opentable($locale['420']);
        } else {
                $cat_name = "";
                $cat_description = "";
                $cat_sorting[0] = "download_title";
                $cat_sorting[1] = "ASC";
                $cat_access = "";
                $formaction = FUSION_SELF.$aidlink;
                $cat_idcat = 0;
                opentable($locale['421']);
        }
        $user_groups = getusergroups(); $access_opts = ""; $sel = "";
        while(list($key, $user_group) = each($user_groups)){
                $sel = ($cat_access == $user_group['0'] ? " selected" : "");
                $access_opts .= "<option value='".$user_group['0']."'$sel>".$user_group['1']."</option>\n";
        }
$ed=$locale['503'];
$del = $locale['504'];
function catTekush ($cats_id, $chort = ''){
global $db_prefix, $cell_color,$ed,$del, $aidlink;
if ($cats_id!=0){$chort.='-';}
$result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$cats_id."' ORDER BY download_cat_id ASC");
           while ($data2 = dbarray($result2)) {
            echo "<tr>
                   <td class='$cell_color'><b>$chort ".$data2['download_cat_name']."<b><br>
                   <span class='small'>".trimlink($data2['download_cat_description'], 45)."</span></td>
                   <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data2['download_cat_access'])."</td>
                   <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data2['download_cat_id']."'>".$ed."</a> -
                   <a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data2['download_cat_id']."'>".$del."</a></td>
                   </tr>\n";
            catTekush($data2['download_cat_id'],$chort);
            }
}
        echo "<form name='addcat' method='post' action='$formaction'>
<table align='center' cellpadding='0' cellspacing='0' width='400'>
<tr>
<td width='1%' class='tbl' style='white-space:nowrap'>".$locale['430']."</td>
<td class='tbl'><input type='text' name='cat_name' value='$cat_name' class='textbox' style='width:200px;'></td>
</tr>
<tr>
<td width='1%' class='tbl' style='white-space:nowrap'>".$locale['431']."</td>
<td class='tbl'><input type='text' name='cat_description' value='$cat_description' class='textbox' style='width:250px;'></td>
</tr>
<tr>
<td width='1%' class='tbl' style='white-space:nowrap'>".$locale['434']."</td>
<td class='tbl'><select name='cat_sort_by' class='textbox'>
<option value='download_id'".($cat_sorting[0] == "download_id" ? " selected" : "").">".$locale['435']."</option>
<option value='download_title'".($cat_sorting[0] == "download_title" ? " selected" : "").">".$locale['436']."</option>
<option value='download_datestamp'".($cat_sorting[0] == "download_datestamp" ? " selected" : "").">".$locale['437']."</option>
</select> -
<select name='cat_sort_order' class='textbox'>
<option value='ASC'".($cat_sorting[1] == "ASC" ? " selected" : "").">".$locale['438']."</option>
<option value='DESC'".($cat_sorting[1] == "DESC" ? " selected" : "").">".$locale['439']."</option>
</select></td>
</tr>
<tr>
<td width='1%' class='tbl' style='white-space:nowrap'>".$locale['433']."</td>
<td class='tbl'><select name='cat_access' class='textbox' style='width:150px;'>
$access_opts</select></td>
</tr>
<tr>
<td width='1%' class='tbl' style='white-space:nowrap'>".$locale['001d']."</td>
<td class='tbl'>
<select name='cat_idc' class='textbox' style='width:150px;' >
<option value='0'".($cat_idcat == 0 ? " selected" : "").">".$locale['002d']."</option>";
function catSelect($cats_id = 0, $chort = ''){
global $db_prefix, $cat_idcat ;
if ($cats_id!=0){$chort.='-';}
$result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$cats_id."' ORDER BY download_cat_id ASC");
while ($data2 = dbarray($result2)) {
      echo "<option value='".$data2['download_cat_id']."'".($cat_idcat == $data2['download_cat_id'] ? " selected" : "").">$chort ".$data2['download_cat_name']."</option>";
      catSelect($data2['download_cat_id'], $chort);
}
}
catSelect();
echo "</select></td>
</tr>
<tr>
<td align='center' colspan='2' class='tbl'>
<input type='submit' name='save_cat' value='".$locale['432']."' class='button'></td>
</tr>
</table>
</form>\n";
        closetable();
        tablebreak();
        opentable($locale['440']);
        echo "<table align='center' width='400' cellspacing='1' cellpadding='0' class='tbl-border'>\n";
        $result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='0' ORDER BY download_cat_id ASC");
        if (dbrows($result) != 0) {
                $i = 0;
                echo "<tr>
<td class='tbl2'>".$locale['441']."</td>
<td align='center' width='1%' class='tbl2' style='white-space:nowrap'>".$locale['445']."</td>
<td align='center' width='1%' class='tbl2' style='white-space:nowrap'>".$locale['502']."</td>
</tr>\n";
                while ($data = dbarray($result)) {
                        $cell_color = ($i % 2 == 0 ? "tbl1" : "tbl2");
                        echo "<tr>
<td class='$cell_color'><b>".$data['download_cat_name']."</b><br>
<span class='small'>".trimlink($data['download_cat_description'], 45)."</span></td>
<td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data['download_cat_access'])."</td>
<td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data['download_cat_id']."'>".$locale['503']."</a> -
<a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data['download_cat_id']."'>".$locale['504']."</a></td>
</tr>\n";

catTekush($data['download_cat_id'],'');
           /*$result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$data['download_cat_id']."' ORDER BY download_cat_id ASC");
           while ($data2 = dbarray($result2)) {
            echo "<tr>
                   <td class='$cell_color'><b>- ".$data2['download_cat_name']."<b><br>
                   <span class='small'>".trimlink($data2['download_cat_description'], 45)."</span></td>
                   <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data2['download_cat_access'])."</td>
                   <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data2['download_cat_id']."'>".$locale['503']."</a> -
                   <a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data2['download_cat_id']."'>".$locale['504']."</a></td>
                   </tr>\n";
                   $result3= dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$data2['download_cat_id']."' ORDER BY download_cat_id ASC");
                   while ($data3 = dbarray($result3)) {
                   echo "<tr>
                          <td class='$cell_color'><b>-- ".$data3['download_cat_name']."<b><br>
                          <span class='small'>".trimlink($data3['download_cat_description'], 45)."</span></td>
                          <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data3['download_cat_access'])."</td>
                          <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data3['download_cat_id']."'>".$locale['503']."</a> -
                          <a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data3['download_cat_id']."'>".$locale['504']."</a></td>
                          </tr>\n";
                          $result4= dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$data3['download_cat_id']."' ORDER BY download_cat_id ASC");
                          while ($data4 = dbarray($result4)) {
                                 echo "<tr>
                                 <td class='$cell_color'><b>--- ".$data4['download_cat_name']."<b><br>
                                 <span class='small'>".trimlink($data4['download_cat_description'], 45)."</span></td>
                                 <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data4['download_cat_access'])."</td>
                                 <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data4['download_cat_id']."'>".$locale['503']."</a> -
                                 <a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data4['download_cat_id']."'>".$locale['504']."</a></td>
                                 </tr>\n";
                                 $result5= dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$data4['download_cat_id']."' ORDER BY download_cat_id ASC");
                                 while ($data5 = dbarray($result5)) {
                                        echo "<tr>
                                        <td class='$cell_color'><b>---- ".$data5['download_cat_name']."<b><br>
                                        <span class='small'>".trimlink($data5['download_cat_description'], 45)."</span></td>
                                        <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'>".getgroupname($data5['download_cat_access'])."</td>
                                        <td align='center' width='1%' class='$cell_color' style='white-space:nowrap'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;cat_id=".$data5['download_cat_id']."'>".$locale['503']."</a> -
                                        <a href='".FUSION_SELF.$aidlink."&step=delete&amp;cat_id=".$data5['download_cat_id']."'>".$locale['504']."</a></td>
                                        </tr>\n";
                                 }
                          }
                   }
            }*/
                        $i++;
                }
                echo "</table>\n";
        } else {
                echo "<tr><td align='center' class='tbl1'>".$locale['508']."</td></tr>\n</table>\n";
        }
        closetable();
}

echo "</td>\n";
require_once BASEDIR."footer.php";
?>