<?
$page = "admin_entries.php";
include "admin_header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['p'])) { $p = (int) $_POST['p']; } elseif(isset($_GET['p'])) { $p = (int) $_GET['p']; } else { $p = 1; }
if(isset($_POST['s'])) { $s = $_POST['s']; } elseif(isset($_GET['s'])) { $s = $_GET['s']; } else { $s = "i"; }
$entries_per_page = 50;

$total_entries = mysql_num_rows(mysql_query("SELECT * FROM bhost_entries"));


if(($total_entries % $entries_per_page) != 0) {
$maxpage = ($total_entries) / $entries_per_page + 1;
} else {
$maxpage = ($total_entries) / $entries_per_page;
}
$maxpage = (int) $maxpage;
if($p > $maxpage) { $p = $maxpage; } elseif($p < 1) { $p = 1; }
$start = ($p - 1) * $entries_per_page;

if($maxpage != 0 & $maxpage != 1) {
if($p - 1 > 0 & $maxpage != 1) {  
$prev = $p - 1;  
$pp = "<a href='admin_entries.php?p=$prev&s=$s'>$admin_entries1</a>";  
} else {
$pp = "<font color='#777777'>$admin_entries1</font>";  
}
if($p + 1 <= $maxpage & $maxpage != 1) {  
$next = $p + 1;  
$np = "<a href='admin_entries.php?p=$next&s=$s'>$admin_entries2</a>";  
} else {
$np = "<font color='#777777'>$admin_entries2</font>";  
}
}




$d = "dd";
$t = "t";
$a = "a";
$i = "i";
$w = "w";


if($s == "dd") {
$sort = "date DESC";
$d = "d";
} elseif($s == "d") {
$sort = "date";
$d = "dd";
} elseif($s == "t") {
$sort = "title";
$t = "td";
} elseif($s == "td") {
$sort = "title DESC";
$t = "t";
} elseif($s == "a") {
$sort = "username, title";
$a = "ad";
} elseif($s == "ad") {
$sort = "username DESC, title";
$a = "a";
} elseif($s == "w") {
$sort = "weblog, title";
$w = "wd";
} elseif($s == "wd") {
$sort = "weblog DESC, title";
$w = "w";
} elseif($s == "i") {
$sort = "e_id DESC";
$i = "id";
} elseif($s == "id") {
$sort = "e_id";
$i = "i";
} else {
$sort = "e_id DESC";
$d = "i";
$s = "id";
}



if($s == "w" | $s == "wd") {
 $entries = mysql_query("SELECT bhost_entries.e_id, bhost_entries.w_id, bhost_entries.author_u_id, bhost_entries.title, bhost_entries.date, bhost_entries.draft, bhost_weblogs.weblog FROM bhost_entries LEFT JOIN bhost_weblogs ON bhost_entries.w_id=bhost_weblogs.w_id ORDER BY $sort LIMIT $start, $entries_per_page");
} else {
 $entries = mysql_query("SELECT bhost_entries.e_id, bhost_entries.w_id, bhost_entries.author_u_id, bhost_entries.title, bhost_entries.date, bhost_entries.draft, bhost_users.username FROM bhost_entries LEFT JOIN bhost_users ON bhost_entries.author_u_id=bhost_users.u_id ORDER BY $sort LIMIT $start, $entries_per_page");
}


if($task == "dodelete") {
while($entry = mysql_fetch_assoc($entries)) {
$var = "delete_$entry[e_id]";
$delete = $_POST[$var];
if($delete == "1") {
mysql_query("DELETE FROM bhost_entries WHERE e_id='$entry[e_id]'");
mysql_query("DELETE FROM bhost_comments WHERE e_id='$entry[e_id]'");
mysql_query("DELETE FROM bhost_trackbacks WHERE e_id='$entry[e_id]'");

// DECREMENT NUM_ENTRIES IN BHOST_USERS AND BHOST_WEBLOGS
mysql_query("UPDATE bhost_users SET num_entries=num_entries-1 WHERE u_id='$entry[author_u_id]'");
mysql_query("UPDATE bhost_weblogs SET num_entries=num_entries-1 WHERE w_id='$entry[w_id]'");
}}
header("Location: admin_entries.php?p=$p&s=$s");
exit();
}


echo $head;

echo "
<h2>$admin_entries3</h2>
$admin_entries4
<br><br>
";


