<?php

	include "library.php";

	$cxn = start_code( true );

    exit_if_search_bot();


	$_SESSION['validation_error'] = "false";
	$_SESSION['validation_errors'] = "";

    $full_log = val( $_GET, "fulllog" );
    
//	page_header( "", $cxn, true );
//	page_header( "", $cxn, true, false, "#D9C4AE" );

	echo "<html><head><meta name='viewport' content='width=device-width, initial-scale=1.0'></head>";

	echo "<style> html { font-size: 9pt; font-family: verdana; } ";
	echo ".glink { font-size: 9pt; font-family: verdana; } ";
    echo "</style>";

	echo "<font color='#999999'>".date( "d M Y  g:ia" )."</font><br><br><br>";
	
	$today_date = date_create( date("Y-m-d") );
	$today_date_str = $today_date->format( "Y-m-d" );		
	
	
	$today_minus_21_days = clone $today_date;

	date_sub( $today_minus_21_days, date_interval_create_from_date_string( '21 days' ));
	$today_minus_21_days_str = $today_minus_21_days->format( "Y-m-d" );		

    
    $day_to_process = clone $today_date;

	$today_minus_365_days = clone $today_date;

	date_sub( $today_minus_365_days, date_interval_create_from_date_string( '365 days' ));
	$today_minus_365_days_str = $today_minus_365_days->format( "Y-m-d" );		
	
	
		/* created on each day */

//	echo "See pathroot./stats_log.csv for a CSV log of the main results.<br><br><br>";
	
	echo "New members<br><br>";

    if ($full_log == "true")
    {    
    	date_sub( $day_to_process, date_interval_create_from_date_string( '365 days' ));
    	$day_to_process_str = $day_to_process->format( "Y-m-d" );		
    }
    else
    {
    	date_sub( $day_to_process, date_interval_create_from_date_string( '13 days' ));
    	$day_to_process_str = $day_to_process->format( "Y-m-d" );		
    }

// 	$result = run_query( $query, $cxn );

	echo "<table>";
	
//	echo "<tr><td>Date</td><td align='center'>New</td><td align='center'>Adwords</td><td align='center'>&nbsp;Organic</td><td>Number</td><td align='center'>&nbsp;&nbsp;&nbsp;&nbsp;Page</td><td>&nbsp;&nbsp;&nbsp;Conversion</td></tr>";
//	echo "<tr><td></td><td align='center'>Members</td><td align='center'>hits</td><td align='center'>hits</td><td>Logins</td><td align='center'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Views</td><td align='center'>&nbsp</td></tr>";

	echo "<tr><td>Date</td><td align='center'>New</td><td align='center'>Adwords</td><td align='center'>&nbsp;Organic</td><td align='center'>&nbsp;&nbsp;&nbsp;&nbsp;Page</td><td>&nbsp;&nbsp;&nbsp;Conversion</td></tr>";
	echo "<tr><td></td><td align='center'>Jobseekers</td><td align='center'>hits</td><td align='center'>hits</td><td align='center'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Views</td><td align='center'>&nbsp</td></tr>";

	$count = 0;
	$total_new_members = 0;
	$total_organic = 0;
	$total_adwords_hits = 0;
	$total_organic_hits = 0;
    $total_logins = 0;
	$total_page_views = 0;
	$total_conversions = 0;
	
	
		// write a log file of the stats
		
    if ($full_log == "true")
    	$fp = fopen( pathroot."/stats_log_full.csv", "w" );
    else
    	$fp = fopen( pathroot."/stats_log.csv", "w" );


	fputs( $fp, "Date,New Members,Organic Members,Adwords Hits,Organic Hits,Members logged in this day,Messages sent this day,Page views,Conversion Rate,,,Adwords Spend,Adsense Income,Surplus,Recovery Rate,Adwords Clicks,Adsense Clicks,Adwords Gross CPC,Adwords Net CPC,Adsense CPC,Conversions,Conversion Rate,Net CP Conversion\n" );

	
