<?
$page = "admin_weblogs.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 = "id"; }
if(isset($_POST['w'])) { $w = $_POST['w']; } elseif(isset($_GET['w'])) { $w = $_GET['w']; } else { $w = ""; }
$weblogs_per_page = 50;

if($w == "") {
 $total_weblogs = mysql_num_rows(mysql_query("SELECT * FROM bhost_weblogs"));
} else {
 $total_weblogs = mysql_num_rows(mysql_query("SELECT * FROM bhost_weblogs WHERE weblog LIKE '%$w%'"));
}

if(($total_weblogs % $weblogs_per_page) != 0) {
$maxpage = ($total_weblogs) / $weblogs_per_page + 1;
} else {
$maxpage = ($total_weblogs) / $weblogs_per_page;
}

$maxpage = (int) $maxpage;
if($p > $maxpage) { $p = $maxpage; } elseif($p < 1) { $p = 1; }
$start = ($p - 1) * $weblogs_per_page;

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

//w_id
$i = "id";
//owner
$o = "o";
//weblog name
$n = "n";
//entries
$e = "e";
//creation date
$d = "d";

if($s == "i") {
$sort = "w_id";
$i = "id";
} elseif($s == "id") {
$sort = "w_id DESC";
$i = "i";
} elseif($s == "o") {
$sort = "owner";
$o = "od";
} elseif($s == "od") {
$sort = "owner DESC";
$o = "o";
} elseif($s == "n") {
$sort = "weblog";
$n = "nd";
} elseif($s == "nd") {
$sort = "weblog DESC";
$n = "n";
} elseif($s == "e") {
$sort = "num_entries";
$e = "ed";
} elseif($s == "ed") {
$sort = "num_entries DESC";
$e = "e";
} elseif($s == "d") {
$sort = "date_created";
$d = "dd";
} elseif($s == "dd") {
$sort = "date_created DESC";
$d = "d";
} else {
$sort = "w_id";
$i = "id";
$s = "i";
}



if($task == "dodelete") {
 if($w == "") {
  $weblogs = mysql_query("SELECT * FROM bhost_weblogs ORDER BY $sort LIMIT $start, $weblogs_per_page");
 } else {
  $weblogs = mysql_query("SELECT * FROM bhost_weblogs WHERE weblog LIKE '%$w%' ORDER BY $sort LIMIT $start, $weblogs_per_page");
 }
while($weblog = mysql_fetch_assoc($weblogs)) {
$var = "delete_$weblog[w_id]";
  if(isset($_POST[$var])) {
    if($_POST[$var] == "yes") {
      mysql_query("DELETE FROM bhost_comments WHERE w_id='$weblog[w_id]'");
      mysql_query("DELETE FROM bhost_entries WHERE w_id='$weblog[w_id]'");
      mysql_query("DELETE FROM bhost_entrycategories WHERE w_id='$weblog[w_id]'");
      mysql_query("DELETE FROM bhost_trackbacks WHERE w_id='$weblog[w_id]'");
      mysql_query("DELETE FROM bhost_weblogs WHERE w_id='$weblog[w_id]'");
    }
  }
}
header("Location: admin_weblogs.php?p=$p&s=$s&w=$w");
exit();
}


if($task == "delete") {
echo $head;
echo "
<h2>$admin_weblogs3</h2>
$admin_weblogs4
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='admin_weblogs.php' method='POST'>
<tr><td>
<input type='submit' value='$admin_weblogs5' class='button'>&nbsp;
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='w' value='$w'>
";
if($w == "") {
 $weblogs = mysql_query("SELECT * FROM bhost_weblogs ORDER BY $sort LIMIT $start, $weblogs_per_page");
} else {
 $weblogs = mysql_query("SELECT * FROM bhost_weblogs WHERE weblog LIKE '%$w%' ORDER BY $sort LIMIT $start, $weblogs_per_page");
}
while($weblog = mysql_fetch_assoc($weblogs)) {
$var = "delete_$weblog[w_id]";
  if(isset($_POST[$var])) {
    if($_POST[$var] == "yes") {
     echo "<input type='hidden' name='delete_$weblog[w_id]' value='yes'>";
    } else {
     echo "<input type='hidden' name='delete_$weblog[w_id]' value='no'>";
    }
  } else {
    echo "<input type='hidden' name='delete_$weblog[w_id]' value='no'>";
  }
}
echo "
<input type='hidden' name='task' value='dodelete'>
</td>
</form>
<form action='admin_weblogs.php' method='POST'>
<td>
<input type='submit' value='$admin_weblogs6' class='button'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='w' value='$w'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}




