\!/ KyuuKazami \!/

Path : /home/kohli/public_html/application/controllers/
Upload :
Current File : /home/kohli/public_html/application/controllers/ajax.php

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

class Ajax extends CI_Controller {
    public function __construct()
    {
        parent::__construct();        
        $this->load->library("common_lib");
        $this->load->model("productm");
        $this->load->model("commonm");
    }
    
    public function disable_newsletter(){
        $this->input->set_cookie("disable_popup","1",86500);
    }
    
    public function save_seller_city(){
        $city_id = urldecode($this->common_lib->get_n_get("city",true));
        $city_detail = $this->commonm->get_all("seller_cities",array("seller_city_id" => $city_id));
        if(count($city_detail) > 0){
            $this->input->set_cookie("seller_city",urlencode($this->common_lib->encode($city_id)),86500 * 30);
            echo json_encode(array("message" => "redirect"));
            exit;
        }else{
            echo json_encode(array("error" => $this->common_lib->render_messages("Some error occured, Please refresh page and try again!","warning")));
            exit;
        }
    }
    
    public function send_newsletter(){
        $newsletter_email = urldecode($this->common_lib->get_get("email",true));
                
        $user_detail = $this->commonm->get_all("users",array("email" => $newsletter_email));
        if(count($user_detail) == 0){
            $password = random_string('alnum',10);
            $enc_password = $this->common_lib->one_way_encrypt($password);
            $customer_data['username'] = $newsletter_email;
            $customer_data['password'] = $enc_password;
            $customer_data['email'] = $newsletter_email;
            $customer_data['from_newsletter'] = 1;
            
            $this->commonm->db_insert($customer_data,"users");
            
            $customer_data['verification_link'] = $this->common_lib->generate_verification_link($newsletter_email);
            
            $this->common_lib->send_registration_email($customer_data,$newsletter_email);
                
            $newsletter_detail = $this->commonm->get_all("newsletter_settings");
            
            $data['coupon_code'] = "";
            if(count($newsletter_detail) > 0){
                if($newsletter_detail[0]->newsletter_coupon > 0){
                    $coupon_detail = $this->commonm->get_all("coupons",array("coupon_id" => $newsletter_detail[0]->newsletter_coupon));
                    if(count($coupon_detail) > 0){
                        $data['coupon_code'] = $coupon_detail[0]->coupon_code;            
                    }        
                }   
            }
                        
            $this->common_lib->send_newsletter_subscribe($data,$newsletter_email);
            echo json_encode(array("message" => $this->common_lib->render_messages("Thanks for subscribing with us. Please check your email for coupon code.","success")));
            exit;
        }else{
            echo json_encode(array("error" => $this->common_lib->render_messages("This email address is already subscribed","warning")));
            exit;
        }
    }
    
    public function save_review(){        
        $form_data = $this->common_lib->get_post("review_data");
        $form_data_fields = explode("&",$form_data);        
        foreach($form_data_fields as $field_pair){
            $field_pair_data = explode("=",$field_pair);
            $field_name = urldecode($field_pair_data[0]);
            $$field_name = urldecode($field_pair_data[1]);            
        }
        $data = array();
        $data['product_id'] = $this->common_lib->decode($token);
        $data['review_title'] = $this->common_lib->fSafeChar($review_title);
        $data['review_description'] = $this->common_lib->fSafeChar($review_description);
        $data['rating'] = $this->common_lib->fSafeNum($rating);
        $data['ip'] = $this->input->server("REMOTE_ADDR");
        $data['user_agent'] = $this->input->server("HTTP_USER_AGENT");
        $data['review_date'] = $this->common_lib->mysqlDateFormat();
        $user_id = 0;
        if($this->common_lib->get_login_id() > 0){
            $customer_detail = $this->common_lib->get_login_detail();
            $full_name = $this->common_lib->get_login_name($customer_detail);
            $email = $customer_detail['email'];
            $user_id = $this->common_lib->get_login_id();                  
        }
        $data['full_name'] = $full_name;
        $data['email'] = $email;
        $data['user_id'] = $user_id;        
        echo $this->commonm->db_insert($data,"product_reviews");        
        exit;        
    }
    
