<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = '4';
include('header.php');

// Update comment?
if ('' != $_POST['update'])
{
	$comment_id = (int) $_POST['comment_id'];
	if ($comment_id)
	{
		$comment = trim($_POST['comment_txt']);
		$comment = nl2br($comment);
		$comment = secure_sql($comment);
		
		$sql = "UPDATE pm_comments 
				SET comment = '". $comment ."' 
				WHERE id = '". $comment_id ."'";
		if ( ! $result = mysql_query($sql))
		{
			$info_msg = '<div class="info_msg_errimg">Comment editing failed. MySQL Error: '. mysql_error() .'</div>';
		}
		else
		{
			$info_msg = '<div class="info_msg_ok">Comment updated.</div>';
		}
		
	}
}

$vid 		= trim($_GET['vid']);
$action 	= $_GET['a'];
$comment_id = (int) trim($_GET['cid']);
$page 		= $_GET['page'];

$filter = '';
$filters = array('articles', 'videos');

if(in_array(strtolower($_GET['filter']), $filters) !== false)
{
	$filter = strtolower($_GET['filter']);
}

if($page == 0)
	$page = 1;

$limit 		= 20;	//	comments per page
$from 		= $page * $limit - ($limit);


//	Batch Delete/Approve Comments
if(($_POST['Submit'] == 'Delete checked') || ($_POST['Submit'] == 'Approve checked'))
{
	$video_ids = array();
	$video_ids = $_POST['video_ids'];
	
	$total_ids = count($video_ids);
	if($total_ids > 0)
	{
		$in_arr = '';
		for($i = 0; $i < $total_ids; $i++)
		{
			$in_arr .= $video_ids[ $i ] . ", ";
		}
		$in_arr = substr($in_arr, 0, -2);
		if(strlen($in_arr) > 0)
		{
			if($_POST['Submit'] == 'Approve checked')
			{
				$sql = "UPDATE pm_comments SET approved = '1' WHERE id IN (" . $in_arr . ")";
				$result = @mysql_query($sql);
	
				if(!$result)
					$info_msg = '<div class="info_msg_errimg">There was an error while updating your database.<br />MySQL returned: '.mysql_error().'</div>';
				else
					$info_msg = '<div class="info_msg_ok">The selected comments have been approved.</div>';
			}
			else
			{
				$sql = "DELETE FROM pm_comments WHERE id IN (" . $in_arr . ")";
				$result = @mysql_query($sql);
	
				if(!$result)
					$info_msg = '<div class="info_msg_errimg">There was an error while updating your database.<br />MySQL returned: '.mysql_error().'</div>';
				else
					$info_msg = '<div class="info_msg_ok">The selected comments have been deleted.</div>';
			}
		}
	}
	else
		$info_msg = '<div class="info_msg_ok">No items were selected!</div>';
}

switch($action)
{
	case 1:
		@mysql_query("DELETE FROM pm_comments WHERE id = '".$comment_id."'");
		$info_msg = '<div class="info_msg_ok">Comment has been deleted.<br /></div>';
	break;
	case 2:
		@mysql_query("UPDATE pm_comments SET approved='1' WHERE id = '".$comment_id."'");
		$info_msg = '<div class="info_msg_ok">Comment has been approved.<br /></div>';
	break;
}

//	Search
if(!empty($_GET['submit']) || !empty($vid))
{
	if(!empty($vid))
	{
		$comments = a_list_comments($vid, 'uniq_id');
	}
	else
	{
		$search_query = ($_POST['keywords'] != '') ? trim($_POST['keywords']) : trim($_GET['keywords']);
		$search_type = ($_POST['search_type'] != '') ? $_POST['search_type'] : $_GET['search_type'];
		$search_query = urldecode($search_query);
		$comments = a_list_comments($search_query, $search_type, $from, $limit, $page);
	}
	$total_comments = $comments['total'];
}
else 
{
	$total_comments = count_entries('pm_comments', '', '');

	if($total_comments - $from == 1)
		$page--;
		
	$comments = a_list_comments('', '', $from, $limit, $page, $filter);
	
	if($total_comments - $from == 1)
		$page++;
	
	$total_comments = $comments['total'];
}

// generate smart pagination
$filename = 'comments.php';
$uri = $_SERVER['REQUEST_URI'];
$uri = explode('?', $uri);

$pagination = '';
$pagination = a_generate_smart_pagination($page, $total_comments, $limit, 1, $filename, $uri[1]);


?>
<div id="sub_menu">
<li><a href="comments.php">Manage comments</a></li>
<li><a href="blacklist.php?list=censored">Censored words</a></li>
<li><a href="blacklist.php?list=blacklist">Blacklisted words</a></li>
</div>
<div id="content">
<h2>Video comments</h2>
<?php echo $info_msg; ?>
<br />
<table width="100%" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td valign="top">
	<form name="search" action="comments.php" method="get" onsubmit="return validateSearch('true');">
	<input type="text" name="keywords" value="<?php echo $_GET['keywords']; ?>" />
	<select name="search_type">
	 <option value="comment" <?php echo ($_GET['search_type'] == "comment") ? 'selected="selected"' : ''; ?> >Comment</option>
	 <option value="videotitle" <?php echo ($_GET['search_type'] == "uniq_id") ? 'selected="selected"' : ''; ?> >Video Unique ID</option>
	 <option value="username" <?php echo ($_GET['search_type'] == "username") ? 'selected="selected"' : ''; ?> >Username</option>
	 <option value="ip" <?php echo ($_GET['search_type'] == "ip") ? 'selected="selected"' : ''; ?> >IP Address</option>
	</select> 
	<input name="submit" type="submit" value="Search" class="inputbutton" /><br />
	</form>
	</td>
    <td width="220" valign="top"><div class="totals">Total comments: <?php echo $total_comments; ?></div></td>
  </tr>
</table>

<form name="comments_checkboxes" action="comments.php?page=<?php echo $page;?>" method="post">
<div style="font-size: 11px;" align="right">
<strong><strong>Filter by</strong></strong>: <a href="comments.php?page=1&filter=articles"><img src="images/ico_filter_articles.png" width="16" height="16" border="0" align="absmiddle" alt="Show only articles" /></a>  
 <a href="comments.php?page=1&filter=videos"><img src="images/ico_filter_videos.png" width="16" height="16" border="0" align="absmiddle" alt="Show only videos" /></a>
 <a href="comments.php" title="Remove filter"><img src="images/ico_filter_remove.gif" width="21" height="15" align="absmiddle" alt="Remove filter" border="0" /></a></div>
<table width="100%" class="tablesorter" cellspacing="1">
 <thead>
  <tr>
	<th align="center" width="20"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></th>
	<th align="center" width="20"> </th>
    <th width="250">Title</th>
    <th width="100">Added</th>
    <th >Comment</th>
    <th width="120">Username</th>
    <th width="100">IP</th>
    <th width="130">Action</th>
  </tr>
 </thead>
 <tbody>
	<?php echo $comments['comments']; ?>
 </tbody>
</table>
<div align="center"><?php echo $pagination; ?></div>
<?php if($total_comments != 0) : ?>
<table>
	<tr style="background-color: #fff;">
	<td>
		<input type="submit" name="Submit" value="Approve checked" class="approvebutton">
	</td>
	<td align="left">
		<input type="submit" name="Submit" value="Delete checked" class="deletebutton">
	</td>
	</tr>
</table>
<?php endif; ?>
</form>

</div>
<?php
include('footer.php');
?>