echo $head;

echo "
<h2>$admin_weblogs7</h2>
$admin_weblogs8
<br><br>

<table cellpadding='0' cellspacing='0'>
<tr><form action='admin_weblogs.php' method='POST'>
<td>$admin_weblogs18&nbsp;</td>
<td><input type='text' name='w' value='$w'>&nbsp;</td>
<td><input class='button' type='submit' value='$admin_weblogs9' style='margin-bottom: 2px;'></td>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='task' value='search'>
</form></tr>
</table>

<br>
";

if($total_weblogs > $weblogs_per_page) { 
echo "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td NOWRAP>$admin_weblogs10 $total_weblogs</td>
<td align='right' NOWRAP style='padding-right: 5px;'>
  <table cellpadding='0' cellspacing='0'>
  <form action='admin_weblogs.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>
  </tr>
  <input type='hidden' name='s' value='$s'>
  <input type='hidden' name='w' value='$w'>
  </form>
  </table>
</td>
</tr>
</table>
";
}

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

<table width='100%' cellpadding='0' cellspacing='0'>
<form action='admin_weblogs.php' name='weblogsform' method='POST'>
<tr>
";

if($w == "") {
 $weblogs = mysql_query("SELECT w_id, owner, weblog, date_created, num_entries AS entries FROM bhost_weblogs ORDER BY $sort LIMIT $start, $weblogs_per_page");
} else {
 $weblogs = mysql_query("SELECT w_id, owner, weblog, date_created, num_entries AS entries FROM bhost_weblogs WHERE weblog LIKE '%$w%' ORDER BY $sort LIMIT $start, $weblogs_per_page");
}

if(mysql_num_rows($weblogs) != 0) {
echo "<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='Check All'></a></td>";
} else {
echo "<td width='1%' class='header' style='padding-right: 0px; padding-bottom: 7px;'><img src='../images/check_disabled.gif' border='0' style='margin-right: 3px;'></td>";
}

echo "
<td class='header' width='1%' style='border-left: none;'><a href='admin_weblogs.php?p=$p&s=$i&w=$w'><b>$admin_weblogs11</b></a></td>
<td class='header' width='70%' style='border-left: none;'><a href='admin_weblogs.php?p=$p&s=$n&w=$w'><b>$admin_weblogs12</b></a></td>
<td class='header' width='14%' style='border-left: none;'><a href='admin_weblogs.php?p=$p&s=$o&w=$w'><b>$admin_weblogs13</b></a></td>
<td class='header' width='14%' style='border-left: none;'><a href='admin_weblogs.php?p=$p&s=$e&w=$w'><b>$admin_weblogs14</b></a></td>
<td class='header' width='1%' style='border-left: none;' NOWRAP><a href='admin_weblogs.php?p=$p&s=$d&w=$w'><b>$admin_weblogs15</b></a></td>
</tr>
";

if(mysql_num_rows($weblogs) == 0) {
echo "
<tr>
<td class='box' colspan='9'>$admin_weblogs16</td>
</tr>
";
}

$count = 0;
while($weblog = mysql_fetch_assoc($weblogs)) {
$count++;
if($count % 2) { $class = "list2"; } else { $class = "list2"; }

echo "
<tr>
<td class='$class' style='border-left: 1px solid #AAAAAA; padding: 3px;' NOWRAP><input type='checkbox' name='delete_$weblog[w_id]' value='yes'></td>
<td class='$class' style='border-left: none;' NOWRAP>$weblog[w_id]&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;'><a href='".url("profile", "", "$weblog[owner]")."' target='_blank'>$weblog[owner]</a>&nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP>$weblog[entries]&nbsp;</td>
<td class='$class' style='border-right: 1px solid #AAAAAA; border-left: none;' NOWRAP>".cdate("M. j, Y", $weblog[date_created])."&nbsp;</td>
</tr>
";
}

echo "
</table>
<br>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='w' value='$w'>
<input type='hidden' name='task' value='delete'>
<input class='button' type='submit' value='$admin_weblogs17'>
</form>
";

echo $foot;
?>