<?
$page = "view_entries.php";
include "manager_header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['page'])) { $page = (int) $_POST['page']; } elseif(isset($_GET['page'])) { $page = (int) $_GET['page']; } else { $page = 1; }
if(isset($_POST['s'])) { $s = $_POST['s']; } elseif(isset($_GET['s'])) { $s = $_GET['s']; } else { $s = "dd"; }
if(isset($_POST['search'])) { $search = str_replace("'", "&#39;", stripslashes($_POST['search'])); } else { $search = ""; }
if(isset($_POST['search_in'])) { $search_in = $_POST['search_in']; } else { $search_in = ""; }
$entries_per_page = "20";


if($search != "") {

 if($search_in == "title") {
  $total_entries = mysql_num_rows(mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND title LIKE '%$search%'"));
 } elseif($search_in == "contents") {
  $total_entries = mysql_num_rows(mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND contents LIKE '%$search%'"));
 } else {
  $total_entries = mysql_num_rows(mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND (title LIKE '%$search%' OR contents LIKE '%$search%')"));
 }
 $maxpage = 1;
 $entries_per_page = $total_entries;

} else {

 $total_entries = mysql_num_rows(mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]'"));

 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($page > $maxpage) { $page = $maxpage; } elseif($page < 1) { $page = 1; }
 $start = ($page - 1) * $entries_per_page;

 if($maxpage != 0 & $maxpage != 1) {
 if($page - 1 > 0 & $maxpage != 1) {  
 $prev = $page - 1;  
 $pp = "<a href='view_entries.php?page=$prev&s=$s'>$view_entries1</a>";  
 } else {
 $pp = "<font class='gray'>$view_entries1</font>";  
 }
 if($page + 1 <= $maxpage & $maxpage != 1) {  
 $next = $page + 1;  
 $np = "<a href='view_entries.php?page=$next&s=$s'>$view_entries2</a>";  
 } else {
 $np = "<font class='gray'>$view_entries2</font>";  
 }
 }
}


$t = "t";
$d = "dd";

if($s == "d") {
$sort = "date";
$d = "dd";
} elseif($s == "dd") {
$sort = "date DESC";
$d = "d";
} elseif($s == "t") {
$sort = "title";
$t = "td";
} elseif($s == "td") {
$sort = "title DESC";
$t = "t";
} else {
$sort = "date";
$d = "dd";
$s = "d";
}


if($task == "delete") {
 if($user_status != "owner" & $user_status != "admin") { $where = "author_u_id='$user_info[u_id]'"; } else { $where = "author_u_id LIKE '%'"; }
if($search != "") {
 if($search_in == "title") {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND $where AND title LIKE '%$search%'");
 } elseif($search_in == "contents") {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND $where AND contents LIKE '%$search%'");
 } else {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND $where AND (title LIKE '%$search%' OR contents LIKE '%$search%')");
 }
} else {
 $entries = mysql_query("SELECT * FROM bhost_entries WHERE $where ORDER BY $sort LIMIT $start, $entries_per_page");
}
while($entry = mysql_fetch_assoc($entries)) {
$var = "delete_$entry[e_id]";
  if(isset($_POST[$var])) {
    if($_POST[$var] == "yes") {
      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("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: view_entries.php?page=$page&s=$s");
exit();
}

if($user_info[fname] == "") { $welcome = "$view_entries3"; } else { $welcome = "$view_entries3 $user_info[fname]."; }
echo $head;
echo "

<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='$view_entries7';
}
} 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='$view_entries18';
}}

function changeAlt(imgObj,newAlt){
imgObj.alt = newAlt;
}


// -->
</SCRIPT>


<h2>$view_entries4</h2>
$view_entries5
<br><br>
<form action='view_entries.php' method='POST' name='search'>
$view_entries19 <input type='text' name='search' value='$search'> $view_entries20
<select name='search_in'>
<option value='title and contents'"; if($search_in == "title and contents") { echo " SELECTED"; } echo ">$view_entries21</option>
<option value='title'"; if($search_in == "title") { echo " SELECTED"; } echo ">$view_entries22</option>
<option value='contents'"; if($search_in == "contents") { echo " SELECTED"; } echo ">$view_entries23</option>
</select>
<input type='submit' value='$view_entries24' class='sbutton' style='margin-bottom: 2px;'>
<input type='hidden' name='task' value='search'>
</form><br>

<script language='JavaScript'>
<!-- 
appendEvent = function(el, evname, func) {
 if (el.attachEvent) { // IE
   el.attachEvent('on' + evname, func);
 } else if (el.addEventListener) { // Gecko / W3C
   el.addEventListener(evname, func, true);
 } else {
   el['on' + evname] = func;
 }
};
appendEvent(window, 'load', windowonload);

function windowonload() { 
window.document.search.search.focus(); 
window.document.search.search.select(); 
} 
// -->
</script>
";


if($total_entries != 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td style='padding-bottom: 5px;' NOWRAP width='10%'>$view_entries6 $total_entries</td>
<td align='right' NOWRAP style='padding-right: 5px;' width='90%'>
";

if($total_entries > $entries_per_page) { 
echo "
  <table cellpadding='0' cellspacing='0'>
  <form action='view_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' maxlength='5' name='page' value='$page' size='1'>"; }
if($np != "") { echo " <font class='gray'>|</font> $np"; }

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

echo "
</td>
</tr>
</table>
";
}




// GET ENTRY LIST
if($search != "") {
 if($search_in == "title") {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND title LIKE '%$search%' ORDER BY $sort");
 } elseif($search_in == "contents") {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND contents LIKE '%$search%' ORDER BY $sort");
 } else {
  $entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND (title LIKE '%$search%' OR contents LIKE '%$search%') ORDER BY $sort");
 }
} else {
$entries = mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' ORDER BY $sort LIMIT $start, $entries_per_page");
}

if(mysql_num_rows($entries) != 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<form action='view_entries.php' method='POST' name='entriesform'>
<tr>
<td width='1%' class='header2' style='padding-bottom: 5px;'><a href='javascript:doCheckAll()'><img src='../images/check.gif' id='check' border='0' alt='$view_entries7'></a></td>
<td width='1%' class='header2'><a href='view_entries.php?page=$page&s=$d' title='$view_entries26'>$view_entries8</a></td>
<td width='96%' class='header2'><a href='view_entries.php?page=$page&s=$t' title='$view_entries27'>$view_entries9</a></td>
"; if(str_replace(" ", "", str_replace(",", "", $weblog_info[level1])) != "" | str_replace(" ", "", str_replace(",", "", $weblog_info[level2])) != "") { echo "<td width='1%' class='header2'>$view_entries28</td>"; } echo "
<td width='1%' class='header'>$view_entries10</td>
</tr>
";
}



// OUTPUT EACH RESULT ENTRY
$count = 0;
while($entry = mysql_fetch_assoc($entries)) {
$count++;
if($count % 2) { $c = "2"; } else { $c = "1"; }
if($entry[draft] == 0) { $draft = "<a target='_blank' href='".url("entry", "$weblog_info[weblog]", "", "$entry[e_id]")."'>$view_entries11</a>"; } else { $draft = "<font class='disabled'>$view_entries12</font>"; }
$date = cdate("$weblog_info[format_date] $weblog_info[format_time]", timezone($entry[date]));
$title = $entry[title];
if($title == "") { $title = "<i>$view_entries13</i>"; }
$author = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$entry[author_u_id]'"));
echo "
<tr>
<td class='list"."$c"."a' NOWRAP style='padding: 3px;'><input type='checkbox' name='delete_$entry[e_id]'"; if(($user_status == "owner" | $user_status == "admin") | ($user_status == "member" & $user_info[u_id] == $entry[author_u_id])) { echo " value='yes'"; } else { echo " value='no' DISABLED"; } echo "></td>
<td class='list"."$c"."a' NOWRAP>$date</td>
<td class='list"."$c"."a'>$title &nbsp;</td>
"; if(str_replace(" ", "", str_replace(",", "", $weblog_info[level1])) != "" | str_replace(" ", "", str_replace(",", "", $weblog_info[level2])) != "") { echo "<td class='list"."$c"."a' NOWRAP><a href='".url("profile", "", $author[username])."' target='_blank'>$author[username]</a></td>"; } echo "
<td class='list"."$c"."b' NOWRAP align='right'>
$draft <font class='disabled'>|</font> 
"; if(($user_status == "owner" | $user_status == "admin") | ($user_status == "member" & $user_info[u_id] == $entry[author_u_id])) { 
 echo "<a href='edit_entry.php?e_id=$entry[e_id]'>$view_entries14</a> <font class='disabled'>|</font> <a href='delete_entry.php?e_id=$entry[e_id]'>$view_entries15</a>";
} else {
 echo "<font class='disabled'>$view_entries14 | $view_entries15 </font>";
}
echo "
</td>
</tr>
";
}


// SHOW NO RESULTS ROW IF NONE FOUND
if(mysql_num_rows($entries) == 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td class='empty'>"; if($search != "") { echo $view_entries25; } else { echo $view_entries16; } echo "</b></td>
</tr>
";
}

echo "</table>";

if(mysql_num_rows($entries) != 0) {
echo "
<br>
<input type='submit' class='button' value='$view_entries17'>
<input type='hidden' name='task' value='delete'>
<input type='hidden' name='page' value='$page'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='search' value='$search'>
<input type='hidden' name='search_in' value='$search_in'>
</form>
";
}

echo $foot;
?>