<?php

	include_once "library.php";

            // command line invocation
            
    if (isset( $argv ))
    {
        if ($argv[1] == "sendit=true")
            $send_it = "true";
    }
    else
    	$send_it = val( $_GET, 'sendit' );

	if ($send_it <> "true")
		echo black( "TEST MODE<br><br>" );

//	$cxn = start_code( true );


		/* kill the redirection of the email to the debugging address */
		/* if logged in as me */

//	session_destroy();

    $count = 0;
    
	$subject = "";
	$html_body = "";

	$today_date = date_create( date("Y-m-d") );
	$today_minus_three_months = clone $today_date;

	date_sub( $today_minus_30_days, date_interval_create_from_date_string( '30 days' ));
	$today_minus_30_days_str = $today_minus_30_days->format( "Y-m-d" );		

	$query = "SELECT * FROM profile WHERE visibility <> 'Hidden'";
	$query .= " and locked = 'no' and deleted = 'no' AND system_user = 'no' and returned_mail = 'no' and email_address_valid = 'yes' and date_created > '".$today_minus_30_days_str."'";
	$query .= " and id not in (SELECT sent_to_id FROM sys_message WHERE type = 'social_media_email') ";
	
	$sent = date("Y-m-d H:i:s");

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

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

//			/* only process members who have been a member for more than 4 days */

		if ((time() - strtotime( $data['date_created'] )) > 4*24*60*60 )
		{
			$domain = val( $_SERVER, "HTTP_HOST" );

			$subject = "Social media sharing message from ".$_SESSION['sitename'];

			$text_body = "Hi ".name_case( first_name_or_handle( $data ) ).",\n\n";
			$text_body .= "Thank you for joining ".$_SESSION['sitename'].".\n\n";
			$text_body .= "It would be very helpful to growing our membership base if you could share a short post about joining the site to your Social Media accounts.\n\n";
			$text_body .= "Just add a short message and include the site address www.chatcity1.net\n\n";
			$text_body .= "Kind Regards\n\n";
			$text_body .= "Site Administration\n";			
			$text_body .= "Chat City\n\n";
			$text_body .= "www.chatcity1.net";

			$html_body = "Hi ".name_case( first_name_or_handle( $data ) ).",<br><br>";
			$html_body .= "Thank you for joining ".$_SESSION['sitename'].".<br><br>";
			$html_body .= "It would be very helpful to growing our membership base if you could share a short post about joining the site to your Social Media accounts.<br><br>";
			$html_body .= "Just add a short message and include the site address www.chatcity1.net<br><br>";			
			$html_body .= "<br><br>Kind Regards<br><br>";
			$html_body .= "Site Administration<br>";			
			$html_body .= "Chat City<br><br>";
			$html_body .= show_link( "www.chatcity1.net", "http://chatcity1.net" );

			if ($send_it == "true")
			{
				send_external( $data['id'], $subject, $text_body, $html_body, $cxn, $cxn_common, "background" );

//				$query2 = "UPDATE profile SET photo_email_sent = 'yes' WHERE id = ".$data['id'];
//				$result2 = run_query( $query2, $cxn );

				$query2 = "INSERT INTO sys_message (sent_to_id, type, sent) VALUES (".$data['id'].", 'social_media_email', '".$sent."')";
				$result2 = run_query( $query2, $cxn );
				
				$count++;
				$total_emails++;				
			}

			echo black( "Social media email sent to member ".$data['handle']."<br>" );
		}
	}


    echo $subject."<br><br>".$html_body;

	echo black( "<br>Finished<br><br>" );
?>