    public function delitemcart(){
        $rowid = $this->common_lib->get_post("row_id");
        $data = array(
            'rowid'   => $rowid,
            'qty'     => 0
        );
        $this->cart->update($data);        
    }
    
    public function updateitemcart(){
        $rowid = $this->common_lib->get_post("row_id");
        $qty = $this->common_lib->get_n_post("qty");
        $data = array(
            'rowid'   => $rowid,
            'qty'     => $qty
        );
        $this->cart->update($data);
        $cart_data = $this->cart->contents();
        $cart = $cart[$rowid];
        $total = 0;
        foreach($cart_data as $items)
        {
         $total +=  $items['qty'] * $items['price'] + $items['printing_price'] + $items['shipping_price'];
        $datas = array("total" => "<strong>".$this->common_lib->currency_price($total)."</strong>");
        }
        echo json_encode($datas);
        exit;        
    }
    
    public function updatecarttotal(){
        $cart_total = $this->cart->total();
        if($cart_total == 0){
            $data = array("html" => $this->common_lib->get_detail_cart_summary());
          //  print_r($cart_total);
            echo json_encode($data);
            exit;
        }
        $cart_data = $this->cart->contents();
        $total = 0;
        foreach($cart_data as $items)
        {
            
         $total +=  $items['qty'] * $items['price'] + $items['printing_price'] + $items['shipping_price'];
        $data = array("total" => $this->common_lib->currency_price($total));
        }
        echo json_encode($data);
        exit;
    }
    
    public function addtocart(){
        $printing_price = 0.00;
        $shipping_price = 0.00;
        $product_data = $this->common_lib->decode($this->common_lib->get_post("data"));
        $size = $this->common_lib->decode($this->common_lib->get_post("size"));
        $product_data = json_decode($product_data,true);
        
        $country = $this->common_lib->get_post("country");
        $qnty = $this->common_lib->get_post("qnty");
        $get_product_price = $this->commonm->get_all("products",array("product_id"=>$product_data['id']));
        $range_qnty = $this->commonm->get_all("range",array("range_id"=>$qnty));
        $rang_qnty = $range_qnty[0]->range;
        $size_price = $this->commonm->get_all("product_sizes",array("size_id" => $size,"product_id" => $product_data['id']));                
        $global_pd_setting = $this->commonm->get_all("product_settings");
        $product_id = $product_data['id'];
        $qty = $this->common_lib->get_post("qty");
        if($rang_qnty >= $qty )
        {
            $quantity = $rang_qnty;
        } else {
            $quantity = $this->common_lib->get_post("qty");
        }
        
        if($get_product_price[0]->deal_status == 1 && $get_product_price[0]->deal_price > 0 && $global_pd_setting[0]->show_deals == 1 && $get_product_price[0]->deal_start_date <= date("Y-m-d") &&  $get_product_price[0]->deal_end_date >= date("Y-m-d")){
                    $product_data['price'] = $get_product_price[0]->deal_price;
         }else{   
        
        
            if($size_price[0]->stock_price > 0){
                $product_data['price'] = $size_price[0]->stock_price;    
            }
            
            if($size_price[0]->stock_discounted_price > 0){
                $product_data['price'] = $size_price[0]->stock_discounted_price;    
            }
        }
                                                                
        $product_data = array_merge($product_data,array("pd" => $this->common_lib->get_post("pd"),"rd" => $this->common_lib->get_post("rd"),"qty" => $quantity,"size" => $size));
        
        $product_data['id'] = $product_data['id']."-".$size; 
        
        if($this->common_lib->get_post("pt") > 0){
        //$printing_price = $range_qnty[0]->printing_price;
       // $printing_price = $get_product_price[0]->printing_cost;
        $printing = $this->commonm->get_all("product_printcost",array("range_id"=>$this->common_lib->get_post("qnty"),"product_id" => $product_id));
        $printing_price = $printing[0]->print_cost;
        }else{
        $printing_price = 0;
        }
        if($country >0)
        
        {
            $cal_product = $this->commonm->get_all("product_calculator",array("country_id"=>$country,"range_id"=>$qnty));
            //print_r($cal_product);
            if(count($cal_product)>0)
            {
                if($cal_product[0]->shipping_price > 0){
                    
                    $shipping_price = $cal_product[0]->shipping_price;
                }
               
            }
        }   
        $product_data['printing_price'] = $printing_price ;
        $product_data['shipping_price'] = $shipping_price;
        $this->cart->insert($product_data); 
       // print_r($product_data);
        echo json_encode(array("success" => 1));
        exit;
    }
    
