<?php
//db connection
$database = "restaura_original_pizza";
$host = 'localhost'; 
$username = 'restaura_taste';
$password = 'Daughters2'; 
include('../includes/mysql_connect.php');


//these codes apply to all taste systems clients
	$clientId = '5412'; // Your Client ID from https://developer.mogreet.com/dashboard
	$token = '9fbd7e0e70fc853dabd24c039f8b3603'; // Your token from https://developer.mogreet.com/dashboard
	
	
if($_POST['sms_submit']){
//echo "submitted";
	$tblock1 = mysql_escape_string($_POST['tblock1']);
	$tblock2 = mysql_escape_string($_POST['tblock2']);
	$tblock3 = mysql_escape_string($_POST['tblock3']);
	$tblock4 = mysql_escape_string($_POST['temail']);
	if((!empty($tblock1))&&(!empty($tblock2))&&(!empty($tblock3))&&($tblock4=="saved email")){
		$number=$tblock1.$tblock2.$tblock3;
		if(is_numeric($number)){
			//check if already subscribed
			$sql22 = "select * from sms_subscribers where mobile_number='$number'";
			$rt22=mysql_query($sql22);   
			if(mysql_num_rows($rt22)>0){
				$alert_msg= "<center>That number is already subscribed!</center>";
			} else {
				//all should be filled in correctly...
				$query = "insert into sms_subscribers (mobile_number) values ($number)";
				mysql_query($query);
				$alert_msg= "<center>Thanks for subscribing! <br>Be sure to watch for upcoming deals!</center>";
									 
			}
			//now check for any subscribers that haven't received the auto response, if an auto response exists
			$smsqry = "select * from sms_subscribers where active=1 and autoresponse_sent=0";
			$res = mysql_query($smsqry);
			if(mysql_num_rows($res)>0){
				//if any new subscribers, need to check for an auto response in the system
				$smsqry2 = "select sms_body from sms_storage where autoresponder=1";
				$res2 = mysql_query($smsqry2);
				//if an autoresponder exists, get message
				if(mysql_num_rows($res2)>0){
					while($dt = mysql_fetch_array($res2)){
						$auto_resp_msg = $dt['sms_body'];
					}
				}
				if($auto_resp_msg<>""){
					//autoresponse exists, new subscribers exist - need to trigger a send now
					$qry="select sms_id, mms_id, sms_plan from admin_options";
					$rt=mysql_query($qry); 
					while($data=mysql_fetch_array($rt)){
						$sms_id = $data[sms_id];
						$mms_id = $data[mms_id];
						$sms_plan = $data[sms_plan];
					}
					require_once('../cmsadmin/includes/Mogreet.php');
					//if(empty($image_url)){//this is a sms message, no image
						$campaign_id=$sms_id;
					//} else {
					//	$campaign_id=$mms_id;
					//}

					$msg_count = 0;
					//now step through subscribers that need to receive autoresponse (should only be 1, but could be multiple)
					while($dt1 = mysql_fetch_array($res)){
						$client = new Mogreet($clientId, $token);
						$response = $client->transaction->send(array(
									'campaign_id' => $campaign_id, // Your SMS campaign ID from https://developer.mogreet.com/dashboard
									'to' => $dt1['mobile_number'],
									'message' => $auto_resp_msg
						));
						if($response->status =="success"){
							$msg_count++;
							if($recipient_list == ""){
								$recipient_list = $dt1[recordkey];
							} else {
								$recipient_list .= ", ".$dt1[recordkey];
							}
							$qry = "update sms_subscribers set autoresponse_sent='1' where recordkey='".$dt1['recordkey']."'";
							mysql_query($qry);
							$qry = "insert into sms_message_log (sms_body, recipient_id, recipient_count) values ('$auto_resp_msg','".$dt1[recordkey]."','$msg_count')";
							//echo $qry;
							mysql_query($qry);
						}
						
					}
					
					
					
				}
				
			}
			
			
			 
			
		} else {
			$alert_msg= "<center>Please enter a 10-digit numeric telephone number.</center>";
		}
	} else {
		$alert_msg= "<center>Please enter a 10-digit numeric telephone number.</center>";
	}
}
?>
<div style="width:200px;">
<?php 
if(!empty( $alert_msg)){
	echo "<font color='red'>".$alert_msg."</font>";
} else {
	echo "<center>Subscribe to text alerts:</center>";
}
?>
<form action="#popup" method="post"><input type="hidden" name="temail" size="1" value="saved email">
<center><input type="text" name="tblock1" size="1" maxlength="3"> - <input type="text" name="tblock2" size="1" maxlength="3"> - <input type="text" name="tblock3" size="1" maxlength="4"></center>
<center><input type="submit" name="sms_submit" value="Subscribe"></center>
</form>
</div>

