<?
$page = "entrycat.php";
include "manager_header.php";

// CHECK IF CATEGORIES ARE DISABLED BY ADMIN
if($owner_group_info[allow_entry_cat] != "1") { exit(); }

// CHECK IF THIS USER IS ONLY A MEMBER
if($user_status == "member") { exit(); }

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['ec_id'])) { $ec_id = $_POST['ec_id']; } elseif(isset($_GET['ec_id'])) { $ec_id = $_GET['ec_id']; } else { $ec_id = 0; }




if($task == "cancel") { 
 header("Location: entrycat.php"); 
 exit(); 
}

if($task == "doadd") {
$name = $_POST['name'];


if(strtolower($name) == "friends" | strtolower($name) == "archive" | preg_match("/page[0-9]+/", $name)) {
$is_error = "yes";
$error = "$entrycat15";
} elseif(preg_match("/[^a-z,A-Z,0-9, ,_,-]/", $name) | preg_match("/[^0-9]/", $name) != TRUE) {
$is_error = "yes";
$error = "$entrycat16";
} elseif(mysql_num_rows(mysql_query("SELECT * FROM bhost_entrycategories WHERE name='$name' AND w_id='$weblog_info[w_id]'")) != 0) {
$is_error = "yes";
$error = "$entrycat17";
} elseif(str_replace(" ", "", $name) == "") {
$is_error = "yes";
$error = "$entrycat18";
}

if($is_error == "yes") {
$task = "add";
} else {
mysql_query("INSERT INTO bhost_entrycategories (w_id, name) VALUES ('$weblog_info[w_id]', '$name')");
header("Location: entrycat.php");
exit();
}}











if($task == "doedit") {
if(mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'")) != 1) { header("Location: entrycat.php"); exit(); }
$name = $_POST['name'];


if(strtolower($name) == "friends" | strtolower($name) == "archive") {
$is_error = "yes";
$error = "$entrycat15";
} elseif(preg_match("/[^a-z,A-Z,0-9, ,_,-]/", $name) | preg_match("/[^0-9]/", $name) != TRUE) {
$is_error = "yes";
$error = "$entrycat16";
} elseif(str_replace(" ", "", $name) == "") {
$is_error = "yes";
$error = "$entrycat18";
}

if($is_error == "yes") {
$task = "edit";
} else {
mysql_query("UPDATE bhost_entrycategories SET name='$name' WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'");
header("Location: entrycat.php");
exit();
}}


if($task == "add" | $task == "edit") {
 if($task == "edit") { 
  if(mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'")) != 1) { header("Location: entrycat.php"); exit(); }
  $entry_cat_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id' LIMIT 1"));
  $name = $entry_cat_info[name];
 }
if(!isset($name)) { $name = ""; }
if(!isset($error)) { $error = ""; }

echo $head;

if($task == "add") {
echo "
<h2>$entrycat19</h2>
$entrycat20
";
} elseif($task == "edit") { 
echo "
<h2>$entrycat21</h2>
$entrycat22
";
}

echo "
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='entrycat.php' method='POST'>
<tr>
<td>$entrycat3:&nbsp;</td>
<td><input type='text' name='name' size='30' class='text' value='$name' maxlength='255'></td>
</tr>
<tr><td colspan='2'>&nbsp;</td></tr>
<tr>
<td>&nbsp;</td>
<td>
";
if($task == "edit") {
 echo "
  <table cellspacing='0' cellpadding='0'>
  <tr>
  <td><input type='submit' name='submit' value='$entrycat12' class='button'></td>
  <input type='hidden' name='task' value='doedit'>
  <input type='hidden' name='ec_id' value='$ec_id'>
  </form>
 ";
} else {
 echo "
  <table cellspacing='0' cellpadding='0'>
  <tr>
  <td><input type='submit' name='submit' value='$entrycat11' class='button'></td>
  <input type='hidden' name='task' value='doadd'>
  </form>
 ";
}
echo "
  <form action='entrycat.php' method='POST'>
  <td>&nbsp;<input type='submit' name='submit' class='button' value='$entrycat13'></td>
  <input type='hidden' name='task' value='cancel'>
  </form>
  </tr>
  </table>
</td></tr>
</table>
$error
";
echo $foot;
exit();

}










if($task == "delete") {
if(mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'")) != 1) { header("Location: entrycat.php"); exit(); }
$entry_cat_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id' LIMIT 1"));
echo $head;
echo "
<h2>$entrycat23</h2>
$entrycat24
<br><br>

<table cellpadding='0' cellspacing='0'>
<tr>
<form action='entrycat.php' method='POST'>
<td><input type='submit' name='submit' value='$entrycat14' class='button'></td>
<input type='hidden' name='task' value='dodelete'>
<input type='hidden' name='ec_id' value='$ec_id'>
</form>
<form action='entrycat.php' method='POST'>
<td>&nbsp;<input type='submit' name='submit' value='$entrycat13' class='button'></td>
<input type='hidden' name='task' value='cancel'>
</form>
</tr>
</form>
</table>
";
echo $foot;
exit();

}


if($task == "dodelete") {
if(mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'")) != 1) { header("Location: entrycat.php"); exit(); }

mysql_query("DELETE FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'");
mysql_query("UPDATE bhost_entries SET ec_id='0' WHERE w_id='$weblog_info[w_id]' AND ec_id='$ec_id'");
header("Location: entrycat.php");
exit();
}



echo $head;
echo "

<h2>$entrycat1</h2>
$entrycat2
<br><br>


<table cellpadding='0' cellspacing='0'>
<tr>
<td width='150' NOWRAP class='header2'>$entrycat3</td>
<td class='header2'>$entrycat4</td>
<td class='header'>$entrycat5</td>
</tr>
";

$none_entries = mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND ec_id='0'"));
echo "
<tr>
<td class='list2a'>$entrycat6</td>
<td class='list2a' align='center'>$none_entries</td>
<td class='list2b'>$entrycat7</td>
</tr>
";

$count = 0;
$entry_cats = mysql_query("SELECT * FROM bhost_entrycategories WHERE w_id='$weblog_info[w_id]' ORDER BY name");
while($entry_cat_info = mysql_fetch_assoc($entry_cats)) {
$count++;
if($count % 2) { $c = "1"; } else { $c = "2"; }
$cat_entries = mysql_num_rows(mysql_query("SELECT ec_id FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND ec_id='$entry_cat_info[ec_id]'"));
$cat_name = str_replace(" ", "+", $entry_cat_info[name]);
echo "
<tr>
<td class='list".$c."a'>$entry_cat_info[name]</td>
<td class='list".$c."a' align='center'>$cat_entries</td>
<td class='list".$c."b'><a target='_blank' href='".url("index_cat", "$weblog_info[weblog]", "", "", "", "", "", "", "$cat_name")."'>$entrycat8</a> <font class='gray'>|</font> <a href='entrycat.php?task=edit&ec_id=$entry_cat_info[ec_id]'>$entrycat9</a> <font class='gray'>|</font> <a href='entrycat.php?task=delete&ec_id=$entry_cat_info[ec_id]'>$entrycat10</a></td>
</tr>
";
}

echo "
</table>

<br>
<form action='entrycat.php' method='POST'>
<input type='submit' value='$entrycat11' class='button'>
<input type='hidden' name='task' value='add'>
</form>
";

echo $foot;

?>