    public function addtocartbulk(){
        $product_data = $this->common_lib->decode($this->common_lib->get_post("data"));
        $size = $this->common_lib->get_post("size");
                
        $qty = $this->common_lib->get_post("qty");
        
        $size_list = explode(",",$size);
        $qty_list = explode(",",$qty);
        
        $index = 0;
        $product_data = json_decode($product_data,true);        
        foreach($size_list as $size){
            $size = $this->common_lib->decode($size);
            $qty = $this->common_lib->fSafeNum($qty_list[$index]);
            
            if($qty > 0){
                
                
                $size_price = $this->commonm->get_all("product_sizes",array("size_id" => $size,"product_id" => $product_data['id']));                
                
                if($size_price[0]->stock_price > 0){
                    $product_data['price'] = $size_price[0]->stock_price;    
                }
                
                if($size_price[0]->stock_discounted_price > 0){
                    $product_data['price'] = $size_price[0]->stock_discounted_price;    
                }
                                                                        
                $bulk_product_data = array_merge($product_data,array("pd" => $this->common_lib->get_post("pd"),"rd" => $this->common_lib->get_post("rd"),"qty" => $qty,"size" => $size));
                
                $bulk_product_data['id'] = $bulk_product_data['id']."-".$size;
                        
                $this->cart->insert($bulk_product_data);
            }
            $index++;
        } 
        echo json_encode(array("success" => 1));
        exit;
    }
    
    
        function product_list()
    {
         $this->db->select('*');
         $this->db->from('products');
        // $this->db->join('product_sizes','product_sizes.product_id = products.product_id');
         $this->db->where("active",1);
         $this->db->limit(32);
         $strps = "";
         $strcl = "";
         
        
            if($_REQUEST['ct'] > 0){
                $this->db->where("category_id",$_REQUEST['ct']);
            }   
            
            if($_REQUEST['pr'] > 0){
                //echo $_REQUEST['pr'];
                if($_REQUEST['pr'] == '1'){
                $this->db->where("price <", "40");
                }
                
                if($_REQUEST['pr'] == '2'){
                 $this->db->where("price >", "40");
                }
            }  
            
            if($_REQUEST['ps']!=""){
                 $strps = "style_id in (".trim($_REQUEST['ps'],",").")";
                 $this->db->where($strps);
            }
            
            if($_REQUEST['cl']!=""){
                 $strcl = "color_id in (".trim($_REQUEST['cl'],",").")";
                 $this->db->where($strcl);
            }
          
           
             $q = $this->db->get();
             $result = $q->result();
            // print_r($result);
           //  echo $this->db->last_query();
            // exit;
            echo '<div id ="loadingmessage"></div>';
             echo '<script>Cart.init();</script>';
             foreach ($result as $obj) {
              echo $this->common_lib->get_product_main_box($obj);
           /*   $thumb = explode("/",$obj->thumb);
            $thum  = $thumb[0]."/".$thumb[1]."/".$thumb[2]."/small-thumb/".$thumb[3];
               
               $product_box = "";
        if($show_wrap || true){
            $product_box .= '
            <div class="col-md-3 col-sm-6 col-xs-12">';
        }
        $data_array = array("is_live_inventory"=>$obj->do_inventory_check,"id"=>$obj->product_id,"name"=>$this->common_lib->get_add_cart_name($obj->product_name),"price"=>$this->common_lib->get_cal_price($obj));
         $this->load->model("commonm");
         $product_size = $this->commonm->get_all("product_sizes",array("product_id" => $obj->product_id));                    
         $size = $this->commonm->get_all("sizes",array("size_id" => $product_size[0]->size_id));
        $data_serialize = json_encode($data_array);
         list($width, $height) = getimagesize($this->common_lib->get_product_image($obj,'small'));
            if($height<=250){$style = "padding-top:40%";
            }elseif($height>=400){$style = "height:230px!important;";}
                else{$style = "";
            }
        
        $coming_soon_badge = "";
        if($obj->coming_soon == 1){
            $coming_soon_badge = "<div class='coming_soon_badge'><img src='".$this->common_lib->get_base_url()."assets/frontend/pages/img/coming_soon.png' /></div>";
        }
        $out_of_stock_badge = "";
        if($obj->out_of_stock == 1 && $obj->coming_soon == 0){
            $out_of_stock_badge = "<div class='coming_soon_badge'><img src='".$this->common_lib->get_base_url()."assets/frontend/pages/img/out_of_stock.png' /></div>";
        }
        $product_box .= '
        <div class="product-item">
            '.$coming_soon_badge.$out_of_stock_badge.'
            <div class="pi-img-wrapper" >
                <a href="'.$this->common_lib->get_product_detail_url($obj).'">
                <img src="'.$this->common_lib->get_product_image($obj,'small').'" style="'.$style.'" class="img-responsive" alt="'.$obj->product_name.'">
                </a>
                <!--div>
                    <a href="'.$this->common_lib->get_product_image($obj,"medium").'" class="btn btn-default fancybox-button">Zoom</a>
                    <a href="#product-pop-up-'.$obj->product_id.'" class="btn btn-default fancybox-fast-view">View</a>
                </div-->
            </div>
            <h3><a href="'.$this->common_lib->get_product_detail_url($obj).'">'.substr($obj->product_name,0,15).'</a></h3>';
            if($size[0]->size !="")
            {
            $product_box .='<div style="text-align:center;">Size: '.$size[0]->size.'</div>';
            } else {
                 $product_box .='<div style="text-align:center;">&nbsp;</div>';
            }
             $product_box .= '<div class="pi-price">'.$this->common_lib->get_product_price($obj,true,"main").'</div>';
            $stock_left = $obj->stock; 
            $show_size_option = $this->common_lib->get_product_setting("show_size_option");                   
            if($show_size_option == 1){
                $stock_left = $this->common_lib->get_product_size_stock($obj->product_id);
            }            
            $product_box .= '
                <div class="cart-buttons ">
                    <a href="'.$this->common_lib->get_product_detail_url($obj).'" class="btn btn-danger">View Detail</a>                    
                </div>';      
            if($obj->do_inventory_check == 1 && $stock_left == 0 && false){
                $product_box .= '
                <div class="cart-buttons ">
                    <button type="button" href="#" class="btn btn-primary add2cart">Out of Stock</button>                    
                </div>';
            }elseif(false){
                $product_box .= '
            <div class="cart-buttons ">
            <button data-add-to-cart="'.$this->common_lib->encode($data_serialize).'" type="submit" data-ref="'.$obj->product_id.'" href="#" id="" class="btn btn-primary add-to-cart-init add2cart">Add to cart</button>
                <div class="progress add-progress" style="left:18px;width:139px;height:37px">
                    <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 100%;">
                        Adding...
                    </div>
                </div>
                </div>';
                
            }
            $product_box .= '
        </div>'.$this->common_lib->get_product_quick_box($obj);
        if($show_wrap || true){
            $product_box .= '
            </div>';    
        }               
                }*/
             echo $product_box;   
        }
}
    
    
    public function product_calculator()
    {
        
        $product_id = $this->common_lib->get_post("p_id");
        $country = $this->common_lib->get_post("country");
        $qnty = $this->common_lib->get_post("qnty");
        $printing_price = 0.00;
        $shipping_price = 0.00;
        $get_product_price = $this->commonm->get_all("products",array("product_id"=>$product_id));
        $range_qnty = $this->commonm->get_all("range",array("range_id"=>$qnty));
        $global_pd_setting = $this->commonm->get_all("product_settings");
            if($get_product_price[0]->deal_status == 1 && $get_product_price[0]->deal_price > 0 && $global_pd_setting[0]->show_deals == 1 && $get_product_price[0]->deal_start_date <= date("Y-m-d") &&  $get_product_price[0]->deal_end_date >= date("Y-m-d")){
                    $price = $get_product_price[0]->deal_price;
                } else{
                    if($get_product_price[0]->promo_price> 0){
                        $price = $get_product_price[0]->promo_price;
                    }else{
                        $price = $get_product_price[0]->price;
                    }
                }
        $total_product_price  = $price * $range_qnty[0]->range;
        $price = array();
        if($this->common_lib->get_post("pt") > 0){
          //  $printing_price = $range_qnty[0]->printing_price;
          $printing = $this->commonm->get_all("product_printcost",array("range_id"=>$qnty,"product_id" => $product_id));
          $printing_price = $printing[0]->print_cost;
          //$printing_price = $get_product_price[0]->printing_cost;
          }  
           
        if($country >0)
        {
            $cal_product = $this->commonm->get_all("product_calculator",array("country_id"=>$country,"range_id"=>$qnty));
           // print_r($cal_product);
            if(count($cal_product)>0){
                if($cal_product[0]->shipping_price > 0){
                    $shipping_price = $cal_product[0]->shipping_price;
                }
            }
        }
        
        $price['product_price'] = $this->common_lib->currency_price($total_product_price);
        $price['printing_price'] = $this->common_lib->currency_price($printing_price) ;
        $price['shipping_price'] = $this->common_lib->currency_price($shipping_price);
        $price['total_price'] = $this->common_lib->currency_price($total_product_price + $printing_price + $shipping_price);
        echo json_encode($price);
    }
   
    
    public function get_cart_overview(){
        echo $this->common_lib->get_cart_summary();
        exit;
    }
    
