<?php
//include_once("fckeditor/fckeditor.php");
include_once('PHPMailer/class.phpmailer.php');
include_once("includes/functions.php");


function sendMail($subject, $content, $userId,  $mail, $mailType) {
	
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysqli_select_db($con, DB_NAME) or die(mysql_error());
	
	//get settings
	$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT site_name, from_email FROM tbl_settings WHERE id = '1'"));
	$fromEmail = $settingsRec['from_email'];
	//$adminRec = mysqli_fetch_array(mysqli_query($con, "SELECT name FROM tbl_admin WHERE admin_id = '1'"));
	//$fromName = $adminRec['name'];
	$fromName = $settingsRec['site_name'];
	
	$mail->From = $fromEmail;
	$mail->FromName = $fromName;
	$mail->IsHTML(true);	
	$mail->Subject = $subject;	

			
	
	if($mailType=='promo'){
		$selectUsrQry = "SELECT * FROM tbl_user WHERE id = '".$userId."' AND promo_notifications = 'yes'";						
		$unsubscribeText = "<br><p>If you are not interested to receive such email notifications, go to your <a href='".BASE_URL."index.php?page=user_edit_profile'>profile page</a> and disable the corresponding email notification checkbox.</p>";
	} else {
		$selectUsrQry = "SELECT * FROM tbl_user WHERE id = '".$userId."'";	
		$unsubscribeText = '';
	}
	$execUsrQry = mysqli_query($con, $selectUsrQry);
	if(mysqli_num_rows($execUsrQry) > 0) {
		$cc = 1;
		$resUsrQry = mysqli_fetch_array($execUsrQry);
			//$resUsrQry['email'] = 'swaroop@pi-digi.com'; 
			$mail->AddAddress($resUsrQry['email'], $resUsrQry['name']);					
			$to_name = $resUsrQry['name'];			
	
					$email_content = '<table width="600" border="0" align="left" cellpadding="0" cellspacing="0">
				  <tr>
				    <td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>
				    <td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>
				  </tr>
				   <tr>
				    <td height="5" colspan="2" align="left" valign="top"></td>
				  </tr>
				  <!--<tr>
				    <td height="49" colspan="2" align="left" valign="top" style="padding:10px 0px 0px 20px; color:#8F0AA9; font-size:24px; font-family:Times New Roman, Times,   serif;">Mail From '.SITE_NAME.' </td>
				  </tr>-->
				  <tr>
				    <td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#8F0AA9 solid 1px;">
				      <tr>
				        <td height="4" colspan="4" align="left" valign="top" bgcolor="#8F0AA9"></td>
				      </tr>
				      <tr>
				        <td height="8" colspan="4" align="left" valign="top"></td>
				      </tr>
				        <tr>
				        <td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $to_name .',</td>
				      </tr>
				          <tr>
				            <td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>
				        </tr>
				      <tr>
				        <td height="10" colspan="4" align="left" valign="top"></td>
				      </tr>
				      <tr>
				        <td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
				          <tr>
				            <td width="8" align="left" valign="top">&nbsp;</td>
				            <td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">'.
				            
				           $content.'<br>
				           '.$unsubscribeText.'	
				
				              </td>
				            <td width="7" align="left" valign="top">&nbsp;</td>
				          </tr>
				        </table></td>
				      </tr><tr>
				        <td colspan="4" align="left" valign="top">&nbsp;</td>
				      </tr>
				    </table></td>
				  </tr>
				</table>';
			$mail->Body = $email_content; 			
			if($mail->Send()){ ?>
				<script language="javascript">
					alert ("Mail sent Successfully");
					window.location="./index.php?page=send_mail&user_id=<?php echo $userId; ?>";
				</script>
			<?php }	else {
				$error = 'Unable to send mail';
				return FALSE;
			}				
		} else {
			$error = 'Sorry, the user doesnt enabled promo emails';
			return FALSE;
		}	
	?>
		
	<?php
}

if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!=''){
	$userId = trim($_REQUEST['user_id']);
	
	//check if promo emails enabled
	$chkRec = mysqli_fetch_array(mysqli_query($con, "SELECT promo_notifications FROM tbl_user WHERE id = '".$userId."'"));
	$promoNotifications = $chkRec['promo_notifications'];
	
	$content ='';
	$status = '';
	$adminId = $_SESSION['adminId'];	
	date_default_timezone_set('UTC');	
	$mail = new PHPMailer();

	if(isset($_POST['Send'])) {
		
		
		
		//$content = stripslashes(trim($_POST['FCKeditor1']));
		$content = trim($_POST['editordata']);
			
		if(strlen($content) == 0){$error = "Please Enter Content";}
		$subject = trim($_POST['subject']);
		if(strlen($subject) == 0){$error = "Please Enter  subject";}
		
		if(isset($_POST['mail_type']) && $_POST['mail_type']!=''){
			$mail_type = mysqli_real_escape_string($con,trim($_POST['mail_type']));
		} else {
			$error = 'Please select mail type';
		}
				
		if(empty($error)) {
			$send = sendMail($subject, $content, $userId,  $mail, $mail_type);
			if($send){
				$success = 'Message has been sent to the user';
			} else {
				$error = 'sorry, something went wrong';
			}
		} 
	}
} else {
	$error = 'Unable to get user id';
}

		
?>