<?php


// includes and security
include_once('../../../../core/includes/master.inc.php');
include_once(DOC_ROOT . '/' . ADMIN_FOLDER_NAME . '/_local_auth.inc.php');

$iDisplayLength = (int) $_REQUEST['iDisplayLength'];
$iDisplayStart  = (int) $_REQUEST['iDisplayStart'];
$sSortDir_0     = $_REQUEST['sSortDir_0'] ? $_REQUEST['sSortDir_0'] : "desc";
$filterText		= strlen($_REQUEST['filterText']) ? $_REQUEST['filterText'] : '';

// get sorting columns
$iSortCol_0     = (int) $_REQUEST['iSortCol_0'];
$sColumns       = trim($_REQUEST['sColumns']);
$arrCols        = explode(",", $sColumns);
$sortColumnName = $arrCols[$iSortCol_0];
$sort           = 'plugin_comments_reports.dateAdded';
switch ($sortColumnName)
{
    case 'date':
        $sort = 'plugin_comments_reports.reportDate, plugin_comments_reports.comId';
        break;
}

if ($filterText)
{
    $sqlClause .= " WHERE users.username = " . $db->quote($filterText);
}

$totalRS = $db->getValue("SELECT COUNT(*) FROM plugin_comments_reports LEFT JOIN users ON plugin_comments_reports.reportedBy = users.id " . $sqlClause);

$sql = "SELECT plugin_comments_reports.*, users.username, plugin_comments.fileId, plugin_comments.userId FROM plugin_comments_reports INNER JOIN plugin_comments ON plugin_comments_reports.comId = plugin_comments.id INNER JOIN users ON plugin_comments_reports.reportedBy = users.id ".$sqlClause." GROUP BY plugin_comments_reports.comId, users.username, plugin_comments.fileId, plugin_comments.userId ORDER BY " . $sort . " " . $sSortDir_0 . " LIMIT " . $iDisplayStart . ", " . $iDisplayLength;

$limitedRS = $db->getRows($sql);
$data = array();
if (COUNT($limitedRS) > 0)
{
    foreach ($limitedRS AS $rs)
    {
		$times = $db->getValue("SELECT COUNT(plugin_comments_reports.id) FROM plugin_comments_reports WHERE comId = ".$rs['comId']);
		$filename = $db->getValue("SELECT file.originalFilename FROM file WHERE file.id = '".$rs['fileId']."' LIMIT 1");
		$lRow = array();
		if ($rs['reports'] <= '4')
        {
			$lRow[] = '<img src="../assets/img/icons/info.png" width="16" height="16" title="Low Report Level" alt="Low Report Level"/>';
		}
		elseif ($rs['reports'] >= '5' && $rs['reports'] <= '9')
		{			
			$lRow[] = '<img src="../assets/img/icons/amber.png" width="16" height="16" title="Medium Report Level" alt="Medium Report Level"/>';
		}
		elseif ($rs['reports'] >= '10')
		{
			$lRow[] = '<img src="../assets/img/icons/pending.png" width="16" height="16" title="High Report Level" alt="High Report Level"/>';
		}

        $lRow[] = coreFunctions::formatDate($rs['reportDate'], SITE_CONFIG_DATE_TIME_FORMAT);
		$lRow[] = '<a href="'.ADMIN_WEB_ROOT.'/file_manage.php" target="_blank">'.validation::safeOutputToScreen($filename, '', '100').'</a>';
		
		$lRow[] = strlen($rs['username']) ? ('<a href="'.ADMIN_WEB_ROOT.'/user_edit.php?id='.$rs['userId'].'" target="_blank">'.validation::safeOutputToScreen($rs['username']).'</a>') : '<span style="color: #777777;">[Anonymous]</span><br/><span style="color:# 777777;font-size:11px;">'.validation::safeOutputToScreen($rs['ipAddress']).'</span>';
        $lRow[] = $times;

        $links = array();
		$links[] = '<a href="#" onClick="viewReports('.(int)$rs['comId'].'); return false;">View Report'.($times >='2' ? 's' : '').'</a>'; 

		$lRow[]  = implode(" | ", $links);	
        $data[] = $lRow;
    }
}

$resultArr = array();
$resultArr["sEcho"]                = intval($_GET['sEcho']);
$resultArr["iTotalRecords"]        = (int) $totalRS;
$resultArr["iTotalDisplayRecords"] = $resultArr["iTotalRecords"];
$resultArr["aaData"]               = $data;
echo json_encode($resultArr);