    function update_cart(){		
        $id	= $this->input->post('id');
        if($this->prod_model->_is_product($id)){
            $quantity	= ($this->input->post('quant') >= 1)? $this->input->post('quant') : 1;
            $sc_array	= $this->cart->contents();
            if(sizeof($sc_array)>0){
                $found	= FALSE;
                foreach($sc_array as $cindex => $cvalue)
                        foreach($cvalue as $key => $value)
                                if($key == 'name' && $value == $id)
                                        $found = TRUE;
                if($found){
                    foreach($sc_array as $cindex => $cvalue)
                        foreach($cvalue as $key => $value)
                            if($key == 'name' && $value == $id){
                                    $price		= ($this->prod_model->_get_price($id) !== FALSE)? $this->prod_model->_get_price($id) : 0;
                                    $data		= array('rowid' => $cvalue['rowid'], 'qty' => $quantity);
                                    echo $this->update_item($data);
                            }
                }
                else echo FALSE;
            }
            else echo FALSE;
        }
        else echo FALSE;
		
    }
    function addtowishlist(){
         if($this->common_lib->get_login_id() == 0){
            echo "error_login";
            exit;
        }
        $product_data = $this->common_lib->decode($this->common_lib->get_post("data"));
        $size = $this->common_lib->decode($this->common_lib->get_post("size"));
        $product_data = json_decode($product_data,true);
        
        $country = $this->common_lib->get_post("country");
        $qnty = $this->common_lib->get_post("qnty");
        $get_product_price = $this->commonm->get_all("products",array("product_id"=>$product_data['id']));
        $range_qnty = $this->commonm->get_all("range",array("range_id"=>$qnty));
        $rang_qnty = $range_qnty[0]->range;
        $size_price = $this->commonm->get_all("product_sizes",array("size_id" => $size,"product_id" => $product_data['id']));                
        $global_pd_setting = $this->commonm->get_all("product_settings");
        $product_id = $product_data['id'];
        $qty = $this->common_lib->get_post("qty");
        if($rang_qnty >= $qty ){
            $quantity = $rang_qnty;
        } else {
            $quantity = $this->common_lib->get_post("qty");
        }
         $data = array(
            'product_id'   => $product_data['id'],
            'quantity'     => $quantity,
            'size_id' => '0', 
            'user_id' => $this->common_lib->get_login_id()
        );
         $this->commonm->db_insert($data,"wishlist");
      //   $this->cart->update($data); 
        //$this->common_lib->add_to_wish_list($data);
      
      echo "A";
        
    }
    
