<?php

class Pages extends Public_Controller {

    public function __construct() {

        parent::__construct();
        $this->load->helper(array('file'));
        $this->load->library(array('Dmailer', 'safe_encrypt'));
        $this->load->model(array('pages/pages_model'));
        $this->form_validation->set_error_delimiters("<div class='required red'>", "</div>");
        $this->page_section_ct = 'static';
    }

    public function index() {
        $friendly_url = $this->uri->uri_string;
        $condition = array('friendly_url' => $friendly_url, 'status' => '1');
        $content = $this->pages_model->get_cms_page($condition);
        $data['content'] = $content;
        $this->load->view('pages/cms_page_view', $data);
    }

    public function aboutus() {
        $friendly_url = $this->uri->uri_string;
        $condition = array('friendly_url' => $friendly_url, 'status' => '1');
        $content = $this->pages_model->get_cms_page($condition);
        $data['content'] = $content;
        $data['banner'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_page='about' and status='1' and CURDATE() between banner_start_date and banner_end_date")->row_array();
        $data['latestnews1'] = $this->db->query("SELECT * FROM wps_news WHERE status='1' order by news_id desc limit 1")->row_array();
        $data['latestnews2'] = $this->db->query("SELECT * FROM wps_news WHERE status='1' order by news_id desc limit 3 offset 1")->result_array();
        $this->load->view('pages/about', $data);
    }


    public function sitemap() {
      
        $this->load->view('pages/sitemaphtml');
    }

    public function resources() {
        $friendly_url = $this->uri->uri_string;
        $condition = array('friendly_url' => $friendly_url, 'status' => '1');
        $content = $this->pages_model->get_cms_page($condition);
        $data['content'] = $content;

        $data['resourceData'] = $this->db->query("SELECT * FROM wps_resources WHERE status='1' order by id desc ")->result_array();
        $this->load->view('pages/resources', $data);
    }

    public function contactus() {

        $adminRes = get_site_email();
        $data['adminRes'] = $adminRes;
        //trace($_SERVER['HTTP_REFERER']);

        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|alpha');
        $this->form_validation->set_rules('location', 'Location', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('phone', 'Phone Number', 'trim|required|numeric|min_length[10]|max_length[10]');
        //$this->form_validation->set_rules('purpose', 'Purpose', 'trim|alpha');
        $this->form_validation->set_rules('message', 'Comment', 'trim|required');
        $this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'trim|required');

        if ($this->form_validation->run() == TRUE) {
            if ($this->input->post()) {
                // trace($_POST);exit();
            }
            $post_data = http_build_query(
                    array(
                        'secret' => CAPTCHA_SECRET,
                        'response' => $_POST['g-recaptcha-response'],
                        'remoteip' => $_SERVER['REMOTE_ADDR'],
                    )
            );
            $opts = array('http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $post_data,
                ),
            );
            $context = stream_context_create($opts);
            $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
            $capcha_result = json_decode($response);
            if (!$capcha_result->success) {
                //throw new Exception('Gah! CAPTCHA verification failed. Please try again', 1);
                $msg = "CAPTCHA verification failed!";
                $this->session->set_userdata(array('msg_type' => 'error'));
                $this->session->set_flashdata('error', $msg);
                $friendly_url = $this->uri->uri_string;
                $condition = array('friendly_url' => $friendly_url, 'status' => '1');
                $content = $this->pages_model->get_cms_page($condition);
                $data['content'] = $content;
                $data['title'] = "Contact Us";
                $this->load->view('contactus', $data);
            } else {

                $posted_data = array(
                    'type' => '1',
                    'first_name' => $this->input->post('first_name'),
                    'location' => $this->input->post('location'),
                    'email' => $this->input->post('email'),
                    'mobile_number' => $this->input->post('phone'),
                    'message' => $this->input->post('message'),
                    // 'purpose' => $this->input->post('purpose'),
                    'receive_date' => $this->config->item('config.date.time')
                );
                $this->pages_model->safe_insert('wps_enquiry', $posted_data, FALSE);

                //Mail
                $content = get_content('wps_auto_respond_mails', '5');
                $name = $this->input->post('first_name');
                $body = $content->email_content;
                $body = str_replace('{mem_name}', 'Admin', $body);
                $body = str_replace('{user_name}', $name, $body);
                $body = str_replace('{email}', $this->input->post('email'), $body);
                $body = str_replace('{phone}', $this->input->post('phone'), $body);
                $body = str_replace('{location}', $this->input->post('location'), $body);
                $body = str_replace('{comments}', $this->input->post('message'), $body);
                $body = str_replace('{site_name}', SITENAME, $body);
                $body = str_replace('{admin_email}', $this->admin_info->admin_email, $body);
                if ($this->admin_info->website_mode == 'Live') {
                    $mail_conf = array(
                        'subject' => "Enquiry from Life Optoelectronics",
                        'to_email' => $adminRes->admin_email,
                        'from_email' => $this->input->post('email'),
                        'from_name' => $name,
                        'body_part' => $body
                    );
                    $this->dmailer->mail_notify($mail_conf);
                    $mail_conf = array(
                        'subject' => "Enquiry from life Optoelectronics",
                        'to_email' => 'webpulseindia@gmail.com',
                        'from_email' => $this->input->post('email'),
                        'from_name' => $name,
                        'body_part' => $body
                    );
                    $this->dmailer->mail_notify($mail_conf);
                } else {
                    $mail_conf = array(
                        'subject' => "Webpulseindia.com - Enquiry from life Optoelectronics",
                        'to_email' => 'test.webpulse@gmail.com',
                        'from_email' => $this->input->post('email'),
                        'from_name' => $name,
                        'body_part' => $body
                    );
                    $this->dmailer->mail_notify($mail_conf);
                }

                $msg = "Your Enquiry has been Submitted Successfully";
                $this->session->set_userdata(array('msg_type' => 'success'));
                $this->session->set_flashdata('success', $msg);
                redirect(base_url('contact-us'));
            }
        }

        $friendly_url = $this->uri->uri_string;
        $condition = array('friendly_url' => $friendly_url, 'status' => '14');
        $content = $this->pages_model->get_cms_page($condition);
        $data['content'] = $content;
        $data['title'] = "Contact Us";
        $this->load->view('contactus', $data);
    }

