<?php
include("includes/site_info.php");
include('session_handler.inc');

if(!preg_match("/\/home.php/",$_SERVER['HTTP_REFERER'])){
	echo "<script>location.href='" . $CONST_LINK_ROOT . "/home.php#csummary'</script>"; die;
}

$WUserID = $_SESSION['Sess_UserId'];
//$_SESSION['startPost'] = "new";

$now = getdate();
$initial_date = date ("F d, Y", mktime (0,0,0,1,1,$now['year']));
$today_date = date ("F d, Y", mktime (0,0,0,$now['mon'],$now['mday'],$now['year']));

$listing_query=mysql_query("SELECT SUM(active) AS active, SUM(inactive) AS inactive, SUM(IFNULL(clickTotal,'0')) AS clickTotal, SUM(IFNULL(ImpTotal,'0')) AS ImpTotal FROM (
(SELECT IFNULL(SUM( IF(expDate>now(),1,0) ),'0') AS active, IFNULL(SUM( IF(expDate< now(),1,0) ),'0') AS inactive, SUM(clickTotal) AS clickTotal, SUM(ImpTotal) AS ImpTotal FROM market_Post LEFT JOIN market_Post_click USING ( postID ) where mem_userid=$WUserID and postDate > concat(year(NOW()),'-01-01') and approve=1) 
UNION 
(SELECT 0 active, COUNT(postID) AS inactive, SUM(clickTotal) AS clickTotal, SUM(ImpTotal) AS ImpTotal FROM market_Post LEFT JOIN market_Post_click USING ( postID ) where mem_userid=$WUserID and postDate > concat(year(NOW()),'-01-01') and approve<>1)
) DATA",$link) or die(mysql_error());
$listing = mysql_fetch_object($listing_query);

if($listing->active>0) {

$first_query=mysql_query("SELECT DATE_FORMAT(postDate, '%M %d, %Y') as postDate, DATE_FORMAT(repostDate, '%M %d, %Y') as repostDate FROM market_Post where mem_userid=$WUserID order by postDate asc limit 1") or die(mysql_error());
$firstTotal = mysql_num_rows($first_query);

$last_query=mysql_query("SELECT DATE_FORMAT(postDate, '%M %d, %Y') as postDate, DATE_FORMAT(repostDate, '%M %d, %Y') as repostDate FROM market_Post where mem_userid=$WUserID order by postDate desc limit 1") or die(mysql_error());
$lastTotal = mysql_num_rows($last_query);
}

$comment_query = mysql_query("SELECT COUNT(comment_id) AS CommentTotal FROM market_Post LEFT JOIN market_Post_comment USING ( postID ) where mem_userid=$WUserID",$link) or die(mysql_error());
$comment = mysql_fetch_object($comment_query);

$activity_query=mysql_query("SELECT * FROM (SELECT activityText, activityID, YEAR(datetime) as year, DATE_FORMAT(datetime, '%M %d, %Y at %h:%i%p') as wYear, DATE_FORMAT(datetime, '%M %d at %h:%i%p') as wtYear, DATE_FORMAT(datetime, '%Y%m%d') as date, mem_username, mem_profileid FROM activities_user LEFT JOIN members ON (userID=mem_userid) WHERE userID='$WUserID' and activityID IN (15, 16) order by datetime desc) data order by date desc",$link) or die(mysql_error());
$activityTotal = mysql_num_rows($activity_query);

?>

<div id="top_panel" class="submenu">
	<?php include ("panel_top.php"); ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
<?php if($_SESSION['startPost']=="new"){?>
$.facebox({ajax:'<?=$CONST_LINK_ROOT?>/postPop.php'}, 'Create&nbsp;Post', 'newPost');
<?php } ?>
});
</script>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
				<tr>
					<td><div class="light_orang_h">
							<div style="width:10px; height:10px; float:left; margin-top:3px; margin-right:5px; background: #0000FF;"><img src="images/spacer.gif" width="10" height="10" /></div>
							Summary </div>
						<div class="dot_line"><img src="images/spacer.gif" width="1" height="1" /></div>
						<div class="solid_line"><img src="images/spacer.gif" width="1" height="1" /></div></td>
				</tr>
				<tr>
					<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable">
							<tr>
								<th colspan="3" align="left"><?php if($firstTotal > 0) {
								$first = mysql_fetch_object($first_query);
								if (!empty($first->repostDate)) echo $first->repostDate; else echo $first->postDate;} else echo $initial_date;
								
								/*if($lastTotal > 0) {
								$last = mysql_fetch_object($last_query);
								if (!empty($last->repostDate)) echo " - ".$last->repostDate; else echo " - ".$last->postDate;} else*/ echo " - ".$today_date;
							?></th>
							</tr>
							<tr>
								<td><?php if($listing->active>0) { ?>
									<a href="javascript:void(null)" onclick="javascript: callboxBottom('active');" title="All Active Listings" class="blue_b"><strong>
									<?=$listing->active?>
									</strong> Active Listings</a>
									<?php } else { ?>
									<strong>
									<?=$listing->active?>
									</strong> Active Listings
									<?php } ?></td>
								<td><strong>
									<?=$listing->ImpTotal?>
									</strong> Impressions</td>
								<td>Shares</td>
							</tr>
							<tr>
								<td><?php if($listing->inactive>0) { ?>
									<a href="javascript:void(null)" onclick="javascript: callboxBottom('inactive');" title="All InActive Listings" class="blue_b"><strong>
									<?=$listing->inactive?>
									</strong> Expired Listings</a>
									<?php } else { ?>
									<strong>
									<?=$listing->inactive?>
									</strong> Expired Listings
									<?php } ?>
									 </td>
								<td><strong>
									<?=$listing->clickTotal?>
									</strong> Clicks</td>
								<td><strong>
									<?=$comment->CommentTotal?>
									</strong> Comments</td>
							</tr>
						</table>
						<div id="showContent">&nbsp;</div></td>
				</tr>
				<?php if($activityTotal > 0) {?>
				<tr>
					<td><div class="light_orang_h">
							<div style="background-color:#FF0000; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div>
							Activity On The Roll </div>
						<div class="dot_line"><img src="images/spacer.gif" width="1" height="1" /></div>
						<div class="solid_line"><img src="images/spacer.gif" width="1" height="1" /></div></td>
				</tr>
				<tr>
					<td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb">
							<tr>
								<td><div id="activityFeed">
										<?php 
		$j=0;
	 while (($activity = mysql_fetch_object($activity_query)) && ($j<12)) {
	 if ($activity->mem_profileid != null) { $profile_name= $activity->mem_profileid; }
	 else { $profile_name= $activity->mem_username; }
	 if ($activity->year==date('Y')) {
	 	$date = $activity->wtYear;
	 } else {
	 	$date = $activity->wYear;
	 }
?>
										<div class="padding_5">
											<?=ucwords($activity->mem_username)?>
											<?=$activity->activityText?>
											<span class="grey_10">
											<?php /*?><?=$date?><?php */?>
											</span> </div>
										<?php $j++;
										} ?>
									</div></td>
							</tr>
						</table></td>
				</tr>
				<?php } ?>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table></td>
	</tr>
</table>
<?php
unset($_SESSION['startPost']);
?>