    function move_to_cart(){
        $wishlist_id = $this->common_lib->get_n_post("row_id");        
        $getlist = $this->commonm->get_all("wishlist",array("wishlist_id" => $wishlist_id));
        $product_id = $getlist[0]->product_id;
        $size_id = $getlist[0]->size_id;
        $color_id = $getlist[0]->color_id;
        $quanitiy = $getlist[0]->quantity;
        
        $getproductdetail = $this->commonm->get_all("products",array("product_id" =>$product_id));
        
        if($getproductdetail[0]->price > 0){
                $product_data['price'] = $getproductdetail[0]->price;    
            }
                    
        if($getproductdetail[0]->promo_price > 0){
            $product_data['price'] = $getproductdetail[0]->promo_price;   
        }
        
        if($size_id > 0){
            
            $getproductdetailsize = $this->commonm->get_all("product_sizes",array("product_id" =>$product_id,"size_id" =>$size_id)); 
            if($getproductdetailsize[0]->stock_price > 0){
                $product_data['price'] = $getproductdetailsize[0]->stock_price;    
            }
                    
            if($getproductdetailsize[0]->stock_discounted_price > 0){
                $product_data['price'] = $getproductdetailsize[0]->stock_discounted_price;    
            }
               
        }
        
        $product_data['name'] = $this->common_lib->get_add_cart_name($getproductdetail[0]->product_name);
        $product_data['id'] = $product_id;
        $product_data['qty'] = $quanitiy;
        $product_data['size_id'] = $size_id;
        $product_data['pd'] = "";
        $product_data['rd'] = "";
        $product_data['is_live_inventory'] = $getproductdetail[0]->do_inventory_check;
        $product_data['printing_price'] = $printing_price ;
        $product_data['shipping_price'] = $shipping_price;
        $this->cart->insert($product_data); 
       // print_r($product_data);
       $this->commonm->_delete_rows("wishlist",array("wishlist_id" => $wishlist_id));
        echo json_encode(array("success" => 1));
        
    }
    
    function remove_from_wishlist(){
        $user_id = $this->common_lib->get_login_id();
        $wishlist_id = $this->common_lib->get_n_post("row_id");        
        $this->commonm->_delete_rows("wishlist",array("wishlist_id" => $wishlist_id,"user_id" => $user_id));
    }
    
    function add_to_wishlist(){
        if($this->common_lib->get_login_id() == 0){
            echo "error_login";
            exit;
        }
        $row_id = $this->common_lib->get_post("row_id");
        $cart = $this->cart->contents();
        $cart = $cart[$row_id];
        $data = array(
            'rowid'   => $row_id,
            'qty'     => 0
        );
        $this->cart->update($data); 
        $this->common_lib->add_to_wish_list($cart);
        exit;
    }
        
    function user_login(){
        
    }
    
    function checkout_step_1(){
        
    }    
    
}

@KyuuKazami