    public function marketarea() {
        $data = array();

        $data['country'] = $this->db->query("SELECT id, name, country_temp_name, status FROM wps_countries_list WHERE status ='1'")->result_array();

        $data['state'] = $this->db->query("SELECT id, name, country_id, temp_title, status FROM wps_states_list WHERE status ='1'")->result_array();

        $this->load->view('marketarea', $data);
    }

    public function QuickEnquiry() {

        $adminRes = get_site_email();
        $data['adminRes'] = $adminRes;
        $url = $_SERVER['HTTP_REFERER'];

        $this->form_validation->set_rules('first_name', 'Name', 'trim|required|alpha');
        $this->form_validation->set_rules('address', 'Location', 'trim|required');
        $this->form_validation->set_rules('email', 'Email id', 'trim|required|valid_email');
        $this->form_validation->set_rules('mobile_number', 'Phone Number', 'trim|required|numeric|min_length[10]|max_length[10]');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');

        if ($this->form_validation->run() == TRUE) {

            $posted_data = array(
                'type' => '1',
                'first_name' => $this->input->post('first_name'),
                'email' => $this->input->post('email'),
                'mobile_number' => $this->input->post('mobile_number'),
                'location' => $this->input->post('address'),
                'post_url' => $url,
                'message' => $this->input->post('message'),
                'receive_date' => $this->config->item('config.date.time'),
            );
            $result = $this->pages_model->safe_insert('wps_enquiry', $posted_data, false);

            $content = get_content('wps_auto_respond_mails', '4');
            $body = $content->email_content;
            $body = str_replace('{mem_name}', 'Admin', $body);
            $body = str_replace('{user_name}', $this->input->post('first_name'), $body);
            $body = str_replace('{user_email}', $this->input->post('email'), $body);
            $body = str_replace('{user_mobile}', $this->input->post('mobile_number'), $body);
            $body = str_replace('{user_location}', $this->input->post('address'), $body);
            $body = str_replace('{user_comments}', $this->input->post('message'), $body);
            $body = str_replace('{site_name}', $this->config->item('site_name'), $body);
            $body = str_replace('{posted_url}', $url, $body);
            $body = str_replace('{admin_email}', $this->admin_info->admin_email, $body);

            $mail_conf = array(
                'subject' => 'Webpulseindia.com : Enquiry from Life Optoelectronics',
                'to_email' => $this->admin_info->admin_email, //$this->admin_info->admin_email
                'reply_to' => $this->input->post('email'),
                'reply_name' => $this->input->post('first_name'),
                'from_email' => $this->input->post('email'),
                'from_name' => $this->input->post('first_name'),
                'body_part' => $body
            );
//            print_r($mail_conf);
//            die;
            $this->dmailer->mail_notify($mail_conf);
            $res = 'success';
        } else {
            $res = '<div class="alert alert-danger">
                      <strong>' . validation_errors() . '
                  </div>';
        }
        echo $res;
        exit;
    }

