<?PHP
$result="";
if($member_db[1] != 1){ msg("error", "Hozzáférés letiltva!", "Nincs jogod ehhez a művelethez."); }

// ********************************************************************************
// Add Category
// ********************************************************************************
if($action == "add")
{
    $cat_name = htmlspecialchars(stripslashes($cat_name));
    if(!$cat_name){ msg("error","Hiba!!!","Írd be a kategória nevét", "javascript:history.go(-1)"); }
    $cat_icon = preg_replace("/ /", "", $cat_icon);
    if($cat_icon == "(optional)"){ $cat_icon = ""; }

    $big_num = file("./data/cat.num.php");
    $big_num = $big_num[0];
    if(!$big_num or $big_num == ""){ $big_num = 1; }

    $all_cats = file("./data/category.db.php");
    foreach($all_cats as $cat_line)
    {
                $cat_arr = explode("|", $cat_line);
        if($cat_arr[1] == $cat_name){ msg("error", "Hiba!!!", "Már létezik egy kategória ilyen névvel", "?mod=categories"); }
        if($cat_arr[0] == $big_num){ $big_num = 33; }
    }
    $new_cats = fopen("./data/category.db.php", "a");
    $cat_name = stripslashes( preg_replace(array("'\|'",), array("&#124",), $cat_name) );
    $cat_icon = stripslashes( preg_replace(array("'\|'",), array("&#124",), $cat_icon) );
        fwrite($new_cats, "$big_num|$cat_name|$cat_icon|$cat_access|||\n");
        fclose($new_cats);
    $big_num ++;

    $num_file = fopen("./data/cat.num.php", "w");
    fwrite($num_file, $big_num);
    fclose($num_file);
}
// ********************************************************************************
// Remove Category
// ********************************************************************************
elseif($action == "remove")
{
        if(!$catid){ msg("error","Hiba!!!","Nincs kategóriaazonosító", "$PHP_SELF?mod=categories"); }

    $old_cats = file("./data/category.db.php");
    $new_cats = fopen("./data/category.db.php", "w");

    foreach($old_cats as $old_cats_line){
            $cat_arr = explode("|", $old_cats_line);
            if($cat_arr[0] != $catid){
                        fwrite($new_cats, $old_cats_line);
        }
    }
        fclose($new_cats);
}
// ********************************************************************************
// Edit Category
// ********************************************************************************
elseif($action == "edit")
{
        if(!$catid){ msg("error","Hiba!!!","Nincs kategóriaazonosító", "$PHP_SELF?mod=categories"); }

    $all_cats = file("./data/category.db.php");
    foreach($all_cats as $cat_line){
            $cat_arr = explode("|", $cat_line);
            if($cat_arr[0] == $catid){

            $if_all_access = ($cat_arr[3] == "" || $cat_arr[3] == "0") ? "selected" :  "";

            $if_1_access = ($cat_arr[3] == "1") ? "selected" :  "";
            $if_2_access = ($cat_arr[3] == "2") ? "selected" :  "";


$msg=<<<HTML
        <form action=$PHP_SELF?mod=categories method=post>
  <table border="0" >
    <tr>
      <td width="100" >Név</td>
      <td  ><input value="$cat_arr[1]" type=text name=cat_name></td>
    </tr>
    <tr>
      <td  >Ikon</td>
      <td  ><input value="$cat_arr[2]" type=text name=cat_icon></td>
    </tr>


    <tr>
        <td >
    Hozzáférési jog
        <td >

        <select name="cat_access">
         <option $if_all_access value="0" selected>Mindenki</option>
         <option $if_2_access value="2">Szerkesztők és admin</option>
         <option $if_1_access value="1">Csak admin</option>

        </select>

    </tr>


    <tr>
      <td></td>
      <td ><br><input type=submit value="Változatások mentése"</td>
    </tr>
  </table>
  <input type=hidden name=action value=doedit>
  <input type=hidden name=catid value=$catid>
  </form>
HTML;

        msg("options", "Kategóriák szerkesztése", $msg);

        }
    }
}
// ********************************************************************************
// DO Edit Category
// ********************************************************************************
elseif($action == "doedit")
{
    $cat_name = htmlspecialchars(stripslashes($cat_name));
    if(!$catid){ msg("error","Hiba!!!","Nincs kategóriaazonosító", "$PHP_SELF?mod=categories"); }
    if($cat_name == ""){ msg("error", "Hiba!!!", "A név mező nem lehet üres.", "javascript:history.go(-1)"); }

    $old_cats = file("./data/category.db.php");
    $new_cats = fopen("./data/category.db.php", "w");
    foreach($old_cats as $cat_line){
            $cat_arr = explode("|", $cat_line);
            if($cat_arr[0] == $catid){
                        fwrite($new_cats, "$catid|$cat_name|$cat_icon|$cat_access|||\n");
        }else{
                        fwrite($new_cats, "$cat_line");
        }
    }
    fclose($new_cats);
}
// ********************************************************************************
// List all Categories
// ********************************************************************************
echoheader("options", "Kategóriák szerkesztése");
echo<<<HTML

        <table border=0 cellpading=0 cellspacing=0 width="645" >
    <form method=post action="$PHP_SELF">
    <td width=321 height="33">
    <b>Kategóriák hozzáadása</b>
