<?php 
session_start();
require 'configuration.php';
require_once 'includes/common.functions.php';
require_once ('classes/database.php');
require_once ('classes/functions.php');
$sqlObj = new mysqlClass;
$funcObj = new funcClass;
$curFile = 'campaign.report.listing.htm';
$table = "##report";
$searchArray ='';

if (isset($_GET['reportType']))
	$uniqueDisabled = ($_GET['reportType'] == 'all') ? 'disabled="disabled"' : '';
else
	$uniqueDisabled = 'disabled="disabled"';
		
$uniqueChecked	= (isset($_GET['chkUnique']) && $_GET['chkUnique'] == '1') ? 'checked="checked"' : '';

$reportType 		= isset($_GET['reportType']) ? $_GET['reportType'] : '';
$unique				= intval(isset($_GET['chkUnique']) ? $_GET['chkUnique'] : '');
$getId 				= base64_decode(base64_decode(base64_decode($_GET['id'])));
$id					= $_GET['id'];

if (empty($getId) || !is_numeric($getId))
{
	die("Invalid URL");
}

if ($unique == 1)
{
	$distinct		= " DISTINCT(EMAIL) ";
	$distinctGroup	= "GROUP BY EMAIL";
}
else
{
	$distinct		= " EMAIL ";	 
	$distinctGroup	= "";
}
$searchIn = "";
if ($reportType == 'view')
	$searchIn	= "AND ACTION = 'view'";
else if ($reportType == 'unsubscribe')
	$searchIn	= "AND ACTION = 'unsubscribe'";
else if ($reportType == 'track')
	$searchIn	= "AND ACTION = 'track'";
else if ($reportType == 'cantread')
	$searchIn	= "AND ACTION = 'cantread'";

$query 				= "SELECT ID, CAMPAIGN_NAME FROM ##campaigns WHERE IS_SENT='1' ORDER BY ID DESC";
$sentCampaignList 	= array();
$sentCampaignList 	= $funcObj->CustomQuery($query);

if (count($sentCampaignList))
{	  
  $activeCampaign 	= $sentCampaignList[0]['CAMPAIGN_NAME'];
  $activeCampaignID	= $sentCampaignList[0]['ID'];
}
foreach ($sentCampaignList as $val)
{
	
	if ($getId  == $val['ID'])
	{
		$activeCampaign 	= $val['CAMPAIGN_NAME'];
		$activeCampaignID	= $val['ID'];
	}
}

$searchTxt = trim($funcObj->AppendSlashes(isset($_REQUEST['searchbox']) ? $_REQUEST['searchbox'] :''));
$searchctr='';
if(!empty($searchTxt))
{
	$searchctr = " AND (";
	$searchArray1=explode(",",$searchArray);
	
	$searchctr.= "EMAIL like '%$searchTxt%' 
	OR USER_IP like '%$searchTxt%' 
	OR BROWSER like '%$searchTxt%' 
	OR OS like '%$searchTxt%' 
	OR REDIRECT_URL like '%$searchTxt%'"; 
	$searchctr .= ") ";
}

$query = "SELECT COUNT({$distinct}) FROM ##report WHERE CAMPAIGN_ID = '$activeCampaignID' AND LENGTH(EMAIL) > 0 $searchIn $searchctr";

$countRecords	= $funcObj->CustomQueryCount($query)[0]["COUNT($distinct)"];


$records	= $funcObj->fetchParameterValue('SHOW_RECORDS_PER_PAGE')[0]['FIELDVALUE'];
if($records > 0)
{
	$limit = intval($records);
}
else
{
	$limit = $configAdminPaging;
}

if($_REQUEST['start'])
	$start = $_REQUEST['start'];
else
	$start = 0;
	
$proCount = $countRecords;
$searchbox =isset($_REQUEST['searchbox']) ? $_REQUEST['searchbox'] :'';
$link = $curFile.'?id='.$id.'&reportType='.$reportType.'&searchbox='.$searchbox.'&chkUnique=' . $unique;