    public function productmodal() {
      
        $adminRes = get_site_email();
        $data['adminRes'] = $adminRes;
//        $url = $_SERVER['HTTP_REFERER'];

        $this->form_validation->set_rules('pro_name', 'Name', 'trim|required|alpha');
        $this->form_validation->set_rules('pro_location', 'Location', 'trim|required');
        $this->form_validation->set_rules('pro_email', 'Email id', 'trim|required|valid_email');
        $this->form_validation->set_rules('pro_mobile', 'Phone Number', 'trim|required|numeric|min_length[10]|max_length[10]');
        $this->form_validation->set_rules('pro_message', 'Message', 'trim|required');

        if ($this->form_validation->run() == TRUE) {

            $posted_data = array(
                'type' => '4',
                'first_name' => $this->input->post('pro_name'),
                'email' => $this->input->post('pro_email'),
                'mobile_number' => $this->input->post('pro_mobile'),
                'location' => $this->input->post('pro_location'),
                'post_url' => $this->input->post('post_url'),
                'message' => $this->input->post('pro_message'),
                'products_id' => $this->input->post('product_id'),
                'product_name' => $this->input->post('product_service'),
                'receive_date' => $this->config->item('config.date.time'),
            );
            $result = $this->pages_model->safe_insert('wps_enquiry', $posted_data, false);

            $content = get_content('wps_auto_respond_mails', '6');
            $body = $content->email_content;
            $body = str_replace('{mem_name}', 'Admin', $body);
            $body = str_replace('{user_name}', $this->input->post('pro_name'), $body);
            $body = str_replace('{user_email}', $this->input->post('pro_email'), $body);
            $body = str_replace('{user_mobile}', $this->input->post('pro_mobile'), $body);
            $body = str_replace('{user_location}', $this->input->post('pro_location'), $body);
            $body = str_replace('{user_comments}', $this->input->post('pro_message'), $body);
             $body = str_replace('{enq}', $this->input->post('product_service'), $body);
            $body = str_replace('{site_name}', $this->config->item('site_name'), $body);
            $body = str_replace('{posted_url}', $this->input->post('post_url'), $body);
            $body = str_replace('{admin_email}', $this->admin_info->admin_email, $body);

            $mail_conf = array(
                'subject' => 'Webpulseindia.com : Enquiry from Life Optoelectronics',
                'to_email' => 'dhruv@lifeopto.in', //$this->admin_info->admin_email
                'reply_to' => $this->input->post('pro_email'),
                'reply_name' => $this->input->post('pro_name'),
                'from_email' => $this->input->post('pro_email'),
                'from_name' => $this->input->post('pro_name'),
                'body_part' => $body
            );
//            print_r($mail_conf);
//            die;
            $this->dmailer->mail_notify($mail_conf);
            $res = 'success';
        } else {
            $res = '<div class="alert alert-danger">
                      <strong>' . validation_errors() . '
                  </div>';
        }
        echo $res;
        exit;
    }

    // public function terms(){
    //     $result = $this->pages_model->termsAndConditions();
    //     $data['content'] = $result['0'];
    //     $this->load->view('terms', $data);
    // }

}

/* End of file pages.php */