<?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#cactive'</script>"; die;
}

$WUserID = $_SESSION['Sess_UserId'];

$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']));

$active_query=mysql_query("SELECT postID, case when char_length(title) > 100 then concat(LEFT(title,100), '...') else title end as title, price, DATE_FORMAT(expDate, '%M %d, %Y at %h:%i%p') as expDate, forTitle, dataName, IFNULL(clickTotal,'0') as clickTotal, IFNULL(ImpTotal,'0') as ImpTotal FROM market_Post LEFT JOIN market_category_for on (optionFor=forID) LEFT JOIN market_category_data on (subCat=dataID) LEFT JOIN market_Post_click USING ( postID ) where mem_userid=$WUserID and approve=1 and expDate > now() order by postDate desc",$link) or die(mysql_error());
$activeTotal = mysql_num_rows($active_query);

$first_query=mysql_query("SELECT DATE_FORMAT(postDate, '%M %d, %Y') as postDate FROM market_Post where mem_userid=$WUserID and approve=1 and expDate > now() order by postDate desc limit 1",$link) or die(mysql_error());
$firstTotal = mysql_num_rows($first_query);

$last_query=mysql_query("SELECT DATE_FORMAT(postDate, '%M %d, %Y') as postDate FROM market_Post where mem_userid=$WUserID and approve=1 and expDate > now() order by postDate asc limit 1",$link) or die(mysql_error());
$lastTotal = mysql_num_rows($last_query);
?>

<div id="top_panel" class="submenu">
	<?php include ("panel_top.php"); ?>
</div>
<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: #6600FF;"><img src="images/spacer.gif" width="10" height="10" /></div>
						All Active Listings </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>
				<?php if($activeTotal>0) {?>
				<tr>
					<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable">
						<tr>
							<th width="100%" align="left">
							<?php if($firstTotal > 0) {
								$first = mysql_fetch_object($first_query);
								echo $first->postDate;} else echo $initial_date;
								
								if($lastTotal > 0) {
								$last = mysql_fetch_object($last_query);
								echo " - ".$last->postDate;} else echo " - ".$today_date;
							?>							</th>
							<th width="1%" align="left" nowrap="nowrap">Clicks</th>
							<th width="1%" align="left" nowrap="nowrap">Impressions</th>
							<th width="1%" align="left" nowrap="nowrap">Comments</th>
							<th width="10%" align="left"> Expiration </th>
							<th width="1%" align="left">Action</th>
						</tr>
						<?php
						while ($sql = mysql_fetch_object($active_query) ) {
						$comment_query = mysql_query("SELECT COUNT(comment_id) AS CommentTotal FROM market_Post_comment where postID=$sql->postID",$link) or die(mysql_error());
						$comment = mysql_fetch_object($comment_query);
						?>
						<tr id="post<?=$sql->postID?>">
							<td><strong><?=$sql->forTitle?>: </strong><a href="#cshowPost?tab=active&postID=<?=$sql->postID?>" class="blue_b" onclick="javascript: callpage(this.href);"><?php echo ucwords($sql->title); if(!empty($sql->price)) echo " for ".strtoupper($sql->price);?></a> in <?=$sql->dataName?></td>
							<td align="center"><?=$sql->clickTotal?></td>
							<td align="center"><?=$sql->ImpTotal?></td>
							<td align="center"><?=$comment->CommentTotal?></td>
							<td nowrap="nowrap"><?=$sql->expDate?></td>
							<td nowrap="nowrap"><a href="#ceditPost?tab=post&postID=<?=$sql->postID?>" onclick="javascript: callpage(this.href);"><span class="black_b">Edit</span> </a>| <a href="javascript: void(null)" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/postDelete.php?postID=<?=$sql->postID?>'}, 'Delete&nbsp;Post', 'deletePost'); return false;" rel="facebox"><span class="red_b">Delete</span></a> </td>
						</tr>
						<?php } ?>
					</table></td>
				</tr>
				<?php } else {?>
				<tr>
					<td><?php  print(NO_RECORDS_SECTION_NAME); ?>!</td>
				</tr>
				<?php }?>
				<tr>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table></td>
	</tr>
</table>