//	for ($i=0; $i < query_num_rows($result); $i++)

    for (; $day_to_process_str <= $today_date_str;)
	{
//    	$query = "SELECT DISTINCT email FROM profile WHERE date(date_created) = '".$day_to_process_str."' AND system_user = 'no' and deleted = 'no'";		    
    	$query = "SELECT DISTINCT email_address FROM jobseeker_profile WHERE date_created >= '".$day_to_process_str." 00:00:00' AND date_created <= '".$day_to_process_str." 23:59:59' AND system_user = 'no' and deleted = 'no'";		    
	    
     	$result = run_query( $query, $cxn );	    
     	
		$num_created = query_num_rows( $result );


//    	$query4 = "SELECT count(*) as num FROM jobseeker_profile WHERE date_created >= '".$day_to_process_str." 00:00:00' AND date_created <= '".$day_to_process_str." 23:59:59' AND (source = 'organic')";
		
// 		$result4 = run_query( $query4, $cxn );

//        $data4 = get_row( $result4 );
        
//        $num_organic = $data4['num'];

//		$query2 = "SELECT DISTINCT ip_address FROM hits WHERE hit_date >= '".$day_to_process_str." 00:00:00' AND hit_date <= '".$day_to_process_str." 23:59:59' AND source = 'adwords'";

// 		$result2 = run_query( $query2, $cxn );

//		$num_adwords = query_num_rows( $result2 );


		$query3 = "SELECT DISTINCT ip_address FROM hits WHERE hit_date >= '".$day_to_process_str." 00:00:00' AND hit_date <= '".$day_to_process_str." 23:59:59' AND (source = 'organic' or source = '.blank')";

 		$result3 = run_query( $query3, $cxn );

	
		$query5 = "SELECT sum(count) as num FROM page_views WHERE page_view_date >= '".$day_to_process_str." 00:00:00' AND page_view_date <= '".$day_to_process_str." 23:59:59'";

 		$result5 = run_query( $query5, $cxn );

        $data5 = get_row( $result5 );
        
        $num_page_views = $data5['num'];


//		$query6 = "SELECT DISTINCT member_id FROM logins_log WHERE date(login_datetime) = '".$day_to_process_str."'";
		$query6 = "SELECT DISTINCT member_id FROM logins_log WHERE login_datetime >= '".$day_to_process_str." 00:00:00' AND login_datetime <= '".$day_to_process_str." 23:59:59'";

 		$result6 = run_query( $query6, $cxn );

		$num_logins_this_date = query_num_rows( $result6 );




		if ($num_adwords != 0)
			$conversion = number_format( ($num_created / $num_adwords)*100, 1 );
		else
			$conversion = "0";

		fputs( $fp, $day_to_process_str.",".$num_created.",".$num_organic.",".$num_adwords.",".query_num_rows( $result3 ).",".$num_logins_this_date.",".$data7['num'].",".$num_page_views.",".$conversion."\n" );


//		if ((time() - strtotime( $data['date_created2'] )) < 14*24*60*60 )
		{
			echo "<tr><td>".substr( $day_to_process_str, 8, 2 )."</td><td align='right'>".$num_created."</td>";

//			echo "<td align='right'>".$num_organic."</td>";
			
			if ($workflow_present)
    			echo "<td align='right'>".number_format( $adwords_clicks_str, 0 )."</td>";
    		else
    		    echo "<td align='right'>".number_format( $num_adwords, 0 )."</td>";

			echo "<td align='right'>".query_num_rows( $result3 )."</td>";

//			echo "<td align='right'>".$num_logins_this_date."</td>";

			echo "<td align='right'>".number_format( $num_page_views, 0 )."</td>";

			if ($workflow_present)
    			echo "<td align='right'>".$conversion_rate_str."</td>";
	    	else
		    	echo "<td align='right'>".$conversion."%</td>";

			echo"</tr>";
		}
	
		date_add( $day_to_process, date_interval_create_from_date_string( '1 day' ));
    	$day_to_process_str = $day_to_process->format( "Y-m-d" );		
	}

	fclose( $fp );


	echo "<tr><td>&nbsp;</td></tr>";

	echo "</table>";

	echo "<br>";


		/* site visitors today */

