<?

// Includes
$EnforceSession = true;
include "includes/includes.php";

// Header
include "includes/header.php";

if( request("SetMonth") != "" && request("SetYear") != "" )
{	
	$iMonth = intval(request("SetMonth"));
	$iYear = intval(request("SetYear"));
}
else
{
	$iMonth = intval(date("m"));
	$iYear = date("Y");
}

$StringMonth = $iMonth;
if( strlen($StringMonth) == 1 ) $StringMonth = "0" . $StringMonth;

$iCurrentMonth = date("m");
$iCurrentDay = date("d");
$iCurrentYear = date("Y");

$arMonths = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/sitehit_report.template" ); $tp->NextAndEchoOutput(); ) 
{
	CheckGlobalEscapes( $tp );
	
	if( $tp->IsEscape("MONTH") )
		echo $arMonths[$iMonth];
	else if( $tp->IsEscape("YEAR") )
		echo $iYear;
	else if( $tp->IsEscape("WEEKDAYS-BEFORE-FIRST-DAY-OF-MONTH") )
	{
		$iFDOMWeekDay = monthStartWeekDay($iMonth, $iYear) + 1;
		for( $iIndex = 1; $iIndex < $iFDOMWeekDay; $iIndex++ )
		{		
			echo $tp->GetEscapeArgument();
		}		
	}		
	else if( $tp->IsEscape("WEEKDAYS-AFTER-LAST-DAY-OF-MONTH") )
	{	
		$iLDOMWeekDay = monthEndWeekDay($iMonth, $iYear) + 1;
		for( $iIndex = $iLDOMWeekDay + 1; $iIndex < 8; $iIndex++ )
		{		
			echo $tp->GetEscapeArgument();
		}
	}
	else if( $tp->IsEscape("PREVIOUS-MONTH") )
	{
		$iPreviousMonth = $iMonth - 1;
		if( $iPreviousMonth == 0 ) $iPreviousMonth = 12;
		echo $iPreviousMonth;
	}
	else if( $tp->IsEscape("PREVIOUS-YEAR") )
	{
		if( $iMonth - 1 == 0 )
			$iPreviousYear = $iYear - 1;
		else
			$iPreviousYear = $iYear;
		
		echo $iPreviousYear;
	}
	else if( $tp->IsEscape("NEXT-MONTH") )
	{
		$iNextMonth = $iMonth + 1;
		if( $iNextMonth == 13 ) $iNextMonth = 1;
		echo $iNextMonth;
	}
	else if( $tp->IsEscape("NEXT-YEAR") )
	{
		if( $iMonth + 1 == 13 )
			$iNextYear = $iYear + 1;
		else
			$iNextYear = $iYear;
		
		echo $iNextYear;	
	}
	else if( $tp->IsEscape("DAYS-IN-MONTH") )
	{			
		$iDaysInMonth =  daysInMonth($iMonth, $iYear);
		for( $iIndex = 1; $iIndex < $iDaysInMonth + 1; $iIndex++ )
		{	
			
			$StringDay = $iIndex;						
			if( strlen($StringDay) == 1 ) $StringDay = "0" . $StringDay;
		
			// Hits
			$Record = SelectSingleRow("SELECT count, studentcount FROM hits WHERE site = '" . $gblSiteName . "' AND hitdate = '" . $iYear . "-" . $StringMonth . "-" . $StringDay . "'");
			$iHits = $Record->GetValue("count");
			$iMemberHits = $Record->GetValue("studentcount");
		
			$tpElem = new PhpTemplate;	
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{
				if( $tpElem->IsEscape("IF-DAY-IS-SUNDAY") )
				{					
					if( intval( strftime( "%w", mktime(	1, 1, 1, $iMonth, $iIndex, $iYear ) ) ) == 0 )
						echo $tpElem->GetEscapeArgument();
				}
				else if( $tpElem->IsEscape("IF-CURRENT-DATE") )
				{
					if( mktime(	1, 1, 1, $iMonth, $iIndex, $iYear ) == mktime( 1, 1, 1, $iCurrentMonth, $iCurrentDay, $iCurrentYear ) )
						echo $tpElem->GetEscapeArgument();	
				}
				else if( $tpElem->IsEscape("DAY") ) echo $iIndex;
				else if( $tpElem->IsEscape("HITS") && $iHits > 0 ) echo number_format($iHits, 0, '.', ',');
				else if( $tpElem->IsEscape("MEMBER-HITS") && $iMemberHits > 0 ) echo number_format($iMemberHits, 0, '.', ',');
			}
		}
		
	}
}

// Footer
include "includes/footer.php";

?>