<?php
require_once("../config.php");
require_once("../startup.php");

// ini_set('display_errors', 1);         
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);                   

require_once("sendEmail.php");
// 	require_once("../sendEmail.src");
require_once(ROOT_DIR . "include/classes/alltables.class.php");



$alltables = new AllTables();

// 	#Sender Name
//     $from_name = $_POST['txt_name'];       

//     #Sender Email
//     $from_email = $_POST['txt_email'];

//     #Recipient Name
//     $to_name = $_POST['txt_recipientname'];

//     #Recipient Email
//     $to_email = $_POST['txt_recipientemail'];

//     #Subject
//     $subject = $_POST['txt_subject']; 


if (isset($_POST['btn_submit'])) 
{
    $fromname = $_POST['txt_fromname'];
    $fromemail = $_POST['txt_fromemail'];
    $recipientname = $_POST['txt_recipientname'];
    $recipientemail = $_POST['txt_recipientemail'];
    $redirect = $_POST['txt_redirect'];
    $subject = $_POST['txt_subject'];

    $username = $_POST['txt_username'];
    $useremail = $_POST['txt_email'];

    $newsdataarray['fname'] = $username;
    $newsdataarray['email'] = $useremail;
    $newsdataarray['fromname'] = $fromname;
    $newsdataarray['fromemail'] = $fromemail;
    $newsdataarray['recipientname'] = $recipientname;
    $newsdataarray['recipientemail'] = $recipientemail;
    $newsdataarray['subject'] = $subject;
    $newsdataarray['redirectpagename'] = $redirect;

    $add_data = $alltables->AddInfo($newsdataarray, 'mstnewsletterinfo');

    $from_name = $fromname;
    $from_email = $fromemail;


    $admin_to_name = $recipientname;
    $admin_to_email = $recipientemail;

    $user_to_name = $username;
    $user_to_email = $useremail;

    $subject = $subject;
    $user_subject = "E-Newsletter Subscription";

    $text_message = '';
    $attachment = '';


    foreach ($_POST as $key => $value) {
        if (($key != "btn_submit") && ($key != "txt_form") && ($key != "txt_fromname") && ($key != "txt_fromemail") && ($key != "txt_recipientemail") && ($key != "txt_recipientname") && ($key != "txt_subject") && ($key != "txt_redirect")) // everything except record ID,title,submit 
        {
            #Replce prefix of input field name
            $key_replace = str_replace(array('txt_', 'cmb_', 'sel_'), '', $key);

            #keep first letter is capital of key
            $keyval = ucfirst($key_replace);

            #Email content
            $emailMsg .= "$keyval : $value <BR>";
        }
    }
    $html_message .= $emailMsg;

    $user_html_message = "Dear $username,
                                <br><br>
                                Thank you for registering the E-newsletter subscription.
                                <br><br>
                                Best Regards,<br>
                                India International Livestock Expo";

    $user_sendmail = sendPHPMail($from_name, $from_email, $user_to_name, $user_to_email, $user_subject, $text_message, $user_html_message, $attachment);

    // $sendmail = sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $attachment);                       
    $admin_html_message = "Dear Admin,
                           <br><br>
                         The following user has filled the newsletter subscription form.
                         <br>
                         Name - $username <br>
                         Email id -$useremail";
    $admin_sendmail = sendPHPMail($from_name, $from_email, $admin_to_name, $admin_to_email, $subject, $text_message, $admin_html_message, $attachment);
    
    if ($user_sendmail == "1") {
        $redirect = DOC_ROOT;
        echo "<script>
                alert('Successfully Subscribed!');
                setTimeout(function() {
                    window.location.href = '$redirect';
                },);                   
              </script>";
        exit();  
    } else {
        echo "<script>
                alert('Subscription Failed! Please try again.');
                setTimeout(function() {
                    window.history.back(); 
                },); 
              </script>";
        exit();
    }
    
}