//	$data = get_first_row( "SELECT count(*) as num FROM hits WHERE hit_date = '".date( "Y-m-d")."' GROUP BY hit_date, source, ip_address", $cxn );

	$query = "SELECT DISTINCT ip_address FROM hits WHERE hit_date = '".date( "Y-m-d")."'";

 	$result = run_query( $query, $cxn );

	echo "<br><br>Site visitors today (total): ".number_format( query_num_rows( $result ), 0)."<br>";



	$query = "SELECT DISTINCT ip_address FROM hits WHERE hit_date = '".date( "Y-m-d")."' AND (source = 'organic' or source = '.blank')";

 	$result = run_query( $query, $cxn );

	echo "<br>Site visitors today (organic): ".query_num_rows( $result )."<br>";



	$query = "SELECT DISTINCT ip_address FROM hits WHERE hit_date = '".date( "Y-m-d")."' AND source = 'adwords'";

 	$result = run_query( $query, $cxn );

	echo "<br>Site visitors today (adwords): ".number_format( query_num_rows( $result ), 0)."<br>";


	$data = get_first_row( "SELECT sum(hits) as num FROM hits WHERE hit_date = '".date( "Y-m-d")."' and source = 'search'", $cxn );

	echo "<br>Searches: ".number_format( $data['num'], 0 )."<br>";


	$query = "SELECT sum(count) as num FROM page_views WHERE date(page_view_date) = '".date( "Y-m-d" )."'";

 	$result = run_query( $query, $cxn );

    $data = get_row( $result );

	echo "<br>Page views: ".number_format( $data['num'], 0 )."<br><br><br>";


		// main stats

	echo "<table>";

	$query = "SELECT DISTINCT email_address FROM jobseeker_profile WHERE system_user = 'no' and deleted = 'no' and date(date_created) = '".date( "Y-m-d", strtotime( date( "Y-m-d") ) )."'";

 	$result = run_query( $query, $cxn );

    $num_created = query_num_rows( $result );
    
	echo "<tr><td>New jobseekers today (excluding system users)</td><td align='right'>".$num_created."</td></tr><br>";



	echo "Last 30 members<br><br><br><table>";

	echo "<tr style='padding-left:15'><td style='padding-left:15'><b>Handle</b></td><td style='padding-left:15'><b>Claimed</b></td><td style='padding-left:15'><b>IP country</b></td><td style='padding-left:15'><b>Source</b></td><td style='padding-left:15' align='left'><b>Created</b></td></tr>";

	echo "<tr><td></td></tr>";
	
	$query = "SELECT * FROM jobseeker_profile WHERE deleted = 'no' and system_user = 'no' ORDER BY id DESC LIMIT 30";
	
	$result = run_query( $query, $cxn );

	for ($i=0; $i < query_num_rows($result); $i++)
	{
		$data = get_row( $result );

//		echo "<tr><td>".show_handle( $data['id'], $data['handle'], "main", "", "stats_screen", "true" )."</td><td>".$data['country']."</td><td>".$data['ip_country']."</td><td>".$data['source']."</td><td>".$data['membership_type']."</td></tr>";

		$time_txt = substr( $data['date_created'], 11, 8 );
		
    	$time_display = date_format( date_create_from_format( "H:i:s", $time_txt ), "g:i" )."&nbsp;".date_format( date_create_from_format( "H:i:s", $time_txt ), "a" );
		
		$url = url_encode( "display_profile_init.php", "showid", $data['id'], "returnpoint", "main", "startpos", "0", "stats_screen", "true" );

		$handle = "<a class='glink' href='".$url."'>".$data['handle']."</a>";

		echo "<tr style='padding-left: 15px'><td>".$handle."</td><td style='padding-left:15'>".$data['country']."</td><td style='padding-left:15'>".$data['ip_country']."</td><td style='padding-left:15'>".$data['source']."</td><td style='padding-left:15' align='right'>".$time_display."</td></tr>";
	}

	echo "</table><br><br>";



		/* latest site visitors */
		
	echo "<br><br><br>Latest site visitors<br><br>";

	echo "<table>";
	
	$query = "SELECT * FROM hits ORDER BY latest_hit DESC LIMIT 20";

 	$result = run_query( $query, $cxn );

	for ($i=0; $i < 20; $i++)
	{
		$this_row = get_row( $result );
		
		$data[$i] = $this_row;
	}

	for ($i=0; $i < 20; $i++)
	{
		$date_txt = substr( $data[$i]['latest_hit'], 0, 10 );
		
		$time_txt = substr( $data[$i]['latest_hit'], 11, 8 );

    	$time_display = date_format( date_create_from_format( "H:i:s", $time_txt ), "g:i" )."&nbsp;".date_format( date_create_from_format( "H:i:s", $time_txt ),  "a" );
	
		echo "<tr><td>&nbsp;".$data[$i]['ip_address']."</td><td>&nbsp;&nbsp;".$data[$i]['ip_country']."&nbsp;</td><td>&nbsp;".$data[$i]['source']."&nbsp;</td><td>&nbsp;".$date_txt." ".$time_display."</td></tr>";
	}
	

	echo "</table><br><br><br>";


	echo "Number of members, split by country<br><br>";

	
		/* list of countries */


	echo "<br><br>Finished";
	
	
    echo "<br><br><br><br><br>";


    echo "</body>";
	
    echo "</html>";


?>