require_once('classes/paging.class.php');
$pagingObj= new Paging;
$pageObj = $pagingObj->Paging( $proCount, $start , $limit);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Segnant Mailer - Listing Report</title>
<link rel="stylesheet" type="text/css" href="includes/css/thickbox.css" />
<link href="includes/css/ajaxfileupload.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="includes/css/segnantmailer.css" />
<link rel="stylesheet" href="includes/css/themes/blue/style.css" type="text/css"  media="print, projection, screen" />
<link rel="stylesheet" type="text/css" href="includes/css/paging.css"  />
<link rel="stylesheet" type="text/css" href="includes/css/ui.core.css"  />
<link rel="stylesheet" type="text/css" href="includes/css/ui.theme.css"  />
<link rel="stylesheet" type="text/css" href="includes/css/ui.accordion.css"  />
<script type="text/javascript" src="includes/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="includes/js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="includes/js/thickbox-compressed.js"></script>
<script type="text/javascript" src="includes/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="includes/js/ui.core.js"></script>
<script type="text/javascript" src="includes/js/ui.accordion.js"></script>
<script type="text/javascript" src="includes/js/common.jquery.js"></script>
<script type="text/javascript" src="includes/js/reports.listing.jquery.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="79" class="headerbg"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="headeritems">
  <tr>
    <td width="32%" rowspan="3" style="padding-left:10px;"><img src="images/logo.png" alt="" border="0" /></td>
    <td width="68%" align="right" style="padding-right:10px;" class="white">&nbsp;</td>
  </tr>
  <tr>
    <td height="42">&nbsp;</td>
  </tr>
  <tr>
    <td align="right" style="padding-right:10px;" class="size14 bold white">&nbsp;</td>
  </tr>
</table>
    </td>
  </tr>

  <tr>
    <td class="canvas" height="300" valign="top"><table width="100%" border="0" cellspacing="10" cellpadding="4">
      <tr>
        <td colspan="2" class="heading" height="50">Campaign Report</td>
        </tr>
      <tr>
        <td colspan="2" bgcolor="#eaeaea" style="padding:10px;">
        <table cellspacing="0" cellpadding="0" class="tabtable" >
<tr>
<?php
				$file = 'campaign.report.listing.htm';
				
				## ADD EXCEPTIONS
				
				$tabArray = array("<td nowrap=\"nowrap\" style=\"cursor:pointer;\" onclick=\"javascript:window.location.href='campaign.report.htm?id=".$_GET['id']."'\"> Graphical Report </td>",
						"<td nowrap=\"nowrap\" style=\"cursor:pointer;\" onclick=\"javascript:window.location.href='campaign.report.listing.htm?id=".$_GET['id']."'\"> Listing Report </td>");
				foreach ($tabArray as $key => $val)
				{
					$strpos = strpos($val, $file);
				
					if ($strpos > 0)
					{
						$newString = str_replace('style="cursor:pointer;"','style="cursor:pointer;" class="active" ',$val);
						echo $newString;
					}
					else
					{
						echo $val;
					}	
				}
				?>
