<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

//Send email with Gmail
class Email extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('email');
        $this->load->library('ion_auth');
        $this->load->library('recaptcha');
    }

    public $data = array('subview' => 'Opps, forgot to set a subview');
    

    /**
     * Email message to admin
     */

    public function sendEmail() {
        
        $this->data['recaptcha'] = $this->recaptcha->get_html();

        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'trim|xss_clean');
        $this->form_validation->set_rules('title', 'Title', 'trim|xss_clean');
        $this->form_validation->set_rules('address', 'Address', 'trim|xss_clean');
        $this->form_validation->set_rules('address2', 'Address-Cont', 'trim|xss_clean');
        $this->form_validation->set_rules('city', 'City', 'trim|xss_clean');
        $this->form_validation->set_rules('state', 'State', 'trim|xss_clean');
        $this->form_validation->set_rules('zipcode', 'Zip Code', 'trim|xss_clean');
        $this->form_validation->set_rules('country', 'Country', 'trim|xss_clean');
        $this->form_validation->set_rules('phone', 'Phone', 'trim|xss_clean');
        $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('messagebox', 'Message', 'max_length[100]|trim|xss_clean');
        $this->form_validation->set_rules('recaptcha_response_field', 'Captcha', 'required|callback__validateCaptcha');

        if ($this->form_validation->run() == FALSE) {
            $this->data['subview'] = 'contact';
            
            if ($this->ion_auth->logged_in() && !$this->ion_auth->is_admin()) {
                //load the view passing $data values
                $this->load->view("layouts/main_logged_in", $this->data);
            } else {
                //load the view passing $data values
                $this->load->view("layouts/main", $this->data);
            }

        } else {

            $fname = $this->input->post('fname');
            $lname = $this->input->post('lname');
            $company = $this->input->post('company');
            $title = $this->input->post('title');
            $address = $this->input->post('address');
            $address2 = $this->input->post('address2');
            $city = $this->input->post('city');
            $state = $this->input->post('state');
            $zipcode = $this->input->post('zipcode');
            $country = $this->input->post('country');
            $phone = $this->input->post('phone');
            $website = $this->input->post('website');
            $email = $this->input->post('email');
            $messagebox = $this->input->post('messagebox');

            $this->email->set_newline("\r\n");

            $this->email->from($email, $fname . ' ' . $lname);
            $this->email->to('leandrocalderin@gmail.com');
            $this->email->subject('Message to Admin');
            $this->email->message(
                    'First Name: ' . $fname . "<br />" .
                    'Last Name: ' . $lname . "<br />" .
                    'Company: ' . $company . "<br />" .
                    'Title: ' . $title . "<br />" .
                    'Address: ' . $address . "<br />" .
                    'Address-Cont: ' . $address2 . "<br />" .
                    'City: ' . $city . "<br />" .
                    'State: ' . $state . "<br />" .
                    'Postal/Zip Codet: ' . $zipcode . "<br />" .
                    'Country: ' . $country . "<br />" .
                    'Phone: ' . $phone . "<br />" .
                    'Website: ' . $website . "<br />" .
                    'Email: ' . $email . "<br />" .
                    'Message: ' . $messagebox . "<br />"
            );

            if ($this->email->send()) {
                $this->session->set_flashdata('email_sent', 'Email has been sent.');
                redirect('contact');  //email/contact
            }
        }
    }

//

    /**
     * Email message to member
     */
    public function sendMessage() {
        
        $user_email = $this->input->post('emailm');
        $content = $this->input->post('messageboxm');
        $hidden = $this->input->post('hiddenfield');

        //echo "<div class=\"alert alert-success\" >Your message has been sent to: " . $hidden . "</div>";

        $this->load->library('email');
        $this->email->set_newline("\r\n");

        $this->email->from($user_email);
        $this->email->to($hidden);
        $this->email->subject('Message from AeroEngine Cloud');
        $this->email->message(
                'From: ' . $user_email . "<br />" .
                'Message: ' . $content . "<br />"
        );
        
        $this->email->send();
        
        echo "<div class=\"alert alert-success\" >Your message has been sent to: " . $hidden . "</div>";
    }
    
    private function validateCaptcha($value) {
        if ($this->recaptcha->check_answer($_SERVER['REMOTE_ADDR'], $this->input->post('recaptcha_challenge_field'), $value)) {
            return TRUE;
        } else {
            $this->form_validation->set_message(__FUNCTION__, lang('recaptcha_incorrect_response'));
            return FALSE;
        }
    }


}