<table border=0 cellpading=0 cellspacing=0 width=300  class="panel" >
    <tr>
    <td width=130 height="25">
    &nbsp;Név
        <td  height="25">
    <input type=text name=cat_name>
    </tr>
    <tr>
        <td height="22">
    &nbsp;Ikon URL
        <td height="22">
        <input onFocus="this.select()" value="(optional)" type=text name=cat_icon>
    </tr>


    <tr>
        <td height="22">
    &nbsp;Hozzáférési jog
        <td height="22">

        <select name="cat_access">
         <option value="0" selected>Mindenki</option>
         <option value="2">Szerkesztők és admin</option>
         <option value="1">Csak admin</option>

        </select>

    </tr>


    <tr>
        <td width=98 height="32">
    &nbsp;
        <td width=206 height="32">
    <input type=submit value="  Kategória hozzáadása  ">
<input type=hidden name=mod value=categories>
<input type=hidden name=action value=add>
    </tr>
    </form>
    </table>


        <td width=320 height="33" align="center">
       <!-- HELP -->
   <table height="25" cellspacing="0" cellpadding="0">
    <tr>
      <td width="25" align=middle><img border="0" src="skins/images/help_small.gif"></td>
      <td >&nbsp;<a onClick="javascript:Help('categories')" href="#">Mik azok a kategóriák<br>
        &nbsp;és hogyan használjam őket?</a></td>
    </tr>
  </table><br>
     <!-- END HELP -->

    <tr>
        <td width=654 colspan="2" height="11">
        <img height=20 border=0 src="skins/images/blank.gif" width=1>
    </tr>
HTML;


$all_cats = file("./data/category.db.php");
$count_categories = 0;
foreach($all_cats as $cat_line)
{
    if($i%2 != 0){ $bg = "bgcolor=#F7F6F4"; }else{ $bg = ""; }
    $i++;
    $cat_arr = explode("|", $cat_line);
    $cat_arr[1] = stripslashes( preg_replace(array("'\"'", "'\''"), array("&quot;", "&#039;"), $cat_arr[1]) );
    $cat_help_names[] = $cat_arr[1];
    $cat_help_ids[] = $cat_arr[0];
    $result .= "

    <tr>
      <td $bg >&nbsp;<b>$cat_arr[0]</b></td>
      <td $bg >$cat_arr[1]</td>
      <td $bg >";
      if($cat_arr[2] != ""){ $result .= "<img border=0 src=\"$cat_arr[2]\" high=40 width=40 alt=\"$cat_arr[2]\">"; }
      else{ $result .= "---"; }
      $result .= "</td><td $bg >";

            $result .= ($cat_arr[3] == "" || $cat_arr[3] == "0") ? "<span title='Mindenki írhat'>---</span>" :  "";
            $result .= ($cat_arr[3] == "1") ? "Csak admin" :  "";
            $result .= ($cat_arr[3] == "2") ? "Szerkesztők és admin" :  "";


      $result .= "</td>
      <td $bg ><a href=\"$PHP_SELF?mod=categories&action=edit&catid=$cat_arr[0]\">[szerkesztés]</a> <a href=\"$PHP_SELF?mod=categories&action=remove&catid=$cat_arr[0]\">[törlés]</a></td>
    </tr>";
        $count_categories ++;
}

if($count_categories == 0){
    echo"<tr>
        <td width=654 colspan=2 height=14>
    <p align=center><br><b>Még nem találhatók kategóriák.</b><br>
       Nem muszáj kategóriákat megadnod, írhatsz híreket nélkülük is.<br>
    </tr>
    <tr>";
}else{
        echo"<tr>
        <td width=654 colspan=2 height=14>
    <b>Categories</b>
    </tr>
    <tr>
        <td width=654 colspan=2 height=1>
  <table width=100% height=100% cellspacing=0 cellpadding=0>
    <tr>
      <td width=6% bgcolor=#F7F6F4>&nbsp;<u>ID</u></td>
      <td width=30% bgcolor=#F7F6F4><u>név</u></td>
      <td width=14% bgcolor=#F7F6F4><u>ikon</u></td>
      <td width=20% bgcolor=#F7F6F4><u>hozzáférés</u></td>
      <td width=20% bgcolor=#F7F6F4><u>művelet</u></td>
    </tr>";

    echo $result;

    echo"</table>";
}

echo"
</table>";
echofooter();
?>