if($total_entries > $entries_per_page) { 
echo "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td NOWRAP>$admin_entries7 $total_entries</td>
<td align='right' NOWRAP style='padding-right: 5px;'>
  <table cellpadding='0' cellspacing='0'>
  <form action='admin_entries.php' method='POST'>
  <tr>
  <td style='padding-bottom: 3px;'>";

if($pp != "") { echo "$pp <font class='gray'>|</font> "; }
if($maxpage != 1 AND $maxpage != 0) { echo "<input type='text' class='text' name='p' value='$p' size='1'>"; }
if($np != "") { echo " <font class='gray'>|</font> $np"; }

echo "
  </td>
  <input type='hidden' name='s' value='$s'>
  </tr>
  </form>
  </table>
</td>
</tr>
</table>
";
}


echo "
<form name='entriesform' action='admin_entries.php' method='POST'>

<script language='JavaScript'> 
<!--
var checkboxcount = 1;
function doCheckAll() {
if(checkboxcount == 0) {
with (document.entriesform) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = false;
}}
checkboxcount = checkboxcount + 1;
window.document.entriesform.check.alt='$admin_entries8';
}
} else
with (document.entriesform) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = true;
}}
checkboxcount = checkboxcount - 1;
window.document.entriesform.check.alt='$admin_entries9';
}}
function changeAlt(imgObj,newAlt){
imgObj.alt = newAlt;
}
// -->
</SCRIPT>
";




if(mysql_num_rows($entries) == 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td style='padding: 7px; border: 1px solid #AAAAAA; background: #FFFFFF;'><b>$admin_entries10</b></td>
</tr>
";
} else {
echo "
<table width='100%' cellpadding='0' cellspacing='0'>
<tr>
<td width='1%' class='header' style='padding-right: 0px; padding-bottom: 7px;'><a href='javascript:doCheckAll()'><img src='../images/check.gif' id='check' border='0' alt='$admin_entries8'></a></td>
<td class='header' width='1%' style='border-left: none;' NOWRAP><a href='admin_entries.php?p=$p&s=$i'><b>$admin_entries22</b></a></td>
<td class='header' width='1%' style='border-left: none;' NOWRAP><a href='admin_entries.php?p=$p&s=$d'><b>$admin_entries12</b></a></td>
<td class='header' width='1%' style='border-left: none;'><a href='admin_entries.php?p=$p&s=$a'><b>$admin_entries13</b></a></td>
<td class='header' width='1%' style='border-left: none;'><a href='admin_entries.php?p=$p&s=$w'><b>$admin_entries11</b></a></td>
<td class='header' width='95%' style='border-left: none;'><a href='admin_entries.php?p=$p&s=$t'><b>$admin_entries14</b></a></td>
<td class='header' width='1%' style='border-left: none;'><b>$admin_entries15</b></td>
</tr>
";
}

$count = 0;
while($entry = mysql_fetch_assoc($entries)) {
$count++;
if($count % 2) { $class = "list2"; } else { $class = "list2"; }
$author = mysql_fetch_assoc(mysql_query("SELECT username FROM bhost_users WHERE u_id='$entry[author_u_id]'"));
$weblog = mysql_fetch_assoc(mysql_query("SELECT weblog FROM bhost_weblogs WHERE w_id='$entry[w_id]'"));
$date = cdate("n/j/y g:i A", $entry[date]);
if($entry[draft] == 0) { $draft = "<a target='_blank' href='".url("entry", "$weblog[weblog]", "$author[username]", "$entry[e_id]")."'>$admin_entries16</a>"; } else { $draft = "<font class='disabled'>$admin_entries17</font>"; }

$title = strip_tags($entry[title]);
if(strlen($title) >= 50) {
$title = substr($title, 0, 50);
$title .= "...";
}

if($title == "") { $title = "<i>$admin_entries18</i>"; }
echo "
<tr>
<td class='$class' style='padding: 3px;' NOWRAP><input type='checkbox' name='delete_$entry[e_id]' value='1'></td>
<td class='$class' style='border-left: none;' NOWRAP>$entry[e_id] &nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP>$date &nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP><a href='".url("profile", "", "$author[username]")."' target='_blank'>$author[username]</a> &nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP><a href='".url("index", "$weblog[weblog]")."' target='_blank'>$weblog[weblog]</a> &nbsp;</td>
<td class='$class' style='border-left: none;'>$title &nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP>$draft <font class='gray'>|</font> <a href='admin_entryedit.php?e_id=$entry[e_id]&p=$p&s=$s'>$admin_entries19</a> <font class='gray'>|</font> <a href='admin_entrydelete.php?e_id=$entry[e_id]&p=$p&s=$s'>$admin_entries20</a></td>
</tr>
";
}


echo "</table>";

if(mysql_num_rows($entries) != 0) {
echo "
<br>
<input type='submit' class='button' value='$admin_entries21'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='task' value='dodelete'>
</form>
";
}

echo $foot;
?>