<?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 );

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

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

	date_sub( $today_minus_370_days, date_interval_create_from_date_string( '370 days' ));
	$today_minus_370_days_str = $today_minus_370_days->format( "Y-m-d" );		


	$query = "SELECT * FROM profile WHERE ";
	$query .= " id not in (SELECT sent_to_id FROM sys_message WHERE type = 'twelve_month_deactivation')";
	$query .= " and deleted = 'no' AND returned_mail = 'no' and system_user = 'no' and email_address_valid = 'yes' and last_login < '".$today_minus_370_days_str."' ORDER BY handle";

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

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

			$subject = "Chat City deactivation of membership";

			$text_body = "Hi ".name_case( first_name_or_handle( $data ) ).",\n\n";
			$text_body .= "Your member registration with Chat City is still active however you have not logged into the site for more than twelve months.\n\n";
			$text_body .= "To protect you from unwanted messages, and to be fair to our other memebers, we will soon deactivate your membership.\n\n";
			$text_body .= "Your profile will no longer appear in search results.\n\n";
			$text_body .= "If you still wish to remain an active member of the site, please log into the site at the web address below to refresh your account.\n\n\n";
			$text_body .= "Kind Regards\n\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 .= "Your member registration with Chat City is still active however you have not logged into the site for more than twelve months.<br><br>";
			$html_body .= "To protect you from unwanted messages, and to be fair to our other memebers, we will soon deactivate your membership.<br><br>";
			$html_body .= "Your profile will no longer appear in search results.<br><br>";
			$html_body .= "If you still wish to remain an active member of the site, please log into the site at the web address below to refresh your account.<br><br><br>";
			$html_body .= "<br><br>Kind Regards<br><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'].", 'twelve_month_deactivation', '".$sent."')";
				$result2 = run_query( $query2, $cxn );
				
				$count++;
				$total_emails++;				
			}

			echo black( "Twelve month deactivation message sent to member ".$data['handle'].", last login ".$data['last_login']."<br>" );
		}
	}


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

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