                                                                                                                                                                                
                                                                                                                                                                                
<?php

/**
 * @version     CVS: 1.0.0
 * @package     com_rtform
 * @subpackage  mod_rtform
 * @author      Radius Theme <support@radiustheme.com>
 * @copyright   2016 RadiusTheme.com
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die;

/**
 * Helper for mod_rtform
 *
 * @package     com_rtform
 * @subpackage  mod_rtform
 * @since       1.6
 */
class ModRtformHelper
{
	
	public static function getAjax()
	{
	// grab recaptcha library
		$app = JFactory::getApplication();
		$rtform_params = JComponentHelper::getParams('com_rtform');
		$recipient = $rtform_params->get('admin_email');
		$enable_recaptcha = $rtform_params->get('enable_recaptcha');
		$sitekey = $rtform_params->get('sitekey');
		$secret = $rtform_params->get('secret');

		jimport('joomla.application.module.helper');
		$input  			= JFactory::getApplication()->input;
		$module 			= JModuleHelper::getModule('mod_rtform');
		$params 			= new JRegistry();
		$params->loadString($module->params);

		$mail 				= JFactory::getMailer();
		$db = JFactory::getDbo();
		$response = null;
		$error = true;
		$data = $msg = null;

		// if submitted check response
		//inputs
		$inputs 			= $input->get('data', array(), 'ARRAY');
		$g_recaptcha = null;
		$sent = false;

		foreach ($inputs as $input) {
			
			if( $input['name'] == 'email' )
			{
				$email = $input['value'];
			}

			if( $input['name'] == 'name' )
			{
				$name = $input['value'];
			}

			if( $input['name'] == 'app_date' )
			{
				$app_date = $input['value'];
			}
			if( $input['name'] == 'phone_no' )
			{
				$phone_no = $input['value'];
			}

			if( $input['name'] == 'message' )
			{
				$main_message = nl2br( $input['value'] );
			}
			if( $input['name'] == 'g-recaptcha-response' )
			{
				$g_recaptcha = $input['value'];
				
			}


		}

		$message ='';
		$message .= JText::_('MOD_RTFORM_LBL_EMAIL_YOUR_NAME'). $name;
		$message .= '<br>'.JText::_('MOD_RTFORM_LBL_EMAIL_YOUR_EMAIL'). $email;
		$message .= '<br>'.JText::_('MOD_RTFORM_LBL_EMAIL_APPT_DATE'). $app_date;
		$message .= '<br>'.JText::_('MOD_RTFORM_LBL_EMAIL_PHONE_NO'). $phone_no;
		$message .= '<br>'.JText::_('MOD_RTFORM_LBL_EMAIL_YOUR_MESSAGE'). $main_message;

		$subject = JText::_('MOD_RTFORM_LBL_EMAIL_SUBJECT'). $name;

	
		$sender = array($email, $name);	
		$mail->setSender($sender);
		$mail->addRecipient($recipient);
		$mail->setSubject($subject);
		$mail->isHTML(true);
		$mail->Encoding = 'base64';	
		$mail->setBody($message);



		if($g_recaptcha){
			// check secret key
			
			require_once "recaptchalib.php";
			$reCaptcha = new ReCaptcha($secret);
			$response = $reCaptcha->verifyResponse(
        		$_SERVER["REMOTE_ADDR"],
        		$g_recaptcha
    		);
    		if($response != null && $response->success){
				$sent = $mail->Send();
    		}

		}else{
			$msg = "Please fill the google reCaptcha!!!";
		}

		if ($sent) {
			$query = "INSERT INTO #__rtform_submissions ( `id`, `name`, `email`, `appointment_date`, `phone`, `message`)
    VALUES (NULL,'" . $name . "','" . $email . "','" . $app_date . "','" . $phone_no . "','" . $main_message . "')";

        	$db->setQuery($query);
        	$db->execute();

			$msg = "Email successfully sent!!!";
			$error = false;
        
		} else {
			$msg = "Error to sending message!!!";
		}

		$return = array(
			'msg' => $msg,
			'error' => $error
		);

		echo json_encode( $return );
		jexit();
	}
}