</tr>
</table>
        <table width="100%" border="0" cellspacing="0" cellpadding="4">
                <tr>
                  <td height="405" bgcolor="#FFFFFF" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">
                      <tr>
                        <td height="55" valign="top" align="right"><form name="formsearch" method="get" style="margin: 0;" action="">
                        <table border="0" align="right">
                        <tr>
                        <td>
                        <fieldset style="border: 1px solid #cccccc;">
						<legend>Search</legend>

                            <table border="0" cellspacing="2" cellpadding="2" align="right">
                              <tr>
                                <td>
                                <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
								</td>
                                <td><strong>Report Type</strong></td>
                                <td><select name="reportType" id="reportType" class="textbox" onchange="updateUniqueChk(this.value)">
                                <option value="all" <?=($reportType == 'all') ? 'selected="selected"' : '' ?>>All</option>
                                <option value="view" <?=($reportType == 'view') ? 'selected="selected"' : '' ?>>Views</option>
                                <option value="track" <?=($reportType == 'track') ? 'selected="selected"' : '' ?>>Clicks</option>
                                <option value="unsubscribe" <?=($reportType == 'unsubscribe') ? 'selected="selected"' : '' ?>>Unsubscribes</option>
                                <option value="cantread" <?=($reportType == 'cantread') ? 'selected="selected"' : '' ?>>Can't Read</option>
                              </select></td>
                                <td width="50">&nbsp;</td>
                                <td nowrap="nowrap"><strong>Search</strong></td>
                                <td><input type="text" name="searchbox" placeholder="search" class="textbox" value="<?=$funcObj->RemoveSlashes($searchbox)?>" style="margin-bottom: 2px;" size="20" /></td>
                                <td><input type="submit" name="submit" value="Search" class="button size2" /></td>
                              </tr>
                              <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td colspan="3" nowrap="nowrap" align="right"><strong>Only show unique records</strong> <input type="checkbox" <?= $uniqueDisabled?> <?= $uniqueChecked?> name="chkUnique" id="chkUnique" value="1" /> </td>
                              </tr>
                            </table>
                            </fieldset>
                            </td>
                            </tr>
                            </table>
                          </form></td>
                      </tr>
                      <?php if (!empty($message))
					  echo "
                      <tr>
                        <td class='message'>$message</td>
                      </tr>
					  ";
					  ?>
                    <tr>
                      <td class="size15 orange bold" height="35">Report for: <?=$activeCampaign?></td>
                    </tr>
                      <tr>
                        <td><form name="frmListing" id="frmListing" method="post" style="margin:0; padding: 0" action=""><table cellspacing="1" class="tablesorter">
                            <thead>
                            <tr>
                              <th align="left" width="12%">Date</th>
                              <th align="left" width="20%">Email</th>
                              <th width="8%" align="left">IP</th>
                              <th width="13%" align="left">Browser</th>
                              <th width="12%" align="left">Operating System</th>
                              <th width="10%" align="center">Action</th>
                              <th align="left">Redirect URL</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
							if(!empty($searchTxt))
							{
								$searchctr = " AND (";
								$searchArray1=explode(",",$searchArray);
								
								$searchctr.= "EMAIL like '%$searchTxt%' 
								OR USER_IP like '%$searchTxt%' 
								OR BROWSER like '%$searchTxt%' 
								OR OS like '%$searchTxt%' 
								OR REDIRECT_URL like '%$searchTxt%'"; 
								$searchctr .= ") ";
							}
						
							$query = "SELECT {$distinct}, ID, CAMPAIGN_ID, USER_IP, BROWSER, OS, PLATFORM, ACTION, REDIRECT_URL, DATE_TIME FROM
							##report WHERE CAMPAIGN_ID = '{$activeCampaignID}' AND LENGTH(EMAIL) > 0 $searchIn $searchctr $distinctGroup ORDER BY ID DESC
							LIMIT {$pageObj->start}, {$pageObj->limit}";
					$resultSet = $funcObj->CustomQuery($query);
					$i=1;
					if(count($resultSet)!=0)
					{
					foreach ($resultSet as $row)
					{
						
						$i++;
						$openDateStr = date("M j, Y", strtotime($row['DATE_TIME']));
						$openDateStr .= ' ' . date("g:i:s a", strtotime($row['DATE_TIME'])) . '';
						echo "<tr>
						<td align='left'>" . $openDateStr . "</td>
						<td align='left'>";
						echo $row['EMAIL']; 
						echo "</td>
						<td align='left'>{$row['USER_IP']}</td>
						<td align='left'>";
						echo ($row['BROWSER'] == 'Unknown Unknown') ? ' Unknown ' : $row['BROWSER'];
						echo "</td>
						<td align='left'>{$row['OS']}</td>
						<td align='center'>";
						echo ($row['ACTION'] == 'track') ? 'click' : $row['ACTION'];
						echo "&nbsp;</td>
						<td align='left'>";
						echo '<a href="'.$row['REDIRECT_URL'].'" target="_blank">' . left($row['REDIRECT_URL'], 35) . '</a>';
						echo "&nbsp;</td></tr>
						";
						}
					}
					else
					{
						echo "<tr><td colspan='7' align='center' height='30' valign='middle' class='row0'>No Record Found... </td></tr>";
					}
					?>
                          </tbody>
                          </table>
                          </form></td>
                      </tr>
                      <tr>
                        <td class="pagingTD"><table cellspacing='0' cellpadding='0'  width='100%'>
                            <tr>
                              <td height='35' style="text-align:left" class="form_base_header"><span class="pagenav">
                                <?=$pageObj->ShowPageInfo()?>
                                </span></td>
                              <td style="text-align:right" class="form_base_header"><span class="pagenav">
                                <?=$pageObj->Show($link)?>
                                </span></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table></td>
              </tr>
              </table></td>
      </tr>
    </table></td>
  </tr>
<? require_once 'includes/inc.footer.php' ?>
</table>
</body>
</html>