<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Public_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model(array( 'products/product_model','category/category_model','members/members_model'));
  }

  public function index() {
     //home category
     $home_cat = array('field' => "*", 'condition' => "AND status='1' AND home_menu = '1'",'limit'=>"4", 'debug' => FALSE);
     $category_result = $this->category_model->getcategory($home_cat);
     $data['home_cat'] = $category_result;
 
     $home_cat1 = array('field' => "*", 'condition' => "AND status='1' AND home_cat = '1'",'limit'=>"4", 'debug' => FALSE);
     $category_result = $this->category_model->getcategory($home_cat1);
     $data['home_cat1'] = $category_result;
$data['top_category'] = $this->db->query("SELECT * FROM wps_categories WHERE parent_id='1' AND status = '1' limit 0,8")->result_array();
      $data['category_collection'] = $this->db->query("SELECT * FROM wps_categories WHERE category_id  IN(44,33,1) AND status = '1' order by category_id Desc limit 0,3")->result_array();
    $data['middlebanner1'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Middle 1' AND status = '1'")->row_array();
    $data['middlebanner2'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Middle 2' AND status = '1'")->row_array();
    $data['bottomBanner1'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 1' AND status = '1'")->row_array();
    $data['bottomBanner2'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 2' AND status = '1'")->row_array();
    $data['bottomBanner3'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 3' AND status = '1'")->row_array();
    $data['bottomBanner4'] = $this->db->query("SELECT * FROM wps_banners WHERE banner_position = 'Index Bottom 4' AND status = '1'")->row_array();
    $data['shopcategory'] =  $this->db->query("SELECT * FROM wps_categories WHERE parent_id = '0' AND status = '1'")->result_array();
    $data['faq'] =  $this->db->query("SELECT * FROM wps_faq WHERE status = '1'")->result_array();

    //Popular products
    $data['popular_products'] = $this->get_popular_product();
    
    //New Arrival products
    $data['new_products'] = $this->get_new_product();
    $data['offer_products'] = $this->get_offer_product();
    $data['trending_products'] = $this->get_trendig_product();
    $data['top_rated_product'] = $this->get_top_rated_product();
      

    $content = "";
    $homeHeading='';
    //Check if it is subdomain
    $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $uri_segments = explode('/', $uri_path);
    $st = $uri_segments[1];
    if (strstr($st, '.html')) {
      $st = substr($st, 0, -5);
    }

    $stArray = $this->db->query("SELECT meta_id, page_url FROM wps_meta_tags WHERE is_fixed='L' AND page_url='" . $st . "'")->row_array();
    if (is_array($stArray) & !empty($stArray)) {
      $locId = $stArray['meta_id'];
      $resprosub = $this->db->query("SELECT * FROM wps_subloccontent WHERE status = '1' AND FIND_IN_SET($locId,location_id)")->row();
      if (is_object($resprosub) && !empty($resprosub)) {
        $key1 = $resprosub->meta_key1;
        $key2 = $resprosub->meta_key2;
        $key3 = $resprosub->meta_key3;
        $content = str_replace('{location}', ucwords(locationName($st)), str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosub->description))));  
        $homeHeading = 'Ride On Battery Operated Car in - '.ucwords(locationName($st));     
      }
    }
    else
    {
      $content = get_db_field_value('wps_cms_pages', 'page_description', "WHERE page_id = '10'");
      $homeHeading = 'Ride On Battery Operated Car in Delhi'; 
    }
    $data['subheading1']= get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
    $data['subheading2']= get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
    $data['subheading3']= get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
    $data['subheading4']= get_db_field_value('wps_cms_pages', 'page_name', "WHERE page_id = '10'");
    $data['homeHeading'] = $homeHeading;
    $data['home'] = $content;

     //banners
    $data['banner'] = $this->db->query("SELECT * FROM wps_banners WHERE status = '1' AND banner_position = 'Index Slider' order by banner_id asc")->result_array();
    $data['blogs'] = $this->db->query("SELECT * FROM wps_blog WHERE status = '1' order by article_id desc limit 3")->result_array();
    
    $this->load->view('view_home', $data);
  }

  public function get_popular_product() {
    $condtion['status'] = '1';
    $condtion['where'] = "popular_product = '1'";
    $condtion['orderby'] = 'products_id DESC';
    $product_list = $this->product_model->get_products('6', '0', $condtion);
    return $product_list;
  }
  
  public function get_new_product() {
    $condtion['status'] = '1';
    $condtion['where'] = "newarrival_product = '1'";
    //$condtion['orderby'] = 'products_id ASC';
    $product_list = $this->product_model->get_products('8', '0', $condtion);
    return $product_list;
  }
    public function get_offer_product() {
    $condtion['status'] = '1';
   // $condtion['where'] = "newarrival_product = '1'";
    //$condtion['orderby'] = 'products_id ASC';
    $product_list = $this->product_model->get_products('3', '0', $condtion);
    return $product_list;
  }
    
      public function get_trendig_product() {
    $condtion['status'] = '1';
    $condtion['where'] = "new_arrival_pro = '1'";
    $condtion['orderby'] = 'products_id Desc';
    $product_list = $this->product_model->get_products('8', '0', $condtion);
    return $product_list;
  }
    
    public function get_top_rated_product() {
    $condtion['status'] = '1';
    $condtion['where'] = "top_rated_product = '1'";
    $condtion['orderby'] = 'products_id Desc';
    $product_list = $this->product_model->get_products('8', '0', $condtion);
    return $product_list;
  }
    
  
  
  public function submit_newslater() {
    
    $this->form_validation->set_rules('newsletter_email', 'Email', 'trim|required|valid_email');
    $email=$this->input->post('newsletter_email');
     $check_email=$this->db->query("select subscriber_email from wps_newsletters where subscriber_email ='$email' and status='1'")->row_array();


   if ($check_email['subscriber_email']!='') {
    $callback_error="<div class='alert alert-danger'>You are Already Registered With Newsletter Service</div>";
echo $callback_error;
   }
   else {
   
    if ($this->form_validation->run() == TRUE) {  
   
      $posted_data = array (
        'subscriber_email' => $this->input->post('newsletter_email'),
        'message' => 'Signup For Newsletter',
        'subscribe_date' => $this->config->item('config.date.time')
      );   

      $result = $this->members_model->safe_insert('wps_newsletters', $posted_data, false);
      $subject = "WebpulseIndia.com - Signup For Newsletter from Toyzoo";
      $content = get_content('wps_auto_respond_mails', '3');
      $body = $content->email_content;
      $body = str_replace('{mem_name}', 'Admin', $body);
      $body = str_replace('{admin_email}', $this->input->post('newsletter_email'), $body);
      $body = str_replace('{date}', $this->config->item('config.date.time'), $body);
      $body = str_replace('{site_name}', $this->config->item('site_name'), $body);
      $mail_conf = array(
        'subject' => $subject,
        'to_email' => $this->input->post('newsletter_email'),
        'reply_to' => $this->input->post('newsletter_email'),
        //'reply_name' => $this->input->post('wname'),
        'from_email' => 'enquiry@webpulseindia.com',
        'from_name' => $this->input->post('newsletter_email'),
        'body_part' => $body
      );

      
    $this->dmailer->mail_notify($mail_conf);           
      $res = 'success';    
    } 
    else {
      $res = '<div class="alert alert-danger">
      <strong>'.validation_errors().'
      </div>';
    }
    echo $res;
    exit;
  }
  }


}
