<?php
// $Id: prune.php,v 1.1 2004/07/11 10:28:22 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : LGPL: See /manual/LICENSES/LGPL.txt
// ------------------------------------------------------------------------- //

include_once('./admin_header.php');
include_once(XOOPS_ROOT_PATH.'/class/xoopspagenav.php');

$days    = !empty($HTTP_POST_VARS['days']) ? intval($HTTP_POST_VARS['days']) : intval($HTTP_GET_VARS['days']);
$days    = empty($days) ? 365 : $days;
$ddays   = (time() - (86400 * $days));

$access  = !empty($HTTP_POST_VARS['access']) ? intval($HTTP_POST_VARS['access']) : intval($HTTP_GET_VARS['access']);
$private = !empty($HTTP_POST_VARS['private']) ? intval($HTTP_POST_VARS['private']) : intval($HTTP_GET_VARS['private']);
$sticky  = !empty($HTTP_POST_VARS['sticky'])  ? intval($HTTP_POST_VARS['sticky'])  : intval($HTTP_GET_VARS['sticky']);
$locked  = !empty($HTTP_POST_VARS['locked'])  ? intval($HTTP_POST_VARS['locked'])  : intval($HTTP_GET_VARS['locked']);
$show    = !empty($HTTP_POST_VARS['show'])    ? intval($HTTP_POST_VARS['show'])    : intval($HTTP_GET_VARS['show']);

$limit   = !empty($HTTP_POST_VARS['limit'])   ? intval($HTTP_POST_VARS['limit'])   : intval($HTTP_GET_VARS['limit']);
$limit   = !empty($limit) ? $limit : 5;

if ($access == 1) {
	$acc1 = " selected='selected'";
	} elseif ($access == 2) {
		$acc2 = " selected='selected'";
		} elseif ($access == 3) {
			$acc3 = " selected='selected'";
		}

if ($private == 1) {
	$pchk1 = " selected='selected'";
	} elseif ($private == 2) {
		$pchk2 = " selected='selected'";
	}

if ($sticky == 1) {
	$schk1 = " selected='selected'";
	} elseif ($sticky == 2) {
		$schk2 = " selected='selected'";
	}

if ($locked == 1) {
	$lchk1 = " selected='selected'";
	} elseif ($locked == 2) {
		$lchk2 = " selected='selected'";
	}

echo "
<h4>"._MD_A_PRUNE."</h4>
"._MD_A_PLIST.":
<form method='post'>
<table align='center' cellpadding='0' cellspacing='5' border='0'><tr>
<td nowrap>
"._MD_A_ACCESSLEVEL."
<select class='select' name='access'>
<option value='0'>"._ANY."</option>
<option value='2'$acc2>"._MD_A_ANONYMOUSPOST."</option>
<option value='1'$acc1>"._MD_A_REGISTERUSERONLY."</option>
<option value='3'$acc3>"._MD_A_MODERATORANDADMINONLY."</option>
</select>
</td>
<td align='right' nowrap>
"._MD_A_PPRIVATE.":
<select class='select' name='private'>
<option value='0'>"._ANY."</option>
<option value='1'$pchk1>"._YES."</option>
<option value='2'$pchk2>"._NO."</option>
</select>
</td>
<td align='right' nowrap>
"._MD_A_PSTICKY.":
<select class='select' name='sticky'>
<option value='0'>"._ANY."</option>
<option value='1'$schk1>"._YES."</option>
<option value='2'$schk2>"._NO."</option>
</select>
</td>
<td align='right' nowrap>
"._MD_A_PLOCKED.":
<select class='select' name='locked'>
<option value='0'>"._ANY."</option>
<option value='1'$lchk1>"._YES."</option>
<option value='2'$lchk2>"._NO."</option>
</select>
</td>
</tr><tr>
<td align='left'>"._MD_A_PLAST.": <input type='text' class='text' size='3' maxlength='3' name='days' value='$days' /></td>
<td align='center' colspan='2'>"._MD_A_PLIMIT.": <input type='text' class='text' size='2' maxlength='2' name='limit' value='$limit' /></td>
<td align='right'>
<input type='hidden' name='show' value='$show' />
<input type='submit' class='button' name='submit' value='"._SEARCH."' />
</td>
</tr></table></form>";

// ------------------------------------------------------------------------- //
if ( !empty($HTTP_POST_VARS['submit']) && !empty($HTTP_POST_VARS['topicid']) ) {
	foreach ($HTTP_POST_VARS['topicid'] as $value) {
		$sql2 = "
			SELECT
			post_id,
			topic_id,
			subject
			FROM
			".$db->prefix("bb_posts")."
			WHERE
			topic_id = $value
			ORDER BY topic_id DESC";

			$result2 = $db->query($sql2);
			while ( list($post_id, $topic_id, $subject) = $db->fetch_row($result2) ) {
				$db->query("DELETE FROM ".$db->prefix("bb_posts")." WHERE post_id=$post_id");
				$db->query("DELETE FROM ".$db->prefix("bb_topics")." WHERE topic_id=$topic_id");

				$subject = $myts->makeTareaData4Show($subject);
				echo sprintf(_MD_A_PDELETED, $subject)."<br />";
			}
	}
	echo "<br /><br />";
}

