<?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 = '2';
include('header.php');

$action	 = (int) $_GET['a'];
$id		 = (int) $_GET['rid'];

$page	 = (int) $_GET['page'];

if(empty($page) || !is_numeric($page) || $page == '')
   $page = 1;
$limit	 = 20;	//	reported videos per page
$from	 = $page * $limit - ($limit);

//	Batch Delete Reports
if($_POST['Submit'] == 'Delete Reports')
{
	$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)
		{
			$sql = "DELETE FROM pm_reports WHERE entry_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 reports have been deleted.</div>';
		}
	}
	else
		$info_msg = '<div class="info_msg_ok">No items were selected!</div>';
}
//	Batch Delete Videos
if($_POST['Submit'] == 'Delete Videos')
{
	$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)
		{
			$categories = array();
			$sql = "SELECT uniq_id, category, url_flv, source_id FROM pm_videos WHERE uniq_id IN (" . $in_arr . ")";
			$result = mysql_query($sql);
			while ($row = mysql_fetch_assoc($result))
			{
				$categories[] = $row;
			}
			mysql_free_result($result);

			$sql = "DELETE FROM pm_videos WHERE uniq_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
			{
				mysql_query("DELETE FROM pm_comments WHERE uniq_id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_ratings WHERE id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_reports WHERE entry_id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_videos_urls WHERE uniq_id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_favorites WHERE uniq_id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_chart WHERE uniq_id IN (" . $in_arr . ")");
				mysql_query("DELETE FROM pm_tags WHERE uniq_id IN (" . $in_arr . ")");
				$info_msg = '<div class="info_msg_ok">The selected videos have been deleted.</div>';
			}
			
			// update video count for each category
			$video_count = array();
			foreach ($categories as $k => $row)
			{
				// delete hosted files
				if ($row['source_id'] == 1)
				{
					if (file_exists(_VIDEOS_DIR_PATH . $row['url_flv']) && strlen($row['url_flv']) > 0)
					{
						unlink(_VIDEOS_DIR_PATH . $row['url_flv']);					
					}
					if (file_exists(_THUMBS_DIR_PATH . $row['uniq_id'] .'-1.jpg'))
					{
						unlink(_THUMBS_DIR_PATH . $row['uniq_id'] .'-1.jpg');
					}
				}
				
				$ids = $row['category'];
				
				$buffer = explode(',', $ids);
				foreach ($buffer as $k => $id)
				{
					$video_count[$id]++;
				}
			}
			
			if (count($video_count) > 0)
			foreach ($video_count as $cid => $count)
			{
				if ('' != $cid)
				{
					$sql = "UPDATE pm_categories SET total_videos=total_videos-". $count ." 
							WHERE id = '". $cid ."'";
					mysql_query($sql);
				}
			}
		}
	}
	else
		$info_msg = '<div class="info_msg_ok">No items were selected!</div>';
}

//	DELETE REPORT
if($action == 1) { 
	
	@mysql_query("DELETE FROM pm_reports WHERE id = '".$id."'");
	$info_msg = '<div class="info_msg_ok">The report has been deleted.</div>';
}
//	DELETE ALL REPORTS
if($action == 2) { 

	@mysql_query("TRUNCATE TABLE pm_reports");
	$info_msg = '<div class="info_msg_ok">The reports have been deleted.</div>';
}

$total_rvideos = count_entries('pm_reports', 'r_type', '1');

if($total_rvideos - $from == 1)
	$page--;
$reported = a_list_vreports( '1', $from, $limit, $page); // 1-videos, 2-comments 

if($total_rvideos - $from == 1)
	$page++;
// generate smart pagination
$filename = 'reports.php';
$pagination = '';

$pagination = a_generate_smart_pagination($page, $total_rvideos, $limit, 1, $filename, '');



?>
<div id="sub_menu">
<li><a href="addvideo.php">Add new video</a></li>
<li><a href="embedvideo.php">Embed new video</a></li>
<li><a href="import.php">Import from Youtube</a></li>
<li><a href="import_user.php">Import from Youtube User</a></li>
<li><a href="reports.php">Reported Videos</a></li>
<li><a href="approve.php">Approve Videos</a></li>
</div>
<div id="content">
<h2>Reported Videos</h2>

<?php echo $info_msg; ?>
<form name="reported_videos_checkboxes" action="reports.php?page=<?php echo $page;?>" method="post">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td><img src="images/remove.png" alt="delete" width="16" height="16" align="top" /> <a href="#" class="b_delete" onClick="del_allreports()">Delete all reports</a></td>
    <td width="220" valign="top"><div class="totals">Total reported videos: <?php echo $total_rvideos; ?></div></td>
  </tr>
</table>
<table class="tablesorter" cellspacing="1">
 <thead>
  <tr> 	
    <th align="center" width="20"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></th>
    <th width="15">&nbsp;</th>
	<th width="90">Unique ID</th>
	<th width="15">&nbsp;</th>
	<th>Video</th>
    <th width="10%">Reason</th>
    <th width="10%">Submitted by</th>
    <th width="20%" align="center">Action</th>
  </tr>
 </thead>
 <tbody>
	<?php echo $reported; ?>
 </tbody>
</table>
<?php
if($total_rvideos != 0) {
?>
<table >
  <tr>
	<td><input type="submit" name="VideoChecker" id="VideoChecker" value="Check status" class="approvebutton" onclick="javascript: return false;"></td>
	<td><input type="submit" name="Submit" value="Delete Videos" class="deletebutton"></td>
	<td colspan="3" align="left"><input type="submit" name="Submit" value="Delete Reports" class="deletebutton"></td>
  </tr>
</table>

<?php
}
?>
</form>
<div id="video_check_message" class="info_msg"></div>
<br />
<table align="center" width="55%">
 <tr>
  <td width="20%">
   <img src="<?php echo VS_UNCHECKED_IMG;?>" width="16" height="16" align="absmiddle" />&nbsp;&nbsp;<small>Unchecked</small>
  </td>
  <td width="20%">
   <img src="<?php echo VS_OK_IMG;?>" width="16" height="16" align="absmiddle" />&nbsp;&nbsp;<small>Video Found</small>
  </td>
  <td width="25%">
   <img src="<?php echo VS_BROKEN_IMG;?>" width="16" height="16" align="absmiddle" />&nbsp;&nbsp;<small>Video Not Found</small>
  </td>
  <td width="35%">
   <img src="<?php echo VS_RESTRICTED_IMG;?>" width="16" height="16" align="absmiddle" />&nbsp;&nbsp;<small>Geo-Restricted access</small>
  </td>
 </tr>
</table>

<div align="center"><?php echo $pagination; ?></div>
</div>
<?php
include('footer.php');
?>