// ------------------------------------------------------------------------- //
$extra_sql = '';

if ($sticky == 1) {
	$extra_sql .= " AND t.topic_sticky = 1";
	} elseif ($sticky == 2) {
		$extra_sql .= " AND t.topic_sticky = 0";
	}

if ($locked == 1) {
	$extra_sql .= " AND t.topic_status = 1";
	} elseif ($locked == 2) {
		$extra_sql .= " AND t.topic_status = 0";
	}

if ($private == 1) {
	$extra_sql .= " AND f.forum_type = 1";
	} elseif ($private == 2) {
		$extra_sql .= " AND f.forum_type = 0";
	}

if ( !empty($access) ) {
	$extra_sql .= " AND f.forum_access = $access";
}

$num_sql = "
		SELECT
		COUNT(*)
		FROM
		".$db->prefix("bb_posts")." p,
		".$db->prefix("bb_forums")." f
		INNER JOIN ".$db->prefix("bb_topics")." t
		ON (
		t.topic_last_post_id = p.post_id
		AND f.forum_id = t.forum_id
		AND p.post_time <= $ddays
		$extra_sql
		) ORDER BY t.topic_id DESC";

$num_result     = $db->query($num_sql);
list($num_rows) = $db->fetch_row($num_result);

// ------------------------------------------------------------------------- //
if ($num_rows > 0) {
	printf(_MD_A_PFOUND, $num_rows);

	$sql = "
		SELECT
		f.forum_name,
		f.forum_type,
		t.topic_id,
		t.topic_title,
		t.forum_id,
		t.topic_sticky,
		t.topic_status,
		t.topic_time,
		t.topic_replies
		FROM
		".$db->prefix("bb_posts")." p,
		".$db->prefix("bb_forums")." f
		INNER JOIN ".$db->prefix("bb_topics")." t
		ON (
		t.topic_last_post_id = p.post_id
		AND f.forum_id = t.forum_id
		AND p.post_time <= $ddays
		$extra_sql
		) ORDER BY t.topic_id DESC";

	echo "
		<script type='text/javascript'>
		function toggleChecks() {
		var formdom = xoopsGetElementById('topiclist');
		var i=0;
		var checked;
		len = formdom.elements.length;
		if (formdom.elements[i].checked == true) {
			checked = false;
			} else {
				checked = true;
			}
		for( i=0; i<len; i++) {
			formdom.elements[i].checked = !formdom.elements[i].checked;
		}
		}
		function confirmDelete() {
		var ok = confirm('"._MD_A_PCONFIRM."');
		if (!ok) {
			return(false);
		}
		}
		</script>
		<form id='topiclist' name='topiclist' method='post' onsubmit='return confirmDelete();'>
		<input type='hidden' name='days' value='$days' />
		<input type='hidden' name='private' value='$private' />
		<input type='hidden' name='sticky' value='$sticky' />
		<input type='hidden' name='locked' value='$locked' />
		<input type='hidden' name='show' value='$show' />
		<input type='hidden' name='limit' value='$limit' />
		<input type='hidden' name='access' value='$access' />
		<table cellpadding='0' cellspacing='0' border='0'>";

	$result = $db->query($sql, $limit, $show);
	while (list($f_fname, $f_type, $t_id, $t_title, $t_forum, $t_sticky, $t_status, $t_time, $t_replies) = $db->fetch_row($result) ) {
		$f_fname = $myts->makeTareaData4Show($f_fname);
		$t_title = $myts->makeTareaData4Show($t_title);
		echo "
			<tr>
			<td><li><a href='".$bbUrl['root']."viewforum.php?forum=$t_forum' target='_blank'>$f_fname</a> :: <a href='".$bbUrl['root']."viewtopic.php?topic_id=$t_id&forum=$t_forum' target='_blank'>$t_title</a> :: ".formatTimestamp($t_time, "s")." :: $t_replies "._MD_REPLIES."</li></td>
			<td><input type='checkbox' class='checkbox' name='topicid[]' value='$t_id' /></td>
			<td>&nbsp;";
			if ($t_sticky == 1) {
				echo " <img src='".$bbImage['sticky']."'>";
			}
			if ($t_status == 1) {
				echo " <img src='".$bbImage['locked']."'>";
			}
			if ($f_type == 1) {
				echo " <img src='".$bbImage['private']."'>";
			}
			echo "</td></tr>";
	}
	echo "<tr>
		<td colspan='2' align='right'><a href=javascript:toggleChecks();>"._MD_A_PCHECK."</a></td>
		</tr></table><br /><input type='submit' class='button' name='submit' value='"._DELETE."'></form>";

	$nav = new XoopsPageNav($num_rows, $limit, $show, "show", "op=list_topics&amp;sticky=$sticky&amp;private=$private&amp;locked=$locked&amp;days=$days&amp;limit=$limit&amp;access=$access");
	echo '<br /><div align="center">'.$nav->renderNav().'</div>';
	} else {
		echo _MD_A_PNFOUND;
	}

CloseTable();
xoops_cp_footer();
exit();
// ------------------------------------------------------------------------- //
?>
